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" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 30 | #include "parser.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 31 | #include "parser_schema.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 32 | #include "path.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 33 | #include "plugins_exts.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 34 | #include "plugins_exts_internal.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 35 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "set.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | #include "tree.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 38 | #include "tree_data.h" |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 39 | #include "tree_data_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 40 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 41 | #include "tree_schema_internal.h" |
| 42 | #include "xpath.h" |
| 43 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 44 | static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 45 | void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod); |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 46 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 47 | static LY_ERR lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname); |
| 48 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 49 | /** |
| 50 | * @brief Duplicate string into dictionary |
| 51 | * @param[in] CTX libyang context of the dictionary. |
| 52 | * @param[in] ORIG String to duplicate. |
| 53 | * @param[out] DUP Where to store the result. |
| 54 | */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 55 | #define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);} |
| 56 | |
| 57 | #define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);} |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 58 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 59 | #define DUP_ARRAY(CTX, ORIG_ARRAY, NEW_ARRAY, DUP_FUNC) \ |
| 60 | if (ORIG_ARRAY) { \ |
| 61 | LY_ARRAY_COUNT_TYPE u; \ |
| 62 | LY_ARRAY_CREATE_RET(CTX, NEW_ARRAY, LY_ARRAY_COUNT(ORIG_ARRAY), LY_EMEM); \ |
| 63 | LY_ARRAY_FOR(ORIG_ARRAY, u) { \ |
| 64 | LY_ARRAY_INCREMENT(NEW_ARRAY); \ |
| 65 | LY_CHECK_RET(DUP_FUNC(CTX, &(NEW_ARRAY)[u], &(ORIG_ARRAY)[u])); \ |
| 66 | } \ |
| 67 | } |
| 68 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 69 | #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] | 70 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 71 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 72 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 73 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 74 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 75 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 76 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 77 | } \ |
| 78 | } |
| 79 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 80 | #define COMPILE_OP_ARRAY_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] | 81 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 82 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 83 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 84 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 85 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 86 | RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 87 | if (RET == LY_EDENIED) { \ |
| 88 | LY_ARRAY_DECREMENT(ARRAY_C); \ |
| 89 | } else if (RET != LY_SUCCESS) { \ |
| 90 | goto GOTO; \ |
| 91 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 92 | } \ |
| 93 | } |
| 94 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 95 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 96 | if (EXTS_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 97 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \ |
| 98 | 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] | 99 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 100 | 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] | 101 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 102 | } \ |
| 103 | } |
| 104 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 105 | #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] | 106 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 107 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 108 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 109 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +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, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 112 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 113 | } \ |
| 114 | } |
| 115 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 116 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 117 | if (MEMBER_P) { \ |
| 118 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 119 | 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] | 120 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 121 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 122 | } |
| 123 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 124 | #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] | 125 | if (MEMBER_P) { \ |
| 126 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 127 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 128 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 129 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 130 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 131 | } |
| 132 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 133 | #define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 134 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 135 | 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] | 136 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 137 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 138 | return LY_EVALID; \ |
| 139 | } \ |
| 140 | } \ |
| 141 | } |
| 142 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 143 | #define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 144 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 145 | 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] | 146 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \ |
| 147 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 148 | return LY_EVALID; \ |
| 149 | } \ |
| 150 | } \ |
| 151 | } |
| 152 | |
| 153 | struct lysc_ext * |
| 154 | lysc_ext_dup(struct lysc_ext *orig) |
| 155 | { |
| 156 | ++orig->refcount; |
| 157 | return orig; |
| 158 | } |
| 159 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 160 | static struct lysc_ext_instance * |
| 161 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 162 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 163 | /* TODO - extensions, increase refcount */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 164 | (void) ctx; |
| 165 | (void) orig; |
| 166 | return NULL; |
| 167 | } |
| 168 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 169 | /** |
| 170 | * @brief Add/replace a leaf default value in unres. |
| 171 | * Can also be used for a single leaf-list default value. |
| 172 | * |
| 173 | * @param[in] ctx Compile context. |
| 174 | * @param[in] leaf Leaf with the default value. |
| 175 | * @param[in] dflt Default value to use. |
| 176 | * @return LY_ERR value. |
| 177 | */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 178 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 179 | lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 180 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 181 | struct lysc_unres_dflt *r = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 182 | uint32_t i; |
| 183 | |
| 184 | for (i = 0; i < ctx->dflts.count; ++i) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 185 | if (((struct lysc_unres_dflt *)ctx->dflts.objs[i])->leaf == leaf) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 186 | /* just replace the default */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 187 | r = ctx->dflts.objs[i]; |
| 188 | lysp_qname_free(ctx->ctx, r->dflt); |
| 189 | free(r->dflt); |
| 190 | break; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 191 | } |
| 192 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 193 | if (!r) { |
| 194 | /* add new unres item */ |
| 195 | r = calloc(1, sizeof *r); |
| 196 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 197 | r->leaf = leaf; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 198 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 199 | LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL)); |
| 200 | } |
| 201 | |
| 202 | r->dflt = malloc(sizeof *r->dflt); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 203 | LY_CHECK_GOTO(!r->dflt, error); |
| 204 | LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 205 | |
| 206 | return LY_SUCCESS; |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 207 | |
| 208 | error: |
| 209 | free(r->dflt); |
| 210 | LOGMEM(ctx->ctx); |
| 211 | return LY_EMEM; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 214 | /** |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 215 | * @brief Add/replace a leaf-list default value(s) in unres. |
| 216 | * |
| 217 | * @param[in] ctx Compile context. |
| 218 | * @param[in] llist Leaf-list with the default value. |
| 219 | * @param[in] dflts Sized array of the default values. |
| 220 | * @return LY_ERR value. |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 221 | */ |
| 222 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 223 | lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 224 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 225 | struct lysc_unres_dflt *r = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 226 | uint32_t i; |
| 227 | |
| 228 | for (i = 0; i < ctx->dflts.count; ++i) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 229 | if (((struct lysc_unres_dflt *)ctx->dflts.objs[i])->llist == llist) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 230 | /* just replace the defaults */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 231 | r = ctx->dflts.objs[i]; |
| 232 | lysp_qname_free(ctx->ctx, r->dflt); |
| 233 | free(r->dflt); |
| 234 | r->dflt = NULL; |
| 235 | FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free); |
| 236 | r->dflts = NULL; |
| 237 | break; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 238 | } |
| 239 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 240 | if (!r) { |
| 241 | r = calloc(1, sizeof *r); |
| 242 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 243 | r->llist = llist; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 244 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 245 | LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL)); |
| 246 | } |
| 247 | |
| 248 | DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 249 | |
| 250 | return LY_SUCCESS; |
| 251 | } |
| 252 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 253 | static void |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 254 | lysc_unres_dflt_free(const struct ly_ctx *ctx, struct lysc_unres_dflt *r) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 255 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 256 | assert(!r->dflt || !r->dflts); |
| 257 | if (r->dflt) { |
| 258 | lysp_qname_free((struct ly_ctx *)ctx, r->dflt); |
| 259 | free(r->dflt); |
| 260 | } else { |
| 261 | FREE_ARRAY((struct ly_ctx *)ctx, r->dflts, lysp_qname_free); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 262 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 263 | free(r); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 266 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 267 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 268 | { |
| 269 | int len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 270 | uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 271 | |
| 272 | if (!name) { |
| 273 | /* removing last path segment */ |
| 274 | if (ctx->path[ctx->path_len - 1] == '}') { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 275 | for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {} |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 276 | if (ctx->path[ctx->path_len] == '=') { |
| 277 | ctx->path[ctx->path_len++] = '}'; |
| 278 | } else { |
| 279 | /* not a top-level special tag, remove also preceiding '/' */ |
| 280 | goto remove_nodelevel; |
| 281 | } |
| 282 | } else { |
| 283 | remove_nodelevel: |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 284 | for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {} |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 285 | if (ctx->path_len == 0) { |
| 286 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 287 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | /* set new terminating NULL-byte */ |
| 291 | ctx->path[ctx->path_len] = '\0'; |
| 292 | } else { |
| 293 | if (ctx->path_len > 1) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 294 | if (!parent && (ctx->path[ctx->path_len - 1] == '}') && (ctx->path[ctx->path_len - 2] != '\'')) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 295 | /* extension of the special tag */ |
| 296 | nextlevel = 2; |
| 297 | --ctx->path_len; |
| 298 | } else { |
| 299 | /* there is already some path, so add next level */ |
| 300 | nextlevel = 1; |
| 301 | } |
| 302 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 303 | |
| 304 | if (nextlevel != 2) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 305 | if ((parent && (parent->module == ctx->mod)) || (!parent && (ctx->path_len > 1) && (name[0] == '{'))) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 306 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 307 | 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] | 308 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 309 | 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] | 310 | } |
| 311 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 312 | 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] | 313 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 314 | if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) { |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 315 | /* output truncated */ |
| 316 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 317 | } else { |
| 318 | ctx->path_len += len; |
| 319 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 324 | * @brief Duplicate the compiled pattern structure. |
| 325 | * |
| 326 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 327 | * |
| 328 | * @param[in] orig The pattern structure to duplicate. |
| 329 | * @return The duplicated structure to use. |
| 330 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 331 | static struct lysc_pattern * |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 332 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 333 | { |
| 334 | ++orig->refcount; |
| 335 | return orig; |
| 336 | } |
| 337 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 338 | /** |
| 339 | * @brief Duplicate the array of compiled patterns. |
| 340 | * |
| 341 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 342 | * |
| 343 | * @param[in] ctx Libyang context for logging. |
| 344 | * @param[in] orig The patterns sized array to duplicate. |
| 345 | * @return New sized array as a copy of @p orig. |
| 346 | * @return NULL in case of memory allocation error. |
| 347 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 348 | static struct lysc_pattern ** |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 349 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 350 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 351 | struct lysc_pattern **dup = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 352 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 353 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 354 | assert(orig); |
| 355 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 356 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 357 | LY_ARRAY_FOR(orig, u) { |
| 358 | dup[u] = lysc_pattern_dup(orig[u]); |
| 359 | LY_ARRAY_INCREMENT(dup); |
| 360 | } |
| 361 | return dup; |
| 362 | } |
| 363 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 364 | /** |
| 365 | * @brief Duplicate compiled range structure. |
| 366 | * |
| 367 | * @param[in] ctx Libyang context for logging. |
| 368 | * @param[in] orig The range structure to be duplicated. |
| 369 | * @return New compiled range structure as a copy of @p orig. |
| 370 | * @return NULL in case of memory allocation error. |
| 371 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 372 | struct lysc_range * |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 373 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 374 | { |
| 375 | struct lysc_range *dup; |
| 376 | LY_ERR ret; |
| 377 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 378 | assert(orig); |
| 379 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 380 | dup = calloc(1, sizeof *dup); |
| 381 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 382 | if (orig->parts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 383 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup); |
| 384 | LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts); |
| 385 | 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] | 386 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 387 | DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup); |
| 388 | DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 389 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 390 | |
| 391 | return dup; |
| 392 | cleanup: |
| 393 | free(dup); |
| 394 | (void) ret; /* set but not used due to the return type */ |
| 395 | return NULL; |
| 396 | } |
| 397 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 398 | /** |
| 399 | * @brief Stack for processing if-feature expressions. |
| 400 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 401 | struct iff_stack { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 402 | size_t size; /**< number of items in the stack */ |
| 403 | size_t index; /**< first empty item */ |
| 404 | 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] | 405 | }; |
| 406 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 407 | /** |
| 408 | * @brief Add @ref ifftokens into the stack. |
| 409 | * @param[in] stack The if-feature stack to use. |
| 410 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 411 | * @return LY_EMEM in case of memory allocation error |
| 412 | * @return LY_ESUCCESS if the value successfully stored. |
| 413 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 414 | static LY_ERR |
| 415 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 416 | { |
| 417 | if (stack->index == stack->size) { |
| 418 | stack->size += 4; |
| 419 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 420 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 421 | } |
| 422 | stack->stack[stack->index++] = value; |
| 423 | return LY_SUCCESS; |
| 424 | } |
| 425 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 426 | /** |
| 427 | * @brief Get (and remove) the last item form the stack. |
| 428 | * @param[in] stack The if-feature stack to use. |
| 429 | * @return The value from the top of the stack. |
| 430 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 431 | static uint8_t |
| 432 | iff_stack_pop(struct iff_stack *stack) |
| 433 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 434 | assert(stack && stack->index); |
| 435 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 436 | stack->index--; |
| 437 | return stack->stack[stack->index]; |
| 438 | } |
| 439 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 440 | /** |
| 441 | * @brief Clean up the stack. |
| 442 | * @param[in] stack The if-feature stack to use. |
| 443 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 444 | static void |
| 445 | iff_stack_clean(struct iff_stack *stack) |
| 446 | { |
| 447 | stack->size = 0; |
| 448 | free(stack->stack); |
| 449 | } |
| 450 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 451 | /** |
| 452 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 453 | * (libyang format of the if-feature expression). |
| 454 | * @param[in,out] list The 2bits array to modify. |
| 455 | * @param[in] op The operand (@ref ifftokens) to store. |
| 456 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 457 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 458 | static void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 459 | iff_setop(uint8_t *list, uint8_t op, size_t pos) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 460 | { |
| 461 | uint8_t *item; |
| 462 | uint8_t mask = 3; |
| 463 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 464 | assert(op <= 3); /* max 2 bits */ |
| 465 | |
| 466 | item = &list[pos / 4]; |
| 467 | mask = mask << 2 * (pos % 4); |
| 468 | *item = (*item) & ~mask; |
| 469 | *item = (*item) | (op << 2 * (pos % 4)); |
| 470 | } |
| 471 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 472 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 473 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 474 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 475 | /** |
| 476 | * @brief Find a feature of the given name and referenced in the given module. |
| 477 | * |
| 478 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 479 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 480 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 481 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 482 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 483 | * assigned till that time will be still valid. |
| 484 | * |
| 485 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 486 | * @param[in] name Name of the feature including possible prefix. |
| 487 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 488 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 489 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 490 | static struct lysc_feature * |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 491 | lys_feature_find(const struct lys_module *mod, const char *name, size_t len) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 492 | { |
| 493 | size_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 494 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 495 | struct lysc_feature *f; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 496 | |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 497 | assert(mod); |
| 498 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 499 | for (i = 0; i < len; ++i) { |
| 500 | if (name[i] == ':') { |
| 501 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 502 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 503 | LY_CHECK_RET(!mod, NULL); |
| 504 | |
| 505 | name = &name[i + 1]; |
| 506 | len = len - i - 1; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /* we have the correct module, get the feature */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 511 | LY_ARRAY_FOR(mod->features, u) { |
| 512 | f = &mod->features[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 513 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 514 | return f; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | return NULL; |
| 519 | } |
| 520 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 521 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 522 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 523 | */ |
| 524 | static LY_ERR |
| 525 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 526 | { |
| 527 | LY_ERR ret = LY_SUCCESS; |
| 528 | |
| 529 | if (!ext_p->compiled) { |
| 530 | lysc_update_path(ctx, NULL, "{extension}"); |
| 531 | lysc_update_path(ctx, NULL, ext_p->name); |
| 532 | |
| 533 | /* compile the extension definition */ |
| 534 | ext_p->compiled = calloc(1, sizeof **ext); |
| 535 | ext_p->compiled->refcount = 1; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 536 | DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done); |
| 537 | DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done); |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 538 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 539 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 540 | |
| 541 | lysc_update_path(ctx, NULL, NULL); |
| 542 | lysc_update_path(ctx, NULL, NULL); |
| 543 | |
| 544 | /* find extension definition plugin */ |
| 545 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 546 | } |
| 547 | |
| 548 | *ext = lysc_ext_dup(ext_p->compiled); |
| 549 | |
| 550 | done: |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 555 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 556 | * |
| 557 | * @param[in] ctx Compilation context. |
| 558 | * @param[in] ext_p Parsed extension instance. |
| 559 | * @param[in,out] ext Prepared compiled extension instance. |
| 560 | * @param[in] parent Extension instance parent. |
| 561 | * @param[in] parent_type Extension instance parent type. |
| 562 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 563 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 564 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 565 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 566 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 567 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 568 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 569 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 570 | size_t u; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 571 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 572 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 573 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 574 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret); |
| 575 | LY_CHECK_RET(ret); |
| 576 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 577 | ext->insubstmt = ext_p->insubstmt; |
| 578 | ext->insubstmt_index = ext_p->insubstmt_index; |
Michal Vasko | 8ce5135 | 2020-10-06 14:07:24 +0200 | [diff] [blame] | 579 | ext->module = ctx->mod; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 580 | ext->parent = parent; |
| 581 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 582 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 583 | 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] | 584 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 585 | /* get module where the extension definition should be placed */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 586 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {} |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 587 | if (ext_p->yin) { |
| 588 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 589 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 590 | * YANG (import) prefix must be done here. */ |
| 591 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 592 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 593 | u = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 594 | } else { |
| 595 | assert(ctx->mod_def->parsed); |
| 596 | LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) { |
| 597 | 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] | 598 | char *s; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 599 | LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 600 | ret = LY_EMEM, cleanup); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 601 | LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 602 | u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 603 | break; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | if (!prefixed_name) { |
| 608 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 609 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 610 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 611 | goto cleanup; |
| 612 | } |
| 613 | } else { |
| 614 | prefixed_name = ext_p->name; |
| 615 | } |
| 616 | lysc_update_path(ctx, NULL, prefixed_name); |
| 617 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 618 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 619 | 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] | 620 | if (!ext_mod) { |
| 621 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 622 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 623 | ret = LY_EVALID; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 624 | goto cleanup; |
| 625 | } else if (!ext_mod->parsed->extensions) { |
| 626 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 627 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 628 | prefixed_name, ext_mod->name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 629 | ret = LY_EVALID; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 630 | goto cleanup; |
| 631 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 632 | } |
| 633 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 634 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 635 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 636 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 637 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 638 | /* compile extension definition and assign it */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 639 | LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 643 | if (!ext->def) { |
| 644 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 645 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 646 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 647 | goto cleanup; |
| 648 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 649 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 650 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 651 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 652 | * 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] | 653 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 654 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 655 | struct lysp_stmt *stmt = NULL; |
| 656 | |
| 657 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 658 | /* ... argument was the first XML child element */ |
| 659 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 660 | /* TODO check namespace of the statement */ |
| 661 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 662 | stmt = ext_p->child; |
| 663 | } |
| 664 | } |
| 665 | } else { |
| 666 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 667 | * with LYS_YIN_ATTR flag */ |
| 668 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 669 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 670 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 671 | break; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | if (!stmt) { |
| 676 | /* missing extension's argument */ |
| 677 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 678 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 679 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 680 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 681 | |
| 682 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 683 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup); |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 684 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 685 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 686 | if (prefixed_name != ext_p->name) { |
| 687 | lydict_remove(ctx->ctx, ext_p->name); |
| 688 | ext_p->name = prefixed_name; |
| 689 | if (!ext_p->argument && ext->argument) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 690 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 691 | } |
| 692 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 693 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 694 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 695 | if (ext->argument) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 696 | lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 697 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 698 | LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 699 | if (ext->argument) { |
| 700 | lysc_update_path(ctx, NULL, NULL); |
| 701 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 702 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 703 | ext_p->compiled = ext; |
| 704 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 705 | cleanup: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 706 | if (prefixed_name && (prefixed_name != ext_p->name)) { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 707 | lydict_remove(ctx->ctx, prefixed_name); |
| 708 | } |
| 709 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 710 | lysc_update_path(ctx, NULL, NULL); |
| 711 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 712 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 713 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 717 | * @brief Compile information from the if-feature statement |
| 718 | * @param[in] ctx Compile context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 719 | * @param[in] qname The if-feature argument to process. It is pointer-to-qname just to unify the compile functions. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 720 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 721 | * @return LY_ERR value. |
| 722 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 723 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 724 | lys_compile_iffeature(struct lysc_ctx *ctx, struct lysp_qname *qname, struct lysc_iffeature *iff) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 725 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 726 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 727 | const char *c = qname->str; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 728 | int64_t i, j; |
| 729 | int8_t op_len, last_not = 0, checkversion = 0; |
| 730 | LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 731 | uint8_t op; |
| 732 | struct iff_stack stack = {0, 0, NULL}; |
| 733 | struct lysc_feature *f; |
| 734 | |
| 735 | assert(c); |
| 736 | |
| 737 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 738 | for (i = j = 0; c[i]; i++) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 739 | if (c[i] == '(') { |
| 740 | j++; |
| 741 | checkversion = 1; |
| 742 | continue; |
| 743 | } else if (c[i] == ')') { |
| 744 | j--; |
| 745 | continue; |
| 746 | } else if (isspace(c[i])) { |
| 747 | checkversion = 1; |
| 748 | continue; |
| 749 | } |
| 750 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 751 | if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) { |
| 752 | uint64_t spaces; |
Michal Vasko | 2b7e558 | 2020-10-07 12:31:23 +0200 | [diff] [blame] | 753 | for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++) {} |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 754 | if (c[i + op_len + spaces] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 755 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 756 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", qname->str); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 757 | return LY_EVALID; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 758 | } else if (!isspace(c[i + op_len])) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 759 | /* feature name starting with the not/and/or */ |
| 760 | last_not = 0; |
| 761 | f_size++; |
| 762 | } else if (c[i] == 'n') { /* not operation */ |
| 763 | if (last_not) { |
| 764 | /* double not */ |
| 765 | expr_size = expr_size - 2; |
| 766 | last_not = 0; |
| 767 | } else { |
| 768 | last_not = 1; |
| 769 | } |
| 770 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 771 | if (f_exp != f_size) { |
| 772 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 773 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", |
| 774 | qname->str, op_len, &c[i]); |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 775 | return LY_EVALID; |
| 776 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 777 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 778 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 779 | /* not a not operation */ |
| 780 | last_not = 0; |
| 781 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 782 | i += op_len; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 783 | } else { |
| 784 | f_size++; |
| 785 | last_not = 0; |
| 786 | } |
| 787 | expr_size++; |
| 788 | |
| 789 | while (!isspace(c[i])) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 790 | if (!c[i] || (c[i] == ')') || (c[i] == '(')) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 791 | i--; |
| 792 | break; |
| 793 | } |
| 794 | i++; |
| 795 | } |
| 796 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 797 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 798 | /* not matching count of ( and ) */ |
| 799 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 800 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", qname->str); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 801 | return LY_EVALID; |
| 802 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 803 | if (f_exp != f_size) { |
| 804 | /* features do not match the needed arguments for the logical operations */ |
| 805 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 806 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 807 | "the required number of operands for the operations.", qname->str); |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 808 | return LY_EVALID; |
| 809 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 810 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 811 | if (checkversion || (expr_size > 1)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 812 | /* check that we have 1.1 module */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 813 | if (qname->mod->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 814 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 815 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", qname->str); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 816 | return LY_EVALID; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | /* allocate the memory */ |
| 821 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 822 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 823 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 824 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 825 | |
| 826 | stack.size = expr_size; |
| 827 | f_size--; expr_size--; /* used as indexes from now */ |
| 828 | |
| 829 | for (i--; i >= 0; i--) { |
| 830 | if (c[i] == ')') { |
| 831 | /* push it on stack */ |
| 832 | iff_stack_push(&stack, LYS_IFF_RP); |
| 833 | continue; |
| 834 | } else if (c[i] == '(') { |
| 835 | /* pop from the stack into result all operators until ) */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 836 | while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 837 | iff_setop(iff->expr, op, expr_size--); |
| 838 | } |
| 839 | continue; |
| 840 | } else if (isspace(c[i])) { |
| 841 | continue; |
| 842 | } |
| 843 | |
| 844 | /* end of operator or operand -> find beginning and get what is it */ |
| 845 | j = i + 1; |
| 846 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 847 | i--; |
| 848 | } |
| 849 | i++; /* go back by one step */ |
| 850 | |
| 851 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 852 | if (stack.index && (stack.stack[stack.index - 1] == LYS_IFF_NOT)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 853 | /* double not */ |
| 854 | iff_stack_pop(&stack); |
| 855 | } else { |
| 856 | /* not has the highest priority, so do not pop from the stack |
| 857 | * as in case of AND and OR */ |
| 858 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 859 | } |
| 860 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 861 | /* as for OR - pop from the stack all operators with the same or higher |
| 862 | * priority and store them to the result, then push the AND to the stack */ |
| 863 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 864 | op = iff_stack_pop(&stack); |
| 865 | iff_setop(iff->expr, op, expr_size--); |
| 866 | } |
| 867 | iff_stack_push(&stack, LYS_IFF_AND); |
| 868 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 869 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 870 | op = iff_stack_pop(&stack); |
| 871 | iff_setop(iff->expr, op, expr_size--); |
| 872 | } |
| 873 | iff_stack_push(&stack, LYS_IFF_OR); |
| 874 | } else { |
| 875 | /* feature name, length is j - i */ |
| 876 | |
| 877 | /* add it to the expression */ |
| 878 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 879 | |
| 880 | /* now get the link to the feature definition */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 881 | f = lys_feature_find(qname->mod, &c[i], j - i); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 882 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 883 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", qname->str, j - i, &c[i]); |
| 884 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 885 | iff->features[f_size] = f; |
| 886 | LY_ARRAY_INCREMENT(iff->features); |
| 887 | f_size--; |
| 888 | } |
| 889 | } |
| 890 | while (stack.index) { |
| 891 | op = iff_stack_pop(&stack); |
| 892 | iff_setop(iff->expr, op, expr_size--); |
| 893 | } |
| 894 | |
| 895 | if (++expr_size || ++f_size) { |
| 896 | /* not all expected operators and operands found */ |
| 897 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 898 | "Invalid value \"%s\" of if-feature - processing error.", qname->str); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 899 | rc = LY_EINT; |
| 900 | } else { |
| 901 | rc = LY_SUCCESS; |
| 902 | } |
| 903 | |
| 904 | error: |
| 905 | /* cleanup */ |
| 906 | iff_stack_clean(&stack); |
| 907 | |
| 908 | return rc; |
| 909 | } |
| 910 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 911 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 912 | * @brief Get the XPath context node for the given schema node. |
| 913 | * @param[in] start The schema node where the XPath expression appears. |
| 914 | * @return The context node to evaluate XPath expression in given schema node. |
| 915 | * @return NULL in case the context node is the root node. |
| 916 | */ |
| 917 | static struct lysc_node * |
| 918 | lysc_xpath_context(struct lysc_node *start) |
| 919 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 920 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 921 | start = start->parent) {} |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 922 | return start; |
| 923 | } |
| 924 | |
| 925 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 926 | * @brief Compile information from the when statement |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 927 | * |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 928 | * @param[in] ctx Compile context. |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 929 | * @param[in] when_p Parsed when structure. |
| 930 | * @param[in] flags Flags of the parsed node with the when statement. |
| 931 | * @param[in] ctx_node Context node for the when statement. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 932 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 933 | * @return LY_ERR value. |
| 934 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 935 | static LY_ERR |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 936 | lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node, |
| 937 | struct lysc_when **when) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 938 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 939 | LY_ERR ret = LY_SUCCESS; |
| 940 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 941 | *when = calloc(1, sizeof **when); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 942 | LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 943 | (*when)->refcount = 1; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 944 | LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond)); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 945 | (*when)->module = ctx->mod_def; |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 946 | (*when)->context = (struct lysc_node *)ctx_node; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 947 | DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done); |
| 948 | DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 949 | 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] | 950 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 951 | |
| 952 | done: |
| 953 | return ret; |
| 954 | } |
| 955 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 956 | /** |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 957 | * @brief Compile information from the when statement by either standard compilation or by reusing |
| 958 | * another compiled when structure. |
| 959 | * |
| 960 | * @param[in] ctx Compile context. |
| 961 | * @param[in] when_p Parsed when structure. |
| 962 | * @param[in] flags Flags of the parsed node with the when statement. |
| 963 | * @param[in] ctx_node Context node for the when statement. |
| 964 | * @param[in] node Compiled node to which add the compiled when. |
| 965 | * @param[in,out] when_c Optional, pointer to the previously compiled @p when_p to be reused. Set to NULL |
| 966 | * for the first call. |
| 967 | * @return LY_ERR value. |
| 968 | */ |
| 969 | static LY_ERR |
| 970 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node, |
| 971 | struct lysc_node *node, struct lysc_when **when_c) |
| 972 | { |
| 973 | struct lysc_when **new_when, ***node_when; |
| 974 | |
| 975 | assert(when_p); |
| 976 | |
| 977 | /* get the when array */ |
| 978 | if (node->nodetype & LYS_ACTION) { |
| 979 | node_when = &((struct lysc_action *)node)->when; |
| 980 | } else if (node->nodetype == LYS_NOTIF) { |
| 981 | node_when = &((struct lysc_notif *)node)->when; |
| 982 | } else { |
| 983 | node_when = &node->when; |
| 984 | } |
| 985 | |
| 986 | /* create new when pointer */ |
| 987 | LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM); |
| 988 | if (!when_c || !(*when_c)) { |
| 989 | /* compile when */ |
| 990 | LY_CHECK_RET(lys_compile_when_(ctx, when_p, flags, ctx_node, new_when)); |
| 991 | |
| 992 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 993 | /* do not check "when" semantics in a grouping */ |
| 994 | LY_CHECK_RET(ly_set_add(&ctx->xpath, node, 0, NULL)); |
| 995 | } |
| 996 | |
| 997 | /* remember the compiled when for sharing */ |
| 998 | if (when_c) { |
| 999 | *when_c = *new_when; |
| 1000 | } |
| 1001 | } else { |
| 1002 | /* use the previously compiled when */ |
| 1003 | ++(*when_c)->refcount; |
| 1004 | *new_when = *when_c; |
| 1005 | |
| 1006 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 1007 | /* in this case check "when" again for all children because of dummy node check */ |
| 1008 | LY_CHECK_RET(ly_set_add(&ctx->xpath, node, 0, NULL)); |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | return LY_SUCCESS; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1016 | * @brief Compile information from the must statement |
| 1017 | * @param[in] ctx Compile context. |
| 1018 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1019 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 1020 | * @return LY_ERR value. |
| 1021 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1022 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1023 | 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] | 1024 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1025 | LY_ERR ret = LY_SUCCESS; |
| 1026 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1027 | LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond)); |
| 1028 | must->module = (struct lys_module *)must_p->arg.mod; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1029 | DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done); |
| 1030 | DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done); |
| 1031 | DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done); |
| 1032 | DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1033 | 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] | 1034 | |
| 1035 | done: |
| 1036 | return ret; |
| 1037 | } |
| 1038 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1039 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1040 | * @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] | 1041 | * @param[in] ctx Compile context. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1042 | * @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] | 1043 | * @return LY_ERR value. |
| 1044 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1045 | static LY_ERR |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1046 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1047 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1048 | const struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1049 | LY_ERR ret = LY_SUCCESS; |
| 1050 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 1051 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 1052 | * 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] | 1053 | * 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] | 1054 | if (!imp_p->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1055 | /* try to use filepath if present */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1056 | if (imp_p->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1057 | struct ly_in *in; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1058 | if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1059 | LOGINT(ctx->ctx); |
| 1060 | } else { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1061 | LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1062 | ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1063 | if (mod != imp_p->module) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1064 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1065 | imp_p->module->filepath, imp_p->module->name); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1066 | mod = NULL; |
| 1067 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1068 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1069 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1070 | } |
| 1071 | if (!mod) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1072 | 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] | 1073 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1074 | imp_p->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1075 | return LY_ENOTFOUND; |
| 1076 | } |
| 1077 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1078 | } |
| 1079 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1080 | return ret; |
| 1081 | } |
| 1082 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1083 | LY_ERR |
| 1084 | lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1085 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1086 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1087 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1088 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1089 | LY_ERR ret = LY_SUCCESS; |
| 1090 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1091 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1092 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1093 | if (!ctx_sc) { |
| 1094 | context.ctx = ctx; |
| 1095 | context.mod = module; |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 1096 | context.mod_def = module; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1097 | context.path_len = 1; |
| 1098 | context.path[0] = '/'; |
| 1099 | ctx_sc = &context; |
| 1100 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1101 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1102 | if (!identities_p) { |
| 1103 | return LY_SUCCESS; |
| 1104 | } |
| 1105 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1106 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1110 | 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] | 1111 | LY_ARRAY_FOR(identities_p, u) { |
| 1112 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 1113 | |
| 1114 | LY_ARRAY_INCREMENT(*identities); |
| 1115 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1116 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done); |
| 1117 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done); |
| 1118 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1119 | (*identities)[offset + u].module = ctx_sc->mod; |
| 1120 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1121 | lys_compile_iffeature, ret, done); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1122 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 1123 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1124 | LYEXT_PAR_IDENT, ret, done); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1125 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 1126 | |
| 1127 | lysc_update_path(ctx_sc, NULL, NULL); |
| 1128 | } |
| 1129 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1130 | done: |
| 1131 | return ret; |
| 1132 | } |
| 1133 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1134 | /** |
| 1135 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1136 | * |
| 1137 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1138 | * |
| 1139 | * @param[in] ctx Compile context for logging. |
| 1140 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1141 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1142 | * @return LY_SUCCESS if everything is ok. |
| 1143 | * @return LY_EVALID if the identity is derived from itself. |
| 1144 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1145 | static LY_ERR |
| 1146 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1147 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1148 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1149 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1150 | struct ly_set recursion = {0}; |
| 1151 | struct lysc_ident *drv; |
| 1152 | |
| 1153 | if (!derived) { |
| 1154 | return LY_SUCCESS; |
| 1155 | } |
| 1156 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1157 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1158 | if (ident == derived[u]) { |
| 1159 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1160 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1161 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1162 | goto cleanup; |
| 1163 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1164 | ret = ly_set_add(&recursion, derived[u], 0, NULL); |
| 1165 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | for (v = 0; v < recursion.count; ++v) { |
| 1169 | drv = recursion.objs[v]; |
| 1170 | if (!drv->derived) { |
| 1171 | continue; |
| 1172 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1173 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1174 | if (ident == drv->derived[u]) { |
| 1175 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1176 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1177 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1178 | goto cleanup; |
| 1179 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1180 | ret = ly_set_add(&recursion, drv->derived[u], 0, NULL); |
| 1181 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1182 | } |
| 1183 | } |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1184 | |
| 1185 | cleanup: |
| 1186 | ly_set_erase(&recursion, NULL); |
| 1187 | return ret; |
| 1188 | } |
| 1189 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1190 | /** |
| 1191 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1192 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1193 | * 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] | 1194 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1195 | * to distinguish these two use cases. |
| 1196 | * |
| 1197 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1198 | * @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] | 1199 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1200 | * @param[in] bases Array of bases of identityref to fill in. |
| 1201 | * @return LY_ERR value. |
| 1202 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1203 | static LY_ERR |
Michal Vasko | 72619ce | 2020-10-06 14:05:32 +0200 | [diff] [blame] | 1204 | lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lys_module *context_module, const char **bases_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1205 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1206 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1207 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1208 | const char *s, *name; |
Michal Vasko | 72619ce | 2020-10-06 14:05:32 +0200 | [diff] [blame] | 1209 | const struct lys_module *mod; |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1210 | struct lysc_ident **idref; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1211 | |
| 1212 | assert(ident || bases); |
| 1213 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1214 | if ((LY_ARRAY_COUNT(bases_p) > 1) && (ctx->mod_def->version < 2)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1215 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1216 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1217 | return LY_EVALID; |
| 1218 | } |
| 1219 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1220 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1221 | s = strchr(bases_p[u], ':'); |
| 1222 | if (s) { |
| 1223 | /* prefixed identity */ |
| 1224 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1225 | 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] | 1226 | } else { |
| 1227 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1228 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1229 | } |
| 1230 | if (!mod) { |
| 1231 | if (ident) { |
| 1232 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1233 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1234 | } else { |
| 1235 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1236 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1237 | } |
| 1238 | return LY_EVALID; |
| 1239 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1240 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1241 | idref = NULL; |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1242 | LY_ARRAY_FOR(mod->identities, v) { |
| 1243 | if (!strcmp(name, mod->identities[v].name)) { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1244 | if (ident) { |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1245 | if (ident == &mod->identities[v]) { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1246 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1247 | "Identity \"%s\" is derived from itself.", ident->name); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1248 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1249 | } |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1250 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived)); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1251 | /* we have match! store the backlink */ |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1252 | LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1253 | *idref = ident; |
| 1254 | } else { |
| 1255 | /* we have match! store the found identity */ |
| 1256 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1257 | *idref = &mod->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1258 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1259 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | if (!idref || !(*idref)) { |
| 1263 | if (ident) { |
| 1264 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1265 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1266 | } else { |
| 1267 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1268 | "Unable to find base (%s) of identityref.", bases_p[u]); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1269 | } |
| 1270 | return LY_EVALID; |
| 1271 | } |
| 1272 | } |
| 1273 | return LY_SUCCESS; |
| 1274 | } |
| 1275 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1276 | /** |
| 1277 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1278 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1279 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1280 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1281 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1282 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1283 | static LY_ERR |
| 1284 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1285 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1286 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1287 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1288 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1289 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1290 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1291 | continue; |
| 1292 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1293 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1294 | 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] | 1295 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1296 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1297 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1298 | return LY_SUCCESS; |
| 1299 | } |
| 1300 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1301 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1302 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1303 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1304 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1305 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1306 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1307 | struct lysc_ctx context = {0}; |
| 1308 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1309 | assert(ctx_sc || ctx); |
| 1310 | |
| 1311 | if (!ctx_sc) { |
| 1312 | context.ctx = ctx; |
| 1313 | context.mod = module; |
| 1314 | context.path_len = 1; |
| 1315 | context.path[0] = '/'; |
| 1316 | ctx_sc = &context; |
| 1317 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1318 | |
| 1319 | if (!features_p) { |
| 1320 | return LY_SUCCESS; |
| 1321 | } |
| 1322 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1323 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1324 | } |
| 1325 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1326 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1327 | 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] | 1328 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1329 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1330 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1331 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1332 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1333 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done); |
| 1334 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done); |
| 1335 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1336 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1337 | (*features)[offset + u].module = ctx_sc->mod; |
| 1338 | |
| 1339 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1340 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1341 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1342 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1343 | done: |
| 1344 | return ret; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1345 | } |
| 1346 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1347 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1348 | * @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] | 1349 | * |
| 1350 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1351 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1352 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1353 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1354 | * being currently processed). |
| 1355 | * @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] | 1356 | * @return LY_SUCCESS if everything is ok. |
| 1357 | * @return LY_EVALID if the feature references indirectly itself. |
| 1358 | */ |
| 1359 | static LY_ERR |
| 1360 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1361 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1362 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1363 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1364 | struct ly_set recursion = {0}; |
| 1365 | struct lysc_feature *drv; |
| 1366 | |
| 1367 | if (!depfeatures) { |
| 1368 | return LY_SUCCESS; |
| 1369 | } |
| 1370 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1371 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1372 | if (feature == depfeatures[u]) { |
| 1373 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1374 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1375 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1376 | goto cleanup; |
| 1377 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1378 | ret = ly_set_add(&recursion, depfeatures[u], 0, NULL); |
| 1379 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | for (v = 0; v < recursion.count; ++v) { |
| 1383 | drv = recursion.objs[v]; |
| 1384 | if (!drv->depfeatures) { |
| 1385 | continue; |
| 1386 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1387 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1388 | if (feature == drv->depfeatures[u]) { |
| 1389 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1390 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1391 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1392 | goto cleanup; |
| 1393 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1394 | ly_set_add(&recursion, drv->depfeatures[u], 0, NULL); |
| 1395 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1396 | } |
| 1397 | } |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1398 | |
| 1399 | cleanup: |
| 1400 | ly_set_erase(&recursion, NULL); |
| 1401 | return ret; |
| 1402 | } |
| 1403 | |
| 1404 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1405 | * @brief Create pre-compiled features array. |
| 1406 | * |
| 1407 | * See lys_feature_precompile() for more details. |
| 1408 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1409 | * @param[in] ctx Compile context. |
| 1410 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1411 | * @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] | 1412 | * @return LY_ERR value. |
| 1413 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1414 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1415 | 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] | 1416 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1417 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1418 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1419 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1420 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1421 | /* find the preprecompiled feature */ |
| 1422 | LY_ARRAY_FOR(features, x) { |
| 1423 | if (strcmp(features[x].name, feature_p->name)) { |
| 1424 | continue; |
| 1425 | } |
| 1426 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1427 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1428 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1429 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1430 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1431 | 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] | 1432 | 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] | 1433 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1434 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1435 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1436 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1437 | /* check for circular dependency - direct reference first,... */ |
| 1438 | if (feature == feature->iffeatures[u].features[v]) { |
| 1439 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1440 | "Feature \"%s\" is referenced from itself.", feature->name); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1441 | return LY_EVALID; |
| 1442 | } |
| 1443 | /* ... and indirect circular reference */ |
| 1444 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1445 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1446 | /* add itself into the dependants list */ |
| 1447 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1448 | *df = feature; |
| 1449 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1450 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1451 | } |
| 1452 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1453 | lysc_update_path(ctx, NULL, NULL); |
| 1454 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1455 | done: |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1456 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1457 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1458 | |
| 1459 | LOGINT(ctx->ctx); |
| 1460 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1461 | } |
| 1462 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1463 | /** |
| 1464 | * @brief Revert compiled list of features back to the precompiled state. |
| 1465 | * |
| 1466 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1467 | * |
| 1468 | * @param[in] ctx Compilation context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1469 | * @param[in] mod The module structure with the features to decompile. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1470 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1471 | static void |
| 1472 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1473 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1474 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1475 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1476 | /* 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] | 1477 | * which may points into the data being freed here */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1478 | LY_ARRAY_FOR(mod->features, u) { |
| 1479 | LY_ARRAY_FOR(mod->features[u].iffeatures, v) { |
| 1480 | lysc_iffeature_free(ctx->ctx, &mod->features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1481 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1482 | LY_ARRAY_FREE(mod->features[u].iffeatures); |
| 1483 | mod->features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1484 | |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1485 | LY_ARRAY_FOR(mod->features[u].exts, v) { |
| 1486 | lysc_ext_instance_free(ctx->ctx, &(mod->features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1487 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1488 | LY_ARRAY_FREE(mod->features[u].exts); |
| 1489 | mod->features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1493 | /** |
| 1494 | * @brief Validate and normalize numeric value from a range definition. |
| 1495 | * @param[in] ctx Compile context. |
| 1496 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1497 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1498 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1499 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1500 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1501 | * @param[in] value String value of the range boundary. |
| 1502 | * @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. |
| 1503 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1504 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1505 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1506 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1507 | 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] | 1508 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1509 | size_t fraction = 0, size; |
| 1510 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1511 | *len = 0; |
| 1512 | |
| 1513 | assert(value); |
| 1514 | /* parse value */ |
| 1515 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1516 | return LY_EVALID; |
| 1517 | } |
| 1518 | |
| 1519 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1520 | ++(*len); |
| 1521 | } |
| 1522 | |
| 1523 | while (isdigit(value[*len])) { |
| 1524 | ++(*len); |
| 1525 | } |
| 1526 | |
| 1527 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1528 | if (basetype == LY_TYPE_DEC64) { |
| 1529 | goto decimal; |
| 1530 | } else { |
| 1531 | *valcopy = strndup(value, *len); |
| 1532 | return LY_SUCCESS; |
| 1533 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1534 | } |
| 1535 | fraction = *len; |
| 1536 | |
| 1537 | ++(*len); |
| 1538 | while (isdigit(value[*len])) { |
| 1539 | ++(*len); |
| 1540 | } |
| 1541 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1542 | if (basetype == LY_TYPE_DEC64) { |
| 1543 | decimal: |
| 1544 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1545 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1546 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1547 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1548 | *len, value, frdigits); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1549 | return LY_EINVAL; |
| 1550 | } |
| 1551 | if (fraction) { |
| 1552 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1553 | } else { |
| 1554 | size = (*len) + frdigits + 1; |
| 1555 | } |
| 1556 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1557 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1558 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1559 | (*valcopy)[size - 1] = '\0'; |
| 1560 | if (fraction) { |
| 1561 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1562 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1563 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1564 | } else { |
| 1565 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1566 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1567 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1568 | } |
| 1569 | return LY_SUCCESS; |
| 1570 | } |
| 1571 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1572 | /** |
| 1573 | * @brief Check that values in range are in ascendant order. |
| 1574 | * @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] | 1575 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1576 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1577 | * @param[in] value Current value to check. |
| 1578 | * @param[in] prev_value The last seen value. |
| 1579 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1580 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1581 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1582 | range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1583 | { |
| 1584 | if (unsigned_value) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1585 | 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] | 1586 | return LY_EEXIST; |
| 1587 | } |
| 1588 | } else { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1589 | if ((max && (prev_value > value)) || (!max && (prev_value >= value))) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1590 | return LY_EEXIST; |
| 1591 | } |
| 1592 | } |
| 1593 | return LY_SUCCESS; |
| 1594 | } |
| 1595 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1596 | /** |
| 1597 | * @brief Set min/max value of the range part. |
| 1598 | * @param[in] ctx Compile context. |
| 1599 | * @param[in] part Range part structure to fill. |
| 1600 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1601 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1602 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1603 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1604 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1605 | * @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] | 1606 | * @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] | 1607 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1608 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1609 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1610 | * frdigits value), LY_EMEM. |
| 1611 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1612 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1613 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype, |
| 1614 | ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1615 | { |
| 1616 | LY_ERR ret = LY_SUCCESS; |
| 1617 | char *valcopy = NULL; |
| 1618 | size_t len; |
| 1619 | |
| 1620 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1621 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1622 | LY_CHECK_GOTO(ret, finalize); |
| 1623 | } |
| 1624 | if (!valcopy && base_range) { |
| 1625 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1626 | 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] | 1627 | } else { |
| 1628 | part->min_64 = base_range->parts[0].min_64; |
| 1629 | } |
| 1630 | if (!first) { |
| 1631 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1632 | } |
| 1633 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1637 | case LY_TYPE_INT8: /* range */ |
| 1638 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1639 | 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] | 1640 | } else if (max) { |
| 1641 | part->max_64 = INT64_C(127); |
| 1642 | } else { |
| 1643 | part->min_64 = INT64_C(-128); |
| 1644 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1645 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1646 | 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] | 1647 | } |
| 1648 | break; |
| 1649 | case LY_TYPE_INT16: /* range */ |
| 1650 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1651 | 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] | 1652 | } else if (max) { |
| 1653 | part->max_64 = INT64_C(32767); |
| 1654 | } else { |
| 1655 | part->min_64 = INT64_C(-32768); |
| 1656 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1657 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1658 | 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] | 1659 | } |
| 1660 | break; |
| 1661 | case LY_TYPE_INT32: /* range */ |
| 1662 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1663 | 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] | 1664 | } else if (max) { |
| 1665 | part->max_64 = INT64_C(2147483647); |
| 1666 | } else { |
| 1667 | part->min_64 = INT64_C(-2147483648); |
| 1668 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1669 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1670 | 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] | 1671 | } |
| 1672 | break; |
| 1673 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1674 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1675 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1676 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1677 | max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1678 | } else if (max) { |
| 1679 | part->max_64 = INT64_C(9223372036854775807); |
| 1680 | } else { |
| 1681 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1682 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1683 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1684 | 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] | 1685 | } |
| 1686 | break; |
| 1687 | case LY_TYPE_UINT8: /* range */ |
| 1688 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1689 | 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] | 1690 | } else if (max) { |
| 1691 | part->max_u64 = UINT64_C(255); |
| 1692 | } else { |
| 1693 | part->min_u64 = UINT64_C(0); |
| 1694 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1695 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1696 | 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] | 1697 | } |
| 1698 | break; |
| 1699 | case LY_TYPE_UINT16: /* range */ |
| 1700 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1701 | 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] | 1702 | } else if (max) { |
| 1703 | part->max_u64 = UINT64_C(65535); |
| 1704 | } else { |
| 1705 | part->min_u64 = UINT64_C(0); |
| 1706 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1707 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1708 | 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] | 1709 | } |
| 1710 | break; |
| 1711 | case LY_TYPE_UINT32: /* range */ |
| 1712 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1713 | 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] | 1714 | } else if (max) { |
| 1715 | part->max_u64 = UINT64_C(4294967295); |
| 1716 | } else { |
| 1717 | part->min_u64 = UINT64_C(0); |
| 1718 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1719 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1720 | 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] | 1721 | } |
| 1722 | break; |
| 1723 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1724 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1725 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1726 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1727 | 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] | 1728 | } else if (max) { |
| 1729 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1730 | } else { |
| 1731 | part->min_u64 = UINT64_C(0); |
| 1732 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1733 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1734 | 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] | 1735 | } |
| 1736 | break; |
| 1737 | default: |
| 1738 | LOGINT(ctx->ctx); |
| 1739 | ret = LY_EINT; |
| 1740 | } |
| 1741 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1742 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1743 | if (ret == LY_EDENIED) { |
| 1744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1745 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1746 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1747 | } else if (ret == LY_EVALID) { |
| 1748 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1749 | "Invalid %s restriction - invalid value \"%s\".", |
| 1750 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1751 | } else if (ret == LY_EEXIST) { |
| 1752 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1753 | "Invalid %s restriction - values are not in ascending order (%s).", |
| 1754 | length_restr ? "length" : "range", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1755 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1756 | } else if (!ret && value) { |
| 1757 | *value = *value + len; |
| 1758 | } |
| 1759 | free(valcopy); |
| 1760 | return ret; |
| 1761 | } |
| 1762 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1763 | /** |
| 1764 | * @brief Compile the parsed range restriction. |
| 1765 | * @param[in] ctx Compile context. |
| 1766 | * @param[in] range_p Parsed range structure to compile. |
| 1767 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1768 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1769 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1770 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1771 | * range restriction must be more restrictive than the base_range. |
| 1772 | * @param[in,out] range Pointer to the created current range structure. |
| 1773 | * @return LY_ERR value. |
| 1774 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1775 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1776 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1777 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1778 | { |
| 1779 | LY_ERR ret = LY_EVALID; |
| 1780 | const char *expr; |
| 1781 | struct lysc_range_part *parts = NULL, *part; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1782 | ly_bool range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1783 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1784 | |
| 1785 | assert(range); |
| 1786 | assert(range_p); |
| 1787 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1788 | expr = range_p->arg.str; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1789 | while (1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1790 | if (isspace(*expr)) { |
| 1791 | ++expr; |
| 1792 | } else if (*expr == '\0') { |
| 1793 | if (range_expected) { |
| 1794 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1795 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1796 | length_restr ? "length" : "range", range_p->arg); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1797 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1798 | } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1799 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1800 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1801 | length_restr ? "length" : "range", range_p->arg); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1802 | goto cleanup; |
| 1803 | } |
| 1804 | parts_done++; |
| 1805 | break; |
| 1806 | } else if (!strncmp(expr, "min", 3)) { |
| 1807 | if (parts) { |
| 1808 | /* min cannot be used elsewhere than in the first part */ |
| 1809 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1810 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1811 | expr - range_p->arg.str, range_p->arg.str); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1812 | goto cleanup; |
| 1813 | } |
| 1814 | expr += 3; |
| 1815 | |
| 1816 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1817 | 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] | 1818 | part->max_64 = part->min_64; |
| 1819 | } else if (*expr == '|') { |
| 1820 | if (!parts || range_expected) { |
| 1821 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1822 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1823 | goto cleanup; |
| 1824 | } |
| 1825 | expr++; |
| 1826 | parts_done++; |
| 1827 | /* process next part of the expression */ |
| 1828 | } else if (!strncmp(expr, "..", 2)) { |
| 1829 | expr += 2; |
| 1830 | while (isspace(*expr)) { |
| 1831 | expr++; |
| 1832 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1833 | if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1834 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1835 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1836 | goto cleanup; |
| 1837 | } |
| 1838 | /* continue expecting the upper boundary */ |
| 1839 | range_expected = 1; |
| 1840 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1841 | /* number */ |
| 1842 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1843 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1844 | 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] | 1845 | range_expected = 0; |
| 1846 | } else { |
| 1847 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1848 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1849 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1850 | part->max_64 = part->min_64; |
| 1851 | } |
| 1852 | |
| 1853 | /* continue with possible another expression part */ |
| 1854 | } else if (!strncmp(expr, "max", 3)) { |
| 1855 | expr += 3; |
| 1856 | while (isspace(*expr)) { |
| 1857 | expr++; |
| 1858 | } |
| 1859 | if (*expr != '\0') { |
| 1860 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1861 | length_restr ? "length" : "range", expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1862 | goto cleanup; |
| 1863 | } |
| 1864 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1865 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1866 | 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] | 1867 | range_expected = 0; |
| 1868 | } else { |
| 1869 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1870 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1871 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1872 | part->min_64 = part->max_64; |
| 1873 | } |
| 1874 | } else { |
| 1875 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1876 | length_restr ? "length" : "range", expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1877 | goto cleanup; |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | /* check with the previous range/length restriction */ |
| 1882 | if (base_range) { |
| 1883 | switch (basetype) { |
| 1884 | case LY_TYPE_BINARY: |
| 1885 | case LY_TYPE_UINT8: |
| 1886 | case LY_TYPE_UINT16: |
| 1887 | case LY_TYPE_UINT32: |
| 1888 | case LY_TYPE_UINT64: |
| 1889 | case LY_TYPE_STRING: |
| 1890 | uns = 1; |
| 1891 | break; |
| 1892 | case LY_TYPE_DEC64: |
| 1893 | case LY_TYPE_INT8: |
| 1894 | case LY_TYPE_INT16: |
| 1895 | case LY_TYPE_INT32: |
| 1896 | case LY_TYPE_INT64: |
| 1897 | uns = 0; |
| 1898 | break; |
| 1899 | default: |
| 1900 | LOGINT(ctx->ctx); |
| 1901 | ret = LY_EINT; |
| 1902 | goto cleanup; |
| 1903 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1904 | for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1905 | if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1906 | goto baseerror; |
| 1907 | } |
| 1908 | /* current lower bound is not lower than the base */ |
| 1909 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1910 | /* base has single value */ |
| 1911 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1912 | /* both lower bounds are the same */ |
| 1913 | if (parts[u].min_64 != parts[u].max_64) { |
| 1914 | /* current continues with a range */ |
| 1915 | goto baseerror; |
| 1916 | } else { |
| 1917 | /* equal single values, move both forward */ |
| 1918 | ++v; |
| 1919 | continue; |
| 1920 | } |
| 1921 | } else { |
| 1922 | /* base is single value lower than current range, so the |
| 1923 | * value from base range is removed in the current, |
| 1924 | * move only base and repeat checking */ |
| 1925 | ++v; |
| 1926 | --u; |
| 1927 | continue; |
| 1928 | } |
| 1929 | } else { |
| 1930 | /* base is the range */ |
| 1931 | if (parts[u].min_64 == parts[u].max_64) { |
| 1932 | /* current is a single value */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1933 | if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1934 | /* current is behind the base range, so base range is omitted, |
| 1935 | * move the base and keep the current for further check */ |
| 1936 | ++v; |
| 1937 | --u; |
| 1938 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1939 | continue; |
| 1940 | } else { |
| 1941 | /* both are ranges - check the higher bound, the lower was already checked */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1942 | if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1943 | /* higher bound is higher than the current higher bound */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1944 | if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1945 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1946 | * continue with the same current, but move the base */ |
| 1947 | --u; |
| 1948 | ++v; |
| 1949 | continue; |
| 1950 | } |
| 1951 | /* current range starts within the base range but end behind it */ |
| 1952 | goto baseerror; |
| 1953 | } else { |
| 1954 | /* current range is smaller than the base, |
| 1955 | * move current, but stay with the base */ |
| 1956 | continue; |
| 1957 | } |
| 1958 | } |
| 1959 | } |
| 1960 | } |
| 1961 | if (u != parts_done) { |
| 1962 | baseerror: |
| 1963 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1964 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1965 | length_restr ? "length" : "range", range_p->arg); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1966 | goto cleanup; |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | if (!(*range)) { |
| 1971 | *range = calloc(1, sizeof **range); |
| 1972 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1973 | } |
| 1974 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1975 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1976 | if (range_p->eapptag) { |
| 1977 | lydict_remove(ctx->ctx, (*range)->eapptag); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1978 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1979 | } |
| 1980 | if (range_p->emsg) { |
| 1981 | lydict_remove(ctx->ctx, (*range)->emsg); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1982 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1983 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1984 | if (range_p->dsc) { |
| 1985 | lydict_remove(ctx->ctx, (*range)->dsc); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1986 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1987 | } |
| 1988 | if (range_p->ref) { |
| 1989 | lydict_remove(ctx->ctx, (*range)->ref); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1990 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1991 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1992 | /* extensions are taken only from the last range by the caller */ |
| 1993 | |
| 1994 | (*range)->parts = parts; |
| 1995 | parts = NULL; |
| 1996 | ret = LY_SUCCESS; |
| 1997 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1998 | LY_ARRAY_FREE(parts); |
| 1999 | |
| 2000 | return ret; |
| 2001 | } |
| 2002 | |
| 2003 | /** |
| 2004 | * @brief Checks pattern syntax. |
| 2005 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2006 | * @param[in] ctx Context. |
| 2007 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2008 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2009 | * @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] | 2010 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2011 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2012 | LY_ERR |
| 2013 | 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] | 2014 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2015 | size_t idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2016 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2017 | int err_code; |
| 2018 | const char *orig_ptr; |
| 2019 | PCRE2_SIZE err_offset; |
| 2020 | pcre2_code *code_local; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2021 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2022 | #define URANGE_LEN 19 |
| 2023 | char *ublock2urange[][2] = { |
| 2024 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 2025 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 2026 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 2027 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 2028 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 2029 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 2030 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 2031 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 2032 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 2033 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 2034 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 2035 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 2036 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 2037 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 2038 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 2039 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 2040 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 2041 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 2042 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 2043 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 2044 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 2045 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 2046 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 2047 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 2048 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 2049 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 2050 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 2051 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 2052 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 2053 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 2054 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 2055 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 2056 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 2057 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 2058 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 2059 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 2060 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 2061 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 2062 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 2063 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 2064 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 2065 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 2066 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 2067 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 2068 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 2069 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 2070 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 2071 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 2072 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 2073 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 2074 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 2075 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 2076 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 2077 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 2078 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 2079 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 2080 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 2081 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 2082 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 2083 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 2084 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 2085 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 2086 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 2087 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 2088 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 2089 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 2090 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 2091 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 2092 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 2093 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 2094 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 2095 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 2096 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 2097 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 2098 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 2099 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 2100 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 2101 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 2102 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 2103 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 2104 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 2105 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 2106 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 2107 | {NULL, NULL} |
| 2108 | }; |
| 2109 | |
| 2110 | /* adjust the expression to a Perl equivalent |
| 2111 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 2112 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2113 | /* allocate space for the transformed pattern */ |
| 2114 | size = strlen(pattern) + 1; |
| 2115 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2116 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2117 | perl_regex[0] = '\0'; |
| 2118 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2119 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 2120 | brack = 0; |
| 2121 | idx = 0; |
| 2122 | orig_ptr = pattern; |
| 2123 | while (orig_ptr[0]) { |
| 2124 | switch (orig_ptr[0]) { |
| 2125 | case '$': |
| 2126 | case '^': |
| 2127 | if (!brack) { |
| 2128 | /* make space for the extra character */ |
| 2129 | ++size; |
| 2130 | perl_regex = ly_realloc(perl_regex, size); |
| 2131 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2132 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2133 | /* print escape slash */ |
| 2134 | perl_regex[idx] = '\\'; |
| 2135 | ++idx; |
| 2136 | } |
| 2137 | break; |
| 2138 | case '[': |
| 2139 | /* must not be escaped */ |
| 2140 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2141 | ++brack; |
| 2142 | } |
| 2143 | break; |
| 2144 | case ']': |
| 2145 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2146 | /* pattern was checked and compiled already */ |
| 2147 | assert(brack); |
| 2148 | --brack; |
| 2149 | } |
| 2150 | break; |
| 2151 | default: |
| 2152 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2153 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2154 | |
| 2155 | /* copy char */ |
| 2156 | perl_regex[idx] = orig_ptr[0]; |
| 2157 | |
| 2158 | ++idx; |
| 2159 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2160 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2161 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2162 | |
| 2163 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2164 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2165 | start = ptr - perl_regex; |
| 2166 | |
| 2167 | ptr = strchr(ptr, '}'); |
| 2168 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2169 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2170 | pattern, perl_regex + start + 2, "unterminated character property"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2171 | free(perl_regex); |
| 2172 | return LY_EVALID; |
| 2173 | } |
| 2174 | end = (ptr - perl_regex) + 1; |
| 2175 | |
| 2176 | /* need more space */ |
| 2177 | if (end - start < URANGE_LEN) { |
| 2178 | 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] | 2179 | 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] | 2180 | } |
| 2181 | |
| 2182 | /* find our range */ |
| 2183 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2184 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2185 | break; |
| 2186 | } |
| 2187 | } |
| 2188 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2189 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2190 | pattern, perl_regex + start + 5, "unknown block name"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2191 | free(perl_regex); |
| 2192 | return LY_EVALID; |
| 2193 | } |
| 2194 | |
| 2195 | /* 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] | 2196 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2197 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2198 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2199 | } |
| 2200 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2201 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2202 | } |
| 2203 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2204 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2205 | /* skip brackets */ |
| 2206 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2207 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2208 | } else { |
| 2209 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2210 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2215 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2216 | PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE, |
| 2217 | &err_code, &err_offset, NULL); |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2218 | if (!code_local) { |
| 2219 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2220 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2221 | 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] | 2222 | free(perl_regex); |
| 2223 | return LY_EVALID; |
| 2224 | } |
| 2225 | free(perl_regex); |
| 2226 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2227 | if (code) { |
| 2228 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2229 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2230 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | return LY_SUCCESS; |
| 2234 | |
| 2235 | #undef URANGE_LEN |
| 2236 | } |
| 2237 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2238 | /** |
| 2239 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2240 | * @param[in] ctx Compile context. |
| 2241 | * @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] | 2242 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2243 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2244 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2245 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2246 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2247 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2248 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2249 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2250 | { |
| 2251 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2252 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2253 | LY_ERR ret = LY_SUCCESS; |
| 2254 | |
| 2255 | /* first, copy the patterns from the base type */ |
| 2256 | if (base_patterns) { |
| 2257 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2258 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2259 | } |
| 2260 | |
| 2261 | LY_ARRAY_FOR(patterns_p, u) { |
| 2262 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2263 | *pattern = calloc(1, sizeof **pattern); |
| 2264 | ++(*pattern)->refcount; |
| 2265 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2266 | ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg.str[1], &(*pattern)->code); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2267 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2268 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2269 | if (patterns_p[u].arg.str[0] == 0x15) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2270 | (*pattern)->inverted = 1; |
| 2271 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2272 | DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2273 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done); |
| 2274 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done); |
| 2275 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done); |
| 2276 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2277 | 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] | 2278 | } |
| 2279 | done: |
| 2280 | return ret; |
| 2281 | } |
| 2282 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2283 | /** |
| 2284 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2285 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2286 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2287 | 0 /* LY_TYPE_UNKNOWN */, |
| 2288 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2289 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2290 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2291 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2292 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2293 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2294 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2295 | 0 /* LY_TYPE_BOOL */, |
| 2296 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2297 | 0 /* LY_TYPE_EMPTY */, |
| 2298 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2299 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2300 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2301 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2302 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2303 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2304 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2305 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2306 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2307 | }; |
| 2308 | |
| 2309 | /** |
| 2310 | * @brief stringification of the YANG built-in data types |
| 2311 | */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2312 | const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = { |
| 2313 | "unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2314 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2315 | "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer" |
| 2316 | }; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2317 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2318 | /** |
| 2319 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2320 | * @param[in] ctx Compile context. |
| 2321 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2322 | * @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] | 2323 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2324 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2325 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2326 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2327 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2328 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2329 | 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] | 2330 | { |
| 2331 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2332 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2333 | int32_t value = 0; |
| 2334 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2335 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2336 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2337 | if (base_enums && (ctx->mod_def->version < 2)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2338 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2339 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2340 | return LY_EVALID; |
| 2341 | } |
| 2342 | |
| 2343 | LY_ARRAY_FOR(enums_p, u) { |
| 2344 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2345 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done); |
| 2346 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done); |
| 2347 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2348 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2349 | if (base_enums) { |
| 2350 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2351 | LY_ARRAY_FOR(base_enums, v) { |
| 2352 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2353 | break; |
| 2354 | } |
| 2355 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2356 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2357 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2358 | "Invalid %s - derived type adds new item \"%s\".", |
| 2359 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2360 | return LY_EVALID; |
| 2361 | } |
| 2362 | match = v; |
| 2363 | } |
| 2364 | |
| 2365 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2366 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2367 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2368 | e->value = (int32_t)enums_p[u].value; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2369 | if (!u || (e->value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2370 | value = e->value + 1; |
| 2371 | } |
| 2372 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2373 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2374 | if (e->value == (*enums)[v].value) { |
| 2375 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2376 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2377 | e->value, e->name, (*enums)[v].name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2378 | return LY_EVALID; |
| 2379 | } |
| 2380 | } |
| 2381 | } else if (base_enums) { |
| 2382 | /* inherit the assigned value */ |
| 2383 | e->value = base_enums[match].value; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2384 | if (!u || (e->value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2385 | value = e->value + 1; |
| 2386 | } |
| 2387 | } else { |
| 2388 | /* assign value automatically */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2389 | if (u && (value == INT32_MIN)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2390 | /* counter overflow */ |
| 2391 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2392 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2393 | "\"%s\" since the highest value is already 2147483647.", e->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2394 | return LY_EVALID; |
| 2395 | } |
| 2396 | e->value = value++; |
| 2397 | } |
| 2398 | } else { /* LY_TYPE_BITS */ |
| 2399 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2400 | e->value = (int32_t)enums_p[u].value; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2401 | if (!u || ((uint32_t)e->value >= position)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2402 | position = (uint32_t)e->value + 1; |
| 2403 | } |
| 2404 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2405 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2406 | if (e->value == (*enums)[v].value) { |
| 2407 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2408 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2409 | (uint32_t)e->value, e->name, (*enums)[v].name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2410 | return LY_EVALID; |
| 2411 | } |
| 2412 | } |
| 2413 | } else if (base_enums) { |
| 2414 | /* inherit the assigned value */ |
| 2415 | e->value = base_enums[match].value; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2416 | if (!u || ((uint32_t)e->value >= position)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2417 | position = (uint32_t)e->value + 1; |
| 2418 | } |
| 2419 | } else { |
| 2420 | /* assign value automatically */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2421 | if (u && (position == 0)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2422 | /* counter overflow */ |
| 2423 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2424 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2425 | "\"%s\" since the highest value is already 4294967295.", e->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2426 | return LY_EVALID; |
| 2427 | } |
| 2428 | e->value = position++; |
| 2429 | } |
| 2430 | } |
| 2431 | |
| 2432 | if (base_enums) { |
| 2433 | /* the assigned values must not change from the derived type */ |
| 2434 | if (e->value != base_enums[match].value) { |
| 2435 | if (basetype == LY_TYPE_ENUM) { |
| 2436 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2437 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2438 | e->name, base_enums[match].value, e->value); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2439 | } else { |
| 2440 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2441 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2442 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2443 | } |
| 2444 | return LY_EVALID; |
| 2445 | } |
| 2446 | } |
| 2447 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2448 | 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] | 2449 | 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] | 2450 | |
| 2451 | if (basetype == LY_TYPE_BITS) { |
| 2452 | /* keep bits ordered by position */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2453 | for (v = u; v && (*enums)[v - 1].value > e->value; --v) {} |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2454 | if (v != u) { |
| 2455 | memcpy(&storage, e, sizeof *e); |
| 2456 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2457 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2458 | } |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | done: |
| 2463 | return ret; |
| 2464 | } |
| 2465 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2466 | /** |
| 2467 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2468 | * |
| 2469 | * path-arg = absolute-path / relative-path |
| 2470 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2471 | * relative-path = 1*(".." "/") descendant-path |
| 2472 | * |
| 2473 | * @param[in,out] path Path to parse. |
| 2474 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2475 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2476 | * @param[out] name Name of the token. |
| 2477 | * @param[out] nam_len Length of the name. |
| 2478 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2479 | * must not be changed between consecutive calls. -1 if the |
| 2480 | * path is absolute. |
| 2481 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2482 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2483 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2484 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2485 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2486 | int32_t *parent_times, ly_bool *has_predicate) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2487 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2488 | int32_t par_times = 0; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2489 | |
| 2490 | assert(path && *path); |
| 2491 | assert(parent_times); |
| 2492 | assert(prefix); |
| 2493 | assert(prefix_len); |
| 2494 | assert(name); |
| 2495 | assert(name_len); |
| 2496 | assert(has_predicate); |
| 2497 | |
| 2498 | *prefix = NULL; |
| 2499 | *prefix_len = 0; |
| 2500 | *name = NULL; |
| 2501 | *name_len = 0; |
| 2502 | *has_predicate = 0; |
| 2503 | |
| 2504 | if (!*parent_times) { |
| 2505 | if (!strncmp(*path, "..", 2)) { |
| 2506 | *path += 2; |
| 2507 | ++par_times; |
| 2508 | while (!strncmp(*path, "/..", 3)) { |
| 2509 | *path += 3; |
| 2510 | ++par_times; |
| 2511 | } |
| 2512 | } |
| 2513 | if (par_times) { |
| 2514 | *parent_times = par_times; |
| 2515 | } else { |
| 2516 | *parent_times = -1; |
| 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | if (**path != '/') { |
| 2521 | return LY_EINVAL; |
| 2522 | } |
| 2523 | /* skip '/' */ |
| 2524 | ++(*path); |
| 2525 | |
| 2526 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2527 | 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] | 2528 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2529 | if (((**path == '/') && (*path)[1]) || !**path) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2530 | /* path continues by another token or this is the last token */ |
| 2531 | return LY_SUCCESS; |
| 2532 | } else if ((*path)[0] != '[') { |
| 2533 | /* unexpected character */ |
| 2534 | return LY_EINVAL; |
| 2535 | } else { |
| 2536 | /* predicate starting with [ */ |
| 2537 | *has_predicate = 1; |
| 2538 | return LY_SUCCESS; |
| 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2543 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2544 | * |
| 2545 | * The set of features used for target must be a subset of features used for the leafref. |
| 2546 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2547 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2548 | * |
| 2549 | * @param[in] refnode The leafref node. |
| 2550 | * @param[in] target Tha target node of the leafref. |
| 2551 | * @return LY_SUCCESS or LY_EVALID; |
| 2552 | */ |
| 2553 | static LY_ERR |
| 2554 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2555 | { |
| 2556 | LY_ERR ret = LY_EVALID; |
| 2557 | const struct lysc_node *iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2558 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2559 | struct ly_set features = {0}; |
| 2560 | |
| 2561 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2562 | if (iter->iffeatures) { |
| 2563 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2564 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2565 | LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | /* we should have, in features set, a superset of features applicable to the target node. |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2572 | * If the feature is not present, we don;t have a subset of features applicable |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2573 | * to the leafref itself. */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2574 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2575 | if (iter->iffeatures) { |
| 2576 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2577 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2578 | if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) { |
| 2579 | /* feature not present */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2580 | goto cleanup; |
| 2581 | } |
| 2582 | } |
| 2583 | } |
| 2584 | } |
| 2585 | } |
| 2586 | ret = LY_SUCCESS; |
| 2587 | |
| 2588 | cleanup: |
| 2589 | ly_set_erase(&features, NULL); |
| 2590 | return ret; |
| 2591 | } |
| 2592 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2593 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2594 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2595 | struct lysc_type **type, const char **units, struct lysp_qname **dflt); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2596 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2597 | /** |
| 2598 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2599 | * @param[in] ctx Compile context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2600 | * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2601 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2602 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2603 | * @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] | 2604 | * @param[in] type_p Parsed type to compile. |
| 2605 | * @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] | 2606 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2607 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2608 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2609 | * @return LY_ERR value. |
| 2610 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2611 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2612 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 2613 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, LY_DATA_TYPE basetype, |
| 2614 | const char *tpdfname, struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2615 | { |
| 2616 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2617 | struct lysc_type_bin *bin; |
| 2618 | struct lysc_type_num *num; |
| 2619 | struct lysc_type_str *str; |
| 2620 | struct lysc_type_bits *bits; |
| 2621 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2622 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2623 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2624 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2625 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2626 | |
| 2627 | switch (basetype) { |
| 2628 | case LY_TYPE_BINARY: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2629 | bin = (struct lysc_type_bin *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2630 | |
| 2631 | /* 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] | 2632 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2633 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2634 | base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2635 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2636 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, cleanup); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2637 | } |
| 2638 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2639 | break; |
| 2640 | case LY_TYPE_BITS: |
| 2641 | /* RFC 7950 9.7 - bits */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2642 | bits = (struct lysc_type_bits *)(*type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2643 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2644 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2645 | base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2646 | (struct lysc_type_bitenum_item **)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2647 | } |
| 2648 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2649 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2650 | /* 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] | 2651 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2652 | 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] | 2653 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2654 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", ""); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2655 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2656 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2657 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2658 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2659 | case LY_TYPE_DEC64: |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2660 | dec = (struct lysc_type_dec *)(*type); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2661 | |
| 2662 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2663 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2664 | if (!type_p->fraction_digits) { |
| 2665 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2666 | 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] | 2667 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2668 | 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] | 2669 | } |
| 2670 | return LY_EVALID; |
| 2671 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2672 | dec->fraction_digits = type_p->fraction_digits; |
| 2673 | } else { |
| 2674 | if (type_p->fraction_digits) { |
| 2675 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
| 2676 | if (tpdfname) { |
| 2677 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2678 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
| 2679 | tpdfname); |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2680 | } else { |
| 2681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2682 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2683 | } |
| 2684 | return LY_EVALID; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2685 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2686 | dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2687 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2688 | |
| 2689 | /* RFC 7950 9.2.4 - range */ |
| 2690 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2691 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2692 | base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2693 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2694 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, cleanup); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2695 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2696 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2697 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2698 | case LY_TYPE_STRING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2699 | str = (struct lysc_type_str *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2700 | |
| 2701 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2702 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2703 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2704 | base ? ((struct lysc_type_str *)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2705 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2706 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, cleanup); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2707 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2708 | } else if (base && ((struct lysc_type_str *)base)->length) { |
| 2709 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | /* RFC 7950 9.4.5 - pattern */ |
| 2713 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2714 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2715 | base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2716 | } else if (base && ((struct lysc_type_str *)base)->patterns) { |
| 2717 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2718 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2719 | break; |
| 2720 | case LY_TYPE_ENUM: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2721 | enumeration = (struct lysc_type_enum *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2722 | |
| 2723 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2724 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2725 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2726 | base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2727 | } |
| 2728 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2729 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2730 | /* 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] | 2731 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2732 | 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] | 2733 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", ""); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2735 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2736 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2737 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2738 | break; |
| 2739 | case LY_TYPE_INT8: |
| 2740 | case LY_TYPE_UINT8: |
| 2741 | case LY_TYPE_INT16: |
| 2742 | case LY_TYPE_UINT16: |
| 2743 | case LY_TYPE_INT32: |
| 2744 | case LY_TYPE_UINT32: |
| 2745 | case LY_TYPE_INT64: |
| 2746 | case LY_TYPE_UINT64: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2747 | num = (struct lysc_type_num *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2748 | |
| 2749 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2750 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2751 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2752 | base ? ((struct lysc_type_num *)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2753 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2754 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, cleanup); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2755 | } |
| 2756 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2757 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2758 | case LY_TYPE_IDENT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2759 | idref = (struct lysc_type_identityref *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2760 | |
| 2761 | /* RFC 7950 9.10.2 - base */ |
| 2762 | if (type_p->bases) { |
| 2763 | if (base) { |
| 2764 | /* only the directly derived identityrefs can contain base specification */ |
| 2765 | if (tpdfname) { |
| 2766 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2767 | "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.", |
| 2768 | tpdfname); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2769 | } else { |
| 2770 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2771 | "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] | 2772 | } |
| 2773 | return LY_EVALID; |
| 2774 | } |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 2775 | LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->mod, type_p->bases, NULL, &idref->bases)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | if (!base && !type_p->flags) { |
| 2779 | /* type derived from identityref built-in type must contain at least one base */ |
| 2780 | if (tpdfname) { |
| 2781 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2782 | } else { |
| 2783 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2784 | } |
| 2785 | return LY_EVALID; |
| 2786 | } |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2787 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2788 | case LY_TYPE_LEAFREF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2789 | lref = (struct lysc_type_leafref *)*type; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2790 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2791 | /* RFC 7950 9.9.3 - require-instance */ |
| 2792 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2793 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2794 | if (tpdfname) { |
| 2795 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2796 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2797 | } else { |
| 2798 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2799 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2800 | } |
| 2801 | return LY_EVALID; |
| 2802 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2803 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2804 | } else if (base) { |
| 2805 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2806 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2807 | } else { |
| 2808 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2809 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2810 | } |
| 2811 | if (type_p->path) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2812 | LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path)); |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 2813 | lref->path_mod = type_p->mod; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2814 | } else if (base) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2815 | LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path)); |
Michal Vasko | 72619ce | 2020-10-06 14:05:32 +0200 | [diff] [blame] | 2816 | lref->path_mod = ((struct lysc_type_leafref *)base)->path_mod; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2817 | } else if (tpdfname) { |
| 2818 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2819 | return LY_EVALID; |
| 2820 | } else { |
| 2821 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2822 | return LY_EVALID; |
| 2823 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2824 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2825 | case LY_TYPE_INST: |
| 2826 | /* RFC 7950 9.9.3 - require-instance */ |
| 2827 | if (type_p->flags & LYS_SET_REQINST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2828 | ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2829 | } else { |
| 2830 | /* default is true */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2831 | ((struct lysc_type_instanceid *)(*type))->require_instance = 1; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2832 | } |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2833 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2834 | case LY_TYPE_UNION: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2835 | un = (struct lysc_type_union *)(*type); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2836 | |
| 2837 | /* RFC 7950 7.4 - type */ |
| 2838 | if (type_p->types) { |
| 2839 | if (base) { |
| 2840 | /* only the directly derived union can contain types specification */ |
| 2841 | if (tpdfname) { |
| 2842 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2843 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2844 | tpdfname); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2845 | } else { |
| 2846 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2847 | "Invalid type substatement for the type not directly derived from union built-in type."); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2848 | } |
| 2849 | return LY_EVALID; |
| 2850 | } |
| 2851 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2852 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2853 | for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2854 | LY_CHECK_RET(lys_compile_type(ctx, context_pnode, context_flags, context_mod, context_name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2855 | &type_p->types[u], &un->types[u + additional], NULL, NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2856 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2857 | /* add space for additional types from the union subtype */ |
| 2858 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2859 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2860 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2861 | |
| 2862 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2863 | 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] | 2864 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2865 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2866 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2867 | 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] | 2868 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2869 | |
| 2870 | lref->basetype = LY_TYPE_LEAFREF; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2871 | LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2872 | lref->refcount = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2873 | lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance; |
Michal Vasko | 72619ce | 2020-10-06 14:05:32 +0200 | [diff] [blame] | 2874 | lref->path_mod = ((struct lysc_type_leafref *)un_aux->types[v])->path_mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2875 | /* TODO extensions */ |
| 2876 | |
| 2877 | } else { |
| 2878 | un->types[u + additional] = un_aux->types[v]; |
| 2879 | ++un_aux->types[v]->refcount; |
| 2880 | } |
| 2881 | ++additional; |
| 2882 | LY_ARRAY_INCREMENT(un->types); |
| 2883 | } |
| 2884 | /* compensate u increment in main loop */ |
| 2885 | --additional; |
| 2886 | |
| 2887 | /* free the replaced union subtype */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2888 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2889 | } else { |
| 2890 | LY_ARRAY_INCREMENT(un->types); |
| 2891 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | if (!base && !type_p->flags) { |
| 2896 | /* type derived from union built-in type must contain at least one type */ |
| 2897 | if (tpdfname) { |
| 2898 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2899 | } else { |
| 2900 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2901 | } |
| 2902 | return LY_EVALID; |
| 2903 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2904 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2905 | case LY_TYPE_BOOL: |
| 2906 | case LY_TYPE_EMPTY: |
| 2907 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2908 | break; |
| 2909 | } |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2910 | |
| 2911 | if (tpdfname) { |
| 2912 | switch (basetype) { |
| 2913 | case LY_TYPE_BINARY: |
| 2914 | type_p->compiled = *type; |
| 2915 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2916 | break; |
| 2917 | case LY_TYPE_BITS: |
| 2918 | type_p->compiled = *type; |
| 2919 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2920 | break; |
| 2921 | case LY_TYPE_DEC64: |
| 2922 | type_p->compiled = *type; |
| 2923 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2924 | break; |
| 2925 | case LY_TYPE_STRING: |
| 2926 | type_p->compiled = *type; |
| 2927 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2928 | break; |
| 2929 | case LY_TYPE_ENUM: |
| 2930 | type_p->compiled = *type; |
| 2931 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2932 | break; |
| 2933 | case LY_TYPE_INT8: |
| 2934 | case LY_TYPE_UINT8: |
| 2935 | case LY_TYPE_INT16: |
| 2936 | case LY_TYPE_UINT16: |
| 2937 | case LY_TYPE_INT32: |
| 2938 | case LY_TYPE_UINT32: |
| 2939 | case LY_TYPE_INT64: |
| 2940 | case LY_TYPE_UINT64: |
| 2941 | type_p->compiled = *type; |
| 2942 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2943 | break; |
| 2944 | case LY_TYPE_IDENT: |
| 2945 | type_p->compiled = *type; |
| 2946 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2947 | break; |
| 2948 | case LY_TYPE_LEAFREF: |
| 2949 | type_p->compiled = *type; |
| 2950 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2951 | break; |
| 2952 | case LY_TYPE_INST: |
| 2953 | type_p->compiled = *type; |
| 2954 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2955 | break; |
| 2956 | case LY_TYPE_UNION: |
| 2957 | type_p->compiled = *type; |
| 2958 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2959 | break; |
| 2960 | case LY_TYPE_BOOL: |
| 2961 | case LY_TYPE_EMPTY: |
| 2962 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2963 | break; |
| 2964 | } |
| 2965 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2966 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2967 | |
| 2968 | cleanup: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2969 | return ret; |
| 2970 | } |
| 2971 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2972 | /** |
| 2973 | * @brief Compile information about the leaf/leaf-list's type. |
| 2974 | * @param[in] ctx Compile context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2975 | * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2976 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2977 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2978 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2979 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2980 | * @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] | 2981 | * @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] | 2982 | * @param[out] dflt Default value for the type. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2983 | * @return LY_ERR value. |
| 2984 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2985 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2986 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2987 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2988 | struct lysc_type **type, const char **units, struct lysp_qname **dflt) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2989 | { |
| 2990 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2991 | ly_bool dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2992 | struct type_context { |
| 2993 | const struct lysp_tpdf *tpdf; |
| 2994 | struct lysp_node *node; |
| 2995 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2996 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2997 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2998 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2999 | struct ly_set tpdf_chain = {0}; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3000 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3001 | (*type) = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3002 | if (dflt) { |
| 3003 | *dflt = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3004 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3005 | |
| 3006 | tctx = calloc(1, sizeof *tctx); |
| 3007 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3008 | for (ret = lysp_type_find(type_p->name, context_pnode, ctx->mod_def->parsed, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3009 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3010 | ret == LY_SUCCESS; |
| 3011 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3012 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3013 | if (basetype) { |
| 3014 | break; |
| 3015 | } |
| 3016 | |
| 3017 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3018 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3019 | tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name); |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 3020 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3021 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3022 | if (units && !*units) { |
| 3023 | /* inherit units */ |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 3024 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret); |
| 3025 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3026 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3027 | if (dflt && !*dflt && tctx->tpdf->dflt.str) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3028 | /* inherit default */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3029 | *dflt = (struct lysp_qname *)&tctx->tpdf->dflt; |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 3030 | assert((*dflt)->mod); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3031 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3032 | if (dummyloops && (!units || *units) && dflt && *dflt) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3033 | basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3034 | break; |
| 3035 | } |
| 3036 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3037 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3038 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3039 | * 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] | 3040 | basetype = tctx->tpdf->type.compiled->basetype; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3041 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 3042 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3043 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3044 | if ((units && !*units) || (dflt && !*dflt)) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3045 | dummyloops = 1; |
| 3046 | goto preparenext; |
| 3047 | } else { |
| 3048 | tctx = NULL; |
| 3049 | break; |
| 3050 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3051 | } |
| 3052 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3053 | /* circular typedef reference detection */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3054 | for (uint32_t u = 0; u < tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3055 | /* local part */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3056 | tctx_iter = (struct type_context *)tpdf_chain.objs[u]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3057 | if ((tctx_iter->mod == tctx->mod) && (tctx_iter->node == tctx->node) && (tctx_iter->tpdf == tctx->tpdf)) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3058 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3059 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3060 | free(tctx); |
| 3061 | ret = LY_EVALID; |
| 3062 | goto cleanup; |
| 3063 | } |
| 3064 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3065 | for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3066 | /* global part for unions corner case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3067 | tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3068 | if ((tctx_iter->mod == tctx->mod) && (tctx_iter->node == tctx->node) && (tctx_iter->tpdf == tctx->tpdf)) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3069 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3070 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3071 | free(tctx); |
| 3072 | ret = LY_EVALID; |
| 3073 | goto cleanup; |
| 3074 | } |
| 3075 | } |
| 3076 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3077 | /* store information for the following processing */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3078 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 3079 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3080 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3081 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3082 | /* prepare next loop */ |
| 3083 | tctx_prev = tctx; |
| 3084 | tctx = calloc(1, sizeof *tctx); |
| 3085 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3086 | } |
| 3087 | free(tctx); |
| 3088 | |
| 3089 | /* allocate type according to the basetype */ |
| 3090 | switch (basetype) { |
| 3091 | case LY_TYPE_BINARY: |
| 3092 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3093 | break; |
| 3094 | case LY_TYPE_BITS: |
| 3095 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3096 | break; |
| 3097 | case LY_TYPE_BOOL: |
| 3098 | case LY_TYPE_EMPTY: |
| 3099 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3100 | break; |
| 3101 | case LY_TYPE_DEC64: |
| 3102 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3103 | break; |
| 3104 | case LY_TYPE_ENUM: |
| 3105 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3106 | break; |
| 3107 | case LY_TYPE_IDENT: |
| 3108 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3109 | break; |
| 3110 | case LY_TYPE_INST: |
| 3111 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3112 | break; |
| 3113 | case LY_TYPE_LEAFREF: |
| 3114 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3115 | break; |
| 3116 | case LY_TYPE_STRING: |
| 3117 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3118 | break; |
| 3119 | case LY_TYPE_UNION: |
| 3120 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3121 | break; |
| 3122 | case LY_TYPE_INT8: |
| 3123 | case LY_TYPE_UINT8: |
| 3124 | case LY_TYPE_INT16: |
| 3125 | case LY_TYPE_UINT16: |
| 3126 | case LY_TYPE_INT32: |
| 3127 | case LY_TYPE_UINT32: |
| 3128 | case LY_TYPE_INT64: |
| 3129 | case LY_TYPE_UINT64: |
| 3130 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3131 | break; |
| 3132 | case LY_TYPE_UNKNOWN: |
| 3133 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3134 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3135 | ret = LY_EVALID; |
| 3136 | goto cleanup; |
| 3137 | } |
| 3138 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3139 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3140 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3141 | ly_data_type2str[basetype]); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3142 | free(*type); |
| 3143 | (*type) = NULL; |
| 3144 | ret = LY_EVALID; |
| 3145 | goto cleanup; |
| 3146 | } |
| 3147 | |
| 3148 | /* get restrictions from the referred typedefs */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3149 | for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3150 | tctx = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3151 | |
| 3152 | /* remember the typedef context for circular check */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3153 | ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
| 3154 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3155 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3156 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3157 | base = tctx->tpdf->type.compiled; |
| 3158 | continue; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3159 | } 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] | 3160 | /* no change, just use the type information from the base */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3161 | base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3162 | ++base->refcount; |
| 3163 | continue; |
| 3164 | } |
| 3165 | |
| 3166 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3167 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3168 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3169 | tctx->tpdf->name, ly_data_type2str[basetype]); |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3170 | ret = LY_EVALID; |
| 3171 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3172 | } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) { |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3173 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3174 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3175 | tctx->tpdf->name, tctx->tpdf->dflt.str); |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3176 | ret = LY_EVALID; |
| 3177 | goto cleanup; |
| 3178 | } |
| 3179 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3180 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3181 | /* TODO user type plugins */ |
| 3182 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3183 | prev_type = *type; |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 3184 | ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, |
| 3185 | &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3186 | LY_CHECK_GOTO(ret, cleanup); |
| 3187 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3188 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3189 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3190 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3191 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3192 | /* process the type definition in leaf */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3193 | if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3194 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3195 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3196 | /* TODO user type plugins */ |
| 3197 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3198 | ++(*type)->refcount; |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 3199 | ret = lys_compile_type_(ctx, context_pnode, context_flags, context_mod, context_name, type_p, basetype, NULL, |
| 3200 | base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3201 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3202 | } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3203 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3204 | free(*type); |
| 3205 | (*type) = base; |
| 3206 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3207 | } |
| 3208 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3209 | 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] | 3210 | |
| 3211 | cleanup: |
| 3212 | ly_set_erase(&tpdf_chain, free); |
| 3213 | return ret; |
| 3214 | } |
| 3215 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3216 | /** |
| 3217 | * @brief Compile status information of the given node. |
| 3218 | * |
| 3219 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3220 | * has the status correctly set during the compilation. |
| 3221 | * |
| 3222 | * @param[in] ctx Compile context |
| 3223 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3224 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3225 | * the compatibility with the parent's status value. |
| 3226 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3227 | * @return LY_ERR value. |
| 3228 | */ |
| 3229 | static LY_ERR |
| 3230 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3231 | { |
| 3232 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3233 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3234 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3235 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3236 | if ((parent_flags & 0x3) != 0x3) { |
| 3237 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3238 | * combination of bits (0x3) which marks the uses_status value */ |
| 3239 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3240 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3241 | } |
| 3242 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3243 | } else { |
| 3244 | (*node_flags) |= LYS_STATUS_CURR; |
| 3245 | } |
| 3246 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3247 | /* check status compatibility with the parent */ |
| 3248 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3249 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3250 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3251 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3252 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3253 | } else { /* LYS_STATUS_DEPRC */ |
| 3254 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3255 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3256 | } |
| 3257 | return LY_EVALID; |
| 3258 | } |
| 3259 | } |
| 3260 | return LY_SUCCESS; |
| 3261 | } |
| 3262 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3263 | /** |
| 3264 | * @brief Check uniqness of the node/action/notification name. |
| 3265 | * |
| 3266 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3267 | * structures, but they share the namespace so we need to check their name collisions. |
| 3268 | * |
| 3269 | * @param[in] ctx Compile context. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3270 | * @param[in] parent Parent of the nodes to check, can be NULL. |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3271 | * @param[in] name Name of the item to find in the given lists. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3272 | * @param[in] exclude Node that was just added that should be excluded from the name checking. |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3273 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3274 | */ |
| 3275 | static LY_ERR |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3276 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name, |
| 3277 | const struct lysc_node *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3278 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3279 | const struct lysc_node *iter, *iter2; |
| 3280 | const struct lysc_action *actions; |
| 3281 | const struct lysc_notif *notifs; |
| 3282 | uint32_t getnext_flags; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3283 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3284 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3285 | #define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name)) |
| 3286 | |
| 3287 | if (exclude->nodetype == LYS_CASE) { |
| 3288 | /* check restricted only to all the cases */ |
| 3289 | assert(parent->nodetype == LYS_CHOICE); |
| 3290 | LY_LIST_FOR(lysc_node_children(parent, 0), iter) { |
| 3291 | if (CHECK_NODE(iter, exclude, name)) { |
| 3292 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case"); |
| 3293 | return LY_EEXIST; |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | return LY_SUCCESS; |
| 3298 | } |
| 3299 | |
| 3300 | /* no reason for our parent to be choice anymore */ |
| 3301 | assert(!parent || (parent->nodetype != LYS_CHOICE)); |
| 3302 | |
| 3303 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 3304 | /* move to the first data definition parent */ |
| 3305 | parent = lysc_data_parent(parent); |
| 3306 | } |
| 3307 | |
| 3308 | getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE; |
| 3309 | if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) { |
| 3310 | getnext_flags |= LYS_GETNEXT_OUTPUT; |
| 3311 | } |
| 3312 | |
| 3313 | iter = NULL; |
| 3314 | while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) { |
| 3315 | if (CHECK_NODE(iter, exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3316 | goto error; |
| 3317 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3318 | |
| 3319 | /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */ |
| 3320 | if (iter->nodetype == LYS_CHOICE) { |
| 3321 | iter2 = NULL; |
| 3322 | while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) { |
| 3323 | if (CHECK_NODE(iter2, exclude, name)) { |
| 3324 | goto error; |
| 3325 | } |
| 3326 | } |
| 3327 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3328 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3329 | |
| 3330 | actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3331 | LY_ARRAY_FOR(actions, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3332 | if (CHECK_NODE(&actions[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3333 | goto error; |
| 3334 | } |
| 3335 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3336 | |
| 3337 | notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3338 | LY_ARRAY_FOR(notifs, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3339 | if (CHECK_NODE(¬ifs[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3340 | goto error; |
| 3341 | } |
| 3342 | } |
| 3343 | return LY_SUCCESS; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3344 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3345 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3346 | 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] | 3347 | return LY_EEXIST; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3348 | |
| 3349 | #undef CHECK_NODE |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3350 | } |
| 3351 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3352 | static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, |
| 3353 | uint16_t uses_status, struct ly_set *child_set); |
| 3354 | |
| 3355 | static LY_ERR lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, |
| 3356 | const struct lysc_node *parent, struct lysp_node **dev_pnode, ly_bool *not_supported); |
| 3357 | |
| 3358 | static LY_ERR lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node); |
| 3359 | |
| 3360 | static void lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3361 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3362 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3363 | * @brief Compile parsed RPC/action schema node information. |
| 3364 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3365 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3366 | * @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] | 3367 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3368 | * @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). |
| 3369 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3370 | * @return LY_SUCCESS on success, |
| 3371 | * @return LY_EVALID on validation error, |
| 3372 | * @return LY_EDENIED on not-supported deviation. |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3373 | */ |
| 3374 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3375 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3376 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3377 | { |
| 3378 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3379 | struct lysp_node *child_p, *dev_pnode = NULL, *dev_input_p = NULL, *dev_output_p = NULL; |
| 3380 | struct lysp_action *orig_action_p = action_p; |
| 3381 | struct lysp_action_inout *inout_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3382 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3383 | ly_bool not_supported; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3384 | uint32_t opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3385 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3386 | lysc_update_path(ctx, parent, action_p->name); |
| 3387 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3388 | /* apply deviation on the action/RPC */ |
| 3389 | LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)action_p, parent, &dev_pnode, ¬_supported)); |
| 3390 | if (not_supported) { |
| 3391 | lysc_update_path(ctx, NULL, NULL); |
| 3392 | return LY_EDENIED; |
| 3393 | } else if (dev_pnode) { |
| 3394 | action_p = (struct lysp_action *)dev_pnode; |
| 3395 | } |
| 3396 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3397 | /* member needed for uniqueness check lys_getnext() */ |
| 3398 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
| 3399 | action->module = ctx->mod; |
| 3400 | action->parent = parent; |
| 3401 | |
| 3402 | LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, action_p->name, (struct lysc_node *)action)); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3403 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3404 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3405 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3406 | "Action \"%s\" is placed inside %s.", action_p->name, |
| 3407 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3408 | return LY_EVALID; |
| 3409 | } |
| 3410 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3411 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3412 | action->sp = orig_action_p; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3413 | } |
| 3414 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3415 | |
| 3416 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3417 | * 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] | 3418 | 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] | 3419 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3420 | DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup); |
| 3421 | DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup); |
| 3422 | DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3423 | 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] | 3424 | 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] | 3425 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3426 | /* connect any action augments */ |
| 3427 | LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)action)); |
| 3428 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3429 | /* input */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3430 | lysc_update_path(ctx, (struct lysc_node *)action, "input"); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3431 | |
| 3432 | /* apply deviations on input */ |
| 3433 | LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->input, (struct lysc_node *)action, |
| 3434 | &dev_input_p, ¬_supported)); |
| 3435 | if (not_supported) { |
| 3436 | inout_p = NULL; |
| 3437 | } else if (dev_input_p) { |
| 3438 | inout_p = (struct lysp_action_inout *)dev_input_p; |
| 3439 | } else { |
| 3440 | inout_p = &action_p->input; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3441 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3442 | |
| 3443 | if (inout_p) { |
| 3444 | action->input.nodetype = LYS_INPUT; |
| 3445 | COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->input.musts, u, lys_compile_must, ret, cleanup); |
| 3446 | COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup); |
| 3447 | ctx->options |= LYSC_OPT_RPC_INPUT; |
| 3448 | |
| 3449 | /* connect any input augments */ |
| 3450 | LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)&action->input)); |
| 3451 | |
| 3452 | LY_LIST_FOR(inout_p->data, child_p) { |
| 3453 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status, NULL)); |
| 3454 | } |
| 3455 | ctx->options = opt_prev; |
| 3456 | } |
| 3457 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3458 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3459 | |
| 3460 | /* output */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3461 | lysc_update_path(ctx, (struct lysc_node *)action, "output"); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3462 | |
| 3463 | /* apply deviations on output */ |
| 3464 | LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->output, (struct lysc_node *)action, |
| 3465 | &dev_output_p, ¬_supported)); |
| 3466 | if (not_supported) { |
| 3467 | inout_p = NULL; |
| 3468 | } else if (dev_output_p) { |
| 3469 | inout_p = (struct lysp_action_inout *)dev_output_p; |
| 3470 | } else { |
| 3471 | inout_p = &action_p->output; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3472 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3473 | |
| 3474 | if (inout_p) { |
| 3475 | action->output.nodetype = LYS_OUTPUT; |
| 3476 | COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->output.musts, u, lys_compile_must, ret, cleanup); |
| 3477 | COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup); |
| 3478 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
| 3479 | |
| 3480 | /* connect any output augments */ |
| 3481 | LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)&action->output)); |
| 3482 | |
| 3483 | LY_LIST_FOR(inout_p->data, child_p) { |
| 3484 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status, NULL)); |
| 3485 | } |
| 3486 | ctx->options = opt_prev; |
| 3487 | } |
| 3488 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3489 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3490 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3491 | if ((action->input.musts || action->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3492 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3493 | ret = ly_set_add(&ctx->xpath, action, 0, NULL); |
| 3494 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3495 | } |
| 3496 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3497 | lysc_update_path(ctx, NULL, NULL); |
| 3498 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3499 | cleanup: |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3500 | lysp_dev_node_free(ctx->ctx, dev_pnode); |
| 3501 | lysp_dev_node_free(ctx->ctx, dev_input_p); |
| 3502 | lysp_dev_node_free(ctx->ctx, dev_output_p); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3503 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3504 | return ret; |
| 3505 | } |
| 3506 | |
| 3507 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3508 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3509 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3510 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3511 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3512 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3513 | * @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] | 3514 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3515 | * @return LY_SUCCESS on success, |
| 3516 | * @return LY_EVALID on validation error, |
| 3517 | * @return LY_EDENIED on not-supported deviation. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3518 | */ |
| 3519 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3520 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3521 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3522 | { |
| 3523 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3524 | struct lysp_node *child_p, *dev_pnode = NULL; |
| 3525 | struct lysp_notif *orig_notif_p = notif_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3526 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3527 | ly_bool not_supported; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3528 | uint32_t opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3529 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3530 | lysc_update_path(ctx, parent, notif_p->name); |
| 3531 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3532 | LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)notif_p, parent, &dev_pnode, ¬_supported)); |
| 3533 | if (not_supported) { |
| 3534 | lysc_update_path(ctx, NULL, NULL); |
| 3535 | return LY_EDENIED; |
| 3536 | } else if (dev_pnode) { |
| 3537 | notif_p = (struct lysp_notif *)dev_pnode; |
| 3538 | } |
| 3539 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3540 | /* member needed for uniqueness check lys_getnext() */ |
| 3541 | notif->nodetype = LYS_NOTIF; |
| 3542 | notif->module = ctx->mod; |
| 3543 | notif->parent = parent; |
| 3544 | |
| 3545 | LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, notif_p->name, (struct lysc_node *)notif)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3546 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3547 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3548 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3549 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
| 3550 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3551 | return LY_EVALID; |
| 3552 | } |
| 3553 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3554 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3555 | notif->sp = orig_notif_p; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3556 | } |
| 3557 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3558 | |
| 3559 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3560 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3561 | ret = lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0)); |
| 3562 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3563 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3564 | DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup); |
| 3565 | DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup); |
| 3566 | DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3567 | 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] | 3568 | 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] | 3569 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3570 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3571 | ret = ly_set_add(&ctx->xpath, notif, 0, NULL); |
| 3572 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3573 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3574 | 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] | 3575 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3576 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3577 | |
| 3578 | /* connect any notification augments */ |
| 3579 | LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)notif)); |
| 3580 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3581 | LY_LIST_FOR(notif_p->data, child_p) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3582 | ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3583 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3584 | } |
| 3585 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3586 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3587 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3588 | cleanup: |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3589 | lysp_dev_node_free(ctx->ctx, dev_pnode); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3590 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3591 | return ret; |
| 3592 | } |
| 3593 | |
| 3594 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3595 | * @brief Compile parsed container node information. |
| 3596 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3597 | * @param[in] pnode Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3598 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3599 | * is enriched with the container-specific information. |
| 3600 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3601 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3602 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3603 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3604 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3605 | struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3606 | struct lysc_node_container *cont = (struct lysc_node_container *)node; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3607 | struct lysp_node *child_p; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3608 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3609 | LY_ERR ret = LY_SUCCESS; |
| 3610 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3611 | if (cont_p->presence) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3612 | /* explicit presence */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3613 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3614 | } else if (cont_p->musts) { |
| 3615 | /* container with a must condition */ |
Radek Krejci | 175f25b | 2020-08-13 12:02:36 +0200 | [diff] [blame] | 3616 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name); |
| 3617 | cont->flags |= LYS_PRESENCE; |
| 3618 | } else if (cont_p->when) { |
| 3619 | /* container with a when condition */ |
| 3620 | 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] | 3621 | cont->flags |= LYS_PRESENCE; |
| 3622 | } else if (cont_p->parent) { |
| 3623 | if (cont_p->parent->nodetype == LYS_CHOICE) { |
| 3624 | /* 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] | 3625 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3626 | cont_p->name, cont_p->parent->name); |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3627 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3628 | } else if ((cont_p->parent->nodetype == LYS_CASE) && |
| 3629 | (((struct lysp_node_case *)cont_p->parent)->child == pnode) && !cont_p->next) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3630 | /* 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] | 3631 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3632 | cont_p->name, cont_p->parent->name); |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3633 | cont->flags |= LYS_PRESENCE; |
| 3634 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3635 | } |
| 3636 | |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3637 | /* more cases when the container has meaning but is kept NP for convenience: |
| 3638 | * - when condition |
| 3639 | * - direct child action/notification |
| 3640 | */ |
| 3641 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3642 | LY_LIST_FOR(cont_p->child, child_p) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3643 | ret = lys_compile_node(ctx, child_p, node, 0, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3644 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3645 | } |
| 3646 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3647 | 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] | 3648 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3649 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3650 | ret = ly_set_add(&ctx->xpath, cont, 0, NULL); |
| 3651 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3652 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3653 | COMPILE_OP_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3654 | COMPILE_OP_ARRAY_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] | 3655 | |
| 3656 | done: |
| 3657 | return ret; |
| 3658 | } |
| 3659 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3660 | /* |
| 3661 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3662 | * @param[in] ctx Compile context. |
| 3663 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3664 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3665 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3666 | * @return LY_ERR value. |
| 3667 | */ |
| 3668 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3669 | lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3670 | struct lysc_node_leaf *leaf) |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3671 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3672 | struct lysp_qname *dflt; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3673 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3674 | LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3675 | leaf->units ? NULL : &leaf->units, &dflt)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3676 | |
| 3677 | /* store default value, if any */ |
| 3678 | if (dflt && !(leaf->flags & LYS_SET_DFLT)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3679 | LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3680 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3681 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3682 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3683 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3684 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3685 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3686 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3687 | LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) { |
| 3688 | 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] | 3689 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3690 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3691 | } |
| 3692 | } |
| 3693 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3694 | if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->mod_def->version < LYS_VERSION_1_1)) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3695 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3696 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3697 | return LY_EVALID; |
| 3698 | } |
| 3699 | } |
| 3700 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3701 | return LY_SUCCESS; |
| 3702 | } |
| 3703 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3704 | /** |
| 3705 | * @brief Compile parsed leaf node information. |
| 3706 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3707 | * @param[in] pnode Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3708 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3709 | * is enriched with the leaf-specific information. |
| 3710 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3711 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3712 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3713 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3714 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3715 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3716 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3717 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3718 | LY_ERR ret = LY_SUCCESS; |
| 3719 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3720 | 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] | 3721 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3722 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3723 | ret = ly_set_add(&ctx->xpath, leaf, 0, NULL); |
| 3724 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3725 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3726 | if (leaf_p->units) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3727 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3728 | leaf->flags |= LYS_SET_UNITS; |
| 3729 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3730 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3731 | /* compile type */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3732 | ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3733 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3734 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3735 | /* store/update default value */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3736 | if (leaf_p->dflt.str) { |
| 3737 | LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3738 | leaf->flags |= LYS_SET_DFLT; |
| 3739 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3740 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3741 | /* checks */ |
| 3742 | if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) { |
| 3743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3744 | "Invalid mandatory leaf with a default value."); |
| 3745 | return LY_EVALID; |
| 3746 | } |
| 3747 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3748 | done: |
| 3749 | return ret; |
| 3750 | } |
| 3751 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3752 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3753 | * @brief Compile parsed leaf-list node information. |
| 3754 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3755 | * @param[in] pnode Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3756 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3757 | * is enriched with the leaf-list-specific information. |
| 3758 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3759 | */ |
| 3760 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3761 | lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3762 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3763 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3764 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3765 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3766 | LY_ERR ret = LY_SUCCESS; |
| 3767 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3768 | 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] | 3769 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3770 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3771 | ret = ly_set_add(&ctx->xpath, llist, 0, NULL); |
| 3772 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3773 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3774 | if (llist_p->units) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3775 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3776 | llist->flags |= LYS_SET_UNITS; |
| 3777 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3778 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3779 | /* compile type */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3780 | ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3781 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 6a044b2 | 2020-01-15 12:25:39 +0100 | [diff] [blame] | 3782 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3783 | /* store/update default values */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3784 | if (llist_p->dflts) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3785 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3786 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3787 | "Leaf-list default values are allowed only in YANG 1.1 modules."); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3788 | return LY_EVALID; |
| 3789 | } |
| 3790 | |
| 3791 | LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3792 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3796 | if (llist->min) { |
| 3797 | llist->flags |= LYS_MAND_TRUE; |
| 3798 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3799 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3800 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3801 | /* checks */ |
| 3802 | if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) { |
| 3803 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3804 | "Invalid mandatory leaf-list with default value(s)."); |
| 3805 | return LY_EVALID; |
| 3806 | } |
| 3807 | |
| 3808 | if (llist->min > llist->max) { |
| 3809 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3810 | llist->min, llist->max); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3811 | return LY_EVALID; |
| 3812 | } |
| 3813 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3814 | done: |
| 3815 | return ret; |
| 3816 | } |
| 3817 | |
| 3818 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3819 | * @brief Compile information about list's uniques. |
| 3820 | * @param[in] ctx Compile context. |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3821 | * @param[in] uniques Sized array list of unique statements. |
| 3822 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3823 | * @return LY_ERR value. |
| 3824 | */ |
| 3825 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3826 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list) |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3827 | { |
| 3828 | LY_ERR ret = LY_SUCCESS; |
| 3829 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3830 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3831 | const char *keystr, *delim; |
| 3832 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3833 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3834 | int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3835 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3836 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3837 | LY_ARRAY_FOR(uniques, v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3838 | config = -1; |
| 3839 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3840 | keystr = uniques[v].str; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3841 | while (keystr) { |
| 3842 | delim = strpbrk(keystr, " \t\n"); |
| 3843 | if (delim) { |
| 3844 | len = delim - keystr; |
| 3845 | while (isspace(*delim)) { |
| 3846 | ++delim; |
| 3847 | } |
| 3848 | } else { |
| 3849 | len = strlen(keystr); |
| 3850 | } |
| 3851 | |
| 3852 | /* unique node must be present */ |
| 3853 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3854 | ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, uniques[v].mod, LYS_LEAF, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3855 | (const struct lysc_node **)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3856 | if (ret != LY_SUCCESS) { |
| 3857 | if (ret == LY_EDENIED) { |
| 3858 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3859 | "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.", |
| 3860 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3861 | } |
| 3862 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3863 | } else if (flags) { |
| 3864 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3865 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3866 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3867 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | /* all referenced leafs must be of the same config type */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3871 | if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) || (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3872 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3873 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3874 | return LY_EVALID; |
| 3875 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3876 | config = 1; |
| 3877 | } else { /* LYS_CONFIG_R */ |
| 3878 | config = 0; |
| 3879 | } |
| 3880 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3881 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3882 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3883 | if (parent->nodetype == LYS_LIST) { |
| 3884 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3885 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3886 | return LY_EVALID; |
| 3887 | } |
| 3888 | } |
| 3889 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3890 | /* check status */ |
| 3891 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3892 | (*key)->flags, (*key)->module, (*key)->name)); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3893 | |
| 3894 | /* mark leaf as unique */ |
| 3895 | (*key)->flags |= LYS_UNIQUE; |
| 3896 | |
| 3897 | /* next unique value in line */ |
| 3898 | keystr = delim; |
| 3899 | } |
| 3900 | /* next unique definition */ |
| 3901 | } |
| 3902 | |
| 3903 | return LY_SUCCESS; |
| 3904 | } |
| 3905 | |
| 3906 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3907 | * @brief Compile parsed list node information. |
| 3908 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3909 | * @param[in] pnode Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3910 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3911 | * is enriched with the list-specific information. |
| 3912 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3913 | */ |
| 3914 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3915 | lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3916 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3917 | struct lysp_node_list *list_p = (struct lysp_node_list *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3918 | struct lysc_node_list *list = (struct lysc_node_list *)node; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3919 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3920 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3921 | size_t len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3922 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3923 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3924 | LY_ERR ret = LY_SUCCESS; |
| 3925 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3926 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3927 | if (list->min) { |
| 3928 | list->flags |= LYS_MAND_TRUE; |
| 3929 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3930 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3931 | |
| 3932 | LY_LIST_FOR(list_p->child, child_p) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3933 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3934 | } |
| 3935 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3936 | 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] | 3937 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3938 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3939 | LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL)); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3940 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3941 | |
| 3942 | /* keys */ |
| 3943 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3944 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3945 | return LY_EVALID; |
| 3946 | } |
| 3947 | |
| 3948 | /* find all the keys (must be direct children) */ |
| 3949 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3950 | if (!keystr) { |
| 3951 | /* keyless list */ |
| 3952 | list->flags |= LYS_KEYLESS; |
| 3953 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3954 | while (keystr) { |
| 3955 | delim = strpbrk(keystr, " \t\n"); |
| 3956 | if (delim) { |
| 3957 | len = delim - keystr; |
| 3958 | while (isspace(*delim)) { |
| 3959 | ++delim; |
| 3960 | } |
| 3961 | } else { |
| 3962 | len = strlen(keystr); |
| 3963 | } |
| 3964 | |
| 3965 | /* key node must be present */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3966 | 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] | 3967 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3968 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3969 | "The list's key \"%.*s\" not found.", len, keystr); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3970 | return LY_EVALID; |
| 3971 | } |
| 3972 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3973 | if (key->flags & LYS_KEY) { |
| 3974 | /* the node was already marked as a key */ |
| 3975 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3976 | "Duplicated key identifier \"%.*s\".", len, keystr); |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3977 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3978 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3979 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3980 | lysc_update_path(ctx, (struct lysc_node *)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3981 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3982 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3983 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3984 | return LY_EVALID; |
| 3985 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3986 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3987 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3988 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3989 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3990 | "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] | 3991 | return LY_EVALID; |
| 3992 | } |
| 3993 | } else { |
| 3994 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3995 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3996 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3997 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3998 | return LY_EVALID; |
| 3999 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4000 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4001 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4002 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4003 | return LY_EVALID; |
| 4004 | } |
| 4005 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4006 | |
| 4007 | /* check status */ |
| 4008 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4009 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4010 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4011 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4012 | if (key->dflt) { |
| 4013 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 4014 | lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype); |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4015 | free(key->dflt); |
| 4016 | key->dflt = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4017 | } |
| 4018 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4019 | key->flags |= LYS_KEY; |
| 4020 | |
| 4021 | /* move it to the correct position */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4022 | if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4023 | /* fix links in closest previous siblings of the key */ |
| 4024 | if (key->next) { |
| 4025 | key->next->prev = key->prev; |
| 4026 | } else { |
| 4027 | /* last child */ |
| 4028 | list->child->prev = key->prev; |
| 4029 | } |
| 4030 | if (key->prev->next) { |
| 4031 | key->prev->next = key->next; |
| 4032 | } |
| 4033 | /* fix links in the key */ |
| 4034 | if (prev_key) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4035 | key->prev = (struct lysc_node *)prev_key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4036 | key->next = prev_key->next; |
| 4037 | } else { |
| 4038 | key->prev = list->child->prev; |
| 4039 | key->next = list->child; |
| 4040 | } |
| 4041 | /* fix links in closes future siblings of the key */ |
| 4042 | if (prev_key) { |
| 4043 | if (prev_key->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4044 | prev_key->next->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4045 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4046 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4047 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4048 | prev_key->next = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4049 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4050 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4051 | } |
| 4052 | /* fix links in parent */ |
| 4053 | if (!key->prev->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4054 | list->child = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4055 | } |
| 4056 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4057 | |
| 4058 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4059 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4060 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4061 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | /* uniques */ |
| 4065 | if (list_p->uniques) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4066 | LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4067 | } |
| 4068 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4069 | COMPILE_OP_ARRAY_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 4070 | COMPILE_OP_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done); |
| 4071 | |
| 4072 | /* checks */ |
| 4073 | if (list->min > list->max) { |
| 4074 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4075 | list->min, list->max); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4076 | return LY_EVALID; |
| 4077 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4078 | |
| 4079 | done: |
| 4080 | return ret; |
| 4081 | } |
| 4082 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4083 | /** |
| 4084 | * @brief Do some checks and set the default choice's case. |
| 4085 | * |
| 4086 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 4087 | * |
| 4088 | * @param[in] ctx Compile context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4089 | * @param[in] dflt Name of the default branch. Can even contain a prefix. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4090 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 4091 | * @return LY_ERR value. |
| 4092 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4093 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4094 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4095 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4096 | struct lysc_node *iter, *node = (struct lysc_node *)ch; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4097 | const struct lys_module *mod; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4098 | const char *prefix = NULL, *name; |
| 4099 | size_t prefix_len = 0; |
| 4100 | |
| 4101 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4102 | name = strchr(dflt->str, ':'); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4103 | if (name) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4104 | prefix = dflt->str; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4105 | prefix_len = name - prefix; |
| 4106 | ++name; |
| 4107 | } else { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4108 | name = dflt->str; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4109 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4110 | if (prefix) { |
| 4111 | mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod); |
| 4112 | if (!mod) { |
| 4113 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4114 | "Default case prefix \"%.*s\" not found in imports of \"%s\".", prefix_len, prefix, dflt->mod->name); |
| 4115 | return LY_EVALID; |
| 4116 | } |
| 4117 | } else { |
| 4118 | mod = node->module; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4119 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4120 | |
| 4121 | ch->dflt = (struct lysc_node_case *)lys_find_child(node, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4122 | if (!ch->dflt) { |
| 4123 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4124 | "Default case \"%s\" not found.", dflt->str); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4125 | return LY_EVALID; |
| 4126 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4127 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4128 | /* no mandatory nodes directly under the default case */ |
| 4129 | LY_LIST_FOR(ch->dflt->child, iter) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4130 | if (iter->parent != (struct lysc_node *)ch->dflt) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4131 | break; |
| 4132 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4133 | if (iter->flags & LYS_MAND_TRUE) { |
| 4134 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4135 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4136 | return LY_EVALID; |
| 4137 | } |
| 4138 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4139 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4140 | if (ch->flags & LYS_MAND_TRUE) { |
| 4141 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid mandatory choice with a default case."); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4142 | return LY_EVALID; |
| 4143 | } |
| 4144 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4145 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4146 | return LY_SUCCESS; |
| 4147 | } |
| 4148 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4149 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4150 | * @brief Compile choice children. |
| 4151 | * |
| 4152 | * @param[in] ctx Compile context |
| 4153 | * @param[in] child_p Parsed choice children nodes. |
| 4154 | * @param[in] node Compiled choice node to compile and add children to. |
| 4155 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4156 | */ |
| 4157 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4158 | lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node, |
| 4159 | struct ly_set *child_set) |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4160 | { |
| 4161 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 4162 | struct lysp_node *child_p_next = child_p->next; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4163 | struct lysp_node_case *cs_p; |
| 4164 | |
| 4165 | if (child_p->nodetype == LYS_CASE) { |
| 4166 | /* standard case under choice */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4167 | ret = lys_compile_node(ctx, child_p, node, 0, child_set); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4168 | } else { |
| 4169 | /* we need the implicit case first, so create a fake parsed case */ |
| 4170 | cs_p = calloc(1, sizeof *cs_p); |
| 4171 | cs_p->nodetype = LYS_CASE; |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 4172 | DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4173 | cs_p->child = child_p; |
| 4174 | |
| 4175 | /* make the child the only case child */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4176 | child_p->next = NULL; |
| 4177 | |
| 4178 | /* compile it normally */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4179 | ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4180 | |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 4181 | free_fake_node: |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4182 | /* free the fake parsed node and correct pointers back */ |
| 4183 | cs_p->child = NULL; |
| 4184 | lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p); |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 4185 | child_p->next = child_p_next; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | return ret; |
| 4189 | } |
| 4190 | |
| 4191 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4192 | * @brief Compile parsed choice node information. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4193 | * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4194 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4195 | * @param[in] pnode Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4196 | * @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] | 4197 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4198 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4199 | */ |
| 4200 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4201 | lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4202 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4203 | struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4204 | struct lysc_node_choice *ch = (struct lysc_node_choice *)node; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4205 | struct lysp_node *child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4206 | LY_ERR ret = LY_SUCCESS; |
| 4207 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4208 | assert(node->nodetype == LYS_CHOICE); |
| 4209 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4210 | LY_LIST_FOR(ch_p->child, child_p) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4211 | LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | /* default branch */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4215 | if (ch_p->dflt.str) { |
| 4216 | 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] | 4217 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4218 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4219 | return ret; |
| 4220 | } |
| 4221 | |
| 4222 | /** |
| 4223 | * @brief Compile parsed anydata or anyxml node information. |
| 4224 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4225 | * @param[in] pnode Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4226 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4227 | * is enriched with the any-specific information. |
| 4228 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4229 | */ |
| 4230 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4231 | lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4232 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4233 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4234 | struct lysc_node_anydata *any = (struct lysc_node_anydata *)node; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4235 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4236 | LY_ERR ret = LY_SUCCESS; |
| 4237 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4238 | 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] | 4239 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4240 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4241 | ret = ly_set_add(&ctx->xpath, any, 0, NULL); |
| 4242 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4243 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4244 | |
| 4245 | if (any->flags & LYS_CONFIG_W) { |
Radek Krejci | 5c4ed7b | 2020-08-12 11:29:44 +0200 | [diff] [blame] | 4246 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4247 | 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] | 4248 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4249 | done: |
| 4250 | return ret; |
| 4251 | } |
| 4252 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4253 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4254 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4255 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4256 | * |
| 4257 | * @param[in] ctx Compile context |
| 4258 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4259 | * the choice itself is expected instead of a specific case node. |
| 4260 | * @param[in] node Schema node to connect into the list. |
| 4261 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4262 | * 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] | 4263 | */ |
| 4264 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4265 | 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] | 4266 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4267 | struct lysc_node **children, *anchor = NULL; |
| 4268 | int insert_after = 0; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4269 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4270 | node->parent = parent; |
| 4271 | |
| 4272 | if (parent) { |
| 4273 | if (parent->nodetype == LYS_CHOICE) { |
| 4274 | assert(node->nodetype == LYS_CASE); |
| 4275 | children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases; |
| 4276 | } else { |
| 4277 | children = lysc_node_children_p(parent, ctx->options); |
| 4278 | } |
| 4279 | assert(children); |
| 4280 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4281 | if (!(*children)) { |
| 4282 | /* first child */ |
| 4283 | *children = node; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4284 | } else if (node->flags & LYS_KEY) { |
| 4285 | /* special handling of adding keys */ |
| 4286 | assert(node->module == parent->module); |
| 4287 | anchor = *children; |
| 4288 | if (anchor->flags & LYS_KEY) { |
| 4289 | while ((anchor->flags & LYS_KEY) && anchor->next) { |
| 4290 | anchor = anchor->next; |
| 4291 | } |
| 4292 | /* insert after the last key */ |
| 4293 | insert_after = 1; |
| 4294 | } /* else insert before anchor (at the beginning) */ |
| 4295 | } else if ((*children)->prev->module == node->module) { |
| 4296 | /* last child is from the same module, keep the order and insert at the end */ |
| 4297 | anchor = (*children)->prev; |
| 4298 | insert_after = 1; |
| 4299 | } else if (parent->module == node->module) { |
| 4300 | /* adding module child after some augments were connected */ |
| 4301 | for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {} |
| 4302 | } else { |
| 4303 | /* some augments are already connected and we are connecting new ones, |
| 4304 | * keep module name order and insert the node into the children list */ |
| 4305 | anchor = *children; |
| 4306 | do { |
| 4307 | anchor = anchor->prev; |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4308 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4309 | /* check that we have not found the last augment node from our module or |
| 4310 | * the first augment node from a "smaller" module or |
| 4311 | * the first node from a local module */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4312 | if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) || |
| 4313 | (anchor->module == parent->module)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4314 | /* insert after */ |
| 4315 | insert_after = 1; |
| 4316 | break; |
| 4317 | } |
| 4318 | |
| 4319 | /* we have traversed all the nodes, insert before anchor (as the first node) */ |
| 4320 | } while (anchor->prev->next); |
| 4321 | } |
| 4322 | |
| 4323 | /* insert */ |
| 4324 | if (anchor) { |
| 4325 | if (insert_after) { |
| 4326 | node->next = anchor->next; |
| 4327 | node->prev = anchor; |
| 4328 | anchor->next = node; |
| 4329 | if (node->next) { |
| 4330 | /* middle node */ |
| 4331 | node->next->prev = node; |
| 4332 | } else { |
| 4333 | /* last node */ |
| 4334 | (*children)->prev = node; |
| 4335 | } |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4336 | } else { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4337 | node->next = anchor; |
| 4338 | node->prev = anchor->prev; |
| 4339 | anchor->prev = node; |
| 4340 | if (anchor == *children) { |
| 4341 | /* first node */ |
| 4342 | *children = node; |
| 4343 | } else { |
| 4344 | /* middle node */ |
| 4345 | node->prev->next = node; |
| 4346 | } |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4347 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4348 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4349 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4350 | /* check the name uniqueness (even for an only child, it may be in case) */ |
| 4351 | if (lys_compile_node_uniqness(ctx, parent, node->name, node)) { |
| 4352 | return LY_EEXIST; |
| 4353 | } |
| 4354 | } else { |
| 4355 | /* top-level element */ |
| 4356 | if (!ctx->mod->compiled->data) { |
| 4357 | ctx->mod->compiled->data = node; |
| 4358 | } else { |
| 4359 | /* insert at the end of the module's top-level nodes list */ |
| 4360 | ctx->mod->compiled->data->prev->next = node; |
| 4361 | node->prev = ctx->mod->compiled->data->prev; |
| 4362 | ctx->mod->compiled->data->prev = node; |
| 4363 | } |
| 4364 | |
| 4365 | /* check the name uniqueness on top-level */ |
| 4366 | if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) { |
| 4367 | return LY_EEXIST; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4368 | } |
| 4369 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4370 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4371 | return LY_SUCCESS; |
| 4372 | } |
| 4373 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4374 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4375 | * @brief Prepare the case structure in choice node for the new data node. |
| 4376 | * |
| 4377 | * 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 |
| 4378 | * created in the choice when the first child was processed. |
| 4379 | * |
| 4380 | * @param[in] ctx Compile context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4381 | * @param[in] pnode 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] | 4382 | * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4383 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4384 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4385 | * @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, |
| 4386 | * 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] | 4387 | */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4388 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4389 | lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4390 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4391 | struct lysp_node *child_p; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4392 | struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4393 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4394 | if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4395 | /* we have to add an implicit case node into the parent choice */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4396 | } else if (pnode->nodetype == LYS_CASE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4397 | /* explicit parent case */ |
| 4398 | LY_LIST_FOR(cs_p->child, child_p) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4399 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4400 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4401 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4402 | LOGINT_RET(ctx->ctx); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4403 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4404 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4405 | return LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4406 | } |
| 4407 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4408 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4409 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4410 | * |
| 4411 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4412 | * the flag to such parents from a mandatory children. |
| 4413 | * |
| 4414 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4415 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4416 | * (mandatory children was removed). |
| 4417 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4418 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4419 | lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add) |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4420 | { |
| 4421 | struct lysc_node *iter; |
| 4422 | |
| 4423 | if (add) { /* set flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4424 | for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4425 | parent = parent->parent) { |
| 4426 | parent->flags |= LYS_MAND_TRUE; |
| 4427 | } |
| 4428 | } else { /* unset flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4429 | for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4430 | 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] | 4431 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4432 | /* there is another mandatory node */ |
| 4433 | return; |
| 4434 | } |
| 4435 | } |
| 4436 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4437 | parent->flags &= ~LYS_MAND_TRUE; |
| 4438 | } |
| 4439 | } |
| 4440 | } |
| 4441 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4442 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4443 | * @brief Compile the parsed augment connecting it into its target. |
| 4444 | * |
| 4445 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4446 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4447 | * are already implemented and compiled. |
| 4448 | * |
| 4449 | * @param[in] ctx Compile context. |
| 4450 | * @param[in] aug_p Parsed augment to compile. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4451 | * @param[in] target Target node of the augment. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4452 | * @return LY_SUCCESS on success. |
| 4453 | * @return LY_EVALID on failure. |
| 4454 | */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4455 | static LY_ERR |
| 4456 | lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysc_node *target) |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4457 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4458 | LY_ERR ret = LY_SUCCESS; |
| 4459 | struct lysp_node *pnode; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4460 | struct lysc_node *node; |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 4461 | struct lysc_when *when_shared = NULL; |
| 4462 | struct lysc_action **actions; |
| 4463 | struct lysc_notif **notifs; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4464 | ly_bool allow_mandatory = 0; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4465 | LY_ARRAY_COUNT_TYPE u; |
| 4466 | struct ly_set child_set = {0}; |
| 4467 | uint32_t i; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4468 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4469 | if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) { |
| 4470 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4471 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4472 | aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4473 | ret = LY_EVALID; |
| 4474 | goto cleanup; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4475 | } |
| 4476 | |
| 4477 | /* check for mandatory nodes |
| 4478 | * - new cases augmenting some choice can have mandatory nodes |
| 4479 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4480 | */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4481 | if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->mod == target->module)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4482 | allow_mandatory = 1; |
| 4483 | } |
| 4484 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4485 | LY_LIST_FOR(aug_p->child, pnode) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4486 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4487 | if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) || |
| 4488 | ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) || |
| 4489 | ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4490 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4491 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4492 | lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4493 | ret = LY_EVALID; |
| 4494 | goto cleanup; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | /* compile the children */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4498 | if (target->nodetype == LYS_CHOICE) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4499 | LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4500 | } else { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4501 | LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4502 | } |
| 4503 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4504 | /* since the augment node is not present in the compiled tree, we need to pass some of its |
| 4505 | * statements to all its children */ |
| 4506 | for (i = 0; i < child_set.count; ++i) { |
| 4507 | node = child_set.snodes[i]; |
| 4508 | if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) { |
| 4509 | node->flags &= ~LYS_MAND_TRUE; |
| 4510 | lys_compile_mandatory_parents(target, 0); |
| 4511 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4512 | "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4513 | ret = LY_EVALID; |
| 4514 | goto cleanup; |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4515 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4516 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4517 | if (aug_p->when) { |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 4518 | /* pass augment's when to all the children */ |
| 4519 | ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target), node, &when_shared); |
| 4520 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4521 | } |
| 4522 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4523 | ly_set_erase(&child_set, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4524 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4525 | |
| 4526 | switch (target->nodetype) { |
| 4527 | case LYS_CONTAINER: |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 4528 | actions = &((struct lysc_node_container *)target)->actions; |
| 4529 | notifs = &((struct lysc_node_container *)target)->notifs; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4530 | break; |
| 4531 | case LYS_LIST: |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 4532 | actions = &((struct lysc_node_list *)target)->actions; |
| 4533 | notifs = &((struct lysc_node_list *)target)->notifs; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4534 | break; |
| 4535 | default: |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 4536 | actions = NULL; |
| 4537 | notifs = NULL; |
| 4538 | break; |
| 4539 | } |
| 4540 | |
| 4541 | if (aug_p->actions) { |
| 4542 | if (!actions) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4544 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4545 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4546 | ret = LY_EVALID; |
| 4547 | goto cleanup; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4548 | } |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 4549 | |
| 4550 | /* compile actions into the target */ |
| 4551 | COMPILE_OP_ARRAY_GOTO(ctx, aug_p->actions, *actions, target, u, lys_compile_action, 0, ret, cleanup); |
| 4552 | |
| 4553 | if (aug_p->when) { |
| 4554 | /* inherit when */ |
| 4555 | LY_ARRAY_FOR(*actions, u) { |
| 4556 | ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target), |
| 4557 | (struct lysc_node *)&(*actions)[u], &when_shared); |
| 4558 | LY_CHECK_GOTO(ret, cleanup); |
| 4559 | } |
| 4560 | } |
| 4561 | } |
| 4562 | if (aug_p->notifs) { |
| 4563 | if (!notifs) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4564 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4565 | "Invalid augment of %s node which is not allowed to contain notification node \"%s\".", |
| 4566 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4567 | ret = LY_EVALID; |
| 4568 | goto cleanup; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4569 | } |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 4570 | |
| 4571 | /* compile notifications into the target */ |
| 4572 | COMPILE_OP_ARRAY_GOTO(ctx, aug_p->notifs, *notifs, target, u, lys_compile_notif, 0, ret, cleanup); |
| 4573 | |
| 4574 | if (aug_p->when) { |
| 4575 | /* inherit when */ |
| 4576 | LY_ARRAY_FOR(*notifs, u) { |
| 4577 | ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target), |
| 4578 | (struct lysc_node *)&(*notifs)[u], &when_shared); |
| 4579 | LY_CHECK_GOTO(ret, cleanup); |
| 4580 | } |
| 4581 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4582 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4583 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4584 | cleanup: |
| 4585 | ly_set_erase(&child_set, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4586 | return ret; |
| 4587 | } |
| 4588 | |
| 4589 | /** |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4590 | * @brief Find grouping for a uses. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4591 | * |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4592 | * @param[in] ctx Compile context. |
| 4593 | * @param[in] uses_p Parsed uses node. |
| 4594 | * @param[out] gpr_p Found grouping on success. |
| 4595 | * @param[out] grp_mod Module of @p grp_p on success. |
| 4596 | * @return LY_ERR value. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4597 | */ |
| 4598 | static LY_ERR |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4599 | lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p, |
| 4600 | struct lys_module **grp_mod) |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4601 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4602 | struct lysp_node *pnode; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4603 | struct lysp_grp *grp; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4604 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4605 | ly_bool found = 0; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4606 | const char *id, *name, *prefix; |
| 4607 | size_t prefix_len, name_len; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4608 | struct lys_module *mod; |
| 4609 | |
| 4610 | *grp_p = NULL; |
| 4611 | *grp_mod = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4612 | |
| 4613 | /* search for the grouping definition */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4614 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4615 | 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] | 4616 | if (prefix) { |
| 4617 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4618 | if (!mod) { |
| 4619 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4620 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4621 | return LY_EVALID; |
| 4622 | } |
| 4623 | } else { |
| 4624 | mod = ctx->mod_def; |
| 4625 | } |
| 4626 | if (mod == ctx->mod_def) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4627 | for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) { |
| 4628 | grp = (struct lysp_grp *)lysp_node_groupings(pnode); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4629 | LY_ARRAY_FOR(grp, u) { |
| 4630 | if (!strcmp(grp[u].name, name)) { |
| 4631 | grp = &grp[u]; |
| 4632 | found = 1; |
| 4633 | break; |
| 4634 | } |
| 4635 | } |
| 4636 | } |
| 4637 | } |
| 4638 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4639 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4640 | grp = mod->parsed->groupings; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4641 | LY_ARRAY_FOR(grp, u) { |
| 4642 | if (!strcmp(grp[u].name, name)) { |
| 4643 | grp = &grp[u]; |
| 4644 | found = 1; |
| 4645 | break; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4646 | } |
| 4647 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4648 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4649 | /* ... and all the submodules */ |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4650 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4651 | grp = mod->parsed->includes[u].submodule->groupings; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4652 | LY_ARRAY_FOR(grp, v) { |
| 4653 | if (!strcmp(grp[v].name, name)) { |
| 4654 | grp = &grp[v]; |
| 4655 | found = 1; |
| 4656 | break; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4657 | } |
| 4658 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4659 | if (found) { |
| 4660 | break; |
| 4661 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4662 | } |
| 4663 | } |
| 4664 | } |
| 4665 | if (!found) { |
| 4666 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4667 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4668 | return LY_EVALID; |
| 4669 | } |
| 4670 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4671 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4672 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4673 | grp->flags |= LYS_USED_GRP; |
| 4674 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4675 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4676 | *grp_p = grp; |
| 4677 | *grp_mod = mod; |
| 4678 | return LY_SUCCESS; |
| 4679 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4680 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4681 | static const struct lys_module *lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, |
| 4682 | size_t nametest_len, const struct lys_module *local_mod, const char **name, size_t *name_len); |
| 4683 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4684 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4685 | lys_nodeid_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct lys_module **target_mod, |
| 4686 | struct lyxp_expr **expr) |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4687 | { |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4688 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4689 | struct lyxp_expr *e = NULL; |
| 4690 | struct lys_module *tmod = NULL, *mod; |
| 4691 | const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid"; |
| 4692 | uint32_t i; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4693 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4694 | /* parse */ |
| 4695 | ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e); |
| 4696 | if (ret) { |
| 4697 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.", |
| 4698 | nodeid_type, nodeid); |
| 4699 | ret = LY_EVALID; |
| 4700 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4701 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4702 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4703 | if (abs) { |
| 4704 | /* absolute schema nodeid */ |
| 4705 | i = 0; |
| 4706 | } else { |
| 4707 | /* descendant schema nodeid */ |
| 4708 | if (e->tokens[0] != LYXP_TOKEN_NAMETEST) { |
| 4709 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".", |
| 4710 | nodeid_type, nodeid, e->tok_len[0], e->expr + e->tok_pos[0]); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4711 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4712 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4713 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4714 | i = 1; |
| 4715 | } |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4716 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4717 | /* check all the tokens */ |
| 4718 | for ( ; i < e->used; i += 2) { |
| 4719 | if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) { |
| 4720 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".", |
| 4721 | nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]); |
| 4722 | ret = LY_EVALID; |
| 4723 | goto cleanup; |
| 4724 | } else if (e->used == i + 1) { |
| 4725 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4726 | "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr); |
| 4727 | ret = LY_EVALID; |
| 4728 | goto cleanup; |
| 4729 | } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) { |
| 4730 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".", |
| 4731 | nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]); |
| 4732 | ret = LY_EVALID; |
| 4733 | goto cleanup; |
| 4734 | } else if (abs) { |
| 4735 | mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1], |
| 4736 | e->tok_len[i + 1], ctx->mod_def, NULL, NULL); |
| 4737 | LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup); |
| 4738 | |
| 4739 | /* only keep the first module */ |
| 4740 | if (!tmod) { |
| 4741 | tmod = mod; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4742 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4743 | |
| 4744 | /* all the modules must be implemented */ |
| 4745 | if (!mod->implemented) { |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 4746 | ret = lys_set_implemented(mod); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4747 | LY_CHECK_GOTO(ret, cleanup); |
| 4748 | } |
| 4749 | } |
| 4750 | } |
| 4751 | |
| 4752 | cleanup: |
| 4753 | if (ret || !expr) { |
| 4754 | lyxp_expr_free(ctx->ctx, e); |
| 4755 | e = NULL; |
| 4756 | } |
| 4757 | if (expr) { |
| 4758 | *expr = ret ? NULL : e; |
| 4759 | } |
| 4760 | if (target_mod) { |
| 4761 | *target_mod = ret ? NULL : tmod; |
| 4762 | } |
| 4763 | return ret; |
| 4764 | } |
| 4765 | |
| 4766 | /** |
| 4767 | * @brief Check whether 2 schema nodeids match. |
| 4768 | * |
| 4769 | * @param[in] ctx libyang context. |
| 4770 | * @param[in] exp1 First schema nodeid. |
| 4771 | * @param[in] exp1_mod Module of @p exp1 nodes without any prefix. |
| 4772 | * @param[in] exp2 Second schema nodeid. |
| 4773 | * @param[in] exp2_mod Module of @p exp2 nodes without any prefix. |
| 4774 | * @return Whether the schema nodeids match or not. |
| 4775 | */ |
| 4776 | static ly_bool |
| 4777 | lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lys_module *exp1_mod, |
| 4778 | const struct lyxp_expr *exp2, const struct lys_module *exp2_mod) |
| 4779 | { |
| 4780 | uint32_t i; |
| 4781 | const struct lys_module *mod1, *mod2; |
| 4782 | const char *name1, *name2; |
| 4783 | size_t name1_len, name2_len; |
| 4784 | |
| 4785 | if (exp1->used != exp2->used) { |
| 4786 | return 0; |
| 4787 | } |
| 4788 | |
| 4789 | for (i = 0; i < exp1->used; ++i) { |
| 4790 | assert(exp1->tokens[i] == exp2->tokens[i]); |
| 4791 | |
| 4792 | if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) { |
| 4793 | /* check modules of all the nodes in the node ID */ |
| 4794 | mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_mod, |
| 4795 | &name1, &name1_len); |
| 4796 | assert(mod1); |
| 4797 | mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_mod, |
| 4798 | &name2, &name2_len); |
| 4799 | assert(mod2); |
| 4800 | |
| 4801 | /* compare modules */ |
| 4802 | if (mod1 != mod2) { |
| 4803 | return 0; |
| 4804 | } |
| 4805 | |
| 4806 | /* compare names */ |
| 4807 | if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) { |
| 4808 | return 0; |
| 4809 | } |
| 4810 | } |
| 4811 | } |
| 4812 | |
| 4813 | return 1; |
| 4814 | } |
| 4815 | |
| 4816 | /** |
| 4817 | * @brief Prepare any uses augments and refines in the context to be applied during uses descendant node compilation. |
| 4818 | * |
| 4819 | * @param[in] ctx Compile context. |
| 4820 | * @param[in] uses_p Parsed uses structure with augments and refines. |
| 4821 | * @param[in] ctx_node Context node of @p uses_p meaning its first data definiition parent. |
| 4822 | * @return LY_ERR value. |
| 4823 | */ |
| 4824 | static LY_ERR |
| 4825 | lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node) |
| 4826 | { |
| 4827 | LY_ERR ret = LY_SUCCESS; |
| 4828 | LY_ARRAY_COUNT_TYPE u; |
| 4829 | struct lyxp_expr *exp = NULL; |
| 4830 | struct lysc_augment *aug; |
| 4831 | struct lysc_refine *rfn; |
| 4832 | struct lysp_refine **new_rfn; |
| 4833 | uint32_t i; |
| 4834 | |
| 4835 | LY_ARRAY_FOR(uses_p->augments, u) { |
| 4836 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4837 | lysc_update_path(ctx, NULL, uses_p->augments[u].nodeid); |
| 4838 | |
| 4839 | /* parse the nodeid */ |
| 4840 | LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, uses_p->augments[u].nodeid, 0, NULL, &exp), cleanup); |
| 4841 | |
| 4842 | /* allocate new compiled augment and store it in the set */ |
| 4843 | aug = calloc(1, sizeof *aug); |
| 4844 | LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 4845 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, LY_SET_OPT_USEASLIST, NULL), cleanup); |
| 4846 | |
| 4847 | aug->nodeid = exp; |
| 4848 | exp = NULL; |
| 4849 | aug->nodeid_ctx_node = ctx_node; |
| 4850 | aug->aug_p = &uses_p->augments[u]; |
| 4851 | |
| 4852 | lysc_update_path(ctx, NULL, NULL); |
| 4853 | lysc_update_path(ctx, NULL, NULL); |
| 4854 | } |
| 4855 | |
| 4856 | LY_ARRAY_FOR(uses_p->refines, u) { |
| 4857 | lysc_update_path(ctx, NULL, "{refine}"); |
| 4858 | lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid); |
| 4859 | |
| 4860 | /* parse the nodeid */ |
| 4861 | LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, uses_p->refines[u].nodeid, 0, NULL, &exp), cleanup); |
| 4862 | |
| 4863 | /* try to find the node in already compiled refines */ |
| 4864 | rfn = NULL; |
| 4865 | for (i = 0; i < ctx->uses_rfns.count; ++i) { |
| 4866 | if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->mod_def, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid, |
| 4867 | ctx->mod_def)) { |
| 4868 | rfn = ctx->uses_rfns.objs[i]; |
| 4869 | break; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4870 | } |
| 4871 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4872 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4873 | if (!rfn) { |
| 4874 | /* allocate new compiled refine */ |
| 4875 | rfn = calloc(1, sizeof *rfn); |
| 4876 | LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 4877 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, LY_SET_OPT_USEASLIST, NULL), cleanup); |
| 4878 | |
| 4879 | rfn->nodeid = exp; |
| 4880 | exp = NULL; |
| 4881 | rfn->nodeid_ctx_node = ctx_node; |
| 4882 | } else { |
| 4883 | /* just free exp */ |
| 4884 | lyxp_expr_free(ctx->ctx, exp); |
| 4885 | exp = NULL; |
| 4886 | } |
| 4887 | |
| 4888 | /* add new parsed refine structure */ |
| 4889 | LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup); |
| 4890 | *new_rfn = &uses_p->refines[u]; |
| 4891 | |
| 4892 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4893 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4894 | } |
| 4895 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4896 | cleanup: |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4897 | lyxp_expr_free(ctx->ctx, exp); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4898 | return ret; |
| 4899 | } |
| 4900 | |
| 4901 | /** |
| 4902 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4903 | * If present, also apply uses's modificators. |
| 4904 | * |
| 4905 | * @param[in] ctx Compile context |
| 4906 | * @param[in] uses_p Parsed uses schema node. |
| 4907 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4908 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4909 | * the compile context. |
| 4910 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4911 | */ |
| 4912 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4913 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set) |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4914 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4915 | struct lysp_node *pnode; |
| 4916 | struct lysc_node *child; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4917 | struct lysp_grp *grp = NULL; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4918 | uint32_t i, grp_stack_count; |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 4919 | struct lys_module *grp_mod, *mod_old = ctx->mod_def; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4920 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 4921 | struct lysc_when *when_shared = NULL; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4922 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4923 | struct lysc_notif **notifs = NULL; |
| 4924 | struct lysc_action **actions = NULL; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4925 | struct ly_set uses_child_set = {0}; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4926 | |
| 4927 | /* find the referenced grouping */ |
| 4928 | LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod)); |
| 4929 | |
| 4930 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4931 | grp_stack_count = ctx->groupings.count; |
| 4932 | LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL)); |
| 4933 | if (grp_stack_count == ctx->groupings.count) { |
| 4934 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4935 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4936 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4937 | return LY_EVALID; |
| 4938 | } |
| 4939 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4940 | /* check status */ |
| 4941 | ret = lysc_check_status(ctx, uses_p->flags, ctx->mod_def, uses_p->name, grp->flags, grp_mod, grp->name); |
| 4942 | LY_CHECK_GOTO(ret, cleanup); |
| 4943 | |
| 4944 | /* compile any augments and refines so they can be applied during the grouping nodes compilation */ |
| 4945 | ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent); |
| 4946 | LY_CHECK_GOTO(ret, cleanup); |
| 4947 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4948 | /* switch context's mod_def */ |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4949 | ctx->mod_def = grp_mod; |
| 4950 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4951 | /* compile data nodes */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4952 | LY_LIST_FOR(grp->data, pnode) { |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4953 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4954 | ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3, &uses_child_set); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4955 | LY_CHECK_GOTO(ret, cleanup); |
| 4956 | } |
| 4957 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4958 | if (child_set) { |
| 4959 | /* add these children to our compiled child_set as well since uses is a schema-only node */ |
| 4960 | LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, LY_SET_OPT_USEASLIST, NULL), cleanup); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4961 | } |
| 4962 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4963 | if (uses_p->when) { |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 4964 | /* pass uses's when to all the data children */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4965 | for (i = 0; i < uses_child_set.count; ++i) { |
| 4966 | child = uses_child_set.snodes[i]; |
| 4967 | |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 4968 | ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent), child, &when_shared); |
| 4969 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4970 | } |
| 4971 | } |
| 4972 | |
| 4973 | /* compile actions */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4974 | if (grp->actions) { |
| 4975 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4976 | if (!actions) { |
| 4977 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4978 | grp->actions[0].name, lys_nodetype2str(grp->actions[0].nodetype), parent->name, |
| 4979 | lys_nodetype2str(parent->nodetype)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4980 | ret = LY_EVALID; |
| 4981 | goto cleanup; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4982 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4983 | COMPILE_OP_ARRAY_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup); |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 4984 | |
| 4985 | if (uses_p->when) { |
| 4986 | /* inherit when */ |
| 4987 | LY_ARRAY_FOR(*actions, u) { |
| 4988 | ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent), |
| 4989 | (struct lysc_node *)&(*actions)[u], &when_shared); |
| 4990 | LY_CHECK_GOTO(ret, cleanup); |
| 4991 | } |
| 4992 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4993 | } |
| 4994 | |
| 4995 | /* compile notifications */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4996 | if (grp->notifs) { |
| 4997 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4998 | if (!notifs) { |
| 4999 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5000 | grp->notifs[0].name, lys_nodetype2str(grp->notifs[0].nodetype), parent->name, |
| 5001 | lys_nodetype2str(parent->nodetype)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5002 | ret = LY_EVALID; |
| 5003 | goto cleanup; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 5004 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5005 | COMPILE_OP_ARRAY_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup); |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 5006 | |
| 5007 | if (uses_p->when) { |
| 5008 | /* inherit when */ |
| 5009 | LY_ARRAY_FOR(*notifs, u) { |
| 5010 | ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent), |
| 5011 | (struct lysc_node *)&(*notifs)[u], &when_shared); |
| 5012 | LY_CHECK_GOTO(ret, cleanup); |
| 5013 | } |
| 5014 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 5015 | } |
| 5016 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5017 | /* check that all augments were applied */ |
| 5018 | for (i = 0; i < ctx->uses_augs.count; ++i) { |
| 5019 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5020 | "Augment target node \"%s\" in grouping \"%s\" was not found.", |
Michal Vasko | 891d753 | 2020-10-07 09:41:38 +0200 | [diff] [blame] | 5021 | ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5022 | ret = LY_ENOTFOUND; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 5023 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 5024 | LY_CHECK_GOTO(ret, cleanup); |
| 5025 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5026 | /* check that all refines were applied */ |
| 5027 | for (i = 0; i < ctx->uses_rfns.count; ++i) { |
| 5028 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5029 | "Refine(s) target node \"%s\" in grouping \"%s\" was not found.", |
Michal Vasko | 891d753 | 2020-10-07 09:41:38 +0200 | [diff] [blame] | 5030 | ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5031 | ret = LY_ENOTFOUND; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5032 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5033 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5034 | |
| 5035 | cleanup: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5036 | /* reload previous context's mod_def */ |
| 5037 | ctx->mod_def = mod_old; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5038 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5039 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5040 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5041 | assert(ctx->groupings.count == grp_stack_count); |
| 5042 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5043 | ly_set_erase(&uses_child_set, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5044 | return ret; |
| 5045 | } |
| 5046 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5047 | static int |
| 5048 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5049 | { |
| 5050 | struct lysp_node *iter; |
| 5051 | int len = 0; |
| 5052 | |
| 5053 | *path = NULL; |
| 5054 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5055 | char *s = *path; |
| 5056 | char *id; |
| 5057 | |
| 5058 | switch (iter->nodetype) { |
| 5059 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5060 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5061 | break; |
| 5062 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5063 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5064 | break; |
| 5065 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5066 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5067 | break; |
| 5068 | default: |
| 5069 | id = strdup(iter->name); |
| 5070 | break; |
| 5071 | } |
| 5072 | |
| 5073 | if (!iter->parent) { |
| 5074 | /* print prefix */ |
| 5075 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5076 | } else { |
| 5077 | /* prefix is the same as in parent */ |
| 5078 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5079 | } |
| 5080 | free(s); |
| 5081 | free(id); |
| 5082 | } |
| 5083 | |
| 5084 | if (len < 0) { |
| 5085 | free(*path); |
| 5086 | *path = NULL; |
| 5087 | } else if (len == 0) { |
| 5088 | *path = strdup("/"); |
| 5089 | len = 1; |
| 5090 | } |
| 5091 | return len; |
| 5092 | } |
| 5093 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5094 | /** |
| 5095 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5096 | * |
| 5097 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5098 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5099 | */ |
| 5100 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5101 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_grp *grp) |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5102 | { |
| 5103 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5104 | char *path; |
| 5105 | int len; |
| 5106 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5107 | struct lysp_node_uses fake_uses = { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5108 | .parent = pnode, |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5109 | .nodetype = LYS_USES, |
| 5110 | .flags = 0, .next = NULL, |
| 5111 | .name = grp->name, |
| 5112 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5113 | .refines = NULL, .augments = NULL |
| 5114 | }; |
| 5115 | struct lysc_node_container fake_container = { |
| 5116 | .nodetype = LYS_CONTAINER, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5117 | .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5118 | .module = ctx->mod, |
| 5119 | .sp = NULL, .parent = NULL, .next = NULL, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5120 | .prev = (struct lysc_node *)&fake_container, |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5121 | .name = "fake", |
| 5122 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5123 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5124 | }; |
| 5125 | |
| 5126 | if (grp->parent) { |
| 5127 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5128 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5129 | |
| 5130 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5131 | if (len < 0) { |
| 5132 | LOGMEM(ctx->ctx); |
| 5133 | return LY_EMEM; |
| 5134 | } |
| 5135 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5136 | ctx->path_len = (uint32_t)len; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5137 | free(path); |
| 5138 | |
| 5139 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5140 | lysc_update_path(ctx, NULL, grp->name); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5141 | 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] | 5142 | lysc_update_path(ctx, NULL, NULL); |
| 5143 | lysc_update_path(ctx, NULL, NULL); |
| 5144 | |
| 5145 | ctx->path_len = 1; |
| 5146 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5147 | |
| 5148 | /* cleanup */ |
| 5149 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5150 | |
| 5151 | return ret; |
| 5152 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5153 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5154 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5155 | * @brief Set config flags for a node. |
| 5156 | * |
| 5157 | * @param[in] ctx Compile context. |
| 5158 | * @param[in] node Compiled node config to set. |
| 5159 | * @param[in] parent Parent of @p node. |
| 5160 | * @return LY_ERR value. |
| 5161 | */ |
| 5162 | static LY_ERR |
| 5163 | lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent) |
| 5164 | { |
| 5165 | if (node->nodetype == LYS_CASE) { |
| 5166 | /* case never has any config */ |
| 5167 | assert(!(node->flags & LYS_CONFIG_MASK)); |
| 5168 | return LY_SUCCESS; |
| 5169 | } |
| 5170 | |
| 5171 | /* adjust parent to always get the ancestor with config */ |
| 5172 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 5173 | parent = parent->parent; |
| 5174 | assert(parent); |
| 5175 | } |
| 5176 | |
| 5177 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
| 5178 | /* ignore config statements inside RPC/action data */ |
| 5179 | node->flags &= ~LYS_CONFIG_MASK; |
| 5180 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5181 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
| 5182 | /* ignore config statements inside Notification data */ |
| 5183 | node->flags &= ~LYS_CONFIG_MASK; |
| 5184 | node->flags |= LYS_CONFIG_R; |
| 5185 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
| 5186 | /* config not explicitely set, inherit it from parent */ |
| 5187 | if (parent) { |
| 5188 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5189 | } else { |
| 5190 | /* default is config true */ |
| 5191 | node->flags |= LYS_CONFIG_W; |
| 5192 | } |
| 5193 | } else { |
| 5194 | /* config set explicitely */ |
| 5195 | node->flags |= LYS_SET_CONFIG; |
| 5196 | } |
| 5197 | |
| 5198 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5199 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5200 | "Configuration node cannot be child of any state data node."); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5201 | return LY_EVALID; |
| 5202 | } |
| 5203 | |
| 5204 | return LY_SUCCESS; |
| 5205 | } |
| 5206 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5207 | static LY_ERR |
| 5208 | lysp_ext_dup(const struct ly_ctx *ctx, struct lysp_ext_instance *ext, const struct lysp_ext_instance *orig_ext) |
| 5209 | { |
| 5210 | LY_ERR ret = LY_SUCCESS; |
| 5211 | |
| 5212 | *ext = *orig_ext; |
| 5213 | DUP_STRING(ctx, orig_ext->name, ext->name, ret); |
| 5214 | DUP_STRING(ctx, orig_ext->argument, ext->argument, ret); |
| 5215 | |
| 5216 | return ret; |
| 5217 | } |
| 5218 | |
| 5219 | static LY_ERR |
| 5220 | lysp_restr_dup(const struct ly_ctx *ctx, struct lysp_restr *restr, const struct lysp_restr *orig_restr) |
| 5221 | { |
| 5222 | LY_ERR ret = LY_SUCCESS; |
| 5223 | |
| 5224 | if (orig_restr) { |
| 5225 | DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret); |
| 5226 | restr->arg.mod = orig_restr->arg.mod; |
| 5227 | DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret); |
| 5228 | DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret); |
| 5229 | DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret); |
| 5230 | DUP_STRING(ctx, orig_restr->ref, restr->ref, ret); |
| 5231 | DUP_ARRAY(ctx, orig_restr->exts, restr->exts, lysp_ext_dup); |
| 5232 | } |
| 5233 | |
| 5234 | return ret; |
| 5235 | } |
| 5236 | |
| 5237 | static LY_ERR |
| 5238 | lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str) |
| 5239 | { |
| 5240 | LY_ERR ret = LY_SUCCESS; |
| 5241 | |
| 5242 | DUP_STRING(ctx, *orig_str, *str, ret); |
| 5243 | |
| 5244 | return ret; |
| 5245 | } |
| 5246 | |
| 5247 | static LY_ERR |
| 5248 | lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname) |
| 5249 | { |
| 5250 | LY_ERR ret = LY_SUCCESS; |
| 5251 | |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 5252 | if (!orig_qname->str) { |
| 5253 | return LY_SUCCESS; |
| 5254 | } |
| 5255 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5256 | DUP_STRING(ctx, orig_qname->str, qname->str, ret); |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 5257 | assert(orig_qname->mod); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5258 | qname->mod = orig_qname->mod; |
| 5259 | |
| 5260 | return ret; |
| 5261 | } |
| 5262 | |
| 5263 | static LY_ERR |
| 5264 | lysp_type_enum_dup(const struct ly_ctx *ctx, struct lysp_type_enum *enm, const struct lysp_type_enum *orig_enm) |
| 5265 | { |
| 5266 | LY_ERR ret = LY_SUCCESS; |
| 5267 | |
| 5268 | DUP_STRING(ctx, orig_enm->name, enm->name, ret); |
| 5269 | DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret); |
| 5270 | DUP_STRING(ctx, orig_enm->ref, enm->ref, ret); |
| 5271 | enm->value = orig_enm->value; |
| 5272 | DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup); |
| 5273 | DUP_ARRAY(ctx, orig_enm->exts, enm->exts, lysp_ext_dup); |
| 5274 | enm->flags = orig_enm->flags; |
| 5275 | |
| 5276 | return ret; |
| 5277 | } |
| 5278 | |
| 5279 | static LY_ERR |
| 5280 | lysp_type_dup(const struct ly_ctx *ctx, struct lysp_type *type, const struct lysp_type *orig_type) |
| 5281 | { |
| 5282 | LY_ERR ret = LY_SUCCESS; |
| 5283 | |
| 5284 | DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done); |
| 5285 | |
| 5286 | if (orig_type->range) { |
| 5287 | type->range = calloc(1, sizeof *type->range); |
| 5288 | LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM); |
| 5289 | LY_CHECK_RET(lysp_restr_dup(ctx, type->range, orig_type->range)); |
| 5290 | } |
| 5291 | |
| 5292 | if (orig_type->length) { |
| 5293 | type->length = calloc(1, sizeof *type->length); |
| 5294 | LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM); |
| 5295 | LY_CHECK_RET(lysp_restr_dup(ctx, type->length, orig_type->length)); |
| 5296 | } |
| 5297 | |
| 5298 | DUP_ARRAY(ctx, orig_type->patterns, type->patterns, lysp_restr_dup); |
| 5299 | DUP_ARRAY(ctx, orig_type->enums, type->enums, lysp_type_enum_dup); |
| 5300 | DUP_ARRAY(ctx, orig_type->bits, type->bits, lysp_type_enum_dup); |
| 5301 | LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, &type->path), done); |
| 5302 | DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup); |
| 5303 | DUP_ARRAY(ctx, orig_type->types, type->types, lysp_type_dup); |
| 5304 | DUP_ARRAY(ctx, orig_type->exts, type->exts, lysp_ext_dup); |
| 5305 | |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 5306 | type->mod = orig_type->mod; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5307 | type->compiled = orig_type->compiled; |
| 5308 | |
| 5309 | type->fraction_digits = orig_type->fraction_digits; |
| 5310 | type->require_instance = orig_type->require_instance; |
| 5311 | type->flags = orig_type->flags; |
| 5312 | |
| 5313 | done: |
| 5314 | return ret; |
| 5315 | } |
| 5316 | |
| 5317 | static LY_ERR |
| 5318 | lysp_when_dup(const struct ly_ctx *ctx, struct lysp_when *when, const struct lysp_when *orig_when) |
| 5319 | { |
| 5320 | LY_ERR ret = LY_SUCCESS; |
| 5321 | |
| 5322 | DUP_STRING(ctx, orig_when->cond, when->cond, ret); |
| 5323 | DUP_STRING(ctx, orig_when->dsc, when->dsc, ret); |
| 5324 | DUP_STRING(ctx, orig_when->ref, when->ref, ret); |
| 5325 | DUP_ARRAY(ctx, orig_when->exts, when->exts, lysp_ext_dup); |
| 5326 | |
| 5327 | return ret; |
| 5328 | } |
| 5329 | |
| 5330 | static LY_ERR |
| 5331 | lysp_node_common_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig) |
| 5332 | { |
| 5333 | LY_ERR ret = LY_SUCCESS; |
| 5334 | |
| 5335 | node->parent = NULL; |
| 5336 | node->nodetype = orig->nodetype; |
| 5337 | node->flags = orig->flags; |
| 5338 | node->next = NULL; |
| 5339 | DUP_STRING(ctx, orig->name, node->name, ret); |
| 5340 | DUP_STRING(ctx, orig->dsc, node->dsc, ret); |
| 5341 | DUP_STRING(ctx, orig->ref, node->ref, ret); |
| 5342 | |
| 5343 | if (orig->when) { |
| 5344 | node->when = calloc(1, sizeof *node->when); |
| 5345 | LY_CHECK_ERR_RET(!node->when, LOGMEM(ctx), LY_EMEM); |
| 5346 | LY_CHECK_RET(lysp_when_dup(ctx, node->when, orig->when)); |
| 5347 | } |
| 5348 | |
| 5349 | DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup); |
| 5350 | DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup); |
| 5351 | |
| 5352 | return ret; |
| 5353 | } |
| 5354 | |
| 5355 | static LY_ERR |
| 5356 | lysp_node_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig) |
| 5357 | { |
| 5358 | LY_ERR ret = LY_SUCCESS; |
| 5359 | struct lysp_node_container *cont; |
| 5360 | const struct lysp_node_container *orig_cont; |
| 5361 | struct lysp_node_leaf *leaf; |
| 5362 | const struct lysp_node_leaf *orig_leaf; |
| 5363 | struct lysp_node_leaflist *llist; |
| 5364 | const struct lysp_node_leaflist *orig_llist; |
| 5365 | struct lysp_node_list *list; |
| 5366 | const struct lysp_node_list *orig_list; |
| 5367 | struct lysp_node_choice *choice; |
| 5368 | const struct lysp_node_choice *orig_choice; |
| 5369 | struct lysp_node_case *cas; |
| 5370 | const struct lysp_node_case *orig_cas; |
| 5371 | struct lysp_node_anydata *any; |
| 5372 | const struct lysp_node_anydata *orig_any; |
| 5373 | |
| 5374 | assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA)); |
| 5375 | |
| 5376 | /* common part */ |
| 5377 | LY_CHECK_RET(lysp_node_common_dup(ctx, node, orig)); |
| 5378 | |
| 5379 | /* specific part */ |
| 5380 | switch (node->nodetype) { |
| 5381 | case LYS_CONTAINER: |
| 5382 | cont = (struct lysp_node_container *)node; |
| 5383 | orig_cont = (const struct lysp_node_container *)orig; |
| 5384 | |
| 5385 | DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup); |
| 5386 | DUP_STRING(ctx, orig_cont->presence, cont->presence, ret); |
| 5387 | /* we do not need the rest */ |
| 5388 | break; |
| 5389 | case LYS_LEAF: |
| 5390 | leaf = (struct lysp_node_leaf *)node; |
| 5391 | orig_leaf = (const struct lysp_node_leaf *)orig; |
| 5392 | |
| 5393 | DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup); |
| 5394 | LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type)); |
| 5395 | DUP_STRING(ctx, orig_leaf->units, leaf->units, ret); |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 5396 | LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5397 | break; |
| 5398 | case LYS_LEAFLIST: |
| 5399 | llist = (struct lysp_node_leaflist *)node; |
| 5400 | orig_llist = (const struct lysp_node_leaflist *)orig; |
| 5401 | |
| 5402 | DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup); |
| 5403 | LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type)); |
| 5404 | DUP_STRING(ctx, orig_llist->units, llist->units, ret); |
| 5405 | DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup); |
| 5406 | llist->min = orig_llist->min; |
| 5407 | llist->max = orig_llist->max; |
| 5408 | break; |
| 5409 | case LYS_LIST: |
| 5410 | list = (struct lysp_node_list *)node; |
| 5411 | orig_list = (const struct lysp_node_list *)orig; |
| 5412 | |
| 5413 | DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup); |
| 5414 | DUP_STRING(ctx, orig_list->key, list->key, ret); |
| 5415 | /* we do not need these arrays */ |
| 5416 | DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup); |
| 5417 | list->min = orig_list->min; |
| 5418 | list->max = orig_list->max; |
| 5419 | break; |
| 5420 | case LYS_CHOICE: |
| 5421 | choice = (struct lysp_node_choice *)node; |
| 5422 | orig_choice = (const struct lysp_node_choice *)orig; |
| 5423 | |
| 5424 | /* we do not need children */ |
| 5425 | LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt)); |
| 5426 | break; |
| 5427 | case LYS_CASE: |
| 5428 | cas = (struct lysp_node_case *)node; |
| 5429 | orig_cas = (const struct lysp_node_case *)orig; |
| 5430 | |
| 5431 | /* we do not need children */ |
| 5432 | (void)cas; |
| 5433 | (void)orig_cas; |
| 5434 | break; |
| 5435 | case LYS_ANYDATA: |
| 5436 | case LYS_ANYXML: |
| 5437 | any = (struct lysp_node_anydata *)node; |
| 5438 | orig_any = (const struct lysp_node_anydata *)orig; |
| 5439 | |
| 5440 | DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup); |
| 5441 | break; |
| 5442 | default: |
| 5443 | LOGINT_RET(ctx); |
| 5444 | } |
| 5445 | |
| 5446 | return ret; |
| 5447 | } |
| 5448 | |
| 5449 | static LY_ERR |
| 5450 | lysp_action_inout_dup(const struct ly_ctx *ctx, struct lysp_action_inout *inout, const struct lysp_action_inout *orig) |
| 5451 | { |
| 5452 | inout->parent = NULL; |
| 5453 | inout->nodetype = orig->nodetype; |
| 5454 | DUP_ARRAY(ctx, orig->musts, inout->musts, lysp_restr_dup); |
| 5455 | /* we dot need these arrays */ |
| 5456 | DUP_ARRAY(ctx, orig->exts, inout->exts, lysp_ext_dup); |
| 5457 | |
| 5458 | return LY_SUCCESS; |
| 5459 | } |
| 5460 | |
| 5461 | static LY_ERR |
| 5462 | lysp_action_dup(const struct ly_ctx *ctx, struct lysp_action *act, const struct lysp_action *orig) |
| 5463 | { |
| 5464 | LY_ERR ret = LY_SUCCESS; |
| 5465 | |
| 5466 | act->parent = NULL; |
| 5467 | act->nodetype = orig->nodetype; |
| 5468 | act->flags = orig->flags; |
| 5469 | DUP_STRING(ctx, orig->name, act->name, ret); |
| 5470 | DUP_STRING(ctx, orig->dsc, act->dsc, ret); |
| 5471 | DUP_STRING(ctx, orig->ref, act->ref, ret); |
| 5472 | DUP_ARRAY(ctx, orig->iffeatures, act->iffeatures, lysp_qname_dup); |
| 5473 | |
| 5474 | act->input.nodetype = orig->input.nodetype; |
| 5475 | act->output.nodetype = orig->output.nodetype; |
| 5476 | /* we do not need choldren of in/out */ |
| 5477 | DUP_ARRAY(ctx, orig->exts, act->exts, lysp_ext_dup); |
| 5478 | |
| 5479 | return ret; |
| 5480 | } |
| 5481 | |
| 5482 | static LY_ERR |
| 5483 | lysp_notif_dup(const struct ly_ctx *ctx, struct lysp_notif *notif, const struct lysp_notif *orig) |
| 5484 | { |
| 5485 | LY_ERR ret = LY_SUCCESS; |
| 5486 | |
| 5487 | notif->parent = NULL; |
| 5488 | notif->nodetype = orig->nodetype; |
| 5489 | notif->flags = orig->flags; |
| 5490 | DUP_STRING(ctx, orig->name, notif->name, ret); |
| 5491 | DUP_STRING(ctx, orig->dsc, notif->dsc, ret); |
| 5492 | DUP_STRING(ctx, orig->ref, notif->ref, ret); |
| 5493 | DUP_ARRAY(ctx, orig->iffeatures, notif->iffeatures, lysp_qname_dup); |
| 5494 | DUP_ARRAY(ctx, orig->musts, notif->musts, lysp_restr_dup); |
| 5495 | /* we do not need these arrays */ |
| 5496 | DUP_ARRAY(ctx, orig->exts, notif->exts, lysp_ext_dup); |
| 5497 | |
| 5498 | return ret; |
| 5499 | } |
| 5500 | |
| 5501 | /** |
| 5502 | * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied. |
| 5503 | * |
| 5504 | * @param[in] ctx libyang context. |
| 5505 | * @param[in] pnode Node to duplicate. |
| 5506 | * @param[in] with_links Whether to also copy any links (child, parent pointers). |
| 5507 | * @param[out] dup_p Duplicated parsed node. |
| 5508 | * @return LY_ERR value. |
| 5509 | */ |
| 5510 | static LY_ERR |
| 5511 | lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p) |
| 5512 | { |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5513 | LY_ERR ret = LY_SUCCESS; |
| 5514 | void *mem = NULL; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5515 | |
| 5516 | if (!pnode) { |
| 5517 | *dup_p = NULL; |
| 5518 | return LY_SUCCESS; |
| 5519 | } |
| 5520 | |
| 5521 | switch (pnode->nodetype) { |
| 5522 | case LYS_CONTAINER: |
| 5523 | mem = calloc(1, sizeof(struct lysp_node_container)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5524 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5525 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5526 | break; |
| 5527 | case LYS_LEAF: |
| 5528 | mem = calloc(1, sizeof(struct lysp_node_leaf)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5529 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5530 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5531 | break; |
| 5532 | case LYS_LEAFLIST: |
| 5533 | mem = calloc(1, sizeof(struct lysp_node_leaflist)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5534 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5535 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5536 | break; |
| 5537 | case LYS_LIST: |
| 5538 | mem = calloc(1, sizeof(struct lysp_node_list)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5539 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5540 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5541 | break; |
| 5542 | case LYS_CHOICE: |
| 5543 | mem = calloc(1, sizeof(struct lysp_node_choice)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5544 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5545 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5546 | break; |
| 5547 | case LYS_CASE: |
| 5548 | mem = calloc(1, sizeof(struct lysp_node_case)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5549 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5550 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5551 | break; |
| 5552 | case LYS_ANYDATA: |
| 5553 | case LYS_ANYXML: |
| 5554 | mem = calloc(1, sizeof(struct lysp_node_anydata)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5555 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5556 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5557 | break; |
| 5558 | case LYS_INPUT: |
| 5559 | case LYS_OUTPUT: |
| 5560 | mem = calloc(1, sizeof(struct lysp_action_inout)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5561 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5562 | LY_CHECK_GOTO(ret = lysp_action_inout_dup(ctx, mem, (struct lysp_action_inout *)pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5563 | break; |
| 5564 | case LYS_ACTION: |
| 5565 | case LYS_RPC: |
| 5566 | mem = calloc(1, sizeof(struct lysp_action)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5567 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5568 | LY_CHECK_GOTO(ret = lysp_action_dup(ctx, mem, (struct lysp_action *)pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5569 | break; |
| 5570 | case LYS_NOTIF: |
| 5571 | mem = calloc(1, sizeof(struct lysp_notif)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5572 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5573 | LY_CHECK_GOTO(ret = lysp_notif_dup(ctx, mem, (struct lysp_notif *)pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5574 | break; |
| 5575 | default: |
| 5576 | LOGINT_RET(ctx); |
| 5577 | } |
| 5578 | |
| 5579 | if (with_links) { |
| 5580 | /* copy also parent and child pointers */ |
| 5581 | ((struct lysp_node *)mem)->parent = pnode->parent; |
| 5582 | switch (pnode->nodetype) { |
| 5583 | case LYS_CONTAINER: |
| 5584 | ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child; |
| 5585 | break; |
| 5586 | case LYS_LIST: |
| 5587 | ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child; |
| 5588 | break; |
| 5589 | case LYS_CHOICE: |
| 5590 | ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child; |
| 5591 | break; |
| 5592 | case LYS_CASE: |
| 5593 | ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child; |
| 5594 | break; |
| 5595 | default: |
| 5596 | break; |
| 5597 | } |
| 5598 | } |
| 5599 | |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5600 | cleanup: |
| 5601 | if (ret) { |
| 5602 | free(mem); |
| 5603 | } else { |
| 5604 | *dup_p = mem; |
| 5605 | } |
| 5606 | return ret; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | #define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \ |
| 5610 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s of %s node - it is not possible to %s \"%s\" property.", \ |
| 5611 | AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\ |
| 5612 | ret = LY_EVALID; \ |
| 5613 | goto cleanup; |
| 5614 | |
| 5615 | #define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \ |
| 5616 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5617 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid %s of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5618 | AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
| 5619 | ret = LY_EVALID; \ |
| 5620 | goto cleanup; \ |
| 5621 | } |
| 5622 | |
| 5623 | /** |
| 5624 | * @brief Apply refine. |
| 5625 | * |
| 5626 | * @param[in] ctx Compile context. |
| 5627 | * @param[in] rfn Refine to apply. |
| 5628 | * @param[in,out] target Refine target. |
| 5629 | * @return LY_ERR value. |
| 5630 | */ |
| 5631 | static LY_ERR |
| 5632 | lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target) |
| 5633 | { |
| 5634 | LY_ERR ret = LY_SUCCESS; |
| 5635 | LY_ARRAY_COUNT_TYPE u; |
| 5636 | struct lysp_qname *qname; |
| 5637 | struct lysp_restr **musts, *must; |
| 5638 | uint32_t *num; |
| 5639 | |
| 5640 | /* default value */ |
| 5641 | if (rfn->dflts) { |
| 5642 | switch (target->nodetype) { |
| 5643 | case LYS_LEAF: |
| 5644 | AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default"); |
| 5645 | |
| 5646 | FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str); |
| 5647 | DUP_STRING_GOTO(ctx->ctx, rfn->dflts[0], ((struct lysp_node_leaf *)target)->dflt.str, ret, cleanup); |
| 5648 | ((struct lysp_node_leaf *)target)->dflt.mod = ctx->mod; |
| 5649 | break; |
| 5650 | case LYS_LEAFLIST: |
| 5651 | if (ctx->mod->version < LYS_VERSION_1_1) { |
| 5652 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5653 | "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
| 5654 | ret = LY_EVALID; |
| 5655 | goto cleanup; |
| 5656 | } |
| 5657 | |
| 5658 | FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free); |
| 5659 | ((struct lysp_node_leaflist *)target)->dflts = NULL; |
| 5660 | LY_ARRAY_FOR(rfn->dflts, u) { |
| 5661 | LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup); |
| 5662 | DUP_STRING_GOTO(ctx->ctx, rfn->dflts[u], qname->str, ret, cleanup); |
| 5663 | qname->mod = ctx->mod; |
| 5664 | } |
| 5665 | break; |
| 5666 | case LYS_CHOICE: |
| 5667 | AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default"); |
| 5668 | |
| 5669 | FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str); |
| 5670 | DUP_STRING_GOTO(ctx->ctx, rfn->dflts[0], ((struct lysp_node_choice *)target)->dflt.str, ret, cleanup); |
| 5671 | ((struct lysp_node_choice *)target)->dflt.mod = ctx->mod; |
| 5672 | break; |
| 5673 | default: |
| 5674 | AMEND_WRONG_NODETYPE("refine", "replace", "default"); |
| 5675 | } |
| 5676 | } |
| 5677 | |
| 5678 | /* description */ |
| 5679 | if (rfn->dsc) { |
| 5680 | FREE_STRING(ctx->ctx, target->dsc); |
| 5681 | DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup); |
| 5682 | } |
| 5683 | |
| 5684 | /* reference */ |
| 5685 | if (rfn->ref) { |
| 5686 | FREE_STRING(ctx->ctx, target->ref); |
| 5687 | DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup); |
| 5688 | } |
| 5689 | |
| 5690 | /* config */ |
| 5691 | if (rfn->flags & LYS_CONFIG_MASK) { |
| 5692 | if (ctx->options & (LYSC_OPT_NOTIFICATION | LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
| 5693 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 5694 | ctx->options & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
| 5695 | } else { |
| 5696 | target->flags &= ~LYS_CONFIG_MASK; |
| 5697 | target->flags |= rfn->flags & LYS_CONFIG_MASK; |
| 5698 | } |
| 5699 | } |
| 5700 | |
| 5701 | /* mandatory */ |
| 5702 | if (rfn->flags & LYS_MAND_MASK) { |
| 5703 | switch (target->nodetype) { |
| 5704 | case LYS_LEAF: |
| 5705 | case LYS_CHOICE: |
| 5706 | case LYS_ANYDATA: |
| 5707 | case LYS_ANYXML: |
| 5708 | break; |
| 5709 | default: |
| 5710 | AMEND_WRONG_NODETYPE("refine", "replace", "mandatory"); |
| 5711 | } |
| 5712 | |
| 5713 | target->flags &= ~LYS_MAND_MASK; |
| 5714 | target->flags |= rfn->flags & LYS_MAND_MASK; |
| 5715 | } |
| 5716 | |
| 5717 | /* presence */ |
| 5718 | if (rfn->presence) { |
| 5719 | switch (target->nodetype) { |
| 5720 | case LYS_CONTAINER: |
| 5721 | break; |
| 5722 | default: |
| 5723 | AMEND_WRONG_NODETYPE("refine", "replace", "presence"); |
| 5724 | } |
| 5725 | |
| 5726 | FREE_STRING(ctx->ctx, ((struct lysp_node_container *)target)->presence); |
| 5727 | DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup); |
| 5728 | } |
| 5729 | |
| 5730 | /* must */ |
| 5731 | if (rfn->musts) { |
| 5732 | switch (target->nodetype) { |
| 5733 | case LYS_CONTAINER: |
| 5734 | case LYS_LIST: |
| 5735 | case LYS_LEAF: |
| 5736 | case LYS_LEAFLIST: |
| 5737 | case LYS_ANYDATA: |
| 5738 | case LYS_ANYXML: |
| 5739 | musts = &((struct lysp_node_container *)target)->musts; |
| 5740 | break; |
| 5741 | default: |
| 5742 | AMEND_WRONG_NODETYPE("refine", "add", "must"); |
| 5743 | } |
| 5744 | |
| 5745 | LY_ARRAY_FOR(rfn->musts, u) { |
| 5746 | LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup); |
| 5747 | LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup); |
| 5748 | } |
| 5749 | } |
| 5750 | |
| 5751 | /* min-elements */ |
| 5752 | if (rfn->flags & LYS_SET_MIN) { |
| 5753 | switch (target->nodetype) { |
| 5754 | case LYS_LEAFLIST: |
| 5755 | num = &((struct lysp_node_leaflist *)target)->min; |
| 5756 | break; |
| 5757 | case LYS_LIST: |
| 5758 | num = &((struct lysp_node_list *)target)->min; |
| 5759 | break; |
| 5760 | default: |
| 5761 | AMEND_WRONG_NODETYPE("refine", "replace", "min-elements"); |
| 5762 | } |
| 5763 | |
| 5764 | *num = rfn->min; |
| 5765 | } |
| 5766 | |
| 5767 | /* max-elements */ |
| 5768 | if (rfn->flags & LYS_SET_MAX) { |
| 5769 | switch (target->nodetype) { |
| 5770 | case LYS_LEAFLIST: |
| 5771 | num = &((struct lysp_node_leaflist *)target)->max; |
| 5772 | break; |
| 5773 | case LYS_LIST: |
| 5774 | num = &((struct lysp_node_list *)target)->max; |
| 5775 | break; |
| 5776 | default: |
| 5777 | AMEND_WRONG_NODETYPE("refine", "replace", "max-elements"); |
| 5778 | } |
| 5779 | |
| 5780 | *num = rfn->max; |
| 5781 | } |
| 5782 | |
| 5783 | /* if-feature */ |
| 5784 | if (rfn->iffeatures) { |
| 5785 | switch (target->nodetype) { |
| 5786 | case LYS_LEAF: |
| 5787 | case LYS_LEAFLIST: |
| 5788 | case LYS_LIST: |
| 5789 | case LYS_CONTAINER: |
| 5790 | case LYS_CHOICE: |
| 5791 | case LYS_CASE: |
| 5792 | case LYS_ANYDATA: |
| 5793 | case LYS_ANYXML: |
| 5794 | break; |
| 5795 | default: |
| 5796 | AMEND_WRONG_NODETYPE("refine", "add", "if-feature"); |
| 5797 | } |
| 5798 | |
| 5799 | LY_ARRAY_FOR(rfn->iffeatures, u) { |
| 5800 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup); |
| 5801 | DUP_STRING_GOTO(ctx->ctx, rfn->iffeatures[u].str, qname->str, ret, cleanup); |
| 5802 | qname->mod = ctx->mod; |
| 5803 | } |
| 5804 | } |
| 5805 | |
| 5806 | /* extension */ |
| 5807 | /* TODO refine extensions */ |
| 5808 | |
| 5809 | cleanup: |
| 5810 | return ret; |
| 5811 | } |
| 5812 | |
| 5813 | /** |
| 5814 | * @brief Apply deviate add. |
| 5815 | * |
| 5816 | * @param[in] ctx Compile context. |
| 5817 | * @param[in] d Deviate add to apply. |
| 5818 | * @param[in,out] target Deviation target. |
| 5819 | * @return LY_ERR value. |
| 5820 | */ |
| 5821 | static LY_ERR |
| 5822 | lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target) |
| 5823 | { |
| 5824 | LY_ERR ret = LY_SUCCESS; |
| 5825 | LY_ARRAY_COUNT_TYPE u; |
| 5826 | struct lysp_qname *qname; |
| 5827 | uint32_t *num; |
| 5828 | struct lysp_restr **musts, *must; |
| 5829 | |
| 5830 | #define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \ |
| 5831 | if (((TYPE)target)->MEMBER) { \ |
| 5832 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5833 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5834 | PROPERTY, ((TYPE)target)->VALUEMEMBER); \ |
| 5835 | ret = LY_EVALID; \ |
| 5836 | goto cleanup; \ |
| 5837 | } |
| 5838 | |
| 5839 | /* [units-stmt] */ |
| 5840 | if (d->units) { |
| 5841 | switch (target->nodetype) { |
| 5842 | case LYS_LEAF: |
| 5843 | case LYS_LEAFLIST: |
| 5844 | break; |
| 5845 | default: |
| 5846 | AMEND_WRONG_NODETYPE("deviation", "add", "units"); |
| 5847 | } |
| 5848 | |
| 5849 | DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units); |
| 5850 | DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup); |
| 5851 | } |
| 5852 | |
| 5853 | /* *must-stmt */ |
| 5854 | if (d->musts) { |
| 5855 | switch (target->nodetype) { |
| 5856 | case LYS_CONTAINER: |
| 5857 | case LYS_LIST: |
| 5858 | case LYS_LEAF: |
| 5859 | case LYS_LEAFLIST: |
| 5860 | case LYS_ANYDATA: |
| 5861 | case LYS_ANYXML: |
| 5862 | musts = &((struct lysp_node_container *)target)->musts; |
| 5863 | break; |
| 5864 | case LYS_NOTIF: |
| 5865 | musts = &((struct lysp_notif *)target)->musts; |
| 5866 | break; |
| 5867 | case LYS_INPUT: |
| 5868 | case LYS_OUTPUT: |
| 5869 | musts = &((struct lysp_action_inout *)target)->musts; |
| 5870 | break; |
| 5871 | default: |
| 5872 | AMEND_WRONG_NODETYPE("deviation", "add", "must"); |
| 5873 | } |
| 5874 | |
| 5875 | LY_ARRAY_FOR(d->musts, u) { |
| 5876 | LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup); |
| 5877 | LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup); |
| 5878 | } |
| 5879 | } |
| 5880 | |
| 5881 | /* *unique-stmt */ |
| 5882 | if (d->uniques) { |
| 5883 | switch (target->nodetype) { |
| 5884 | case LYS_LIST: |
| 5885 | break; |
| 5886 | default: |
| 5887 | AMEND_WRONG_NODETYPE("deviation", "add", "unique"); |
| 5888 | } |
| 5889 | |
| 5890 | LY_ARRAY_FOR(d->uniques, u) { |
| 5891 | LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup); |
| 5892 | DUP_STRING_GOTO(ctx->ctx, d->uniques[u], qname->str, ret, cleanup); |
| 5893 | qname->mod = ctx->mod; |
| 5894 | } |
| 5895 | } |
| 5896 | |
| 5897 | /* *default-stmt */ |
| 5898 | if (d->dflts) { |
| 5899 | switch (target->nodetype) { |
| 5900 | case LYS_LEAF: |
| 5901 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 5902 | DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str); |
| 5903 | |
| 5904 | DUP_STRING_GOTO(ctx->ctx, d->dflts[0], ((struct lysp_node_leaf *)target)->dflt.str, ret, cleanup); |
| 5905 | ((struct lysp_node_leaf *)target)->dflt.mod = ctx->mod; |
| 5906 | break; |
| 5907 | case LYS_LEAFLIST: |
| 5908 | LY_ARRAY_FOR(d->dflts, u) { |
| 5909 | LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup); |
| 5910 | DUP_STRING_GOTO(ctx->ctx, d->dflts[u], qname->str, ret, cleanup); |
| 5911 | qname->mod = ctx->mod; |
| 5912 | } |
| 5913 | break; |
| 5914 | case LYS_CHOICE: |
| 5915 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 5916 | DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str); |
| 5917 | |
| 5918 | DUP_STRING_GOTO(ctx->ctx, d->dflts[0], ((struct lysp_node_choice *)target)->dflt.str, ret, cleanup); |
| 5919 | ((struct lysp_node_choice *)target)->dflt.mod = ctx->mod; |
| 5920 | break; |
| 5921 | default: |
| 5922 | AMEND_WRONG_NODETYPE("deviation", "add", "default"); |
| 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | /* [config-stmt] */ |
| 5927 | if (d->flags & LYS_CONFIG_MASK) { |
| 5928 | switch (target->nodetype) { |
| 5929 | case LYS_CONTAINER: |
| 5930 | case LYS_LEAF: |
| 5931 | case LYS_LEAFLIST: |
| 5932 | case LYS_LIST: |
| 5933 | case LYS_CHOICE: |
| 5934 | case LYS_ANYDATA: |
| 5935 | case LYS_ANYXML: |
| 5936 | break; |
| 5937 | default: |
| 5938 | AMEND_WRONG_NODETYPE("deviation", "add", "config"); |
| 5939 | } |
| 5940 | |
| 5941 | if (target->flags & LYS_CONFIG_MASK) { |
| 5942 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5943 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5944 | target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5945 | ret = LY_EVALID; |
| 5946 | goto cleanup; |
| 5947 | } |
| 5948 | |
| 5949 | target->flags |= d->flags & LYS_CONFIG_MASK; |
| 5950 | } |
| 5951 | |
| 5952 | /* [mandatory-stmt] */ |
| 5953 | if (d->flags & LYS_MAND_MASK) { |
| 5954 | switch (target->nodetype) { |
| 5955 | case LYS_LEAF: |
| 5956 | case LYS_CHOICE: |
| 5957 | case LYS_ANYDATA: |
| 5958 | case LYS_ANYXML: |
| 5959 | break; |
| 5960 | default: |
| 5961 | AMEND_WRONG_NODETYPE("deviation", "add", "mandatory"); |
| 5962 | } |
| 5963 | |
| 5964 | if (target->flags & LYS_MAND_MASK) { |
| 5965 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5966 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5967 | target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5968 | ret = LY_EVALID; |
| 5969 | goto cleanup; |
| 5970 | } |
| 5971 | |
| 5972 | target->flags |= d->flags & LYS_MAND_MASK; |
| 5973 | } |
| 5974 | |
| 5975 | /* [min-elements-stmt] */ |
| 5976 | if (d->flags & LYS_SET_MIN) { |
| 5977 | switch (target->nodetype) { |
| 5978 | case LYS_LEAFLIST: |
| 5979 | num = &((struct lysp_node_leaflist *)target)->min; |
| 5980 | break; |
| 5981 | case LYS_LIST: |
| 5982 | num = &((struct lysp_node_list *)target)->min; |
| 5983 | break; |
| 5984 | default: |
| 5985 | AMEND_WRONG_NODETYPE("deviation", "add", "min-elements"); |
| 5986 | } |
| 5987 | |
| 5988 | if (target->flags & LYS_SET_MIN) { |
| 5989 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5990 | "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num); |
| 5991 | ret = LY_EVALID; |
| 5992 | goto cleanup; |
| 5993 | } |
| 5994 | |
| 5995 | *num = d->min; |
| 5996 | } |
| 5997 | |
| 5998 | /* [max-elements-stmt] */ |
| 5999 | if (d->flags & LYS_SET_MAX) { |
| 6000 | switch (target->nodetype) { |
| 6001 | case LYS_LEAFLIST: |
| 6002 | num = &((struct lysp_node_leaflist *)target)->max; |
| 6003 | break; |
| 6004 | case LYS_LIST: |
| 6005 | num = &((struct lysp_node_list *)target)->max; |
| 6006 | break; |
| 6007 | default: |
| 6008 | AMEND_WRONG_NODETYPE("deviation", "add", "max-elements"); |
| 6009 | } |
| 6010 | |
| 6011 | if (target->flags & LYS_SET_MAX) { |
| 6012 | if (*num) { |
| 6013 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6014 | "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").", |
| 6015 | *num); |
| 6016 | } else { |
| 6017 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6018 | "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\")."); |
| 6019 | } |
| 6020 | ret = LY_EVALID; |
| 6021 | goto cleanup; |
| 6022 | } |
| 6023 | |
| 6024 | *num = d->max; |
| 6025 | } |
| 6026 | |
| 6027 | cleanup: |
| 6028 | return ret; |
| 6029 | } |
| 6030 | |
| 6031 | /** |
| 6032 | * @brief Apply deviate delete. |
| 6033 | * |
| 6034 | * @param[in] ctx Compile context. |
| 6035 | * @param[in] d Deviate delete to apply. |
| 6036 | * @param[in,out] target Deviation target. |
| 6037 | * @return LY_ERR value. |
| 6038 | */ |
| 6039 | static LY_ERR |
| 6040 | lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target) |
| 6041 | { |
| 6042 | LY_ERR ret = LY_SUCCESS; |
| 6043 | struct lysp_restr **musts; |
| 6044 | LY_ARRAY_COUNT_TYPE u, v; |
| 6045 | struct lysp_qname **uniques, **dflts; |
| 6046 | |
| 6047 | #define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \ |
| 6048 | LY_ARRAY_FOR(d->DEV_ARRAY, u) { \ |
| 6049 | int found = 0; \ |
| 6050 | LY_ARRAY_FOR(ORIG_ARRAY, v) { \ |
| 6051 | if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \ |
| 6052 | found = 1; \ |
| 6053 | break; \ |
| 6054 | } \ |
| 6055 | } \ |
| 6056 | if (!found) { \ |
| 6057 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 6058 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 6059 | PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \ |
| 6060 | ret = LY_EVALID; \ |
| 6061 | goto cleanup; \ |
| 6062 | } \ |
| 6063 | LY_ARRAY_DECREMENT(ORIG_ARRAY); \ |
| 6064 | FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \ |
| 6065 | memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \ |
| 6066 | } \ |
| 6067 | if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \ |
| 6068 | LY_ARRAY_FREE(ORIG_ARRAY); \ |
| 6069 | ORIG_ARRAY = NULL; \ |
| 6070 | } |
| 6071 | |
| 6072 | #define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 6073 | if (!((TYPE)target)->MEMBER) { \ |
| 6074 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
| 6075 | ret = LY_EVALID; \ |
| 6076 | goto cleanup; \ |
| 6077 | } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \ |
| 6078 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 6079 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \ |
| 6080 | PROPERTY, VALUE, ((TYPE)target)->MEMBER); \ |
| 6081 | ret = LY_EVALID; \ |
| 6082 | goto cleanup; \ |
| 6083 | } |
| 6084 | |
| 6085 | /* [units-stmt] */ |
| 6086 | if (d->units) { |
| 6087 | switch (target->nodetype) { |
| 6088 | case LYS_LEAF: |
| 6089 | case LYS_LEAFLIST: |
| 6090 | break; |
| 6091 | default: |
| 6092 | AMEND_WRONG_NODETYPE("deviation", "delete", "units"); |
| 6093 | } |
| 6094 | |
| 6095 | DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units); |
| 6096 | FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units); |
| 6097 | ((struct lysp_node_leaf *)target)->units = NULL; |
| 6098 | } |
| 6099 | |
| 6100 | /* *must-stmt */ |
| 6101 | if (d->musts) { |
| 6102 | switch (target->nodetype) { |
| 6103 | case LYS_CONTAINER: |
| 6104 | case LYS_LIST: |
| 6105 | case LYS_LEAF: |
| 6106 | case LYS_LEAFLIST: |
| 6107 | case LYS_ANYDATA: |
| 6108 | case LYS_ANYXML: |
| 6109 | musts = &((struct lysp_node_container *)target)->musts; |
| 6110 | break; |
| 6111 | case LYS_NOTIF: |
| 6112 | musts = &((struct lysp_notif *)target)->musts; |
| 6113 | break; |
| 6114 | case LYS_INPUT: |
| 6115 | case LYS_OUTPUT: |
| 6116 | musts = &((struct lysp_action_inout *)target)->musts; |
| 6117 | break; |
| 6118 | default: |
| 6119 | AMEND_WRONG_NODETYPE("deviation", "delete", "must"); |
| 6120 | } |
| 6121 | |
| 6122 | DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must"); |
| 6123 | } |
| 6124 | |
| 6125 | /* *unique-stmt */ |
| 6126 | if (d->uniques) { |
| 6127 | switch (target->nodetype) { |
| 6128 | case LYS_LIST: |
| 6129 | break; |
| 6130 | default: |
| 6131 | AMEND_WRONG_NODETYPE("deviation", "delete", "unique"); |
| 6132 | } |
| 6133 | |
| 6134 | uniques = &((struct lysp_node_list *)target)->uniques; |
| 6135 | DEV_DEL_ARRAY(uniques, *uniques, , .str, lysp_qname_free, "unique"); |
| 6136 | } |
| 6137 | |
| 6138 | /* *default-stmt */ |
| 6139 | if (d->dflts) { |
| 6140 | switch (target->nodetype) { |
| 6141 | case LYS_LEAF: |
| 6142 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 6143 | DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0]); |
| 6144 | |
| 6145 | FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str); |
| 6146 | ((struct lysp_node_leaf *)target)->dflt.str = NULL; |
| 6147 | break; |
| 6148 | case LYS_LEAFLIST: |
| 6149 | dflts = &((struct lysp_node_leaflist *)target)->dflts; |
| 6150 | DEV_DEL_ARRAY(dflts, *dflts, , .str, lysp_qname_free, "default"); |
| 6151 | break; |
| 6152 | case LYS_CHOICE: |
| 6153 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 6154 | DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0]); |
| 6155 | |
| 6156 | FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str); |
| 6157 | ((struct lysp_node_choice *)target)->dflt.str = NULL; |
| 6158 | break; |
| 6159 | default: |
| 6160 | AMEND_WRONG_NODETYPE("deviation", "delete", "default"); |
| 6161 | } |
| 6162 | } |
| 6163 | |
| 6164 | cleanup: |
| 6165 | return ret; |
| 6166 | } |
| 6167 | |
| 6168 | /** |
| 6169 | * @brief Apply deviate replace. |
| 6170 | * |
| 6171 | * @param[in] ctx Compile context. |
| 6172 | * @param[in] d Deviate replace to apply. |
| 6173 | * @param[in,out] target Deviation target. |
| 6174 | * @return LY_ERR value. |
| 6175 | */ |
| 6176 | static LY_ERR |
| 6177 | lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target) |
| 6178 | { |
| 6179 | LY_ERR ret = LY_SUCCESS; |
| 6180 | uint32_t *num; |
| 6181 | |
| 6182 | #define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 6183 | if (!((TYPE)target)->MEMBER) { \ |
| 6184 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
| 6185 | ret = LY_EVALID; \ |
| 6186 | goto cleanup; \ |
| 6187 | } |
| 6188 | |
| 6189 | /* [type-stmt] */ |
| 6190 | if (d->type) { |
| 6191 | switch (target->nodetype) { |
| 6192 | case LYS_LEAF: |
| 6193 | case LYS_LEAFLIST: |
| 6194 | break; |
| 6195 | default: |
| 6196 | AMEND_WRONG_NODETYPE("deviation", "replace", "type"); |
| 6197 | } |
| 6198 | |
| 6199 | lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type); |
| 6200 | lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type); |
| 6201 | } |
| 6202 | |
| 6203 | /* [units-stmt] */ |
| 6204 | if (d->units) { |
| 6205 | switch (target->nodetype) { |
| 6206 | case LYS_LEAF: |
| 6207 | case LYS_LEAFLIST: |
| 6208 | break; |
| 6209 | default: |
| 6210 | AMEND_WRONG_NODETYPE("deviation", "replace", "units"); |
| 6211 | } |
| 6212 | |
| 6213 | DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units); |
| 6214 | FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units); |
| 6215 | DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup); |
| 6216 | } |
| 6217 | |
| 6218 | /* [default-stmt] */ |
| 6219 | if (d->dflt) { |
| 6220 | switch (target->nodetype) { |
| 6221 | case LYS_LEAF: |
| 6222 | DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt); |
| 6223 | |
| 6224 | FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str); |
| 6225 | DUP_STRING_GOTO(ctx->ctx, d->dflt, ((struct lysp_node_leaf *)target)->dflt.str, ret, cleanup); |
| 6226 | ((struct lysp_node_leaf *)target)->dflt.mod = ctx->mod; |
| 6227 | break; |
| 6228 | case LYS_CHOICE: |
| 6229 | DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt); |
| 6230 | |
| 6231 | FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str); |
| 6232 | DUP_STRING_GOTO(ctx->ctx, d->dflt, ((struct lysp_node_choice *)target)->dflt.str, ret, cleanup); |
| 6233 | ((struct lysp_node_choice *)target)->dflt.mod = ctx->mod; |
| 6234 | break; |
| 6235 | default: |
| 6236 | AMEND_WRONG_NODETYPE("deviation", "replace", "default"); |
| 6237 | } |
| 6238 | } |
| 6239 | |
| 6240 | /* [config-stmt] */ |
| 6241 | if (d->flags & LYS_CONFIG_MASK) { |
| 6242 | switch (target->nodetype) { |
| 6243 | case LYS_CONTAINER: |
| 6244 | case LYS_LEAF: |
| 6245 | case LYS_LEAFLIST: |
| 6246 | case LYS_LIST: |
| 6247 | case LYS_CHOICE: |
| 6248 | case LYS_ANYDATA: |
| 6249 | case LYS_ANYXML: |
| 6250 | break; |
| 6251 | default: |
| 6252 | AMEND_WRONG_NODETYPE("deviation", "replace", "config"); |
| 6253 | } |
| 6254 | |
| 6255 | if (!(target->flags & LYS_CONFIG_MASK)) { |
| 6256 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6257 | "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6258 | ret = LY_EVALID; |
| 6259 | goto cleanup; |
| 6260 | } |
| 6261 | |
| 6262 | target->flags &= ~LYS_CONFIG_MASK; |
| 6263 | target->flags |= d->flags & LYS_CONFIG_MASK; |
| 6264 | } |
| 6265 | |
| 6266 | /* [mandatory-stmt] */ |
| 6267 | if (d->flags & LYS_MAND_MASK) { |
| 6268 | switch (target->nodetype) { |
| 6269 | case LYS_LEAF: |
| 6270 | case LYS_CHOICE: |
| 6271 | case LYS_ANYDATA: |
| 6272 | case LYS_ANYXML: |
| 6273 | break; |
| 6274 | default: |
| 6275 | AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory"); |
| 6276 | } |
| 6277 | |
| 6278 | if (!(target->flags & LYS_MAND_MASK)) { |
| 6279 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6280 | "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6281 | ret = LY_EVALID; |
| 6282 | goto cleanup; |
| 6283 | } |
| 6284 | |
| 6285 | target->flags &= ~LYS_MAND_MASK; |
| 6286 | target->flags |= d->flags & LYS_MAND_MASK; |
| 6287 | } |
| 6288 | |
| 6289 | /* [min-elements-stmt] */ |
| 6290 | if (d->flags & LYS_SET_MIN) { |
| 6291 | switch (target->nodetype) { |
| 6292 | case LYS_LEAFLIST: |
| 6293 | num = &((struct lysp_node_leaflist *)target)->min; |
| 6294 | break; |
| 6295 | case LYS_LIST: |
| 6296 | num = &((struct lysp_node_list *)target)->min; |
| 6297 | break; |
| 6298 | default: |
| 6299 | AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements"); |
| 6300 | } |
| 6301 | |
| 6302 | if (!(target->flags & LYS_SET_MIN)) { |
| 6303 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6304 | "Invalid deviation replacing \"min-elements\" property which is not present."); |
| 6305 | ret = LY_EVALID; |
| 6306 | goto cleanup; |
| 6307 | } |
| 6308 | |
| 6309 | *num = d->min; |
| 6310 | } |
| 6311 | |
| 6312 | /* [max-elements-stmt] */ |
| 6313 | if (d->flags & LYS_SET_MAX) { |
| 6314 | switch (target->nodetype) { |
| 6315 | case LYS_LEAFLIST: |
| 6316 | num = &((struct lysp_node_leaflist *)target)->max; |
| 6317 | break; |
| 6318 | case LYS_LIST: |
| 6319 | num = &((struct lysp_node_list *)target)->max; |
| 6320 | break; |
| 6321 | default: |
| 6322 | AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements"); |
| 6323 | } |
| 6324 | |
| 6325 | if (!(target->flags & LYS_SET_MAX)) { |
| 6326 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6327 | "Invalid deviation replacing \"max-elements\" property which is not present."); |
| 6328 | ret = LY_EVALID; |
| 6329 | goto cleanup; |
| 6330 | } |
| 6331 | |
| 6332 | *num = d->max; |
| 6333 | } |
| 6334 | |
| 6335 | cleanup: |
| 6336 | return ret; |
| 6337 | } |
| 6338 | |
| 6339 | /** |
| 6340 | * @brief Get module of a single nodeid node name test. |
| 6341 | * |
| 6342 | * @param[in] ctx libyang context. |
| 6343 | * @param[in] nametest Nametest with an optional prefix. |
| 6344 | * @param[in] nametest_len Length of @p nametest. |
| 6345 | * @param[in] local_mod Module to return in case of no prefix. |
| 6346 | * @param[out] name Optional pointer to the name test without the prefix. |
| 6347 | * @param[out] name_len Length of @p name. |
| 6348 | * @return Resolved module. |
| 6349 | */ |
| 6350 | static const struct lys_module * |
| 6351 | lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len, |
| 6352 | const struct lys_module *local_mod, const char **name, size_t *name_len) |
| 6353 | { |
| 6354 | const struct lys_module *target_mod; |
| 6355 | const char *ptr; |
| 6356 | |
| 6357 | ptr = ly_strnchr(nametest, ':', nametest_len); |
| 6358 | if (ptr) { |
| 6359 | target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_PREF_SCHEMA, (void *)local_mod); |
| 6360 | if (!target_mod) { |
| 6361 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 6362 | "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
| 6363 | nametest_len, nametest, ptr - nametest, nametest, local_mod->name); |
| 6364 | return NULL; |
| 6365 | } |
| 6366 | |
| 6367 | if (name) { |
| 6368 | *name = ptr + 1; |
| 6369 | *name_len = nametest_len - ((ptr - nametest) + 1); |
| 6370 | } |
| 6371 | } else { |
| 6372 | target_mod = local_mod; |
| 6373 | if (name) { |
| 6374 | *name = nametest; |
| 6375 | *name_len = nametest_len; |
| 6376 | } |
| 6377 | } |
| 6378 | |
| 6379 | return target_mod; |
| 6380 | } |
| 6381 | |
| 6382 | /** |
| 6383 | * @brief Check whether a parsed node matches a single schema nodeid name test. |
| 6384 | * |
| 6385 | * @param[in] pnode Parsed node to consider. |
| 6386 | * @param[in] pnode_mod Compiled @p pnode to-be module. |
| 6387 | * @param[in] mod Expected module. |
| 6388 | * @param[in] name Expected name. |
| 6389 | * @param[in] name_len Length of @p name. |
| 6390 | * @return Whether it is a match or not. |
| 6391 | */ |
| 6392 | static ly_bool |
| 6393 | lysp_schema_nodeid_match_pnode(const struct lysp_node *pnode, const struct lys_module *pnode_mod, |
| 6394 | const struct lys_module *mod, const char *name, size_t name_len) |
| 6395 | { |
| 6396 | const char *pname; |
| 6397 | |
| 6398 | /* compare with the module of the node */ |
| 6399 | if (pnode_mod != mod) { |
| 6400 | return 0; |
| 6401 | } |
| 6402 | |
| 6403 | /* compare names */ |
| 6404 | if (pnode->nodetype & (LYS_ACTION | LYS_RPC)) { |
| 6405 | pname = ((struct lysp_action *)pnode)->name; |
| 6406 | } else if (pnode->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6407 | pname = (pnode->nodetype & LYS_INPUT) ? "input" : "output"; |
| 6408 | } else { |
| 6409 | pname = pnode->name; |
| 6410 | } |
| 6411 | if (ly_strncmp(pname, name, name_len)) { |
| 6412 | return 0; |
| 6413 | } |
| 6414 | |
| 6415 | return 1; |
| 6416 | } |
| 6417 | |
| 6418 | /** |
| 6419 | * @brief Check whether a compiled node matches a single schema nodeid name test. |
| 6420 | * |
| 6421 | * @param[in,out] node Compiled node to consider. On a match it is moved to its parent. |
| 6422 | * @param[in] mod Expected module. |
| 6423 | * @param[in] name Expected name. |
| 6424 | * @param[in] name_len Length of @p name. |
| 6425 | * @return Whether it is a match or not. |
| 6426 | */ |
| 6427 | static ly_bool |
| 6428 | lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name, |
| 6429 | size_t name_len) |
| 6430 | { |
| 6431 | const struct lys_module *node_mod; |
| 6432 | const char *node_name; |
| 6433 | |
| 6434 | /* compare with the module of the node */ |
| 6435 | if ((*node)->nodetype == LYS_INPUT) { |
| 6436 | node_mod = ((struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, input)))->module; |
| 6437 | } else if ((*node)->nodetype == LYS_OUTPUT) { |
| 6438 | node_mod = ((struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, output)))->module; |
| 6439 | } else { |
| 6440 | node_mod = (*node)->module; |
| 6441 | } |
| 6442 | if (node_mod != mod) { |
| 6443 | return 0; |
| 6444 | } |
| 6445 | |
| 6446 | /* compare names */ |
| 6447 | if ((*node)->nodetype == LYS_INPUT) { |
| 6448 | node_name = "input"; |
| 6449 | } else if ((*node)->nodetype == LYS_OUTPUT) { |
| 6450 | node_name = "output"; |
| 6451 | } else { |
| 6452 | node_name = (*node)->name; |
| 6453 | } |
| 6454 | if (ly_strncmp(node_name, name, name_len)) { |
| 6455 | return 0; |
| 6456 | } |
| 6457 | |
| 6458 | if ((*node)->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6459 | /* move up from input/output */ |
| 6460 | if ((*node)->nodetype == LYS_INPUT) { |
| 6461 | (*node) = (struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, input)); |
| 6462 | } else { |
| 6463 | (*node) = (struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, output)); |
| 6464 | } |
| 6465 | } else if ((*node)->parent && ((*node)->parent->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 6466 | /* move to the input/output */ |
| 6467 | if ((*node)->flags & LYS_CONFIG_W) { |
| 6468 | *node = (struct lysc_node *)&((struct lysc_action *)(*node)->parent)->input; |
| 6469 | } else { |
| 6470 | *node = (struct lysc_node *)&((struct lysc_action *)(*node)->parent)->output; |
| 6471 | } |
| 6472 | } else { |
| 6473 | /* move to next parent */ |
| 6474 | *node = (*node)->parent; |
| 6475 | } |
| 6476 | |
| 6477 | return 1; |
| 6478 | } |
| 6479 | |
| 6480 | /** |
| 6481 | * @brief Check whether a node matches specific schema nodeid. |
| 6482 | * |
| 6483 | * @param[in] exp Parsed nodeid to match. |
| 6484 | * @param[in] exp_mod Module to use for nodes in @p exp without a prefix. |
| 6485 | * @param[in] ctx_node Initial context node that should match, only for descendant paths. |
| 6486 | * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched. |
| 6487 | * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled. |
| 6488 | * @param[in] pnode_mod Compiled @p pnode to-be module. |
| 6489 | * @return Whether it is a match or not. |
| 6490 | */ |
| 6491 | static ly_bool |
| 6492 | lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lys_module *exp_mod, const struct lysc_node *ctx_node, |
| 6493 | const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod) |
| 6494 | { |
| 6495 | uint32_t i; |
| 6496 | const struct lys_module *mod; |
| 6497 | const char *name; |
| 6498 | size_t name_len; |
| 6499 | |
| 6500 | /* compare last node in the node ID */ |
| 6501 | i = exp->used - 1; |
| 6502 | |
| 6503 | /* get exp node ID module */ |
| 6504 | mod = lys_schema_node_get_module(exp_mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_mod, &name, &name_len); |
| 6505 | assert(mod); |
| 6506 | |
| 6507 | if (pnode) { |
| 6508 | /* compare on the last parsed-only node */ |
| 6509 | if (!lysp_schema_nodeid_match_pnode(pnode, pnode_mod, mod, name, name_len)) { |
| 6510 | return 0; |
| 6511 | } |
| 6512 | } else { |
| 6513 | /* using parent directly */ |
| 6514 | if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) { |
| 6515 | return 0; |
| 6516 | } |
| 6517 | } |
| 6518 | |
| 6519 | /* now compare all the compiled parents */ |
| 6520 | while (i > 1) { |
| 6521 | i -= 2; |
| 6522 | assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST); |
| 6523 | |
| 6524 | if (!parent) { |
| 6525 | /* no more parents but path continues */ |
| 6526 | return 0; |
| 6527 | } |
| 6528 | |
| 6529 | /* get exp node ID module */ |
| 6530 | mod = lys_schema_node_get_module(exp_mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_mod, &name, |
| 6531 | &name_len); |
| 6532 | assert(mod); |
| 6533 | |
| 6534 | /* compare with the parent */ |
| 6535 | if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) { |
| 6536 | return 0; |
| 6537 | } |
| 6538 | } |
| 6539 | |
| 6540 | if (ctx_node && (ctx_node != parent)) { |
| 6541 | /* descendant path has not finished in the context node */ |
| 6542 | return 0; |
| 6543 | } else if (!ctx_node && parent) { |
| 6544 | /* some parent was not matched */ |
| 6545 | return 0; |
| 6546 | } |
| 6547 | |
| 6548 | return 1; |
| 6549 | } |
| 6550 | |
| 6551 | static void |
| 6552 | lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug) |
| 6553 | { |
| 6554 | if (aug) { |
| 6555 | lyxp_expr_free(ctx, aug->nodeid); |
| 6556 | |
| 6557 | free(aug); |
| 6558 | } |
| 6559 | } |
| 6560 | |
| 6561 | static void |
| 6562 | lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev) |
| 6563 | { |
| 6564 | if (dev) { |
| 6565 | lyxp_expr_free(ctx, dev->nodeid); |
| 6566 | LY_ARRAY_FREE(dev->devs); |
| 6567 | LY_ARRAY_FREE(dev->dev_mods); |
| 6568 | |
| 6569 | free(dev); |
| 6570 | } |
| 6571 | } |
| 6572 | |
| 6573 | static void |
| 6574 | lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn) |
| 6575 | { |
| 6576 | if (rfn) { |
| 6577 | lyxp_expr_free(ctx, rfn->nodeid); |
| 6578 | LY_ARRAY_FREE(rfn->rfns); |
| 6579 | |
| 6580 | free(rfn); |
| 6581 | } |
| 6582 | } |
| 6583 | |
| 6584 | static void |
| 6585 | lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode) |
| 6586 | { |
| 6587 | if (!dev_pnode) { |
| 6588 | return; |
| 6589 | } |
| 6590 | |
| 6591 | switch (dev_pnode->nodetype) { |
| 6592 | case LYS_CONTAINER: |
| 6593 | ((struct lysp_node_container *)dev_pnode)->child = NULL; |
| 6594 | break; |
| 6595 | case LYS_LIST: |
| 6596 | ((struct lysp_node_list *)dev_pnode)->child = NULL; |
| 6597 | break; |
| 6598 | case LYS_CHOICE: |
| 6599 | ((struct lysp_node_choice *)dev_pnode)->child = NULL; |
| 6600 | break; |
| 6601 | case LYS_CASE: |
| 6602 | ((struct lysp_node_case *)dev_pnode)->child = NULL; |
| 6603 | break; |
| 6604 | case LYS_LEAF: |
| 6605 | case LYS_LEAFLIST: |
| 6606 | case LYS_ANYXML: |
| 6607 | case LYS_ANYDATA: |
| 6608 | /* no children */ |
| 6609 | break; |
| 6610 | case LYS_NOTIF: |
| 6611 | ((struct lysp_notif *)dev_pnode)->data = NULL; |
| 6612 | lysp_notif_free((struct ly_ctx *)ctx, (struct lysp_notif *)dev_pnode); |
| 6613 | free(dev_pnode); |
| 6614 | return; |
| 6615 | case LYS_RPC: |
| 6616 | case LYS_ACTION: |
| 6617 | ((struct lysp_action *)dev_pnode)->input.data = NULL; |
| 6618 | ((struct lysp_action *)dev_pnode)->output.data = NULL; |
| 6619 | lysp_action_free((struct ly_ctx *)ctx, (struct lysp_action *)dev_pnode); |
| 6620 | free(dev_pnode); |
| 6621 | return; |
| 6622 | case LYS_INPUT: |
| 6623 | case LYS_OUTPUT: |
| 6624 | ((struct lysp_action_inout *)dev_pnode)->data = NULL; |
| 6625 | lysp_action_inout_free((struct ly_ctx *)ctx, (struct lysp_action_inout *)dev_pnode); |
| 6626 | free(dev_pnode); |
| 6627 | return; |
| 6628 | default: |
| 6629 | LOGINT(ctx); |
| 6630 | return; |
| 6631 | } |
| 6632 | |
| 6633 | lysp_node_free((struct ly_ctx *)ctx, dev_pnode); |
| 6634 | } |
| 6635 | |
| 6636 | /** |
| 6637 | * @brief Compile and apply any precompiled deviations and refines targetting a node. |
| 6638 | * |
| 6639 | * @param[in] ctx Compile context. |
| 6640 | * @param[in] pnode Parsed node to consider. |
| 6641 | * @param[in] parent First compiled parent of @p pnode. |
| 6642 | * @param[out] dev_pnode Copy of parsed node @p pnode with deviations and refines, if any. NULL if there are none. |
| 6643 | * @param[out] no_supported Whether a not-supported deviation is defined for the node. |
| 6644 | * @return LY_ERR value. |
| 6645 | */ |
| 6646 | static LY_ERR |
| 6647 | lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent, |
| 6648 | struct lysp_node **dev_pnode, ly_bool *not_supported) |
| 6649 | { |
| 6650 | LY_ERR ret = LY_SUCCESS; |
| 6651 | uint32_t i; |
| 6652 | LY_ARRAY_COUNT_TYPE u; |
| 6653 | struct lys_module *orig_mod = ctx->mod, *orig_mod_def = ctx->mod_def; |
| 6654 | char orig_path[LYSC_CTX_BUFSIZE]; |
| 6655 | struct lysc_refine *rfn; |
| 6656 | struct lysc_deviation *dev; |
| 6657 | struct lysp_deviation *dev_p; |
| 6658 | struct lysp_deviate *d; |
| 6659 | |
| 6660 | *dev_pnode = NULL; |
| 6661 | *not_supported = 0; |
| 6662 | |
| 6663 | for (i = 0; i < ctx->uses_rfns.count; ++i) { |
| 6664 | rfn = ctx->uses_rfns.objs[i]; |
| 6665 | |
| 6666 | if (!lysp_schema_nodeid_match(rfn->nodeid, ctx->mod, rfn->nodeid_ctx_node, parent, pnode, ctx->mod)) { |
| 6667 | /* not our target node */ |
| 6668 | continue; |
| 6669 | } |
| 6670 | |
| 6671 | if (!*dev_pnode) { |
| 6672 | /* first refine on this node, create a copy first */ |
| 6673 | LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup); |
| 6674 | } |
| 6675 | |
| 6676 | /* apply all the refines by changing (the copy of) the parsed node */ |
| 6677 | LY_ARRAY_FOR(rfn->rfns, u) { |
| 6678 | /* apply refine, keep the current path and add to it */ |
| 6679 | lysc_update_path(ctx, NULL, "{refine}"); |
| 6680 | lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid); |
| 6681 | ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode); |
| 6682 | lysc_update_path(ctx, NULL, NULL); |
| 6683 | lysc_update_path(ctx, NULL, NULL); |
| 6684 | LY_CHECK_GOTO(ret, cleanup); |
| 6685 | } |
| 6686 | |
| 6687 | /* refine was applied, remove it */ |
| 6688 | lysc_refine_free(ctx->ctx, rfn); |
| 6689 | ly_set_rm_index(&ctx->uses_rfns, i, NULL); |
| 6690 | |
| 6691 | /* all the refines for one target node are in one structure, we are done */ |
| 6692 | break; |
| 6693 | } |
| 6694 | |
| 6695 | for (i = 0; i < ctx->devs.count; ++i) { |
| 6696 | dev = ctx->devs.objs[i]; |
| 6697 | |
| 6698 | if (!lysp_schema_nodeid_match(dev->nodeid, dev->nodeid_mod, NULL, parent, pnode, ctx->mod_def)) { |
| 6699 | /* not our target node */ |
| 6700 | continue; |
| 6701 | } |
| 6702 | |
| 6703 | if (dev->not_supported) { |
| 6704 | /* it is not supported, no more deviations */ |
| 6705 | *not_supported = 1; |
| 6706 | goto dev_applied; |
| 6707 | } |
| 6708 | |
| 6709 | if (!*dev_pnode) { |
| 6710 | /* first deviation on this node, create a copy first */ |
| 6711 | LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup); |
| 6712 | } |
| 6713 | |
| 6714 | /* apply all the deviates by changing (the copy of) the parsed node */ |
| 6715 | LY_ARRAY_FOR(dev->devs, u) { |
| 6716 | dev_p = dev->devs[u]; |
| 6717 | LY_LIST_FOR(dev_p->deviates, d) { |
| 6718 | /* generate correct path */ |
| 6719 | strcpy(orig_path, ctx->path); |
| 6720 | ctx->path_len = 1; |
| 6721 | ctx->mod = (struct lys_module *)dev->dev_mods[u]; |
| 6722 | ctx->mod_def = (struct lys_module *)dev->dev_mods[u]; |
| 6723 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 6724 | lysc_update_path(ctx, NULL, dev_p->nodeid); |
| 6725 | |
| 6726 | switch (d->mod) { |
| 6727 | case LYS_DEV_ADD: |
| 6728 | ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode); |
| 6729 | break; |
| 6730 | case LYS_DEV_DELETE: |
| 6731 | ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode); |
| 6732 | break; |
| 6733 | case LYS_DEV_REPLACE: |
| 6734 | ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode); |
| 6735 | break; |
| 6736 | default: |
| 6737 | LOGINT(ctx->ctx); |
| 6738 | ret = LY_EINT; |
| 6739 | } |
| 6740 | |
| 6741 | /* restore previous path */ |
| 6742 | strcpy(ctx->path, orig_path); |
| 6743 | ctx->path_len = strlen(ctx->path); |
| 6744 | ctx->mod = orig_mod; |
| 6745 | ctx->mod_def = orig_mod_def; |
| 6746 | |
| 6747 | LY_CHECK_GOTO(ret, cleanup); |
| 6748 | } |
| 6749 | } |
| 6750 | |
| 6751 | dev_applied: |
| 6752 | /* deviation was applied, remove it */ |
| 6753 | lysc_deviation_free(ctx->ctx, dev); |
| 6754 | ly_set_rm_index(&ctx->devs, i, NULL); |
| 6755 | |
| 6756 | /* all the deviations for one target node are in one structure, we are done */ |
| 6757 | break; |
| 6758 | } |
| 6759 | |
| 6760 | cleanup: |
| 6761 | if (ret) { |
| 6762 | lysp_dev_node_free(ctx->ctx, *dev_pnode); |
| 6763 | *dev_pnode = NULL; |
| 6764 | *not_supported = 0; |
| 6765 | } |
| 6766 | return ret; |
| 6767 | } |
| 6768 | |
| 6769 | /** |
| 6770 | * @brief Compile and apply any precompiled top-level or uses augments targetting a node. |
| 6771 | * |
| 6772 | * @param[in] ctx Compile context. |
| 6773 | * @param[in] node Compiled node to consider. |
| 6774 | * @return LY_ERR value. |
| 6775 | */ |
| 6776 | static LY_ERR |
| 6777 | lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node) |
| 6778 | { |
| 6779 | LY_ERR ret = LY_SUCCESS; |
| 6780 | struct lys_module *orig_mod = ctx->mod, *orig_mod_def = ctx->mod_def; |
| 6781 | uint32_t i; |
| 6782 | char orig_path[LYSC_CTX_BUFSIZE]; |
| 6783 | struct lysc_augment *aug; |
| 6784 | |
| 6785 | /* uses augments */ |
| 6786 | for (i = 0; i < ctx->uses_augs.count; ) { |
| 6787 | aug = ctx->uses_augs.objs[i]; |
| 6788 | |
| 6789 | if (!lysp_schema_nodeid_match(aug->nodeid, ctx->mod, aug->nodeid_ctx_node, node, NULL, NULL)) { |
| 6790 | /* not our target node */ |
| 6791 | ++i; |
| 6792 | continue; |
| 6793 | } |
| 6794 | |
| 6795 | /* apply augment, keep the current path and add to it */ |
| 6796 | lysc_update_path(ctx, NULL, "{augment}"); |
| 6797 | lysc_update_path(ctx, NULL, aug->aug_p->nodeid); |
| 6798 | ret = lys_compile_augment(ctx, aug->aug_p, node); |
| 6799 | lysc_update_path(ctx, NULL, NULL); |
| 6800 | lysc_update_path(ctx, NULL, NULL); |
| 6801 | LY_CHECK_GOTO(ret, cleanup); |
| 6802 | |
| 6803 | /* augment was applied, remove it (index may have changed because other augments could have been applied) */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6804 | ly_set_rm(&ctx->uses_augs, aug, NULL); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 6805 | lysc_augment_free(ctx->ctx, aug); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6806 | } |
| 6807 | |
| 6808 | /* top-level augments */ |
| 6809 | for (i = 0; i < ctx->augs.count; ) { |
| 6810 | aug = ctx->augs.objs[i]; |
| 6811 | |
| 6812 | if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_mod, NULL, node, NULL, NULL)) { |
| 6813 | /* not our target node */ |
| 6814 | ++i; |
| 6815 | continue; |
| 6816 | } |
| 6817 | |
| 6818 | /* apply augment, use the path and modules from the augment */ |
| 6819 | strcpy(orig_path, ctx->path); |
| 6820 | ctx->path_len = 1; |
| 6821 | lysc_update_path(ctx, NULL, "{augment}"); |
| 6822 | lysc_update_path(ctx, NULL, aug->aug_p->nodeid); |
| 6823 | ctx->mod = (struct lys_module *)aug->nodeid_mod; |
| 6824 | ctx->mod_def = (struct lys_module *)aug->nodeid_mod; |
| 6825 | ret = lys_compile_augment(ctx, aug->aug_p, node); |
| 6826 | strcpy(ctx->path, orig_path); |
| 6827 | ctx->path_len = strlen(ctx->path); |
| 6828 | LY_CHECK_GOTO(ret, cleanup); |
| 6829 | |
| 6830 | /* augment was applied, remove it */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6831 | ly_set_rm(&ctx->augs, aug, NULL); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 6832 | lysc_augment_free(ctx->ctx, aug); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6833 | } |
| 6834 | |
| 6835 | cleanup: |
| 6836 | ctx->mod = orig_mod; |
| 6837 | ctx->mod_def = orig_mod_def; |
| 6838 | return ret; |
| 6839 | } |
| 6840 | |
| 6841 | /** |
| 6842 | * @brief Prepare a top-level augment to be applied during data nodes compilation. |
| 6843 | * |
| 6844 | * @param[in] ctx Compile context. |
| 6845 | * @param[in] aug_p Parsed augment to be applied. |
| 6846 | * @param[in] mod_def Local module for @p aug_p. |
| 6847 | * @return LY_ERR value. |
| 6848 | */ |
| 6849 | static LY_ERR |
| 6850 | lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lys_module *mod_def) |
| 6851 | { |
| 6852 | LY_ERR ret = LY_SUCCESS; |
| 6853 | struct lyxp_expr *exp = NULL; |
| 6854 | struct lysc_augment *aug; |
| 6855 | const struct lys_module *mod; |
| 6856 | |
| 6857 | /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */ |
| 6858 | ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp); |
| 6859 | LY_CHECK_GOTO(ret, cleanup); |
| 6860 | |
| 6861 | mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], mod_def, NULL, NULL); |
| 6862 | LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup); |
| 6863 | if (mod != ctx->mod) { |
| 6864 | /* augment for another module, ignore */ |
| 6865 | goto cleanup; |
| 6866 | } |
| 6867 | |
| 6868 | /* allocate new compiled augment and store it in the set */ |
| 6869 | aug = calloc(1, sizeof *aug); |
| 6870 | LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 6871 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, LY_SET_OPT_USEASLIST, NULL), cleanup); |
| 6872 | |
| 6873 | aug->nodeid = exp; |
| 6874 | exp = NULL; |
| 6875 | aug->nodeid_mod = mod_def; |
| 6876 | aug->aug_p = aug_p; |
| 6877 | |
| 6878 | cleanup: |
| 6879 | lyxp_expr_free(ctx->ctx, exp); |
| 6880 | return ret; |
| 6881 | } |
| 6882 | |
| 6883 | /** |
| 6884 | * @brief Prepare all top-level augments for the current module to be applied during data nodes compilation. |
| 6885 | * |
| 6886 | * @param[in] ctx Compile context. |
| 6887 | * @return LY_ERR value. |
| 6888 | */ |
| 6889 | static LY_ERR |
| 6890 | lys_precompile_own_augments(struct lysc_ctx *ctx) |
| 6891 | { |
| 6892 | LY_ARRAY_COUNT_TYPE u, v, w; |
| 6893 | const struct lys_module *aug_mod; |
| 6894 | |
| 6895 | LY_ARRAY_FOR(ctx->mod->augmented_by, u) { |
| 6896 | aug_mod = ctx->mod->augmented_by[u]; |
| 6897 | |
| 6898 | /* collect all module augments */ |
| 6899 | LY_ARRAY_FOR(aug_mod->parsed->augments, v) { |
| 6900 | LY_CHECK_RET(lys_precompile_own_augment(ctx, &aug_mod->parsed->augments[v], aug_mod)); |
| 6901 | } |
| 6902 | |
| 6903 | /* collect all submodules augments */ |
| 6904 | LY_ARRAY_FOR(aug_mod->parsed->includes, v) { |
| 6905 | LY_ARRAY_FOR(aug_mod->parsed->includes[v].submodule->augments, w) { |
| 6906 | LY_CHECK_RET(lys_precompile_own_augment(ctx, &aug_mod->parsed->includes[v].submodule->augments[w], aug_mod)); |
| 6907 | } |
| 6908 | } |
| 6909 | } |
| 6910 | |
| 6911 | return LY_SUCCESS; |
| 6912 | } |
| 6913 | |
| 6914 | /** |
| 6915 | * @brief Prepare a deviation to be applied during data nodes compilation. |
| 6916 | * |
| 6917 | * @param[in] ctx Compile context. |
| 6918 | * @param[in] dev_p Parsed deviation to be applied. |
| 6919 | * @param[in] mod_def Local module for @p dev_p. |
| 6920 | * @return LY_ERR value. |
| 6921 | */ |
| 6922 | static LY_ERR |
| 6923 | lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lys_module *mod_def) |
| 6924 | { |
| 6925 | LY_ERR ret = LY_SUCCESS; |
| 6926 | struct lysc_deviation *dev = NULL; |
| 6927 | struct lyxp_expr *exp = NULL; |
| 6928 | struct lysp_deviation **new_dev; |
| 6929 | const struct lys_module *mod, **new_dev_mod; |
| 6930 | uint32_t i; |
| 6931 | |
| 6932 | /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */ |
| 6933 | ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp); |
| 6934 | LY_CHECK_GOTO(ret, cleanup); |
| 6935 | |
| 6936 | mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], mod_def, NULL, NULL); |
| 6937 | LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup); |
| 6938 | if (mod != ctx->mod) { |
| 6939 | /* deviation for another module, ignore */ |
| 6940 | goto cleanup; |
| 6941 | } |
| 6942 | |
| 6943 | /* try to find the node in already compiled deviations */ |
| 6944 | for (i = 0; i < ctx->devs.count; ++i) { |
| 6945 | if (lys_abs_schema_nodeid_match(ctx->ctx, exp, mod_def, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid, |
| 6946 | ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid_mod)) { |
| 6947 | dev = ctx->devs.objs[i]; |
| 6948 | break; |
| 6949 | } |
| 6950 | } |
| 6951 | |
| 6952 | if (!dev) { |
| 6953 | /* allocate new compiled deviation */ |
| 6954 | dev = calloc(1, sizeof *dev); |
| 6955 | LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 6956 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, LY_SET_OPT_USEASLIST, NULL), cleanup); |
| 6957 | |
| 6958 | dev->nodeid = exp; |
| 6959 | exp = NULL; |
| 6960 | dev->nodeid_mod = mod_def; |
| 6961 | } |
| 6962 | |
| 6963 | /* add new parsed deviation structure */ |
| 6964 | LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup); |
| 6965 | *new_dev = dev_p; |
| 6966 | LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_mods, new_dev_mod, ret, cleanup); |
| 6967 | *new_dev_mod = mod_def; |
| 6968 | |
| 6969 | cleanup: |
| 6970 | lyxp_expr_free(ctx->ctx, exp); |
| 6971 | return ret; |
| 6972 | } |
| 6973 | |
| 6974 | /** |
| 6975 | * @brief Prepare all deviations for the current module to be applied during data nodes compilation. |
| 6976 | * |
| 6977 | * @param[in] ctx Compile context. |
| 6978 | * @return LY_ERR value. |
| 6979 | */ |
| 6980 | static LY_ERR |
| 6981 | lys_precompile_own_deviations(struct lysc_ctx *ctx) |
| 6982 | { |
| 6983 | LY_ARRAY_COUNT_TYPE u, v, w; |
| 6984 | const struct lys_module *dev_mod; |
| 6985 | struct lysc_deviation *dev; |
| 6986 | struct lysp_deviate *d; |
| 6987 | int not_supported; |
| 6988 | uint32_t i; |
| 6989 | |
| 6990 | LY_ARRAY_FOR(ctx->mod->deviated_by, u) { |
| 6991 | dev_mod = ctx->mod->deviated_by[u]; |
| 6992 | |
| 6993 | /* compile all module deviations */ |
| 6994 | LY_ARRAY_FOR(dev_mod->parsed->deviations, v) { |
| 6995 | LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod)); |
| 6996 | } |
| 6997 | |
| 6998 | /* compile all submodules deviations */ |
| 6999 | LY_ARRAY_FOR(dev_mod->parsed->includes, v) { |
| 7000 | LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) { |
| 7001 | LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w], dev_mod)); |
| 7002 | } |
| 7003 | } |
| 7004 | } |
| 7005 | |
| 7006 | /* set not-supported flags for all the deviations */ |
| 7007 | for (i = 0; i < ctx->devs.count; ++i) { |
| 7008 | dev = ctx->devs.objs[i]; |
| 7009 | not_supported = 0; |
| 7010 | |
| 7011 | LY_ARRAY_FOR(dev->devs, u) { |
| 7012 | LY_LIST_FOR(dev->devs[u]->deviates, d) { |
| 7013 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 7014 | not_supported = 1; |
| 7015 | break; |
| 7016 | } |
| 7017 | } |
| 7018 | if (not_supported) { |
| 7019 | break; |
| 7020 | } |
| 7021 | } |
| 7022 | if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) { |
| 7023 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7024 | "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr); |
| 7025 | return LY_EVALID; |
| 7026 | } |
| 7027 | |
| 7028 | dev->not_supported = not_supported; |
| 7029 | } |
| 7030 | |
| 7031 | return LY_SUCCESS; |
| 7032 | } |
| 7033 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7034 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7035 | * @brief Compile parsed schema node information. |
| 7036 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7037 | * @param[in] pnode Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7038 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 7039 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 7040 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 7041 | * @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). |
| 7042 | * 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] | 7043 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 7044 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7045 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7046 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status, |
| 7047 | struct ly_set *child_set) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7048 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 7049 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 7050 | struct lysc_node *node = NULL; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7051 | struct lysp_node *dev_pnode = NULL, *orig_pnode = pnode; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7052 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7053 | ly_bool not_supported; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7054 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7055 | 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] | 7056 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7057 | if (pnode->nodetype != LYS_USES) { |
| 7058 | lysc_update_path(ctx, parent, pnode->name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7059 | } else { |
| 7060 | lysc_update_path(ctx, NULL, "{uses}"); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7061 | lysc_update_path(ctx, NULL, pnode->name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7062 | } |
| 7063 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7064 | switch (pnode->nodetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7065 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7066 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7067 | node_compile_spec = lys_compile_node_container; |
| 7068 | break; |
| 7069 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7070 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7071 | node_compile_spec = lys_compile_node_leaf; |
| 7072 | break; |
| 7073 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7074 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 7075 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7076 | break; |
| 7077 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7078 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 7079 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7080 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7081 | case LYS_CHOICE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7082 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 7083 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7084 | break; |
Michal Vasko | 891d753 | 2020-10-07 09:41:38 +0200 | [diff] [blame] | 7085 | case LYS_CASE: |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7086 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case)); |
| 7087 | node_compile_spec = lys_compile_node_case; |
| 7088 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7089 | case LYS_ANYXML: |
| 7090 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7091 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 7092 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7093 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7094 | case LYS_USES: |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7095 | ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7096 | lysc_update_path(ctx, NULL, NULL); |
| 7097 | lysc_update_path(ctx, NULL, NULL); |
| 7098 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7099 | default: |
| 7100 | LOGINT(ctx->ctx); |
| 7101 | return LY_EINT; |
| 7102 | } |
| 7103 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7104 | |
| 7105 | /* compile any deviations for this node */ |
| 7106 | LY_CHECK_ERR_RET(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, ¬_supported), |
| 7107 | free(node), ret); |
| 7108 | if (not_supported) { |
| 7109 | free(node); |
| 7110 | lysc_update_path(ctx, NULL, NULL); |
| 7111 | return LY_SUCCESS; |
| 7112 | } else if (dev_pnode) { |
| 7113 | pnode = dev_pnode; |
| 7114 | } |
| 7115 | |
| 7116 | node->nodetype = pnode->nodetype; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7117 | node->module = ctx->mod; |
| 7118 | node->prev = node; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7119 | node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7120 | |
| 7121 | /* config */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7122 | ret = lys_compile_config(ctx, node, parent); |
| 7123 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7124 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7125 | /* list ordering */ |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 7126 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 7127 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 7128 | LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 7129 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 7130 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 7131 | node->flags &= ~LYS_ORDBY_MASK; |
| 7132 | node->flags |= LYS_ORDBY_SYSTEM; |
| 7133 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 7134 | /* default ordering is system */ |
| 7135 | node->flags |= LYS_ORDBY_SYSTEM; |
| 7136 | } |
| 7137 | } |
| 7138 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7139 | /* status - it is not inherited by specification, but it does not make sense to have |
| 7140 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7141 | 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] | 7142 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7143 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7144 | node->sp = orig_pnode; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7145 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7146 | DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error); |
| 7147 | DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error); |
| 7148 | DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7149 | COMPILE_ARRAY_GOTO(ctx, pnode->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7150 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7151 | /* insert into parent's children/compiled module (we can no longer free the node separately on error) */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7152 | LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup); |
| 7153 | |
Michal Vasko | 4865ac4 | 2020-10-12 16:31:06 +0200 | [diff] [blame^] | 7154 | if (pnode->when) { |
| 7155 | /* compile when */ |
| 7156 | ret = lys_compile_when(ctx, pnode->when, pnode->flags, lysc_xpath_context(node), node, NULL); |
| 7157 | LY_CHECK_GOTO(ret, cleanup); |
| 7158 | } |
| 7159 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7160 | /* connect any augments */ |
| 7161 | LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7162 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7163 | /* nodetype-specific part */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7164 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7165 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7166 | /* final compilation tasks that require the node to be connected */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7167 | COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, LYEXT_PAR_NODE, ret, cleanup); |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 7168 | if (node->flags & LYS_MAND_TRUE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7169 | /* inherit LYS_MAND_TRUE in parent containers */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 7170 | lys_compile_mandatory_parents(parent, 1); |
| 7171 | } |
| 7172 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7173 | if (child_set) { |
| 7174 | /* add the new node into set */ |
| 7175 | LY_CHECK_GOTO(ret = ly_set_add(child_set, node, LY_SET_OPT_USEASLIST, NULL), cleanup); |
| 7176 | } |
| 7177 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7178 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7179 | lysp_dev_node_free(ctx->ctx, dev_pnode); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7180 | return LY_SUCCESS; |
| 7181 | |
| 7182 | error: |
| 7183 | lysc_node_free(ctx->ctx, node); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7184 | cleanup: |
| 7185 | if (dev_pnode) { |
| 7186 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_OTHER, "Compilation of a deviated and/or refined node failed."); |
| 7187 | lysp_dev_node_free(ctx->ctx, dev_pnode); |
| 7188 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7189 | return ret; |
| 7190 | } |
| 7191 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7192 | /** |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7193 | * @brief Add a module reference into an array, checks for duplicities. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7194 | * |
| 7195 | * @param[in] ctx Compile context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7196 | * @param[in] mod Module reference to add. |
| 7197 | * @param[in,out] mod_array Module sized array to add to. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7198 | * @return LY_ERR value. |
| 7199 | */ |
| 7200 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7201 | lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array) |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7202 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7203 | LY_ARRAY_COUNT_TYPE u; |
| 7204 | struct lys_module **new_mod; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7205 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7206 | LY_ARRAY_FOR(*mod_array, u) { |
| 7207 | if ((*mod_array)[u] == mod) { |
| 7208 | /* already there */ |
| 7209 | return LY_EEXIST; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7210 | } |
| 7211 | } |
| 7212 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7213 | /* add the new module ref */ |
| 7214 | LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM); |
| 7215 | *new_mod = mod; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7216 | |
| 7217 | return LY_SUCCESS; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7218 | } |
| 7219 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7220 | /** |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7221 | * @brief Compile top-level augments and deviations defined in the current module. |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 7222 | * Generally, just add the module refence to the target modules. But in case |
| 7223 | * of foreign augments, they are directly applied. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7224 | * |
| 7225 | * @param[in] ctx Compile context. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7226 | * @return LY_ERR value. |
| 7227 | */ |
| 7228 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7229 | lys_precompile_augments_deviations(struct lysc_ctx *ctx) |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7230 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7231 | LY_ERR ret = LY_SUCCESS; |
| 7232 | LY_ARRAY_COUNT_TYPE u, v; |
| 7233 | const struct lysp_module *mod_p; |
| 7234 | const struct lysc_node *target; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7235 | struct lys_module *mod; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7236 | struct lysp_submodule *submod; |
| 7237 | ly_bool has_dev = 0; |
| 7238 | uint16_t flags; |
| 7239 | uint32_t idx, opt_prev = ctx->options; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7240 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 7241 | for (idx = 0; idx < ctx->ctx->implementing.count; ++idx) { |
| 7242 | if (ctx->mod == ctx->ctx->implementing.objs[idx]) { |
| 7243 | break; |
| 7244 | } |
| 7245 | } |
| 7246 | if (idx == ctx->ctx->implementing.count) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7247 | /* it was already implemented and all the augments and deviations fully applied */ |
| 7248 | return LY_SUCCESS; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7249 | } |
| 7250 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 7251 | mod_p = ctx->mod->parsed; |
| 7252 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7253 | LY_ARRAY_FOR(mod_p->augments, u) { |
| 7254 | lysc_update_path(ctx, NULL, "{augment}"); |
| 7255 | lysc_update_path(ctx, NULL, mod_p->augments[u].nodeid); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7256 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7257 | /* get target module */ |
| 7258 | ret = lys_nodeid_check(ctx, mod_p->augments[u].nodeid, 1, &mod, NULL); |
| 7259 | LY_CHECK_RET(ret); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7260 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7261 | /* add this module into the target module augmented_by, if not there already from previous augments */ |
| 7262 | lys_array_add_mod_ref(ctx, ctx->mod, &mod->augmented_by); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7263 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7264 | /* if we are compiling this module, we cannot add augments to it yet */ |
| 7265 | if (mod != ctx->mod) { |
| 7266 | /* apply the augment, find the target node first */ |
| 7267 | flags = 0; |
| 7268 | ret = lysc_resolve_schema_nodeid(ctx, mod_p->augments[u].nodeid, 0, NULL, ctx->mod_def, 0, &target, &flags); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7269 | LY_CHECK_RET(ret); |
| 7270 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7271 | /* apply the augment */ |
| 7272 | ctx->options |= flags; |
| 7273 | ret = lys_compile_augment(ctx, &mod_p->augments[u], (struct lysc_node *)target); |
| 7274 | ctx->options = opt_prev; |
| 7275 | LY_CHECK_RET(ret); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7276 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7277 | |
| 7278 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7279 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7280 | } |
| 7281 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7282 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 7283 | /* get target module */ |
| 7284 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 7285 | lysc_update_path(ctx, NULL, mod_p->deviations[u].nodeid); |
| 7286 | ret = lys_nodeid_check(ctx, mod_p->deviations[u].nodeid, 1, &mod, NULL); |
| 7287 | lysc_update_path(ctx, NULL, NULL); |
| 7288 | lysc_update_path(ctx, NULL, NULL); |
| 7289 | LY_CHECK_RET(ret); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 7290 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7291 | /* add this module into the target module deviated_by, if not there already from previous deviations */ |
| 7292 | lys_array_add_mod_ref(ctx, ctx->mod, &mod->deviated_by); |
| 7293 | |
| 7294 | /* new deviation added to the target module */ |
| 7295 | has_dev = 1; |
| 7296 | } |
| 7297 | |
| 7298 | /* the same for augments and deviations in submodules */ |
| 7299 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 7300 | submod = mod_p->includes[v].submodule; |
| 7301 | LY_ARRAY_FOR(submod->augments, u) { |
| 7302 | lysc_update_path(ctx, NULL, "{augment}"); |
| 7303 | lysc_update_path(ctx, NULL, submod->augments[u].nodeid); |
| 7304 | |
| 7305 | ret = lys_nodeid_check(ctx, submod->augments[u].nodeid, 1, &mod, NULL); |
| 7306 | LY_CHECK_RET(ret); |
| 7307 | |
| 7308 | lys_array_add_mod_ref(ctx, ctx->mod, &mod->augmented_by); |
| 7309 | if (mod != ctx->mod) { |
| 7310 | flags = 0; |
| 7311 | ret = lysc_resolve_schema_nodeid(ctx, mod_p->augments[u].nodeid, 0, NULL, ctx->mod_def, 0, &target, &flags); |
| 7312 | LY_CHECK_RET(ret); |
| 7313 | |
| 7314 | ctx->options |= flags; |
| 7315 | ret = lys_compile_augment(ctx, &submod->augments[u], (struct lysc_node *)target); |
| 7316 | ctx->options = opt_prev; |
| 7317 | LY_CHECK_RET(ret); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 7318 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7319 | |
| 7320 | lysc_update_path(ctx, NULL, NULL); |
| 7321 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 7322 | } |
| 7323 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7324 | LY_ARRAY_FOR(submod->deviations, u) { |
| 7325 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 7326 | lysc_update_path(ctx, NULL, submod->deviations[u].nodeid); |
| 7327 | ret = lys_nodeid_check(ctx, submod->deviations[u].nodeid, 1, &mod, NULL); |
| 7328 | lysc_update_path(ctx, NULL, NULL); |
| 7329 | lysc_update_path(ctx, NULL, NULL); |
| 7330 | LY_CHECK_RET(ret); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 7331 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7332 | lys_array_add_mod_ref(ctx, ctx->mod, &mod->deviated_by); |
| 7333 | has_dev = 1; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 7334 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7335 | } |
| 7336 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7337 | if (!has_dev) { |
| 7338 | /* no need to recompile any modules */ |
| 7339 | return LY_SUCCESS; |
| 7340 | } |
| 7341 | |
| 7342 | /* free all the modules in descending order */ |
| 7343 | idx = ctx->ctx->list.count; |
| 7344 | do { |
| 7345 | --idx; |
| 7346 | mod = ctx->ctx->list.objs[idx]; |
| 7347 | /* skip this module */ |
| 7348 | if (mod == mod_p->mod) { |
| 7349 | continue; |
| 7350 | } |
| 7351 | |
| 7352 | if (mod->implemented && mod->compiled) { |
| 7353 | /* keep information about features state in the module */ |
| 7354 | lys_feature_precompile_revert(ctx, mod); |
| 7355 | |
| 7356 | /* free the module */ |
| 7357 | lysc_module_free(mod->compiled, NULL); |
| 7358 | mod->compiled = NULL; |
| 7359 | } |
| 7360 | } while (idx); |
| 7361 | |
| 7362 | /* recompile all the modules in ascending order */ |
| 7363 | for (idx = 0; idx < ctx->ctx->list.count; ++idx) { |
| 7364 | mod = ctx->ctx->list.objs[idx]; |
| 7365 | |
| 7366 | /* skip this module */ |
| 7367 | if (mod == mod_p->mod) { |
| 7368 | continue; |
| 7369 | } |
| 7370 | |
| 7371 | if (mod->implemented) { |
| 7372 | /* compile */ |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 7373 | LY_CHECK_GOTO(ret = lys_compile(mod, 0), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7374 | } |
| 7375 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7376 | |
| 7377 | cleanup: |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7378 | return ret; |
| 7379 | } |
| 7380 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7381 | static void * |
| 7382 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 7383 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7384 | for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7385 | if (substmts[u].stmt == stmt) { |
| 7386 | return substmts[u].storage; |
| 7387 | } |
| 7388 | } |
| 7389 | return NULL; |
| 7390 | } |
| 7391 | |
| 7392 | LY_ERR |
| 7393 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 7394 | { |
| 7395 | LY_ERR ret = LY_EVALID, r; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7396 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7397 | struct lysp_stmt *stmt; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7398 | struct lysp_qname qname; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7399 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7400 | |
| 7401 | /* check for invalid substatements */ |
| 7402 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 7403 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 7404 | continue; |
| 7405 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7406 | for (u = 0; substmts[u].stmt; ++u) { |
| 7407 | if (substmts[u].stmt == stmt->kw) { |
| 7408 | break; |
| 7409 | } |
| 7410 | } |
| 7411 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7412 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7413 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7414 | goto cleanup; |
| 7415 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7416 | } |
| 7417 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7418 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 7419 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7420 | /* keep order of the processing the same as the order in the defined substmts, |
| 7421 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 7422 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7423 | ly_bool stmt_present = 0; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7424 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7425 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 7426 | if (substmts[u].stmt != stmt->kw) { |
| 7427 | continue; |
| 7428 | } |
| 7429 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7430 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7431 | if (substmts[u].storage) { |
| 7432 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7433 | case LY_STMT_STATUS: |
| 7434 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 7435 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 7436 | break; |
| 7437 | case LY_STMT_UNITS: { |
| 7438 | const char **units; |
| 7439 | |
| 7440 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 7441 | /* single item */ |
| 7442 | if (*((const char **)substmts[u].storage)) { |
| 7443 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 7444 | goto cleanup; |
| 7445 | } |
| 7446 | units = (const char **)substmts[u].storage; |
| 7447 | } else { |
| 7448 | /* sized array */ |
| 7449 | const char ***units_array = (const char ***)substmts[u].storage; |
| 7450 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 7451 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7452 | r = lydict_insert(ctx->ctx, stmt->arg, 0, units); |
| 7453 | LY_CHECK_ERR_GOTO(r, ret = r, cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7454 | break; |
| 7455 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7456 | case LY_STMT_TYPE: { |
| 7457 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 7458 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 7459 | |
| 7460 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 7461 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7462 | if (*(struct lysc_type **)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7463 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 7464 | goto cleanup; |
| 7465 | } |
| 7466 | compiled = substmts[u].storage; |
| 7467 | } else { |
| 7468 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7469 | struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7470 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7471 | compiled = (void *)type; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7472 | } |
| 7473 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7474 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7475 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7476 | flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type **)compiled, |
| 7477 | units && !*units ? units : 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] | 7478 | lysp_type_free(ctx->ctx, parsed); |
| 7479 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7480 | break; |
| 7481 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7482 | case LY_STMT_IF_FEATURE: { |
| 7483 | struct lysc_iffeature *iff = NULL; |
| 7484 | |
| 7485 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 7486 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7487 | if (((struct lysc_iffeature *)substmts[u].storage)->features) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7488 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 7489 | goto cleanup; |
| 7490 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7491 | iff = (struct lysc_iffeature *)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7492 | } else { |
| 7493 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7494 | struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7495 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 7496 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7497 | qname.str = stmt->arg; |
| 7498 | qname.mod = ctx->mod_def; |
| 7499 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &qname, iff), ret = r, cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7500 | break; |
| 7501 | } |
| 7502 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 7503 | * 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] | 7504 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7505 | 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.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7506 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7507 | goto cleanup; |
| 7508 | } |
| 7509 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7510 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7511 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7512 | if (((substmts[u].cardinality == LY_STMT_CARD_MAND) || (substmts[u].cardinality == LY_STMT_CARD_SOME)) && !stmt_present) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7513 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7514 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7515 | goto cleanup; |
| 7516 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7517 | } |
| 7518 | |
| 7519 | ret = LY_SUCCESS; |
| 7520 | |
| 7521 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7522 | return ret; |
| 7523 | } |
| 7524 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7525 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7526 | * @brief Check when for cyclic dependencies. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7527 | * |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7528 | * @param[in] set Set with all the referenced nodes. |
| 7529 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 7530 | * @return LY_ERR value |
| 7531 | */ |
| 7532 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7533 | 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] | 7534 | { |
| 7535 | struct lyxp_set tmp_set; |
| 7536 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7537 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7538 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7539 | struct lysc_when *when; |
| 7540 | LY_ERR ret = LY_SUCCESS; |
| 7541 | |
| 7542 | memset(&tmp_set, 0, sizeof tmp_set); |
| 7543 | |
| 7544 | /* prepare in_ctx of the set */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 7545 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7546 | xp_scnode = &set->val.scnodes[i]; |
| 7547 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7548 | if (xp_scnode->in_ctx != -1) { |
| 7549 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7550 | xp_scnode->in_ctx = 1; |
| 7551 | } |
| 7552 | } |
| 7553 | |
| 7554 | for (i = 0; i < set->used; ++i) { |
| 7555 | xp_scnode = &set->val.scnodes[i]; |
| 7556 | if (xp_scnode->in_ctx != 1) { |
| 7557 | /* already checked */ |
| 7558 | continue; |
| 7559 | } |
| 7560 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7561 | if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) || |
| 7562 | !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7563 | /* no when to check */ |
| 7564 | xp_scnode->in_ctx = 0; |
| 7565 | continue; |
| 7566 | } |
| 7567 | |
| 7568 | node = xp_scnode->scnode; |
| 7569 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7570 | LY_ARRAY_FOR(node->when, u) { |
| 7571 | when = node->when[u]; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 7572 | ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7573 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7574 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7575 | 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] | 7576 | goto cleanup; |
| 7577 | } |
| 7578 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7579 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7580 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7581 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7582 | /* try to find this node in our set */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 7583 | uint32_t idx; |
| 7584 | if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7585 | 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] | 7586 | ret = LY_EVALID; |
| 7587 | goto cleanup; |
| 7588 | } |
| 7589 | |
| 7590 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7591 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7592 | } else { |
| 7593 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7594 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7595 | } |
| 7596 | } |
| 7597 | |
| 7598 | /* merge this set into the global when set */ |
| 7599 | lyxp_set_scnode_merge(set, &tmp_set); |
| 7600 | } |
| 7601 | |
| 7602 | /* check when of non-data parents as well */ |
| 7603 | node = node->parent; |
| 7604 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 7605 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7606 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 7607 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7608 | } |
| 7609 | |
| 7610 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7611 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7612 | return ret; |
| 7613 | } |
| 7614 | |
| 7615 | /** |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7616 | * @brief Check when/must expressions of a node on a complete compiled schema tree. |
| 7617 | * |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7618 | * @param[in] ctx Compile context. |
| 7619 | * @param[in] node Node to check. |
| 7620 | * @return LY_ERR value |
| 7621 | */ |
| 7622 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7623 | 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] | 7624 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7625 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7626 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7627 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7628 | uint32_t opts; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7629 | ly_bool input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7630 | struct lysc_when **when = NULL; |
| 7631 | struct lysc_must *musts = NULL; |
| 7632 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7633 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7634 | |
| 7635 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7636 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7637 | if (node->flags & LYS_CONFIG_R) { |
Michal Vasko | 2b7e558 | 2020-10-07 12:31:23 +0200 | [diff] [blame] | 7638 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent) {} |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7639 | if (op) { |
| 7640 | /* we are actually in output */ |
| 7641 | opts = LYXP_SCNODE_OUTPUT; |
| 7642 | } |
| 7643 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7644 | |
| 7645 | switch (node->nodetype) { |
| 7646 | case LYS_CONTAINER: |
| 7647 | when = ((struct lysc_node_container *)node)->when; |
| 7648 | musts = ((struct lysc_node_container *)node)->musts; |
| 7649 | break; |
| 7650 | case LYS_CHOICE: |
| 7651 | when = ((struct lysc_node_choice *)node)->when; |
| 7652 | break; |
| 7653 | case LYS_LEAF: |
| 7654 | when = ((struct lysc_node_leaf *)node)->when; |
| 7655 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 7656 | break; |
| 7657 | case LYS_LEAFLIST: |
| 7658 | when = ((struct lysc_node_leaflist *)node)->when; |
| 7659 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 7660 | break; |
| 7661 | case LYS_LIST: |
| 7662 | when = ((struct lysc_node_list *)node)->when; |
| 7663 | musts = ((struct lysc_node_list *)node)->musts; |
| 7664 | break; |
| 7665 | case LYS_ANYXML: |
| 7666 | case LYS_ANYDATA: |
| 7667 | when = ((struct lysc_node_anydata *)node)->when; |
| 7668 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 7669 | break; |
| 7670 | case LYS_CASE: |
| 7671 | when = ((struct lysc_node_case *)node)->when; |
| 7672 | break; |
| 7673 | case LYS_NOTIF: |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 7674 | when = ((struct lysc_notif *)node)->when; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7675 | musts = ((struct lysc_notif *)node)->musts; |
| 7676 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7677 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7678 | case LYS_ACTION: |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 7679 | /* first process when and input musts */ |
| 7680 | when = ((struct lysc_action *)node)->when; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7681 | musts = ((struct lysc_action *)node)->input.musts; |
| 7682 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7683 | default: |
| 7684 | /* nothing to check */ |
| 7685 | break; |
| 7686 | } |
| 7687 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7688 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7689 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 7690 | ret = lyxp_atomize(when[u]->cond, LY_PREF_SCHEMA, when[u]->module, when[u]->context ? when[u]->context : node, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7691 | 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] | 7692 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7693 | 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] | 7694 | goto cleanup; |
| 7695 | } |
| 7696 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7697 | ctx->path[0] = '\0'; |
| 7698 | 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] | 7699 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7700 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7701 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 7702 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7703 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7704 | /* 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] | 7705 | ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7706 | schema->name); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7707 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7708 | |
| 7709 | /* check dummy node accessing */ |
| 7710 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7711 | 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] | 7712 | ret = LY_EVALID; |
| 7713 | goto cleanup; |
| 7714 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7715 | } |
| 7716 | } |
| 7717 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7718 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7719 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7720 | LY_CHECK_GOTO(ret, cleanup); |
| 7721 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7722 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7723 | } |
| 7724 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7725 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7726 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7727 | LY_ARRAY_FOR(musts, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 7728 | 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] | 7729 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7730 | 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] | 7731 | goto cleanup; |
| 7732 | } |
| 7733 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7734 | ctx->path[0] = '\0'; |
| 7735 | 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] | 7736 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7737 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7738 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7739 | /* 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] | 7740 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7741 | 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] | 7742 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7743 | } |
| 7744 | } |
| 7745 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7746 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7747 | } |
| 7748 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7749 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7750 | /* now check output musts */ |
| 7751 | input_done = 1; |
Michal Vasko | ceab6dd | 2020-10-09 16:53:36 +0200 | [diff] [blame] | 7752 | when = NULL; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7753 | musts = ((struct lysc_action *)node)->output.musts; |
| 7754 | opts = LYXP_SCNODE_OUTPUT; |
| 7755 | goto check_musts; |
| 7756 | } |
| 7757 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7758 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7759 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7760 | return ret; |
| 7761 | } |
| 7762 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7763 | /** |
| 7764 | * @brief Check leafref for its target existence on a complete compiled schema tree. |
| 7765 | * |
| 7766 | * @param[in] ctx Compile context. |
| 7767 | * @param[in] node Context node for the leafref. |
| 7768 | * @param[in] lref Leafref to resolve. |
| 7769 | * @return LY_ERR value. |
| 7770 | */ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7771 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7772 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 7773 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7774 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7775 | struct ly_path *p; |
| 7776 | struct lysc_type *type; |
| 7777 | |
| 7778 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7779 | |
| 7780 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 7781 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7782 | lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, |
| 7783 | LY_PREF_SCHEMA, (void *)lref->path_mod, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7784 | |
| 7785 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7786 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7787 | ly_path_free(node->module->ctx, p); |
| 7788 | |
| 7789 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7790 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7791 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 7792 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7793 | return LY_EVALID; |
| 7794 | } |
| 7795 | |
| 7796 | /* check status */ |
| 7797 | ctx->path[0] = '\0'; |
| 7798 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7799 | ctx->path_len = strlen(ctx->path); |
| 7800 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 7801 | return LY_EVALID; |
| 7802 | } |
| 7803 | ctx->path_len = 1; |
| 7804 | ctx->path[1] = '\0'; |
| 7805 | |
| 7806 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7807 | if (lref->require_instance) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 7808 | for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {} |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7809 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7810 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7811 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7812 | return LY_EVALID; |
| 7813 | } |
| 7814 | } |
| 7815 | |
| 7816 | /* store the target's type and check for circular chain of leafrefs */ |
| 7817 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 7818 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 7819 | if (type == (struct lysc_type *)lref) { |
| 7820 | /* circular chain detected */ |
| 7821 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7822 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7823 | return LY_EVALID; |
| 7824 | } |
| 7825 | } |
| 7826 | |
| 7827 | /* check if leafref and its target are under common if-features */ |
| 7828 | if (lys_compile_leafref_features_validate(node, target)) { |
| 7829 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7830 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7831 | " features applicable to the leafref itself.", lref->path->expr); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7832 | return LY_EVALID; |
| 7833 | } |
| 7834 | |
| 7835 | return LY_SUCCESS; |
| 7836 | } |
| 7837 | |
| 7838 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7839 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7840 | { |
| 7841 | struct lysc_ext_instance *ext; |
| 7842 | struct lysp_ext_instance *ext_p = NULL; |
| 7843 | struct lysp_stmt *stmt; |
| 7844 | const struct lys_module *ext_mod; |
| 7845 | LY_ERR ret = LY_SUCCESS; |
| 7846 | |
| 7847 | /* create the parsed extension instance manually */ |
| 7848 | ext_p = calloc(1, sizeof *ext_p); |
| 7849 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7850 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup); |
| 7851 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7852 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7853 | ext_p->insubstmt_index = 0; |
| 7854 | |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 7855 | ext_p->child = stmt = calloc(1, sizeof *ext_p->child); |
| 7856 | LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7857 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup); |
| 7858 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7859 | stmt->kw = LY_STMT_TYPE; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7860 | |
| 7861 | /* allocate new extension instance */ |
| 7862 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7863 | |
| 7864 | /* manually get extension definition module */ |
| 7865 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7866 | |
| 7867 | /* compile the extension instance */ |
| 7868 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7869 | |
| 7870 | cleanup: |
| 7871 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7872 | free(ext_p); |
| 7873 | return ret; |
| 7874 | } |
| 7875 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7876 | /** |
| 7877 | * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree. |
| 7878 | * |
| 7879 | * @param[in] ctx Compile context. |
| 7880 | * @param[in] node Leaf or leaf-list to compile the default value(s) for. |
| 7881 | * @param[in] type Type of the default value. |
| 7882 | * @param[in] dflt Default value. |
| 7883 | * @param[in] dflt_mod Local module for @p dflt. |
| 7884 | * @param[in,out] storage Storage for the compiled default value. |
| 7885 | * @return LY_ERR value. |
| 7886 | */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7887 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7888 | lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 7889 | const struct lys_module *dflt_mod, struct lyd_value *storage) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7890 | { |
| 7891 | LY_ERR ret; |
| 7892 | struct ly_err_item *err = NULL; |
| 7893 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7894 | ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), 0, LY_PREF_SCHEMA, (void *)dflt_mod, LYD_HINT_SCHEMA, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7895 | node, storage, &err); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7896 | if (ret == LY_EINCOMPLETE) { |
| 7897 | /* we have no data so we will not be resolving it */ |
| 7898 | ret = LY_SUCCESS; |
| 7899 | } |
| 7900 | |
| 7901 | if (ret) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7902 | ctx->path[0] = '\0'; |
| 7903 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7904 | if (err) { |
| 7905 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7906 | "Invalid default - value does not fit the type (%s).", err->msg); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7907 | ly_err_free(err); |
| 7908 | } else { |
| 7909 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7910 | "Invalid default - value does not fit the type."); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7911 | } |
| 7912 | return ret; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7913 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7914 | |
| 7915 | ++((struct lysc_type *)storage->realtype)->refcount; |
| 7916 | return LY_SUCCESS; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7917 | } |
| 7918 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7919 | /** |
| 7920 | * @brief Compile default value of a leaf expecting a complete compiled schema tree. |
| 7921 | * |
| 7922 | * @param[in] ctx Compile context. |
| 7923 | * @param[in] leaf Leaf that the default value is for. |
| 7924 | * @param[in] dflt Default value to compile. |
| 7925 | * @return LY_ERR value. |
| 7926 | */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7927 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7928 | lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7929 | { |
| 7930 | LY_ERR ret; |
| 7931 | |
| 7932 | assert(!leaf->dflt); |
| 7933 | |
| 7934 | if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) { |
| 7935 | /* ignore default values for keys and mandatory leaves */ |
| 7936 | return LY_SUCCESS; |
| 7937 | } |
| 7938 | |
| 7939 | /* allocate the default value */ |
| 7940 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 7941 | LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM); |
| 7942 | |
| 7943 | /* store the default value */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7944 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt->str, dflt->mod, leaf->dflt); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7945 | if (ret) { |
| 7946 | free(leaf->dflt); |
| 7947 | leaf->dflt = NULL; |
| 7948 | } |
| 7949 | |
| 7950 | return ret; |
| 7951 | } |
| 7952 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7953 | /** |
| 7954 | * @brief Compile default values of a leaf-list expecting a complete compiled schema tree. |
| 7955 | * |
| 7956 | * @param[in] ctx Compile context. |
| 7957 | * @param[in] llist Leaf-list that the default value(s) are for. |
| 7958 | * @param[in] dflt Default value to compile, in case of a single value. |
| 7959 | * @param[in] dflts Sized array of default values, in case of more values. |
| 7960 | * @return LY_ERR value. |
| 7961 | */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7962 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7963 | lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt, |
| 7964 | struct lysp_qname *dflts) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7965 | { |
| 7966 | LY_ERR ret; |
| 7967 | LY_ARRAY_COUNT_TYPE orig_count, u, v; |
| 7968 | |
| 7969 | assert(dflt || dflts); |
| 7970 | |
| 7971 | if (llist->dflts) { |
| 7972 | /* there were already some defaults and we are adding new by deviations */ |
| 7973 | assert(dflts); |
| 7974 | orig_count = LY_ARRAY_COUNT(llist->dflts); |
| 7975 | } else { |
| 7976 | orig_count = 0; |
| 7977 | } |
| 7978 | |
| 7979 | /* allocate new items */ |
| 7980 | if (dflts) { |
| 7981 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM); |
| 7982 | } else { |
| 7983 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM); |
| 7984 | } |
| 7985 | |
| 7986 | /* fill each new default value */ |
| 7987 | if (dflts) { |
| 7988 | LY_ARRAY_FOR(dflts, u) { |
| 7989 | llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7990 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u].str, dflts[u].mod, |
| 7991 | llist->dflts[orig_count + u]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7992 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret); |
| 7993 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7994 | } |
| 7995 | } else { |
| 7996 | llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7997 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt->str, dflt->mod, |
| 7998 | llist->dflts[orig_count]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7999 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret); |
| 8000 | LY_ARRAY_INCREMENT(llist->dflts); |
| 8001 | } |
| 8002 | |
| 8003 | /* check default value uniqueness */ |
| 8004 | if (llist->flags & LYS_CONFIG_W) { |
| 8005 | /* configuration data values must be unique - so check the default values */ |
| 8006 | for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) { |
| 8007 | for (v = 0; v < u; ++v) { |
| 8008 | if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 8009 | lysc_update_path(ctx, llist->parent, llist->name); |
| 8010 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8011 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", |
| 8012 | llist->dflts[u]->canonical); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 8013 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 8014 | return LY_EVALID; |
| 8015 | } |
| 8016 | } |
| 8017 | } |
| 8018 | } |
| 8019 | |
| 8020 | return LY_SUCCESS; |
| 8021 | } |
| 8022 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8023 | /** |
| 8024 | * @brief Finish compilation of all the unres sets of a compile context. |
| 8025 | * |
| 8026 | * @param[in] ctx Compile context with unres sets. |
| 8027 | * @return LY_ERR value. |
| 8028 | */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 8029 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8030 | lys_compile_unres(struct lysc_ctx *ctx) |
| 8031 | { |
| 8032 | struct lysc_node *node; |
| 8033 | struct lysc_type *type, *typeiter; |
| 8034 | struct lysc_type_leafref *lref; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8035 | struct lysc_augment *aug; |
| 8036 | struct lysc_deviation *dev; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 8037 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8038 | uint32_t i; |
| 8039 | |
| 8040 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 8041 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 8042 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 8043 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 8044 | node = ctx->leafrefs.objs[i]; |
| 8045 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 8046 | type = ((struct lysc_node_leaf *)node)->type; |
| 8047 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 8048 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 8049 | } else if (type->basetype == LY_TYPE_UNION) { |
| 8050 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 8051 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 8052 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 8053 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 8054 | } |
| 8055 | } |
| 8056 | } |
| 8057 | } |
| 8058 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 8059 | /* store pointer to the real type */ |
| 8060 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 8061 | if (type->basetype == LY_TYPE_LEAFREF) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 8062 | for (typeiter = ((struct lysc_type_leafref *)type)->realtype; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8063 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 8064 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 8065 | ((struct lysc_type_leafref *)type)->realtype = typeiter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8066 | } else if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 8067 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 8068 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 8069 | for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8070 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 8071 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 8072 | ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8073 | } |
| 8074 | } |
| 8075 | } |
| 8076 | } |
| 8077 | |
| 8078 | /* check xpath */ |
| 8079 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 8080 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 8081 | } |
| 8082 | |
| 8083 | /* finish incomplete default values compilation */ |
| 8084 | for (i = 0; i < ctx->dflts.count; ++i) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8085 | struct lysc_unres_dflt *r = ctx->dflts.objs[i]; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 8086 | if (r->leaf->nodetype == LYS_LEAF) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8087 | LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 8088 | } else { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8089 | LY_CHECK_RET(lys_compile_unres_llist_dflts(ctx, r->llist, r->dflt, r->dflts)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8090 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8091 | } |
| 8092 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8093 | /* check that all augments were applied */ |
| 8094 | for (i = 0; i < ctx->augs.count; ++i) { |
| 8095 | aug = ctx->augs.objs[i]; |
| 8096 | LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 8097 | "Augment target node \"%s\" from module \"%s\" was not found.", aug->nodeid->expr, |
| 8098 | aug->nodeid_mod->name); |
| 8099 | } |
| 8100 | if (ctx->augs.count) { |
| 8101 | return LY_ENOTFOUND; |
| 8102 | } |
| 8103 | |
| 8104 | /* check that all deviations were applied */ |
| 8105 | for (i = 0; i < ctx->devs.count; ++i) { |
| 8106 | dev = ctx->devs.objs[i]; |
| 8107 | LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 8108 | "Deviation(s) target node \"%s\" from module \"%s\" was not found.", dev->nodeid->expr, |
| 8109 | dev->nodeid_mod->name); |
| 8110 | } |
| 8111 | if (ctx->devs.count) { |
| 8112 | return LY_ENOTFOUND; |
| 8113 | } |
| 8114 | |
| 8115 | return LY_SUCCESS; |
| 8116 | } |
| 8117 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 8118 | void |
| 8119 | lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod) |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8120 | { |
| 8121 | uint32_t i; |
| 8122 | LY_ARRAY_COUNT_TYPE u, count; |
| 8123 | struct lys_module *m; |
| 8124 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 8125 | for (i = 0; i < ctx->list.count; ++i) { |
| 8126 | m = ctx->list.objs[i]; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8127 | |
| 8128 | if (m->augmented_by) { |
| 8129 | count = LY_ARRAY_COUNT(m->augmented_by); |
| 8130 | for (u = 0; u < count; ++u) { |
| 8131 | if (m->augmented_by[u] == mod) { |
| 8132 | /* keep the order */ |
| 8133 | if (u < count - 1) { |
| 8134 | memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by); |
| 8135 | } |
| 8136 | LY_ARRAY_DECREMENT(m->augmented_by); |
| 8137 | break; |
| 8138 | } |
| 8139 | } |
| 8140 | if (!LY_ARRAY_COUNT(m->augmented_by)) { |
| 8141 | LY_ARRAY_FREE(m->augmented_by); |
| 8142 | m->augmented_by = NULL; |
| 8143 | } |
| 8144 | } |
| 8145 | |
| 8146 | if (m->deviated_by) { |
| 8147 | count = LY_ARRAY_COUNT(m->deviated_by); |
| 8148 | for (u = 0; u < count; ++u) { |
| 8149 | if (m->deviated_by[u] == mod) { |
| 8150 | /* keep the order */ |
| 8151 | if (u < count - 1) { |
| 8152 | memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by); |
| 8153 | } |
| 8154 | LY_ARRAY_DECREMENT(m->deviated_by); |
| 8155 | break; |
| 8156 | } |
| 8157 | } |
| 8158 | if (!LY_ARRAY_COUNT(m->deviated_by)) { |
| 8159 | LY_ARRAY_FREE(m->deviated_by); |
| 8160 | m->deviated_by = NULL; |
| 8161 | } |
| 8162 | } |
| 8163 | } |
| 8164 | } |
| 8165 | |
| 8166 | /** |
| 8167 | * @brief Compile features in the current module and all its submodules. |
| 8168 | * |
| 8169 | * @param[in] ctx Compile context. |
| 8170 | * @return LY_ERR value. |
| 8171 | */ |
| 8172 | static LY_ERR |
| 8173 | lys_compile_features(struct lysc_ctx *ctx) |
| 8174 | { |
| 8175 | struct lysp_submodule *submod; |
| 8176 | LY_ARRAY_COUNT_TYPE u, v; |
| 8177 | |
| 8178 | if (!ctx->mod->features) { |
| 8179 | /* features are compiled directly into the module structure, |
| 8180 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
| 8181 | LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, ctx->mod->parsed->features, &ctx->mod->features)); |
| 8182 | LY_ARRAY_FOR(ctx->mod->parsed->includes, v) { |
| 8183 | submod = ctx->mod->parsed->includes[v].submodule; |
| 8184 | LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, submod->features, &ctx->mod->features)); |
| 8185 | } |
| 8186 | } |
| 8187 | |
| 8188 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 8189 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 8190 | * are present. */ |
| 8191 | LY_ARRAY_FOR(ctx->mod->parsed->features, u) { |
| 8192 | LY_CHECK_RET(lys_feature_precompile_finish(ctx, &ctx->mod->parsed->features[u], ctx->mod->features)); |
| 8193 | } |
| 8194 | |
| 8195 | lysc_update_path(ctx, NULL, "{submodule}"); |
| 8196 | LY_ARRAY_FOR(ctx->mod->parsed->includes, v) { |
| 8197 | submod = ctx->mod->parsed->includes[v].submodule; |
| 8198 | |
| 8199 | lysc_update_path(ctx, NULL, submod->name); |
| 8200 | LY_ARRAY_FOR(submod->features, u) { |
| 8201 | LY_CHECK_RET(lys_feature_precompile_finish(ctx, &submod->features[u], ctx->mod->features)); |
| 8202 | } |
| 8203 | lysc_update_path(ctx, NULL, NULL); |
| 8204 | } |
| 8205 | lysc_update_path(ctx, NULL, NULL); |
| 8206 | |
| 8207 | return LY_SUCCESS; |
| 8208 | } |
| 8209 | |
| 8210 | /** |
| 8211 | * @brief Compile identites in the current module and all its submodules. |
| 8212 | * |
| 8213 | * @param[in] ctx Compile context. |
| 8214 | * @return LY_ERR value. |
| 8215 | */ |
| 8216 | static LY_ERR |
| 8217 | lys_compile_identities(struct lysc_ctx *ctx) |
| 8218 | { |
| 8219 | struct lysp_submodule *submod; |
| 8220 | LY_ARRAY_COUNT_TYPE u; |
| 8221 | |
| 8222 | if (!ctx->mod->identities) { |
| 8223 | LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, ctx->mod->parsed->identities, &ctx->mod->identities)); |
| 8224 | LY_ARRAY_FOR(ctx->mod->parsed->includes, u) { |
| 8225 | submod = ctx->mod->parsed->includes[u].submodule; |
| 8226 | LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, submod->identities, &ctx->mod->identities)); |
| 8227 | } |
| 8228 | } |
| 8229 | |
| 8230 | if (ctx->mod->parsed->identities) { |
| 8231 | LY_CHECK_RET(lys_compile_identities_derived(ctx, ctx->mod->parsed->identities, ctx->mod->identities)); |
| 8232 | } |
| 8233 | lysc_update_path(ctx, NULL, "{submodule}"); |
| 8234 | LY_ARRAY_FOR(ctx->mod->parsed->includes, u) { |
| 8235 | |
| 8236 | submod = ctx->mod->parsed->includes[u].submodule; |
| 8237 | if (submod->identities) { |
| 8238 | lysc_update_path(ctx, NULL, submod->name); |
| 8239 | LY_CHECK_RET(lys_compile_identities_derived(ctx, submod->identities, ctx->mod->identities)); |
| 8240 | lysc_update_path(ctx, NULL, NULL); |
| 8241 | } |
| 8242 | } |
| 8243 | lysc_update_path(ctx, NULL, NULL); |
| 8244 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8245 | return LY_SUCCESS; |
| 8246 | } |
| 8247 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8248 | LY_ERR |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8249 | lys_compile(struct lys_module *mod, uint32_t options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8250 | { |
| 8251 | struct lysc_ctx ctx = {0}; |
| 8252 | struct lysc_module *mod_c; |
| 8253 | struct lysp_module *sp; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8254 | struct lysp_submodule *submod; |
| 8255 | struct lysp_node *pnode; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 8256 | struct lysp_grp *grps; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 8257 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 8258 | uint32_t i; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 8259 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8260 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8261 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 8262 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8263 | if (!mod->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 8264 | /* just imported modules are not compiled */ |
| 8265 | return LY_SUCCESS; |
| 8266 | } |
| 8267 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8268 | /* context will be changed */ |
| 8269 | ++mod->ctx->module_set_id; |
| 8270 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8271 | sp = mod->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8272 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8273 | ctx.ctx = mod->ctx; |
| 8274 | ctx.mod = mod; |
| 8275 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 8276 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 8277 | ctx.path_len = 1; |
| 8278 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8279 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8280 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 8281 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 8282 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8283 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8284 | /* process imports */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 8285 | LY_ARRAY_FOR(sp->imports, u) { |
| 8286 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 8287 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 8288 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8289 | /* features */ |
| 8290 | LY_CHECK_GOTO(ret = lys_compile_features(&ctx), error); |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 8291 | |
| 8292 | /* identities, work similarly to features with the precompilation */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8293 | LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), error); |
| 8294 | |
| 8295 | /* augments and deviations */ |
| 8296 | LY_CHECK_GOTO(ret = lys_precompile_augments_deviations(&ctx), error); |
| 8297 | |
| 8298 | /* compile augments and deviations of our module from other modules so they can be applied during compilation */ |
| 8299 | LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), error); |
| 8300 | LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8301 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 8302 | /* data nodes */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8303 | LY_LIST_FOR(sp->data, pnode) { |
| 8304 | LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8305 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 8306 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8307 | /* top-level RPCs and notifications */ |
| 8308 | COMPILE_OP_ARRAY_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 8309 | COMPILE_OP_ARRAY_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] | 8310 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8311 | /* extension instances */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 8312 | 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] | 8313 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8314 | /* the same for submodules */ |
| 8315 | LY_ARRAY_FOR(sp->includes, u) { |
| 8316 | submod = sp->includes[u].submodule; |
| 8317 | LY_LIST_FOR(submod->data, pnode) { |
| 8318 | ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL); |
| 8319 | LY_CHECK_GOTO(ret, error); |
| 8320 | } |
| 8321 | |
| 8322 | COMPILE_OP_ARRAY_GOTO(&ctx, submod->rpcs, mod_c->rpcs, NULL, v, lys_compile_action, 0, ret, error); |
| 8323 | COMPILE_OP_ARRAY_GOTO(&ctx, submod->notifs, mod_c->notifs, NULL, v, lys_compile_notif, 0, ret, error); |
| 8324 | |
| 8325 | COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error); |
| 8326 | } |
| 8327 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8328 | /* finish compilation for all unresolved items in the context */ |
| 8329 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 8330 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 8331 | /* validate non-instantiated groupings from the parsed schema, |
| 8332 | * without it we would accept even the schemas with invalid grouping specification */ |
| 8333 | ctx.options |= LYSC_OPT_GROUPING; |
| 8334 | LY_ARRAY_FOR(sp->groupings, u) { |
| 8335 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8336 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &sp->groupings[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 8337 | } |
| 8338 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8339 | LY_LIST_FOR(sp->data, pnode) { |
| 8340 | grps = (struct lysp_grp *)lysp_node_groupings(pnode); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 8341 | LY_ARRAY_FOR(grps, u) { |
| 8342 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8343 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 8344 | } |
| 8345 | } |
| 8346 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8347 | LY_ARRAY_FOR(sp->includes, u) { |
| 8348 | submod = sp->includes[u].submodule; |
| 8349 | LY_ARRAY_FOR(submod->groupings, u) { |
| 8350 | if (!(submod->groupings[u].flags & LYS_USED_GRP)) { |
| 8351 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &submod->groupings[u]), error); |
| 8352 | } |
| 8353 | } |
| 8354 | LY_LIST_FOR(submod->data, pnode) { |
| 8355 | grps = (struct lysp_grp *)lysp_node_groupings(pnode); |
| 8356 | LY_ARRAY_FOR(grps, u) { |
| 8357 | if (!(grps[u].flags & LYS_USED_GRP)) { |
| 8358 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error); |
| 8359 | } |
| 8360 | } |
| 8361 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 8362 | } |
| 8363 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 8364 | #if 0 |
| 8365 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 8366 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 8367 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 8368 | * the anotation definitions available in the internal schema structure. */ |
| 8369 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 8370 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 8371 | lys_free(mod, NULL, 1, 1); |
| 8372 | return NULL; |
| 8373 | } |
| 8374 | } |
| 8375 | #endif |
| 8376 | |
| 8377 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8378 | if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
| 8379 | 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] | 8380 | } |
| 8381 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8382 | /* there can be no leftover deviations */ |
| 8383 | LY_CHECK_ERR_GOTO(ctx.devs.count, LOGINT(ctx.ctx); ret = LY_EINT, error); |
| 8384 | |
| 8385 | for (i = 0; i < ctx.dflts.count; ++i) { |
| 8386 | lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]); |
| 8387 | } |
| 8388 | ly_set_erase(&ctx.dflts, NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8389 | ly_set_erase(&ctx.xpath, NULL); |
| 8390 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 8391 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 8392 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8393 | ly_set_erase(&ctx.augs, NULL); |
| 8394 | ly_set_erase(&ctx.devs, NULL); |
| 8395 | ly_set_erase(&ctx.uses_augs, NULL); |
| 8396 | ly_set_erase(&ctx.uses_rfns, NULL); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 8397 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 8398 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8399 | lysp_module_free(mod->parsed); |
| 8400 | mod->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8401 | } |
| 8402 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8403 | return LY_SUCCESS; |
| 8404 | |
| 8405 | error: |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 8406 | lys_precompile_augments_deviations_revert(ctx.ctx, mod); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8407 | lys_feature_precompile_revert(&ctx, mod); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8408 | for (i = 0; i < ctx.dflts.count; ++i) { |
| 8409 | lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]); |
| 8410 | } |
| 8411 | ly_set_erase(&ctx.dflts, NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8412 | ly_set_erase(&ctx.xpath, NULL); |
| 8413 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 8414 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 8415 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8416 | for (i = 0; i < ctx.augs.count; ++i) { |
| 8417 | lysc_augment_free(ctx.ctx, ctx.augs.objs[i]); |
| 8418 | } |
| 8419 | ly_set_erase(&ctx.augs, NULL); |
| 8420 | for (i = 0; i < ctx.devs.count; ++i) { |
| 8421 | lysc_deviation_free(ctx.ctx, ctx.devs.objs[i]); |
| 8422 | } |
| 8423 | ly_set_erase(&ctx.devs, NULL); |
| 8424 | for (i = 0; i < ctx.uses_augs.count; ++i) { |
| 8425 | lysc_augment_free(ctx.ctx, ctx.uses_augs.objs[i]); |
| 8426 | } |
| 8427 | ly_set_erase(&ctx.uses_augs, NULL); |
| 8428 | for (i = 0; i < ctx.uses_rfns.count; ++i) { |
| 8429 | lysc_refine_free(ctx.ctx, ctx.uses_rfns.objs[i]); |
| 8430 | } |
| 8431 | ly_set_erase(&ctx.uses_rfns, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8432 | lysc_module_free(mod_c, NULL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8433 | mod->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 8434 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8435 | return ret; |
| 8436 | } |