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 |
| 927 | * @param[in] ctx Compile context. |
| 928 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 929 | * @param[in] flags Flags of the node with the "when" defiition. |
| 930 | * @param[in] node Node that inherited the "when" definition, must be connected to parents. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 931 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 932 | * @return LY_ERR value. |
| 933 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 934 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 935 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 936 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 937 | LY_ERR ret = LY_SUCCESS; |
| 938 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 939 | *when = calloc(1, sizeof **when); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 940 | LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 941 | (*when)->refcount = 1; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 942 | 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] | 943 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 944 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 945 | DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done); |
| 946 | DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 947 | 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] | 948 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 949 | |
| 950 | done: |
| 951 | return ret; |
| 952 | } |
| 953 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 954 | /** |
| 955 | * @brief Compile information from the must statement |
| 956 | * @param[in] ctx Compile context. |
| 957 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 958 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 959 | * @return LY_ERR value. |
| 960 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 961 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 962 | 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] | 963 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 964 | LY_ERR ret = LY_SUCCESS; |
| 965 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 966 | LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond)); |
| 967 | must->module = (struct lys_module *)must_p->arg.mod; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 968 | DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done); |
| 969 | DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done); |
| 970 | DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done); |
| 971 | DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 972 | 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] | 973 | |
| 974 | done: |
| 975 | return ret; |
| 976 | } |
| 977 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 978 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 979 | * @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] | 980 | * @param[in] ctx Compile context. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 981 | * @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] | 982 | * @return LY_ERR value. |
| 983 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 984 | static LY_ERR |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 985 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 986 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 987 | const struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 988 | LY_ERR ret = LY_SUCCESS; |
| 989 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 990 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 991 | * 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] | 992 | * 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] | 993 | if (!imp_p->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 994 | /* try to use filepath if present */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 995 | if (imp_p->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 996 | struct ly_in *in; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 997 | if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 998 | LOGINT(ctx->ctx); |
| 999 | } else { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1000 | 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^] | 1001 | ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1002 | if (mod != imp_p->module) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1003 | 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^] | 1004 | imp_p->module->filepath, imp_p->module->name); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1005 | mod = NULL; |
| 1006 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1007 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1008 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1009 | } |
| 1010 | if (!mod) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1011 | 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] | 1012 | 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^] | 1013 | imp_p->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1014 | return LY_ENOTFOUND; |
| 1015 | } |
| 1016 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1017 | } |
| 1018 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1019 | return ret; |
| 1020 | } |
| 1021 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1022 | LY_ERR |
| 1023 | 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] | 1024 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1025 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1026 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1027 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1028 | LY_ERR ret = LY_SUCCESS; |
| 1029 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1030 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1031 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1032 | if (!ctx_sc) { |
| 1033 | context.ctx = ctx; |
| 1034 | context.mod = module; |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 1035 | context.mod_def = module; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1036 | context.path_len = 1; |
| 1037 | context.path[0] = '/'; |
| 1038 | ctx_sc = &context; |
| 1039 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1040 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1041 | if (!identities_p) { |
| 1042 | return LY_SUCCESS; |
| 1043 | } |
| 1044 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1045 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1049 | 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] | 1050 | LY_ARRAY_FOR(identities_p, u) { |
| 1051 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 1052 | |
| 1053 | LY_ARRAY_INCREMENT(*identities); |
| 1054 | 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] | 1055 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done); |
| 1056 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done); |
| 1057 | 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] | 1058 | (*identities)[offset + u].module = ctx_sc->mod; |
| 1059 | 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^] | 1060 | lys_compile_iffeature, ret, done); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1061 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 1062 | 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^] | 1063 | LYEXT_PAR_IDENT, ret, done); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1064 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 1065 | |
| 1066 | lysc_update_path(ctx_sc, NULL, NULL); |
| 1067 | } |
| 1068 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1069 | done: |
| 1070 | return ret; |
| 1071 | } |
| 1072 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1073 | /** |
| 1074 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1075 | * |
| 1076 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1077 | * |
| 1078 | * @param[in] ctx Compile context for logging. |
| 1079 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1080 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1081 | * @return LY_SUCCESS if everything is ok. |
| 1082 | * @return LY_EVALID if the identity is derived from itself. |
| 1083 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1084 | static LY_ERR |
| 1085 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1086 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1087 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1088 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1089 | struct ly_set recursion = {0}; |
| 1090 | struct lysc_ident *drv; |
| 1091 | |
| 1092 | if (!derived) { |
| 1093 | return LY_SUCCESS; |
| 1094 | } |
| 1095 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1096 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1097 | if (ident == derived[u]) { |
| 1098 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1099 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1100 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1101 | goto cleanup; |
| 1102 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1103 | ret = ly_set_add(&recursion, derived[u], 0, NULL); |
| 1104 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | for (v = 0; v < recursion.count; ++v) { |
| 1108 | drv = recursion.objs[v]; |
| 1109 | if (!drv->derived) { |
| 1110 | continue; |
| 1111 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1112 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1113 | if (ident == drv->derived[u]) { |
| 1114 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1115 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1116 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1117 | goto cleanup; |
| 1118 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1119 | ret = ly_set_add(&recursion, drv->derived[u], 0, NULL); |
| 1120 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1121 | } |
| 1122 | } |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1123 | |
| 1124 | cleanup: |
| 1125 | ly_set_erase(&recursion, NULL); |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1129 | /** |
| 1130 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1131 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1132 | * 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] | 1133 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1134 | * to distinguish these two use cases. |
| 1135 | * |
| 1136 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1137 | * @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] | 1138 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1139 | * @param[in] bases Array of bases of identityref to fill in. |
| 1140 | * @return LY_ERR value. |
| 1141 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1142 | static LY_ERR |
Michal Vasko | 72619ce | 2020-10-06 14:05:32 +0200 | [diff] [blame] | 1143 | 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] | 1144 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1145 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1146 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1147 | const char *s, *name; |
Michal Vasko | 72619ce | 2020-10-06 14:05:32 +0200 | [diff] [blame] | 1148 | const struct lys_module *mod; |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1149 | struct lysc_ident **idref; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1150 | |
| 1151 | assert(ident || bases); |
| 1152 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1153 | if ((LY_ARRAY_COUNT(bases_p) > 1) && (ctx->mod_def->version < 2)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1154 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1155 | "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] | 1156 | return LY_EVALID; |
| 1157 | } |
| 1158 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1159 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1160 | s = strchr(bases_p[u], ':'); |
| 1161 | if (s) { |
| 1162 | /* prefixed identity */ |
| 1163 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1164 | 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] | 1165 | } else { |
| 1166 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1167 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1168 | } |
| 1169 | if (!mod) { |
| 1170 | if (ident) { |
| 1171 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1172 | "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] | 1173 | } else { |
| 1174 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1175 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1176 | } |
| 1177 | return LY_EVALID; |
| 1178 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1179 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1180 | idref = NULL; |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1181 | LY_ARRAY_FOR(mod->identities, v) { |
| 1182 | if (!strcmp(name, mod->identities[v].name)) { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1183 | if (ident) { |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1184 | if (ident == &mod->identities[v]) { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1185 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1186 | "Identity \"%s\" is derived from itself.", ident->name); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1187 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1188 | } |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1189 | 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] | 1190 | /* we have match! store the backlink */ |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1191 | 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] | 1192 | *idref = ident; |
| 1193 | } else { |
| 1194 | /* we have match! store the found identity */ |
| 1195 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1196 | *idref = &mod->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1197 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1198 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | if (!idref || !(*idref)) { |
| 1202 | if (ident) { |
| 1203 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1204 | "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] | 1205 | } else { |
| 1206 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1207 | "Unable to find base (%s) of identityref.", bases_p[u]); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1208 | } |
| 1209 | return LY_EVALID; |
| 1210 | } |
| 1211 | } |
| 1212 | return LY_SUCCESS; |
| 1213 | } |
| 1214 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1215 | /** |
| 1216 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1217 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1218 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1219 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1220 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1221 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1222 | static LY_ERR |
| 1223 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1224 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1225 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1226 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1227 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1228 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1229 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1230 | continue; |
| 1231 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1232 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1233 | 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] | 1234 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1235 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1236 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1237 | return LY_SUCCESS; |
| 1238 | } |
| 1239 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1240 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1241 | 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] | 1242 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1243 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1244 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1245 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1246 | struct lysc_ctx context = {0}; |
| 1247 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1248 | assert(ctx_sc || ctx); |
| 1249 | |
| 1250 | if (!ctx_sc) { |
| 1251 | context.ctx = ctx; |
| 1252 | context.mod = module; |
| 1253 | context.path_len = 1; |
| 1254 | context.path[0] = '/'; |
| 1255 | ctx_sc = &context; |
| 1256 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1257 | |
| 1258 | if (!features_p) { |
| 1259 | return LY_SUCCESS; |
| 1260 | } |
| 1261 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1262 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1265 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1266 | 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] | 1267 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1268 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1269 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1270 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1271 | 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] | 1272 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done); |
| 1273 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done); |
| 1274 | 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] | 1275 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1276 | (*features)[offset + u].module = ctx_sc->mod; |
| 1277 | |
| 1278 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1279 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1280 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1281 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1282 | done: |
| 1283 | return ret; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1286 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1287 | * @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] | 1288 | * |
| 1289 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1290 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1291 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1292 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1293 | * being currently processed). |
| 1294 | * @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] | 1295 | * @return LY_SUCCESS if everything is ok. |
| 1296 | * @return LY_EVALID if the feature references indirectly itself. |
| 1297 | */ |
| 1298 | static LY_ERR |
| 1299 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1300 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1301 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1302 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1303 | struct ly_set recursion = {0}; |
| 1304 | struct lysc_feature *drv; |
| 1305 | |
| 1306 | if (!depfeatures) { |
| 1307 | return LY_SUCCESS; |
| 1308 | } |
| 1309 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1310 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1311 | if (feature == depfeatures[u]) { |
| 1312 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1313 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1314 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1315 | goto cleanup; |
| 1316 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1317 | ret = ly_set_add(&recursion, depfeatures[u], 0, NULL); |
| 1318 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | for (v = 0; v < recursion.count; ++v) { |
| 1322 | drv = recursion.objs[v]; |
| 1323 | if (!drv->depfeatures) { |
| 1324 | continue; |
| 1325 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1326 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1327 | if (feature == drv->depfeatures[u]) { |
| 1328 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1329 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1330 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1331 | goto cleanup; |
| 1332 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1333 | ly_set_add(&recursion, drv->depfeatures[u], 0, NULL); |
| 1334 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1335 | } |
| 1336 | } |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1337 | |
| 1338 | cleanup: |
| 1339 | ly_set_erase(&recursion, NULL); |
| 1340 | return ret; |
| 1341 | } |
| 1342 | |
| 1343 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1344 | * @brief Create pre-compiled features array. |
| 1345 | * |
| 1346 | * See lys_feature_precompile() for more details. |
| 1347 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1348 | * @param[in] ctx Compile context. |
| 1349 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1350 | * @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] | 1351 | * @return LY_ERR value. |
| 1352 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1353 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1354 | 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] | 1355 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1356 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1357 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1358 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1359 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1360 | /* find the preprecompiled feature */ |
| 1361 | LY_ARRAY_FOR(features, x) { |
| 1362 | if (strcmp(features[x].name, feature_p->name)) { |
| 1363 | continue; |
| 1364 | } |
| 1365 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1366 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1367 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1368 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1369 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1370 | 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] | 1371 | 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] | 1372 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1373 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1374 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1375 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1376 | /* check for circular dependency - direct reference first,... */ |
| 1377 | if (feature == feature->iffeatures[u].features[v]) { |
| 1378 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1379 | "Feature \"%s\" is referenced from itself.", feature->name); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1380 | return LY_EVALID; |
| 1381 | } |
| 1382 | /* ... and indirect circular reference */ |
| 1383 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1384 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1385 | /* add itself into the dependants list */ |
| 1386 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1387 | *df = feature; |
| 1388 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1389 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1390 | } |
| 1391 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1392 | lysc_update_path(ctx, NULL, NULL); |
| 1393 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1394 | done: |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1395 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1396 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1397 | |
| 1398 | LOGINT(ctx->ctx); |
| 1399 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1400 | } |
| 1401 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1402 | /** |
| 1403 | * @brief Revert compiled list of features back to the precompiled state. |
| 1404 | * |
| 1405 | * 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] | 1406 | * |
| 1407 | * @param[in] ctx Compilation context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1408 | * @param[in] mod The module structure with the features to decompile. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1409 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1410 | static void |
| 1411 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1412 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1413 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1414 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1415 | /* 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] | 1416 | * which may points into the data being freed here */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1417 | LY_ARRAY_FOR(mod->features, u) { |
| 1418 | LY_ARRAY_FOR(mod->features[u].iffeatures, v) { |
| 1419 | lysc_iffeature_free(ctx->ctx, &mod->features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1420 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1421 | LY_ARRAY_FREE(mod->features[u].iffeatures); |
| 1422 | mod->features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1423 | |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1424 | LY_ARRAY_FOR(mod->features[u].exts, v) { |
| 1425 | lysc_ext_instance_free(ctx->ctx, &(mod->features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1426 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1427 | LY_ARRAY_FREE(mod->features[u].exts); |
| 1428 | mod->features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1432 | /** |
| 1433 | * @brief Validate and normalize numeric value from a range definition. |
| 1434 | * @param[in] ctx Compile context. |
| 1435 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1436 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1437 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1438 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1439 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1440 | * @param[in] value String value of the range boundary. |
| 1441 | * @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. |
| 1442 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1443 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1444 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1445 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1446 | 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] | 1447 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1448 | size_t fraction = 0, size; |
| 1449 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1450 | *len = 0; |
| 1451 | |
| 1452 | assert(value); |
| 1453 | /* parse value */ |
| 1454 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1455 | return LY_EVALID; |
| 1456 | } |
| 1457 | |
| 1458 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1459 | ++(*len); |
| 1460 | } |
| 1461 | |
| 1462 | while (isdigit(value[*len])) { |
| 1463 | ++(*len); |
| 1464 | } |
| 1465 | |
| 1466 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1467 | if (basetype == LY_TYPE_DEC64) { |
| 1468 | goto decimal; |
| 1469 | } else { |
| 1470 | *valcopy = strndup(value, *len); |
| 1471 | return LY_SUCCESS; |
| 1472 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1473 | } |
| 1474 | fraction = *len; |
| 1475 | |
| 1476 | ++(*len); |
| 1477 | while (isdigit(value[*len])) { |
| 1478 | ++(*len); |
| 1479 | } |
| 1480 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1481 | if (basetype == LY_TYPE_DEC64) { |
| 1482 | decimal: |
| 1483 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1484 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1485 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1486 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1487 | *len, value, frdigits); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1488 | return LY_EINVAL; |
| 1489 | } |
| 1490 | if (fraction) { |
| 1491 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1492 | } else { |
| 1493 | size = (*len) + frdigits + 1; |
| 1494 | } |
| 1495 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1496 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1497 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1498 | (*valcopy)[size - 1] = '\0'; |
| 1499 | if (fraction) { |
| 1500 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1501 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1502 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1503 | } else { |
| 1504 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1505 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1506 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1507 | } |
| 1508 | return LY_SUCCESS; |
| 1509 | } |
| 1510 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1511 | /** |
| 1512 | * @brief Check that values in range are in ascendant order. |
| 1513 | * @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] | 1514 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1515 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1516 | * @param[in] value Current value to check. |
| 1517 | * @param[in] prev_value The last seen value. |
| 1518 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1519 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1520 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1521 | 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] | 1522 | { |
| 1523 | if (unsigned_value) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1524 | 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] | 1525 | return LY_EEXIST; |
| 1526 | } |
| 1527 | } else { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1528 | if ((max && (prev_value > value)) || (!max && (prev_value >= value))) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1529 | return LY_EEXIST; |
| 1530 | } |
| 1531 | } |
| 1532 | return LY_SUCCESS; |
| 1533 | } |
| 1534 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1535 | /** |
| 1536 | * @brief Set min/max value of the range part. |
| 1537 | * @param[in] ctx Compile context. |
| 1538 | * @param[in] part Range part structure to fill. |
| 1539 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1540 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1541 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1542 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1543 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1544 | * @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] | 1545 | * @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] | 1546 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1547 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1548 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1549 | * frdigits value), LY_EMEM. |
| 1550 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1551 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1552 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype, |
| 1553 | 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] | 1554 | { |
| 1555 | LY_ERR ret = LY_SUCCESS; |
| 1556 | char *valcopy = NULL; |
| 1557 | size_t len; |
| 1558 | |
| 1559 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1560 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1561 | LY_CHECK_GOTO(ret, finalize); |
| 1562 | } |
| 1563 | if (!valcopy && base_range) { |
| 1564 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1565 | 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] | 1566 | } else { |
| 1567 | part->min_64 = base_range->parts[0].min_64; |
| 1568 | } |
| 1569 | if (!first) { |
| 1570 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1571 | } |
| 1572 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1576 | case LY_TYPE_INT8: /* range */ |
| 1577 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1578 | 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] | 1579 | } else if (max) { |
| 1580 | part->max_64 = INT64_C(127); |
| 1581 | } else { |
| 1582 | part->min_64 = INT64_C(-128); |
| 1583 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1584 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1585 | 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] | 1586 | } |
| 1587 | break; |
| 1588 | case LY_TYPE_INT16: /* range */ |
| 1589 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1590 | 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] | 1591 | } else if (max) { |
| 1592 | part->max_64 = INT64_C(32767); |
| 1593 | } else { |
| 1594 | part->min_64 = INT64_C(-32768); |
| 1595 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1596 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1597 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1598 | } |
| 1599 | break; |
| 1600 | case LY_TYPE_INT32: /* range */ |
| 1601 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1602 | 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] | 1603 | } else if (max) { |
| 1604 | part->max_64 = INT64_C(2147483647); |
| 1605 | } else { |
| 1606 | part->min_64 = INT64_C(-2147483648); |
| 1607 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1608 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1609 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1610 | } |
| 1611 | break; |
| 1612 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1613 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1614 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1615 | 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^] | 1616 | max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1617 | } else if (max) { |
| 1618 | part->max_64 = INT64_C(9223372036854775807); |
| 1619 | } else { |
| 1620 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1621 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1622 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1623 | 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] | 1624 | } |
| 1625 | break; |
| 1626 | case LY_TYPE_UINT8: /* range */ |
| 1627 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1628 | 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] | 1629 | } else if (max) { |
| 1630 | part->max_u64 = UINT64_C(255); |
| 1631 | } else { |
| 1632 | part->min_u64 = UINT64_C(0); |
| 1633 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1634 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1635 | 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] | 1636 | } |
| 1637 | break; |
| 1638 | case LY_TYPE_UINT16: /* range */ |
| 1639 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1640 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1641 | } else if (max) { |
| 1642 | part->max_u64 = UINT64_C(65535); |
| 1643 | } else { |
| 1644 | part->min_u64 = UINT64_C(0); |
| 1645 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1646 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1647 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1648 | } |
| 1649 | break; |
| 1650 | case LY_TYPE_UINT32: /* range */ |
| 1651 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1652 | 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] | 1653 | } else if (max) { |
| 1654 | part->max_u64 = UINT64_C(4294967295); |
| 1655 | } else { |
| 1656 | part->min_u64 = UINT64_C(0); |
| 1657 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1658 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1659 | 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] | 1660 | } |
| 1661 | break; |
| 1662 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1663 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1664 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1665 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1666 | 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] | 1667 | } else if (max) { |
| 1668 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1669 | } else { |
| 1670 | part->min_u64 = UINT64_C(0); |
| 1671 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1672 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1673 | 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] | 1674 | } |
| 1675 | break; |
| 1676 | default: |
| 1677 | LOGINT(ctx->ctx); |
| 1678 | ret = LY_EINT; |
| 1679 | } |
| 1680 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1681 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1682 | if (ret == LY_EDENIED) { |
| 1683 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1684 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1685 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1686 | } else if (ret == LY_EVALID) { |
| 1687 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1688 | "Invalid %s restriction - invalid value \"%s\".", |
| 1689 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1690 | } else if (ret == LY_EEXIST) { |
| 1691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1692 | "Invalid %s restriction - values are not in ascending order (%s).", |
| 1693 | length_restr ? "length" : "range", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1694 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1695 | } else if (!ret && value) { |
| 1696 | *value = *value + len; |
| 1697 | } |
| 1698 | free(valcopy); |
| 1699 | return ret; |
| 1700 | } |
| 1701 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1702 | /** |
| 1703 | * @brief Compile the parsed range restriction. |
| 1704 | * @param[in] ctx Compile context. |
| 1705 | * @param[in] range_p Parsed range structure to compile. |
| 1706 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1707 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1708 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1709 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1710 | * range restriction must be more restrictive than the base_range. |
| 1711 | * @param[in,out] range Pointer to the created current range structure. |
| 1712 | * @return LY_ERR value. |
| 1713 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1714 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1715 | 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] | 1716 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1717 | { |
| 1718 | LY_ERR ret = LY_EVALID; |
| 1719 | const char *expr; |
| 1720 | struct lysc_range_part *parts = NULL, *part; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1721 | ly_bool range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1722 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1723 | |
| 1724 | assert(range); |
| 1725 | assert(range_p); |
| 1726 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1727 | expr = range_p->arg.str; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1728 | while (1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1729 | if (isspace(*expr)) { |
| 1730 | ++expr; |
| 1731 | } else if (*expr == '\0') { |
| 1732 | if (range_expected) { |
| 1733 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1734 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1735 | length_restr ? "length" : "range", range_p->arg); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1736 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1737 | } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1738 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1739 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1740 | length_restr ? "length" : "range", range_p->arg); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1741 | goto cleanup; |
| 1742 | } |
| 1743 | parts_done++; |
| 1744 | break; |
| 1745 | } else if (!strncmp(expr, "min", 3)) { |
| 1746 | if (parts) { |
| 1747 | /* min cannot be used elsewhere than in the first part */ |
| 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 - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1750 | expr - range_p->arg.str, range_p->arg.str); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1751 | goto cleanup; |
| 1752 | } |
| 1753 | expr += 3; |
| 1754 | |
| 1755 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1756 | 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] | 1757 | part->max_64 = part->min_64; |
| 1758 | } else if (*expr == '|') { |
| 1759 | if (!parts || range_expected) { |
| 1760 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1761 | "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] | 1762 | goto cleanup; |
| 1763 | } |
| 1764 | expr++; |
| 1765 | parts_done++; |
| 1766 | /* process next part of the expression */ |
| 1767 | } else if (!strncmp(expr, "..", 2)) { |
| 1768 | expr += 2; |
| 1769 | while (isspace(*expr)) { |
| 1770 | expr++; |
| 1771 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1772 | if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1773 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1774 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1775 | goto cleanup; |
| 1776 | } |
| 1777 | /* continue expecting the upper boundary */ |
| 1778 | range_expected = 1; |
| 1779 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1780 | /* number */ |
| 1781 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1782 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1783 | 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] | 1784 | range_expected = 0; |
| 1785 | } else { |
| 1786 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1787 | 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^] | 1788 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1789 | part->max_64 = part->min_64; |
| 1790 | } |
| 1791 | |
| 1792 | /* continue with possible another expression part */ |
| 1793 | } else if (!strncmp(expr, "max", 3)) { |
| 1794 | expr += 3; |
| 1795 | while (isspace(*expr)) { |
| 1796 | expr++; |
| 1797 | } |
| 1798 | if (*expr != '\0') { |
| 1799 | 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^] | 1800 | length_restr ? "length" : "range", expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1801 | goto cleanup; |
| 1802 | } |
| 1803 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1804 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1805 | 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] | 1806 | range_expected = 0; |
| 1807 | } else { |
| 1808 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1809 | 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^] | 1810 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1811 | part->min_64 = part->max_64; |
| 1812 | } |
| 1813 | } else { |
| 1814 | 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^] | 1815 | length_restr ? "length" : "range", expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1816 | goto cleanup; |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | /* check with the previous range/length restriction */ |
| 1821 | if (base_range) { |
| 1822 | switch (basetype) { |
| 1823 | case LY_TYPE_BINARY: |
| 1824 | case LY_TYPE_UINT8: |
| 1825 | case LY_TYPE_UINT16: |
| 1826 | case LY_TYPE_UINT32: |
| 1827 | case LY_TYPE_UINT64: |
| 1828 | case LY_TYPE_STRING: |
| 1829 | uns = 1; |
| 1830 | break; |
| 1831 | case LY_TYPE_DEC64: |
| 1832 | case LY_TYPE_INT8: |
| 1833 | case LY_TYPE_INT16: |
| 1834 | case LY_TYPE_INT32: |
| 1835 | case LY_TYPE_INT64: |
| 1836 | uns = 0; |
| 1837 | break; |
| 1838 | default: |
| 1839 | LOGINT(ctx->ctx); |
| 1840 | ret = LY_EINT; |
| 1841 | goto cleanup; |
| 1842 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1843 | 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^] | 1844 | 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] | 1845 | goto baseerror; |
| 1846 | } |
| 1847 | /* current lower bound is not lower than the base */ |
| 1848 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1849 | /* base has single value */ |
| 1850 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1851 | /* both lower bounds are the same */ |
| 1852 | if (parts[u].min_64 != parts[u].max_64) { |
| 1853 | /* current continues with a range */ |
| 1854 | goto baseerror; |
| 1855 | } else { |
| 1856 | /* equal single values, move both forward */ |
| 1857 | ++v; |
| 1858 | continue; |
| 1859 | } |
| 1860 | } else { |
| 1861 | /* base is single value lower than current range, so the |
| 1862 | * value from base range is removed in the current, |
| 1863 | * move only base and repeat checking */ |
| 1864 | ++v; |
| 1865 | --u; |
| 1866 | continue; |
| 1867 | } |
| 1868 | } else { |
| 1869 | /* base is the range */ |
| 1870 | if (parts[u].min_64 == parts[u].max_64) { |
| 1871 | /* current is a single value */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1872 | 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] | 1873 | /* current is behind the base range, so base range is omitted, |
| 1874 | * move the base and keep the current for further check */ |
| 1875 | ++v; |
| 1876 | --u; |
| 1877 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1878 | continue; |
| 1879 | } else { |
| 1880 | /* both are ranges - check the higher bound, the lower was already checked */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1881 | 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] | 1882 | /* higher bound is higher than the current higher bound */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1883 | 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] | 1884 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1885 | * continue with the same current, but move the base */ |
| 1886 | --u; |
| 1887 | ++v; |
| 1888 | continue; |
| 1889 | } |
| 1890 | /* current range starts within the base range but end behind it */ |
| 1891 | goto baseerror; |
| 1892 | } else { |
| 1893 | /* current range is smaller than the base, |
| 1894 | * move current, but stay with the base */ |
| 1895 | continue; |
| 1896 | } |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | if (u != parts_done) { |
| 1901 | baseerror: |
| 1902 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1903 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1904 | length_restr ? "length" : "range", range_p->arg); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1905 | goto cleanup; |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | if (!(*range)) { |
| 1910 | *range = calloc(1, sizeof **range); |
| 1911 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1912 | } |
| 1913 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1914 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1915 | if (range_p->eapptag) { |
| 1916 | lydict_remove(ctx->ctx, (*range)->eapptag); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1917 | 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] | 1918 | } |
| 1919 | if (range_p->emsg) { |
| 1920 | lydict_remove(ctx->ctx, (*range)->emsg); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1921 | 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] | 1922 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1923 | if (range_p->dsc) { |
| 1924 | lydict_remove(ctx->ctx, (*range)->dsc); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1925 | 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] | 1926 | } |
| 1927 | if (range_p->ref) { |
| 1928 | lydict_remove(ctx->ctx, (*range)->ref); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1929 | 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] | 1930 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1931 | /* extensions are taken only from the last range by the caller */ |
| 1932 | |
| 1933 | (*range)->parts = parts; |
| 1934 | parts = NULL; |
| 1935 | ret = LY_SUCCESS; |
| 1936 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1937 | LY_ARRAY_FREE(parts); |
| 1938 | |
| 1939 | return ret; |
| 1940 | } |
| 1941 | |
| 1942 | /** |
| 1943 | * @brief Checks pattern syntax. |
| 1944 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1945 | * @param[in] ctx Context. |
| 1946 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1947 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1948 | * @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] | 1949 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1950 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1951 | LY_ERR |
| 1952 | 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] | 1953 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1954 | size_t idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1955 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1956 | int err_code; |
| 1957 | const char *orig_ptr; |
| 1958 | PCRE2_SIZE err_offset; |
| 1959 | pcre2_code *code_local; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 1960 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1961 | #define URANGE_LEN 19 |
| 1962 | char *ublock2urange[][2] = { |
| 1963 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1964 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1965 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1966 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1967 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1968 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1969 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1970 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1971 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1972 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1973 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1974 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1975 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1976 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1977 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1978 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1979 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1980 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1981 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1982 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1983 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1984 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1985 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1986 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1987 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1988 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1989 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1990 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1991 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1992 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1993 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1994 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1995 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1996 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1997 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1998 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1999 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 2000 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 2001 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 2002 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 2003 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 2004 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 2005 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 2006 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 2007 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 2008 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 2009 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 2010 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 2011 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 2012 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 2013 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 2014 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 2015 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 2016 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 2017 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 2018 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 2019 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 2020 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 2021 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 2022 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 2023 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 2024 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 2025 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 2026 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 2027 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 2028 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 2029 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 2030 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 2031 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 2032 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 2033 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 2034 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 2035 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 2036 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 2037 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 2038 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 2039 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 2040 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 2041 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 2042 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 2043 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 2044 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 2045 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 2046 | {NULL, NULL} |
| 2047 | }; |
| 2048 | |
| 2049 | /* adjust the expression to a Perl equivalent |
| 2050 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 2051 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2052 | /* allocate space for the transformed pattern */ |
| 2053 | size = strlen(pattern) + 1; |
| 2054 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2055 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2056 | perl_regex[0] = '\0'; |
| 2057 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2058 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 2059 | brack = 0; |
| 2060 | idx = 0; |
| 2061 | orig_ptr = pattern; |
| 2062 | while (orig_ptr[0]) { |
| 2063 | switch (orig_ptr[0]) { |
| 2064 | case '$': |
| 2065 | case '^': |
| 2066 | if (!brack) { |
| 2067 | /* make space for the extra character */ |
| 2068 | ++size; |
| 2069 | perl_regex = ly_realloc(perl_regex, size); |
| 2070 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2071 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2072 | /* print escape slash */ |
| 2073 | perl_regex[idx] = '\\'; |
| 2074 | ++idx; |
| 2075 | } |
| 2076 | break; |
| 2077 | case '[': |
| 2078 | /* must not be escaped */ |
| 2079 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2080 | ++brack; |
| 2081 | } |
| 2082 | break; |
| 2083 | case ']': |
| 2084 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2085 | /* pattern was checked and compiled already */ |
| 2086 | assert(brack); |
| 2087 | --brack; |
| 2088 | } |
| 2089 | break; |
| 2090 | default: |
| 2091 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2092 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2093 | |
| 2094 | /* copy char */ |
| 2095 | perl_regex[idx] = orig_ptr[0]; |
| 2096 | |
| 2097 | ++idx; |
| 2098 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2099 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2100 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2101 | |
| 2102 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2103 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2104 | start = ptr - perl_regex; |
| 2105 | |
| 2106 | ptr = strchr(ptr, '}'); |
| 2107 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2108 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2109 | pattern, perl_regex + start + 2, "unterminated character property"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2110 | free(perl_regex); |
| 2111 | return LY_EVALID; |
| 2112 | } |
| 2113 | end = (ptr - perl_regex) + 1; |
| 2114 | |
| 2115 | /* need more space */ |
| 2116 | if (end - start < URANGE_LEN) { |
| 2117 | 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] | 2118 | 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] | 2119 | } |
| 2120 | |
| 2121 | /* find our range */ |
| 2122 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2123 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2124 | break; |
| 2125 | } |
| 2126 | } |
| 2127 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2128 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2129 | pattern, perl_regex + start + 5, "unknown block name"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2130 | free(perl_regex); |
| 2131 | return LY_EVALID; |
| 2132 | } |
| 2133 | |
| 2134 | /* 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] | 2135 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2136 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2137 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2138 | } |
| 2139 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2140 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2141 | } |
| 2142 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2143 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2144 | /* skip brackets */ |
| 2145 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2146 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2147 | } else { |
| 2148 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2149 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2154 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2155 | PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE, |
| 2156 | &err_code, &err_offset, NULL); |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2157 | if (!code_local) { |
| 2158 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2159 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2160 | 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] | 2161 | free(perl_regex); |
| 2162 | return LY_EVALID; |
| 2163 | } |
| 2164 | free(perl_regex); |
| 2165 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2166 | if (code) { |
| 2167 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2168 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2169 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | return LY_SUCCESS; |
| 2173 | |
| 2174 | #undef URANGE_LEN |
| 2175 | } |
| 2176 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2177 | /** |
| 2178 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2179 | * @param[in] ctx Compile context. |
| 2180 | * @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] | 2181 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2182 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2183 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2184 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2185 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2186 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2187 | 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] | 2188 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2189 | { |
| 2190 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2191 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2192 | LY_ERR ret = LY_SUCCESS; |
| 2193 | |
| 2194 | /* first, copy the patterns from the base type */ |
| 2195 | if (base_patterns) { |
| 2196 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2197 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2198 | } |
| 2199 | |
| 2200 | LY_ARRAY_FOR(patterns_p, u) { |
| 2201 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2202 | *pattern = calloc(1, sizeof **pattern); |
| 2203 | ++(*pattern)->refcount; |
| 2204 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2205 | 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] | 2206 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2207 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2208 | if (patterns_p[u].arg.str[0] == 0x15) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2209 | (*pattern)->inverted = 1; |
| 2210 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2211 | 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] | 2212 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done); |
| 2213 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done); |
| 2214 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done); |
| 2215 | 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] | 2216 | 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] | 2217 | } |
| 2218 | done: |
| 2219 | return ret; |
| 2220 | } |
| 2221 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2222 | /** |
| 2223 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2224 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2225 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2226 | 0 /* LY_TYPE_UNKNOWN */, |
| 2227 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2228 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2229 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2230 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2231 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2232 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2233 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2234 | 0 /* LY_TYPE_BOOL */, |
| 2235 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2236 | 0 /* LY_TYPE_EMPTY */, |
| 2237 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2238 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2239 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2240 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2241 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2242 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2243 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2244 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2245 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2246 | }; |
| 2247 | |
| 2248 | /** |
| 2249 | * @brief stringification of the YANG built-in data types |
| 2250 | */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2251 | const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = { |
| 2252 | "unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2253 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2254 | "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer" |
| 2255 | }; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2256 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2257 | /** |
| 2258 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2259 | * @param[in] ctx Compile context. |
| 2260 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2261 | * @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] | 2262 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2263 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2264 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2265 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2266 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2267 | 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] | 2268 | 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] | 2269 | { |
| 2270 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2271 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2272 | int32_t value = 0; |
| 2273 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2274 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2275 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2276 | if (base_enums && (ctx->mod_def->version < 2)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2277 | 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^] | 2278 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2279 | return LY_EVALID; |
| 2280 | } |
| 2281 | |
| 2282 | LY_ARRAY_FOR(enums_p, u) { |
| 2283 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2284 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done); |
| 2285 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done); |
| 2286 | 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] | 2287 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2288 | if (base_enums) { |
| 2289 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2290 | LY_ARRAY_FOR(base_enums, v) { |
| 2291 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2292 | break; |
| 2293 | } |
| 2294 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2295 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2296 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2297 | "Invalid %s - derived type adds new item \"%s\".", |
| 2298 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2299 | return LY_EVALID; |
| 2300 | } |
| 2301 | match = v; |
| 2302 | } |
| 2303 | |
| 2304 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2305 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2306 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2307 | e->value = (int32_t)enums_p[u].value; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2308 | if (!u || (e->value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2309 | value = e->value + 1; |
| 2310 | } |
| 2311 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2312 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2313 | if (e->value == (*enums)[v].value) { |
| 2314 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2315 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2316 | e->value, e->name, (*enums)[v].name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2317 | return LY_EVALID; |
| 2318 | } |
| 2319 | } |
| 2320 | } else if (base_enums) { |
| 2321 | /* inherit the assigned value */ |
| 2322 | e->value = base_enums[match].value; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2323 | if (!u || (e->value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2324 | value = e->value + 1; |
| 2325 | } |
| 2326 | } else { |
| 2327 | /* assign value automatically */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2328 | if (u && (value == INT32_MIN)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2329 | /* counter overflow */ |
| 2330 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2331 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2332 | "\"%s\" since the highest value is already 2147483647.", e->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2333 | return LY_EVALID; |
| 2334 | } |
| 2335 | e->value = value++; |
| 2336 | } |
| 2337 | } else { /* LY_TYPE_BITS */ |
| 2338 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2339 | e->value = (int32_t)enums_p[u].value; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2340 | if (!u || ((uint32_t)e->value >= position)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2341 | position = (uint32_t)e->value + 1; |
| 2342 | } |
| 2343 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2344 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2345 | if (e->value == (*enums)[v].value) { |
| 2346 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2347 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2348 | (uint32_t)e->value, e->name, (*enums)[v].name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2349 | return LY_EVALID; |
| 2350 | } |
| 2351 | } |
| 2352 | } else if (base_enums) { |
| 2353 | /* inherit the assigned value */ |
| 2354 | e->value = base_enums[match].value; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2355 | if (!u || ((uint32_t)e->value >= position)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2356 | position = (uint32_t)e->value + 1; |
| 2357 | } |
| 2358 | } else { |
| 2359 | /* assign value automatically */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2360 | if (u && (position == 0)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2361 | /* counter overflow */ |
| 2362 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2363 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2364 | "\"%s\" since the highest value is already 4294967295.", e->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2365 | return LY_EVALID; |
| 2366 | } |
| 2367 | e->value = position++; |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | if (base_enums) { |
| 2372 | /* the assigned values must not change from the derived type */ |
| 2373 | if (e->value != base_enums[match].value) { |
| 2374 | if (basetype == LY_TYPE_ENUM) { |
| 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 of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2377 | e->name, base_enums[match].value, e->value); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2378 | } else { |
| 2379 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2380 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2381 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2382 | } |
| 2383 | return LY_EVALID; |
| 2384 | } |
| 2385 | } |
| 2386 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2387 | 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] | 2388 | 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] | 2389 | |
| 2390 | if (basetype == LY_TYPE_BITS) { |
| 2391 | /* keep bits ordered by position */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2392 | for (v = u; v && (*enums)[v - 1].value > e->value; --v) {} |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2393 | if (v != u) { |
| 2394 | memcpy(&storage, e, sizeof *e); |
| 2395 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2396 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2397 | } |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | done: |
| 2402 | return ret; |
| 2403 | } |
| 2404 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2405 | /** |
| 2406 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2407 | * |
| 2408 | * path-arg = absolute-path / relative-path |
| 2409 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2410 | * relative-path = 1*(".." "/") descendant-path |
| 2411 | * |
| 2412 | * @param[in,out] path Path to parse. |
| 2413 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2414 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2415 | * @param[out] name Name of the token. |
| 2416 | * @param[out] nam_len Length of the name. |
| 2417 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2418 | * must not be changed between consecutive calls. -1 if the |
| 2419 | * path is absolute. |
| 2420 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2421 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2422 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2423 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2424 | 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] | 2425 | int32_t *parent_times, ly_bool *has_predicate) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2426 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2427 | int32_t par_times = 0; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2428 | |
| 2429 | assert(path && *path); |
| 2430 | assert(parent_times); |
| 2431 | assert(prefix); |
| 2432 | assert(prefix_len); |
| 2433 | assert(name); |
| 2434 | assert(name_len); |
| 2435 | assert(has_predicate); |
| 2436 | |
| 2437 | *prefix = NULL; |
| 2438 | *prefix_len = 0; |
| 2439 | *name = NULL; |
| 2440 | *name_len = 0; |
| 2441 | *has_predicate = 0; |
| 2442 | |
| 2443 | if (!*parent_times) { |
| 2444 | if (!strncmp(*path, "..", 2)) { |
| 2445 | *path += 2; |
| 2446 | ++par_times; |
| 2447 | while (!strncmp(*path, "/..", 3)) { |
| 2448 | *path += 3; |
| 2449 | ++par_times; |
| 2450 | } |
| 2451 | } |
| 2452 | if (par_times) { |
| 2453 | *parent_times = par_times; |
| 2454 | } else { |
| 2455 | *parent_times = -1; |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | if (**path != '/') { |
| 2460 | return LY_EINVAL; |
| 2461 | } |
| 2462 | /* skip '/' */ |
| 2463 | ++(*path); |
| 2464 | |
| 2465 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2466 | 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] | 2467 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2468 | if (((**path == '/') && (*path)[1]) || !**path) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2469 | /* path continues by another token or this is the last token */ |
| 2470 | return LY_SUCCESS; |
| 2471 | } else if ((*path)[0] != '[') { |
| 2472 | /* unexpected character */ |
| 2473 | return LY_EINVAL; |
| 2474 | } else { |
| 2475 | /* predicate starting with [ */ |
| 2476 | *has_predicate = 1; |
| 2477 | return LY_SUCCESS; |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2482 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2483 | * |
| 2484 | * The set of features used for target must be a subset of features used for the leafref. |
| 2485 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2486 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2487 | * |
| 2488 | * @param[in] refnode The leafref node. |
| 2489 | * @param[in] target Tha target node of the leafref. |
| 2490 | * @return LY_SUCCESS or LY_EVALID; |
| 2491 | */ |
| 2492 | static LY_ERR |
| 2493 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2494 | { |
| 2495 | LY_ERR ret = LY_EVALID; |
| 2496 | const struct lysc_node *iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2497 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2498 | struct ly_set features = {0}; |
| 2499 | |
| 2500 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2501 | if (iter->iffeatures) { |
| 2502 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2503 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2504 | 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] | 2505 | } |
| 2506 | } |
| 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | /* 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] | 2511 | * 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] | 2512 | * to the leafref itself. */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2513 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2514 | if (iter->iffeatures) { |
| 2515 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2516 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2517 | if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) { |
| 2518 | /* feature not present */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2519 | goto cleanup; |
| 2520 | } |
| 2521 | } |
| 2522 | } |
| 2523 | } |
| 2524 | } |
| 2525 | ret = LY_SUCCESS; |
| 2526 | |
| 2527 | cleanup: |
| 2528 | ly_set_erase(&features, NULL); |
| 2529 | return ret; |
| 2530 | } |
| 2531 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2532 | 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] | 2533 | 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] | 2534 | struct lysc_type **type, const char **units, struct lysp_qname **dflt); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2535 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2536 | /** |
| 2537 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2538 | * @param[in] ctx Compile context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2539 | * @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] | 2540 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2541 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2542 | * @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] | 2543 | * @param[in] type_p Parsed type to compile. |
| 2544 | * @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] | 2545 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2546 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2547 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2548 | * @return LY_ERR value. |
| 2549 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2550 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2551 | 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] | 2552 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, LY_DATA_TYPE basetype, |
| 2553 | const char *tpdfname, struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2554 | { |
| 2555 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2556 | struct lysc_type_bin *bin; |
| 2557 | struct lysc_type_num *num; |
| 2558 | struct lysc_type_str *str; |
| 2559 | struct lysc_type_bits *bits; |
| 2560 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2561 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2562 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2563 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2564 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2565 | |
| 2566 | switch (basetype) { |
| 2567 | case LY_TYPE_BINARY: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2568 | bin = (struct lysc_type_bin *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2569 | |
| 2570 | /* 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] | 2571 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2572 | 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^] | 2573 | base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2574 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2575 | 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] | 2576 | } |
| 2577 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2578 | break; |
| 2579 | case LY_TYPE_BITS: |
| 2580 | /* RFC 7950 9.7 - bits */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2581 | bits = (struct lysc_type_bits *)(*type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2582 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2583 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2584 | 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] | 2585 | (struct lysc_type_bitenum_item **)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2586 | } |
| 2587 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2588 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2589 | /* 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] | 2590 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2591 | 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] | 2592 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2593 | 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] | 2594 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2595 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2596 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2597 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2598 | case LY_TYPE_DEC64: |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2599 | dec = (struct lysc_type_dec *)(*type); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2600 | |
| 2601 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2602 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2603 | if (!type_p->fraction_digits) { |
| 2604 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2605 | 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] | 2606 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2607 | 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] | 2608 | } |
| 2609 | return LY_EVALID; |
| 2610 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2611 | dec->fraction_digits = type_p->fraction_digits; |
| 2612 | } else { |
| 2613 | if (type_p->fraction_digits) { |
| 2614 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
| 2615 | if (tpdfname) { |
| 2616 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2617 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
| 2618 | tpdfname); |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2619 | } else { |
| 2620 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2621 | "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] | 2622 | } |
| 2623 | return LY_EVALID; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2624 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2625 | dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2626 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2627 | |
| 2628 | /* RFC 7950 9.2.4 - range */ |
| 2629 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2630 | 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^] | 2631 | base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2632 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2633 | 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] | 2634 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2635 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2636 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2637 | case LY_TYPE_STRING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2638 | str = (struct lysc_type_str *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2639 | |
| 2640 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2641 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2642 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2643 | base ? ((struct lysc_type_str *)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2644 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2645 | 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] | 2646 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2647 | } else if (base && ((struct lysc_type_str *)base)->length) { |
| 2648 | 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] | 2649 | } |
| 2650 | |
| 2651 | /* RFC 7950 9.4.5 - pattern */ |
| 2652 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2653 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2654 | base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2655 | } else if (base && ((struct lysc_type_str *)base)->patterns) { |
| 2656 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns); |
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; |
| 2659 | case LY_TYPE_ENUM: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2660 | enumeration = (struct lysc_type_enum *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2661 | |
| 2662 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2663 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2664 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2665 | base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2666 | } |
| 2667 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2668 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2669 | /* 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] | 2670 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2671 | 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] | 2672 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2673 | 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] | 2674 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2675 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2676 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2677 | break; |
| 2678 | case LY_TYPE_INT8: |
| 2679 | case LY_TYPE_UINT8: |
| 2680 | case LY_TYPE_INT16: |
| 2681 | case LY_TYPE_UINT16: |
| 2682 | case LY_TYPE_INT32: |
| 2683 | case LY_TYPE_UINT32: |
| 2684 | case LY_TYPE_INT64: |
| 2685 | case LY_TYPE_UINT64: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2686 | num = (struct lysc_type_num *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2687 | |
| 2688 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2689 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2690 | 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^] | 2691 | base ? ((struct lysc_type_num *)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2692 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2693 | 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] | 2694 | } |
| 2695 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2696 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2697 | case LY_TYPE_IDENT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2698 | idref = (struct lysc_type_identityref *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2699 | |
| 2700 | /* RFC 7950 9.10.2 - base */ |
| 2701 | if (type_p->bases) { |
| 2702 | if (base) { |
| 2703 | /* only the directly derived identityrefs can contain base specification */ |
| 2704 | if (tpdfname) { |
| 2705 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2706 | "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.", |
| 2707 | tpdfname); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2708 | } else { |
| 2709 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2710 | "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] | 2711 | } |
| 2712 | return LY_EVALID; |
| 2713 | } |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 2714 | 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] | 2715 | } |
| 2716 | |
| 2717 | if (!base && !type_p->flags) { |
| 2718 | /* type derived from identityref built-in type must contain at least one base */ |
| 2719 | if (tpdfname) { |
| 2720 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2721 | } else { |
| 2722 | 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] | 2723 | } |
| 2724 | return LY_EVALID; |
| 2725 | } |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2726 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2727 | case LY_TYPE_LEAFREF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2728 | lref = (struct lysc_type_leafref *)*type; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2729 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2730 | /* RFC 7950 9.9.3 - require-instance */ |
| 2731 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2732 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2733 | if (tpdfname) { |
| 2734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2735 | "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] | 2736 | } else { |
| 2737 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2738 | "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] | 2739 | } |
| 2740 | return LY_EVALID; |
| 2741 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2742 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2743 | } else if (base) { |
| 2744 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2745 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2746 | } else { |
| 2747 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2748 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2749 | } |
| 2750 | if (type_p->path) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2751 | 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] | 2752 | lref->path_mod = type_p->mod; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2753 | } else if (base) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2754 | 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] | 2755 | lref->path_mod = ((struct lysc_type_leafref *)base)->path_mod; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2756 | } else if (tpdfname) { |
| 2757 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2758 | return LY_EVALID; |
| 2759 | } else { |
| 2760 | 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] | 2761 | return LY_EVALID; |
| 2762 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2763 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2764 | case LY_TYPE_INST: |
| 2765 | /* RFC 7950 9.9.3 - require-instance */ |
| 2766 | if (type_p->flags & LYS_SET_REQINST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2767 | ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2768 | } else { |
| 2769 | /* default is true */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2770 | ((struct lysc_type_instanceid *)(*type))->require_instance = 1; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2771 | } |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2772 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2773 | case LY_TYPE_UNION: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2774 | un = (struct lysc_type_union *)(*type); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2775 | |
| 2776 | /* RFC 7950 7.4 - type */ |
| 2777 | if (type_p->types) { |
| 2778 | if (base) { |
| 2779 | /* only the directly derived union can contain types specification */ |
| 2780 | if (tpdfname) { |
| 2781 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2782 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2783 | tpdfname); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2784 | } else { |
| 2785 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2786 | "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] | 2787 | } |
| 2788 | return LY_EVALID; |
| 2789 | } |
| 2790 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2791 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2792 | 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] | 2793 | 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^] | 2794 | &type_p->types[u], &un->types[u + additional], NULL, NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2795 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2796 | /* add space for additional types from the union subtype */ |
| 2797 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2798 | 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^] | 2799 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2800 | |
| 2801 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2802 | 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] | 2803 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2804 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2805 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2806 | 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] | 2807 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2808 | |
| 2809 | lref->basetype = LY_TYPE_LEAFREF; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2810 | 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] | 2811 | lref->refcount = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2812 | 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] | 2813 | 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] | 2814 | /* TODO extensions */ |
| 2815 | |
| 2816 | } else { |
| 2817 | un->types[u + additional] = un_aux->types[v]; |
| 2818 | ++un_aux->types[v]->refcount; |
| 2819 | } |
| 2820 | ++additional; |
| 2821 | LY_ARRAY_INCREMENT(un->types); |
| 2822 | } |
| 2823 | /* compensate u increment in main loop */ |
| 2824 | --additional; |
| 2825 | |
| 2826 | /* free the replaced union subtype */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2827 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2828 | } else { |
| 2829 | LY_ARRAY_INCREMENT(un->types); |
| 2830 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | if (!base && !type_p->flags) { |
| 2835 | /* type derived from union built-in type must contain at least one type */ |
| 2836 | if (tpdfname) { |
| 2837 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2838 | } else { |
| 2839 | 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] | 2840 | } |
| 2841 | return LY_EVALID; |
| 2842 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2843 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2844 | case LY_TYPE_BOOL: |
| 2845 | case LY_TYPE_EMPTY: |
| 2846 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2847 | break; |
| 2848 | } |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2849 | |
| 2850 | if (tpdfname) { |
| 2851 | switch (basetype) { |
| 2852 | case LY_TYPE_BINARY: |
| 2853 | type_p->compiled = *type; |
| 2854 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2855 | break; |
| 2856 | case LY_TYPE_BITS: |
| 2857 | type_p->compiled = *type; |
| 2858 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2859 | break; |
| 2860 | case LY_TYPE_DEC64: |
| 2861 | type_p->compiled = *type; |
| 2862 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2863 | break; |
| 2864 | case LY_TYPE_STRING: |
| 2865 | type_p->compiled = *type; |
| 2866 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2867 | break; |
| 2868 | case LY_TYPE_ENUM: |
| 2869 | type_p->compiled = *type; |
| 2870 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2871 | break; |
| 2872 | case LY_TYPE_INT8: |
| 2873 | case LY_TYPE_UINT8: |
| 2874 | case LY_TYPE_INT16: |
| 2875 | case LY_TYPE_UINT16: |
| 2876 | case LY_TYPE_INT32: |
| 2877 | case LY_TYPE_UINT32: |
| 2878 | case LY_TYPE_INT64: |
| 2879 | case LY_TYPE_UINT64: |
| 2880 | type_p->compiled = *type; |
| 2881 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2882 | break; |
| 2883 | case LY_TYPE_IDENT: |
| 2884 | type_p->compiled = *type; |
| 2885 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2886 | break; |
| 2887 | case LY_TYPE_LEAFREF: |
| 2888 | type_p->compiled = *type; |
| 2889 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2890 | break; |
| 2891 | case LY_TYPE_INST: |
| 2892 | type_p->compiled = *type; |
| 2893 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2894 | break; |
| 2895 | case LY_TYPE_UNION: |
| 2896 | type_p->compiled = *type; |
| 2897 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2898 | break; |
| 2899 | case LY_TYPE_BOOL: |
| 2900 | case LY_TYPE_EMPTY: |
| 2901 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2902 | break; |
| 2903 | } |
| 2904 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2905 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2906 | |
| 2907 | cleanup: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2908 | return ret; |
| 2909 | } |
| 2910 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2911 | /** |
| 2912 | * @brief Compile information about the leaf/leaf-list's type. |
| 2913 | * @param[in] ctx Compile context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2914 | * @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] | 2915 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2916 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2917 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2918 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2919 | * @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] | 2920 | * @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] | 2921 | * @param[out] dflt Default value for the type. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2922 | * @return LY_ERR value. |
| 2923 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2924 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2925 | 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] | 2926 | 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] | 2927 | struct lysc_type **type, const char **units, struct lysp_qname **dflt) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2928 | { |
| 2929 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2930 | ly_bool dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2931 | struct type_context { |
| 2932 | const struct lysp_tpdf *tpdf; |
| 2933 | struct lysp_node *node; |
| 2934 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2935 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2936 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2937 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2938 | struct ly_set tpdf_chain = {0}; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2939 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2940 | (*type) = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2941 | if (dflt) { |
| 2942 | *dflt = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2943 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2944 | |
| 2945 | tctx = calloc(1, sizeof *tctx); |
| 2946 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2947 | 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^] | 2948 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2949 | ret == LY_SUCCESS; |
| 2950 | 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^] | 2951 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2952 | if (basetype) { |
| 2953 | break; |
| 2954 | } |
| 2955 | |
| 2956 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2957 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2958 | 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] | 2959 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2960 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2961 | if (units && !*units) { |
| 2962 | /* inherit units */ |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 2963 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret); |
| 2964 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2965 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2966 | if (dflt && !*dflt && tctx->tpdf->dflt.str) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2967 | /* inherit default */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2968 | *dflt = (struct lysp_qname *)&tctx->tpdf->dflt; |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 2969 | assert((*dflt)->mod); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2970 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2971 | if (dummyloops && (!units || *units) && dflt && *dflt) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2972 | 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] | 2973 | break; |
| 2974 | } |
| 2975 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2976 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2977 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2978 | * 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] | 2979 | basetype = tctx->tpdf->type.compiled->basetype; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2980 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 2981 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2982 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2983 | if ((units && !*units) || (dflt && !*dflt)) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2984 | dummyloops = 1; |
| 2985 | goto preparenext; |
| 2986 | } else { |
| 2987 | tctx = NULL; |
| 2988 | break; |
| 2989 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2990 | } |
| 2991 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2992 | /* circular typedef reference detection */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2993 | for (uint32_t u = 0; u < tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2994 | /* local part */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2995 | tctx_iter = (struct type_context *)tpdf_chain.objs[u]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2996 | 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] | 2997 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 2998 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2999 | free(tctx); |
| 3000 | ret = LY_EVALID; |
| 3001 | goto cleanup; |
| 3002 | } |
| 3003 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3004 | for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3005 | /* global part for unions corner case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3006 | tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3007 | 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] | 3008 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3009 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3010 | free(tctx); |
| 3011 | ret = LY_EVALID; |
| 3012 | goto cleanup; |
| 3013 | } |
| 3014 | } |
| 3015 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3016 | /* store information for the following processing */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3017 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 3018 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3019 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3020 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3021 | /* prepare next loop */ |
| 3022 | tctx_prev = tctx; |
| 3023 | tctx = calloc(1, sizeof *tctx); |
| 3024 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3025 | } |
| 3026 | free(tctx); |
| 3027 | |
| 3028 | /* allocate type according to the basetype */ |
| 3029 | switch (basetype) { |
| 3030 | case LY_TYPE_BINARY: |
| 3031 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3032 | break; |
| 3033 | case LY_TYPE_BITS: |
| 3034 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3035 | break; |
| 3036 | case LY_TYPE_BOOL: |
| 3037 | case LY_TYPE_EMPTY: |
| 3038 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3039 | break; |
| 3040 | case LY_TYPE_DEC64: |
| 3041 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3042 | break; |
| 3043 | case LY_TYPE_ENUM: |
| 3044 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3045 | break; |
| 3046 | case LY_TYPE_IDENT: |
| 3047 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3048 | break; |
| 3049 | case LY_TYPE_INST: |
| 3050 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3051 | break; |
| 3052 | case LY_TYPE_LEAFREF: |
| 3053 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3054 | break; |
| 3055 | case LY_TYPE_STRING: |
| 3056 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3057 | break; |
| 3058 | case LY_TYPE_UNION: |
| 3059 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3060 | break; |
| 3061 | case LY_TYPE_INT8: |
| 3062 | case LY_TYPE_UINT8: |
| 3063 | case LY_TYPE_INT16: |
| 3064 | case LY_TYPE_UINT16: |
| 3065 | case LY_TYPE_INT32: |
| 3066 | case LY_TYPE_UINT32: |
| 3067 | case LY_TYPE_INT64: |
| 3068 | case LY_TYPE_UINT64: |
| 3069 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3070 | break; |
| 3071 | case LY_TYPE_UNKNOWN: |
| 3072 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3073 | "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] | 3074 | ret = LY_EVALID; |
| 3075 | goto cleanup; |
| 3076 | } |
| 3077 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3078 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3079 | 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^] | 3080 | ly_data_type2str[basetype]); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3081 | free(*type); |
| 3082 | (*type) = NULL; |
| 3083 | ret = LY_EVALID; |
| 3084 | goto cleanup; |
| 3085 | } |
| 3086 | |
| 3087 | /* get restrictions from the referred typedefs */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3088 | for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3089 | tctx = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3090 | |
| 3091 | /* remember the typedef context for circular check */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3092 | ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
| 3093 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3094 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3095 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3096 | base = tctx->tpdf->type.compiled; |
| 3097 | continue; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3098 | } 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] | 3099 | /* no change, just use the type information from the base */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3100 | 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] | 3101 | ++base->refcount; |
| 3102 | continue; |
| 3103 | } |
| 3104 | |
| 3105 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3106 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3107 | 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^] | 3108 | tctx->tpdf->name, ly_data_type2str[basetype]); |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3109 | ret = LY_EVALID; |
| 3110 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3111 | } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) { |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3112 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3113 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3114 | tctx->tpdf->name, tctx->tpdf->dflt.str); |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3115 | ret = LY_EVALID; |
| 3116 | goto cleanup; |
| 3117 | } |
| 3118 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3119 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3120 | /* TODO user type plugins */ |
| 3121 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3122 | prev_type = *type; |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 3123 | ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, |
| 3124 | &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3125 | LY_CHECK_GOTO(ret, cleanup); |
| 3126 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3127 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3128 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3129 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3130 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3131 | /* process the type definition in leaf */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3132 | if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3133 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3134 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3135 | /* TODO user type plugins */ |
| 3136 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3137 | ++(*type)->refcount; |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 3138 | ret = lys_compile_type_(ctx, context_pnode, context_flags, context_mod, context_name, type_p, basetype, NULL, |
| 3139 | base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3140 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3141 | } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3142 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3143 | free(*type); |
| 3144 | (*type) = base; |
| 3145 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3146 | } |
| 3147 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3148 | 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] | 3149 | |
| 3150 | cleanup: |
| 3151 | ly_set_erase(&tpdf_chain, free); |
| 3152 | return ret; |
| 3153 | } |
| 3154 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3155 | /** |
| 3156 | * @brief Compile status information of the given node. |
| 3157 | * |
| 3158 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3159 | * has the status correctly set during the compilation. |
| 3160 | * |
| 3161 | * @param[in] ctx Compile context |
| 3162 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3163 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3164 | * the compatibility with the parent's status value. |
| 3165 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3166 | * @return LY_ERR value. |
| 3167 | */ |
| 3168 | static LY_ERR |
| 3169 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3170 | { |
| 3171 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3172 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3173 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3174 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3175 | if ((parent_flags & 0x3) != 0x3) { |
| 3176 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3177 | * combination of bits (0x3) which marks the uses_status value */ |
| 3178 | 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] | 3179 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3180 | } |
| 3181 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3182 | } else { |
| 3183 | (*node_flags) |= LYS_STATUS_CURR; |
| 3184 | } |
| 3185 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3186 | /* check status compatibility with the parent */ |
| 3187 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3188 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3189 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3190 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3191 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3192 | } else { /* LYS_STATUS_DEPRC */ |
| 3193 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3194 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3195 | } |
| 3196 | return LY_EVALID; |
| 3197 | } |
| 3198 | } |
| 3199 | return LY_SUCCESS; |
| 3200 | } |
| 3201 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3202 | /** |
| 3203 | * @brief Check uniqness of the node/action/notification name. |
| 3204 | * |
| 3205 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3206 | * structures, but they share the namespace so we need to check their name collisions. |
| 3207 | * |
| 3208 | * @param[in] ctx Compile context. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3209 | * @param[in] parent Parent of the nodes to check, can be NULL. |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3210 | * @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] | 3211 | * @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] | 3212 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3213 | */ |
| 3214 | static LY_ERR |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3215 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name, |
| 3216 | const struct lysc_node *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3217 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3218 | const struct lysc_node *iter, *iter2; |
| 3219 | const struct lysc_action *actions; |
| 3220 | const struct lysc_notif *notifs; |
| 3221 | uint32_t getnext_flags; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3222 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3223 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3224 | #define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name)) |
| 3225 | |
| 3226 | if (exclude->nodetype == LYS_CASE) { |
| 3227 | /* check restricted only to all the cases */ |
| 3228 | assert(parent->nodetype == LYS_CHOICE); |
| 3229 | LY_LIST_FOR(lysc_node_children(parent, 0), iter) { |
| 3230 | if (CHECK_NODE(iter, exclude, name)) { |
| 3231 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case"); |
| 3232 | return LY_EEXIST; |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | return LY_SUCCESS; |
| 3237 | } |
| 3238 | |
| 3239 | /* no reason for our parent to be choice anymore */ |
| 3240 | assert(!parent || (parent->nodetype != LYS_CHOICE)); |
| 3241 | |
| 3242 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 3243 | /* move to the first data definition parent */ |
| 3244 | parent = lysc_data_parent(parent); |
| 3245 | } |
| 3246 | |
| 3247 | getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE; |
| 3248 | if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) { |
| 3249 | getnext_flags |= LYS_GETNEXT_OUTPUT; |
| 3250 | } |
| 3251 | |
| 3252 | iter = NULL; |
| 3253 | while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) { |
| 3254 | if (CHECK_NODE(iter, exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3255 | goto error; |
| 3256 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3257 | |
| 3258 | /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */ |
| 3259 | if (iter->nodetype == LYS_CHOICE) { |
| 3260 | iter2 = NULL; |
| 3261 | while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) { |
| 3262 | if (CHECK_NODE(iter2, exclude, name)) { |
| 3263 | goto error; |
| 3264 | } |
| 3265 | } |
| 3266 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3267 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3268 | |
| 3269 | actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3270 | LY_ARRAY_FOR(actions, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3271 | if (CHECK_NODE(&actions[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3272 | goto error; |
| 3273 | } |
| 3274 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3275 | |
| 3276 | notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3277 | LY_ARRAY_FOR(notifs, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3278 | if (CHECK_NODE(¬ifs[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3279 | goto error; |
| 3280 | } |
| 3281 | } |
| 3282 | return LY_SUCCESS; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3283 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3284 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3285 | 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] | 3286 | return LY_EEXIST; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3287 | |
| 3288 | #undef CHECK_NODE |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3289 | } |
| 3290 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3291 | static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, |
| 3292 | uint16_t uses_status, struct ly_set *child_set); |
| 3293 | |
| 3294 | static LY_ERR lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, |
| 3295 | const struct lysc_node *parent, struct lysp_node **dev_pnode, ly_bool *not_supported); |
| 3296 | |
| 3297 | static LY_ERR lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node); |
| 3298 | |
| 3299 | 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] | 3300 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3301 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3302 | * @brief Compile parsed RPC/action schema node information. |
| 3303 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3304 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3305 | * @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] | 3306 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3307 | * @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). |
| 3308 | * 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] | 3309 | * @return LY_SUCCESS on success, |
| 3310 | * @return LY_EVALID on validation error, |
| 3311 | * @return LY_EDENIED on not-supported deviation. |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3312 | */ |
| 3313 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3314 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3315 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3316 | { |
| 3317 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3318 | struct lysp_node *child_p, *dev_pnode = NULL, *dev_input_p = NULL, *dev_output_p = NULL; |
| 3319 | struct lysp_action *orig_action_p = action_p; |
| 3320 | struct lysp_action_inout *inout_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3321 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3322 | ly_bool not_supported; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3323 | uint32_t opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3324 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3325 | lysc_update_path(ctx, parent, action_p->name); |
| 3326 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3327 | /* apply deviation on the action/RPC */ |
| 3328 | LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)action_p, parent, &dev_pnode, ¬_supported)); |
| 3329 | if (not_supported) { |
| 3330 | lysc_update_path(ctx, NULL, NULL); |
| 3331 | return LY_EDENIED; |
| 3332 | } else if (dev_pnode) { |
| 3333 | action_p = (struct lysp_action *)dev_pnode; |
| 3334 | } |
| 3335 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3336 | /* member needed for uniqueness check lys_getnext() */ |
| 3337 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
| 3338 | action->module = ctx->mod; |
| 3339 | action->parent = parent; |
| 3340 | |
| 3341 | 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] | 3342 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3343 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3344 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3345 | "Action \"%s\" is placed inside %s.", action_p->name, |
| 3346 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3347 | return LY_EVALID; |
| 3348 | } |
| 3349 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3350 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3351 | action->sp = orig_action_p; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3352 | } |
| 3353 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3354 | |
| 3355 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3356 | * 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] | 3357 | 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] | 3358 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3359 | DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup); |
| 3360 | DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup); |
| 3361 | DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3362 | 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] | 3363 | 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] | 3364 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3365 | /* connect any action augments */ |
| 3366 | LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)action)); |
| 3367 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3368 | /* input */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3369 | lysc_update_path(ctx, (struct lysc_node *)action, "input"); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3370 | |
| 3371 | /* apply deviations on input */ |
| 3372 | LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->input, (struct lysc_node *)action, |
| 3373 | &dev_input_p, ¬_supported)); |
| 3374 | if (not_supported) { |
| 3375 | inout_p = NULL; |
| 3376 | } else if (dev_input_p) { |
| 3377 | inout_p = (struct lysp_action_inout *)dev_input_p; |
| 3378 | } else { |
| 3379 | inout_p = &action_p->input; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3380 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3381 | |
| 3382 | if (inout_p) { |
| 3383 | action->input.nodetype = LYS_INPUT; |
| 3384 | COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->input.musts, u, lys_compile_must, ret, cleanup); |
| 3385 | COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup); |
| 3386 | ctx->options |= LYSC_OPT_RPC_INPUT; |
| 3387 | |
| 3388 | /* connect any input augments */ |
| 3389 | LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)&action->input)); |
| 3390 | |
| 3391 | LY_LIST_FOR(inout_p->data, child_p) { |
| 3392 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status, NULL)); |
| 3393 | } |
| 3394 | ctx->options = opt_prev; |
| 3395 | } |
| 3396 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3397 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3398 | |
| 3399 | /* output */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3400 | lysc_update_path(ctx, (struct lysc_node *)action, "output"); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3401 | |
| 3402 | /* apply deviations on output */ |
| 3403 | LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->output, (struct lysc_node *)action, |
| 3404 | &dev_output_p, ¬_supported)); |
| 3405 | if (not_supported) { |
| 3406 | inout_p = NULL; |
| 3407 | } else if (dev_output_p) { |
| 3408 | inout_p = (struct lysp_action_inout *)dev_output_p; |
| 3409 | } else { |
| 3410 | inout_p = &action_p->output; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3411 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3412 | |
| 3413 | if (inout_p) { |
| 3414 | action->output.nodetype = LYS_OUTPUT; |
| 3415 | COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->output.musts, u, lys_compile_must, ret, cleanup); |
| 3416 | COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup); |
| 3417 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
| 3418 | |
| 3419 | /* connect any output augments */ |
| 3420 | LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)&action->output)); |
| 3421 | |
| 3422 | LY_LIST_FOR(inout_p->data, child_p) { |
| 3423 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status, NULL)); |
| 3424 | } |
| 3425 | ctx->options = opt_prev; |
| 3426 | } |
| 3427 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3428 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3429 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3430 | if ((action->input.musts || action->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3431 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3432 | ret = ly_set_add(&ctx->xpath, action, 0, NULL); |
| 3433 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3434 | } |
| 3435 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3436 | lysc_update_path(ctx, NULL, NULL); |
| 3437 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3438 | cleanup: |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3439 | lysp_dev_node_free(ctx->ctx, dev_pnode); |
| 3440 | lysp_dev_node_free(ctx->ctx, dev_input_p); |
| 3441 | lysp_dev_node_free(ctx->ctx, dev_output_p); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3442 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3443 | return ret; |
| 3444 | } |
| 3445 | |
| 3446 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3447 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3448 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3449 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3450 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3451 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3452 | * @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] | 3453 | * 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] | 3454 | * @return LY_SUCCESS on success, |
| 3455 | * @return LY_EVALID on validation error, |
| 3456 | * @return LY_EDENIED on not-supported deviation. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3457 | */ |
| 3458 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3459 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3460 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3461 | { |
| 3462 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3463 | struct lysp_node *child_p, *dev_pnode = NULL; |
| 3464 | struct lysp_notif *orig_notif_p = notif_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3465 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3466 | ly_bool not_supported; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3467 | uint32_t opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3468 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3469 | lysc_update_path(ctx, parent, notif_p->name); |
| 3470 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3471 | LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)notif_p, parent, &dev_pnode, ¬_supported)); |
| 3472 | if (not_supported) { |
| 3473 | lysc_update_path(ctx, NULL, NULL); |
| 3474 | return LY_EDENIED; |
| 3475 | } else if (dev_pnode) { |
| 3476 | notif_p = (struct lysp_notif *)dev_pnode; |
| 3477 | } |
| 3478 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3479 | /* member needed for uniqueness check lys_getnext() */ |
| 3480 | notif->nodetype = LYS_NOTIF; |
| 3481 | notif->module = ctx->mod; |
| 3482 | notif->parent = parent; |
| 3483 | |
| 3484 | 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] | 3485 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3486 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3487 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3488 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
| 3489 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3490 | return LY_EVALID; |
| 3491 | } |
| 3492 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3493 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3494 | notif->sp = orig_notif_p; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3495 | } |
| 3496 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3497 | |
| 3498 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3499 | * 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] | 3500 | ret = lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0)); |
| 3501 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3502 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3503 | DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup); |
| 3504 | DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup); |
| 3505 | DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3506 | 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] | 3507 | 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] | 3508 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3509 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3510 | ret = ly_set_add(&ctx->xpath, notif, 0, NULL); |
| 3511 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3512 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3513 | 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] | 3514 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3515 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3516 | |
| 3517 | /* connect any notification augments */ |
| 3518 | LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)notif)); |
| 3519 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3520 | LY_LIST_FOR(notif_p->data, child_p) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3521 | 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] | 3522 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3523 | } |
| 3524 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3525 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3526 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3527 | cleanup: |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3528 | lysp_dev_node_free(ctx->ctx, dev_pnode); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3529 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3530 | return ret; |
| 3531 | } |
| 3532 | |
| 3533 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3534 | * @brief Compile parsed container node information. |
| 3535 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3536 | * @param[in] pnode Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3537 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3538 | * is enriched with the container-specific information. |
| 3539 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3540 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3541 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3542 | 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] | 3543 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3544 | struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3545 | struct lysc_node_container *cont = (struct lysc_node_container *)node; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3546 | struct lysp_node *child_p; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3547 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3548 | LY_ERR ret = LY_SUCCESS; |
| 3549 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3550 | if (cont_p->presence) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3551 | /* explicit presence */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3552 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3553 | } else if (cont_p->musts) { |
| 3554 | /* container with a must condition */ |
Radek Krejci | 175f25b | 2020-08-13 12:02:36 +0200 | [diff] [blame] | 3555 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name); |
| 3556 | cont->flags |= LYS_PRESENCE; |
| 3557 | } else if (cont_p->when) { |
| 3558 | /* container with a when condition */ |
| 3559 | 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] | 3560 | cont->flags |= LYS_PRESENCE; |
| 3561 | } else if (cont_p->parent) { |
| 3562 | if (cont_p->parent->nodetype == LYS_CHOICE) { |
| 3563 | /* 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] | 3564 | 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^] | 3565 | cont_p->name, cont_p->parent->name); |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3566 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3567 | } else if ((cont_p->parent->nodetype == LYS_CASE) && |
| 3568 | (((struct lysp_node_case *)cont_p->parent)->child == pnode) && !cont_p->next) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3569 | /* 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] | 3570 | 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^] | 3571 | cont_p->name, cont_p->parent->name); |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3572 | cont->flags |= LYS_PRESENCE; |
| 3573 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3574 | } |
| 3575 | |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3576 | /* more cases when the container has meaning but is kept NP for convenience: |
| 3577 | * - when condition |
| 3578 | * - direct child action/notification |
| 3579 | */ |
| 3580 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3581 | LY_LIST_FOR(cont_p->child, child_p) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3582 | ret = lys_compile_node(ctx, child_p, node, 0, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3583 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3584 | } |
| 3585 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3586 | 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] | 3587 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3588 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3589 | ret = ly_set_add(&ctx->xpath, cont, 0, NULL); |
| 3590 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3591 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3592 | COMPILE_OP_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3593 | 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] | 3594 | |
| 3595 | done: |
| 3596 | return ret; |
| 3597 | } |
| 3598 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3599 | /* |
| 3600 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3601 | * @param[in] ctx Compile context. |
| 3602 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3603 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3604 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3605 | * @return LY_ERR value. |
| 3606 | */ |
| 3607 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3608 | 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] | 3609 | struct lysc_node_leaf *leaf) |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3610 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3611 | struct lysp_qname *dflt; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3612 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3613 | 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^] | 3614 | leaf->units ? NULL : &leaf->units, &dflt)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3615 | |
| 3616 | /* store default value, if any */ |
| 3617 | if (dflt && !(leaf->flags & LYS_SET_DFLT)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3618 | LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3619 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3620 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3621 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3622 | /* 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] | 3623 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3624 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3625 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3626 | LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) { |
| 3627 | 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] | 3628 | /* 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] | 3629 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3630 | } |
| 3631 | } |
| 3632 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3633 | 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] | 3634 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3635 | "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] | 3636 | return LY_EVALID; |
| 3637 | } |
| 3638 | } |
| 3639 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3640 | return LY_SUCCESS; |
| 3641 | } |
| 3642 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3643 | /** |
| 3644 | * @brief Compile parsed leaf node information. |
| 3645 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3646 | * @param[in] pnode Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3647 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3648 | * is enriched with the leaf-specific information. |
| 3649 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3650 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3651 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3652 | 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] | 3653 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3654 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3655 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3656 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3657 | LY_ERR ret = LY_SUCCESS; |
| 3658 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3659 | 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] | 3660 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3661 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3662 | ret = ly_set_add(&ctx->xpath, leaf, 0, NULL); |
| 3663 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3664 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3665 | if (leaf_p->units) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3666 | 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] | 3667 | leaf->flags |= LYS_SET_UNITS; |
| 3668 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3669 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3670 | /* compile type */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3671 | ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3672 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3673 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3674 | /* store/update default value */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3675 | if (leaf_p->dflt.str) { |
| 3676 | 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] | 3677 | leaf->flags |= LYS_SET_DFLT; |
| 3678 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3679 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3680 | /* checks */ |
| 3681 | if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) { |
| 3682 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3683 | "Invalid mandatory leaf with a default value."); |
| 3684 | return LY_EVALID; |
| 3685 | } |
| 3686 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3687 | done: |
| 3688 | return ret; |
| 3689 | } |
| 3690 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3691 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3692 | * @brief Compile parsed leaf-list node information. |
| 3693 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3694 | * @param[in] pnode Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3695 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3696 | * is enriched with the leaf-list-specific information. |
| 3697 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3698 | */ |
| 3699 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3700 | 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] | 3701 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3702 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3703 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3704 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3705 | LY_ERR ret = LY_SUCCESS; |
| 3706 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3707 | 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] | 3708 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3709 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3710 | ret = ly_set_add(&ctx->xpath, llist, 0, NULL); |
| 3711 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3712 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3713 | if (llist_p->units) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3714 | 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] | 3715 | llist->flags |= LYS_SET_UNITS; |
| 3716 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3717 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3718 | /* compile type */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3719 | 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] | 3720 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 6a044b2 | 2020-01-15 12:25:39 +0100 | [diff] [blame] | 3721 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3722 | /* store/update default values */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3723 | if (llist_p->dflts) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3724 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3725 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3726 | "Leaf-list default values are allowed only in YANG 1.1 modules."); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3727 | return LY_EVALID; |
| 3728 | } |
| 3729 | |
| 3730 | 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] | 3731 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3735 | if (llist->min) { |
| 3736 | llist->flags |= LYS_MAND_TRUE; |
| 3737 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3738 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3739 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3740 | /* checks */ |
| 3741 | if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) { |
| 3742 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3743 | "Invalid mandatory leaf-list with default value(s)."); |
| 3744 | return LY_EVALID; |
| 3745 | } |
| 3746 | |
| 3747 | if (llist->min > llist->max) { |
| 3748 | 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^] | 3749 | llist->min, llist->max); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3750 | return LY_EVALID; |
| 3751 | } |
| 3752 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3753 | done: |
| 3754 | return ret; |
| 3755 | } |
| 3756 | |
| 3757 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3758 | * @brief Compile information about list's uniques. |
| 3759 | * @param[in] ctx Compile context. |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3760 | * @param[in] uniques Sized array list of unique statements. |
| 3761 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3762 | * @return LY_ERR value. |
| 3763 | */ |
| 3764 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3765 | 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] | 3766 | { |
| 3767 | LY_ERR ret = LY_SUCCESS; |
| 3768 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3769 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3770 | const char *keystr, *delim; |
| 3771 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3772 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3773 | 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] | 3774 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3775 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3776 | LY_ARRAY_FOR(uniques, v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3777 | config = -1; |
| 3778 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3779 | keystr = uniques[v].str; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3780 | while (keystr) { |
| 3781 | delim = strpbrk(keystr, " \t\n"); |
| 3782 | if (delim) { |
| 3783 | len = delim - keystr; |
| 3784 | while (isspace(*delim)) { |
| 3785 | ++delim; |
| 3786 | } |
| 3787 | } else { |
| 3788 | len = strlen(keystr); |
| 3789 | } |
| 3790 | |
| 3791 | /* unique node must be present */ |
| 3792 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3793 | 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^] | 3794 | (const struct lysc_node **)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3795 | if (ret != LY_SUCCESS) { |
| 3796 | if (ret == LY_EDENIED) { |
| 3797 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3798 | "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.", |
| 3799 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3800 | } |
| 3801 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3802 | } else if (flags) { |
| 3803 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3804 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3805 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3806 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | /* all referenced leafs must be of the same config type */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3810 | 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] | 3811 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3812 | "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] | 3813 | return LY_EVALID; |
| 3814 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3815 | config = 1; |
| 3816 | } else { /* LYS_CONFIG_R */ |
| 3817 | config = 0; |
| 3818 | } |
| 3819 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3820 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3821 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3822 | if (parent->nodetype == LYS_LIST) { |
| 3823 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3824 | "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] | 3825 | return LY_EVALID; |
| 3826 | } |
| 3827 | } |
| 3828 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3829 | /* check status */ |
| 3830 | 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] | 3831 | (*key)->flags, (*key)->module, (*key)->name)); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3832 | |
| 3833 | /* mark leaf as unique */ |
| 3834 | (*key)->flags |= LYS_UNIQUE; |
| 3835 | |
| 3836 | /* next unique value in line */ |
| 3837 | keystr = delim; |
| 3838 | } |
| 3839 | /* next unique definition */ |
| 3840 | } |
| 3841 | |
| 3842 | return LY_SUCCESS; |
| 3843 | } |
| 3844 | |
| 3845 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3846 | * @brief Compile parsed list node information. |
| 3847 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3848 | * @param[in] pnode Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3849 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3850 | * is enriched with the list-specific information. |
| 3851 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3852 | */ |
| 3853 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3854 | 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] | 3855 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3856 | struct lysp_node_list *list_p = (struct lysp_node_list *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3857 | struct lysc_node_list *list = (struct lysc_node_list *)node; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3858 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3859 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3860 | size_t len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3861 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3862 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3863 | LY_ERR ret = LY_SUCCESS; |
| 3864 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3865 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3866 | if (list->min) { |
| 3867 | list->flags |= LYS_MAND_TRUE; |
| 3868 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3869 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3870 | |
| 3871 | LY_LIST_FOR(list_p->child, child_p) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3872 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3873 | } |
| 3874 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3875 | 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] | 3876 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3877 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3878 | LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL)); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3879 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3880 | |
| 3881 | /* keys */ |
| 3882 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3883 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3884 | return LY_EVALID; |
| 3885 | } |
| 3886 | |
| 3887 | /* find all the keys (must be direct children) */ |
| 3888 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3889 | if (!keystr) { |
| 3890 | /* keyless list */ |
| 3891 | list->flags |= LYS_KEYLESS; |
| 3892 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3893 | while (keystr) { |
| 3894 | delim = strpbrk(keystr, " \t\n"); |
| 3895 | if (delim) { |
| 3896 | len = delim - keystr; |
| 3897 | while (isspace(*delim)) { |
| 3898 | ++delim; |
| 3899 | } |
| 3900 | } else { |
| 3901 | len = strlen(keystr); |
| 3902 | } |
| 3903 | |
| 3904 | /* key node must be present */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3905 | 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] | 3906 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3907 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3908 | "The list's key \"%.*s\" not found.", len, keystr); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3909 | return LY_EVALID; |
| 3910 | } |
| 3911 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3912 | if (key->flags & LYS_KEY) { |
| 3913 | /* the node was already marked as a key */ |
| 3914 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3915 | "Duplicated key identifier \"%.*s\".", len, keystr); |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3916 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3917 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3918 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3919 | lysc_update_path(ctx, (struct lysc_node *)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3920 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3921 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3922 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3923 | return LY_EVALID; |
| 3924 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3925 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3926 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3927 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3928 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3929 | "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] | 3930 | return LY_EVALID; |
| 3931 | } |
| 3932 | } else { |
| 3933 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3934 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3935 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3936 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3937 | return LY_EVALID; |
| 3938 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3939 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3940 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3941 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3942 | return LY_EVALID; |
| 3943 | } |
| 3944 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3945 | |
| 3946 | /* check status */ |
| 3947 | 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^] | 3948 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3949 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3950 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3951 | if (key->dflt) { |
| 3952 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3953 | lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype); |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3954 | free(key->dflt); |
| 3955 | key->dflt = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3956 | } |
| 3957 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3958 | key->flags |= LYS_KEY; |
| 3959 | |
| 3960 | /* move it to the correct position */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 3961 | 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] | 3962 | /* fix links in closest previous siblings of the key */ |
| 3963 | if (key->next) { |
| 3964 | key->next->prev = key->prev; |
| 3965 | } else { |
| 3966 | /* last child */ |
| 3967 | list->child->prev = key->prev; |
| 3968 | } |
| 3969 | if (key->prev->next) { |
| 3970 | key->prev->next = key->next; |
| 3971 | } |
| 3972 | /* fix links in the key */ |
| 3973 | if (prev_key) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3974 | key->prev = (struct lysc_node *)prev_key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3975 | key->next = prev_key->next; |
| 3976 | } else { |
| 3977 | key->prev = list->child->prev; |
| 3978 | key->next = list->child; |
| 3979 | } |
| 3980 | /* fix links in closes future siblings of the key */ |
| 3981 | if (prev_key) { |
| 3982 | if (prev_key->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3983 | prev_key->next->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3984 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3985 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3986 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3987 | prev_key->next = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3988 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3989 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3990 | } |
| 3991 | /* fix links in parent */ |
| 3992 | if (!key->prev->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3993 | list->child = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3994 | } |
| 3995 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3996 | |
| 3997 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3998 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3999 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4000 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | /* uniques */ |
| 4004 | if (list_p->uniques) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4005 | 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] | 4006 | } |
| 4007 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4008 | COMPILE_OP_ARRAY_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 4009 | COMPILE_OP_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done); |
| 4010 | |
| 4011 | /* checks */ |
| 4012 | if (list->min > list->max) { |
| 4013 | 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^] | 4014 | list->min, list->max); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4015 | return LY_EVALID; |
| 4016 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4017 | |
| 4018 | done: |
| 4019 | return ret; |
| 4020 | } |
| 4021 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4022 | /** |
| 4023 | * @brief Do some checks and set the default choice's case. |
| 4024 | * |
| 4025 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 4026 | * |
| 4027 | * @param[in] ctx Compile context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4028 | * @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] | 4029 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 4030 | * @return LY_ERR value. |
| 4031 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4032 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4033 | 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] | 4034 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4035 | struct lysc_node *iter, *node = (struct lysc_node *)ch; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4036 | const struct lys_module *mod; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4037 | const char *prefix = NULL, *name; |
| 4038 | size_t prefix_len = 0; |
| 4039 | |
| 4040 | /* 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] | 4041 | name = strchr(dflt->str, ':'); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4042 | if (name) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4043 | prefix = dflt->str; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4044 | prefix_len = name - prefix; |
| 4045 | ++name; |
| 4046 | } else { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4047 | name = dflt->str; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4048 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4049 | if (prefix) { |
| 4050 | mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod); |
| 4051 | if (!mod) { |
| 4052 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4053 | "Default case prefix \"%.*s\" not found in imports of \"%s\".", prefix_len, prefix, dflt->mod->name); |
| 4054 | return LY_EVALID; |
| 4055 | } |
| 4056 | } else { |
| 4057 | mod = node->module; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4058 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4059 | |
| 4060 | 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] | 4061 | if (!ch->dflt) { |
| 4062 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4063 | "Default case \"%s\" not found.", dflt->str); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4064 | return LY_EVALID; |
| 4065 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4066 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4067 | /* no mandatory nodes directly under the default case */ |
| 4068 | LY_LIST_FOR(ch->dflt->child, iter) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4069 | if (iter->parent != (struct lysc_node *)ch->dflt) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4070 | break; |
| 4071 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4072 | if (iter->flags & LYS_MAND_TRUE) { |
| 4073 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4074 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4075 | return LY_EVALID; |
| 4076 | } |
| 4077 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4078 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4079 | if (ch->flags & LYS_MAND_TRUE) { |
| 4080 | 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] | 4081 | return LY_EVALID; |
| 4082 | } |
| 4083 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4084 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4085 | return LY_SUCCESS; |
| 4086 | } |
| 4087 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4088 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4089 | * @brief Compile choice children. |
| 4090 | * |
| 4091 | * @param[in] ctx Compile context |
| 4092 | * @param[in] child_p Parsed choice children nodes. |
| 4093 | * @param[in] node Compiled choice node to compile and add children to. |
| 4094 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4095 | */ |
| 4096 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4097 | lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node, |
| 4098 | struct ly_set *child_set) |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4099 | { |
| 4100 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 4101 | struct lysp_node *child_p_next = child_p->next; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4102 | struct lysp_node_case *cs_p; |
| 4103 | |
| 4104 | if (child_p->nodetype == LYS_CASE) { |
| 4105 | /* standard case under choice */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4106 | ret = lys_compile_node(ctx, child_p, node, 0, child_set); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4107 | } else { |
| 4108 | /* we need the implicit case first, so create a fake parsed case */ |
| 4109 | cs_p = calloc(1, sizeof *cs_p); |
| 4110 | cs_p->nodetype = LYS_CASE; |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 4111 | 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] | 4112 | cs_p->child = child_p; |
| 4113 | |
| 4114 | /* make the child the only case child */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4115 | child_p->next = NULL; |
| 4116 | |
| 4117 | /* compile it normally */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4118 | 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] | 4119 | |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 4120 | free_fake_node: |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4121 | /* free the fake parsed node and correct pointers back */ |
| 4122 | cs_p->child = NULL; |
| 4123 | lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p); |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 4124 | child_p->next = child_p_next; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4125 | } |
| 4126 | |
| 4127 | return ret; |
| 4128 | } |
| 4129 | |
| 4130 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4131 | * @brief Compile parsed choice node information. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4132 | * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4133 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4134 | * @param[in] pnode Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4135 | * @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] | 4136 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4137 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4138 | */ |
| 4139 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4140 | 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] | 4141 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4142 | struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4143 | struct lysc_node_choice *ch = (struct lysc_node_choice *)node; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4144 | struct lysp_node *child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4145 | LY_ERR ret = LY_SUCCESS; |
| 4146 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4147 | assert(node->nodetype == LYS_CHOICE); |
| 4148 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4149 | LY_LIST_FOR(ch_p->child, child_p) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4150 | 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] | 4151 | } |
| 4152 | |
| 4153 | /* default branch */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4154 | if (ch_p->dflt.str) { |
| 4155 | 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] | 4156 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4157 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4158 | return ret; |
| 4159 | } |
| 4160 | |
| 4161 | /** |
| 4162 | * @brief Compile parsed anydata or anyxml node information. |
| 4163 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4164 | * @param[in] pnode Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4165 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4166 | * is enriched with the any-specific information. |
| 4167 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4168 | */ |
| 4169 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4170 | 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] | 4171 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4172 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4173 | struct lysc_node_anydata *any = (struct lysc_node_anydata *)node; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4174 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4175 | LY_ERR ret = LY_SUCCESS; |
| 4176 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4177 | 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] | 4178 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4179 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4180 | ret = ly_set_add(&ctx->xpath, any, 0, NULL); |
| 4181 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4182 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4183 | |
| 4184 | if (any->flags & LYS_CONFIG_W) { |
Radek Krejci | 5c4ed7b | 2020-08-12 11:29:44 +0200 | [diff] [blame] | 4185 | 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^] | 4186 | 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] | 4187 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4188 | done: |
| 4189 | return ret; |
| 4190 | } |
| 4191 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4192 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4193 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4194 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4195 | * |
| 4196 | * @param[in] ctx Compile context |
| 4197 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4198 | * the choice itself is expected instead of a specific case node. |
| 4199 | * @param[in] node Schema node to connect into the list. |
| 4200 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4201 | * 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] | 4202 | */ |
| 4203 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4204 | 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] | 4205 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4206 | struct lysc_node **children, *anchor = NULL; |
| 4207 | int insert_after = 0; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4208 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4209 | node->parent = parent; |
| 4210 | |
| 4211 | if (parent) { |
| 4212 | if (parent->nodetype == LYS_CHOICE) { |
| 4213 | assert(node->nodetype == LYS_CASE); |
| 4214 | children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases; |
| 4215 | } else { |
| 4216 | children = lysc_node_children_p(parent, ctx->options); |
| 4217 | } |
| 4218 | assert(children); |
| 4219 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4220 | if (!(*children)) { |
| 4221 | /* first child */ |
| 4222 | *children = node; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4223 | } else if (node->flags & LYS_KEY) { |
| 4224 | /* special handling of adding keys */ |
| 4225 | assert(node->module == parent->module); |
| 4226 | anchor = *children; |
| 4227 | if (anchor->flags & LYS_KEY) { |
| 4228 | while ((anchor->flags & LYS_KEY) && anchor->next) { |
| 4229 | anchor = anchor->next; |
| 4230 | } |
| 4231 | /* insert after the last key */ |
| 4232 | insert_after = 1; |
| 4233 | } /* else insert before anchor (at the beginning) */ |
| 4234 | } else if ((*children)->prev->module == node->module) { |
| 4235 | /* last child is from the same module, keep the order and insert at the end */ |
| 4236 | anchor = (*children)->prev; |
| 4237 | insert_after = 1; |
| 4238 | } else if (parent->module == node->module) { |
| 4239 | /* adding module child after some augments were connected */ |
| 4240 | for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {} |
| 4241 | } else { |
| 4242 | /* some augments are already connected and we are connecting new ones, |
| 4243 | * keep module name order and insert the node into the children list */ |
| 4244 | anchor = *children; |
| 4245 | do { |
| 4246 | anchor = anchor->prev; |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4247 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4248 | /* check that we have not found the last augment node from our module or |
| 4249 | * the first augment node from a "smaller" module or |
| 4250 | * the first node from a local module */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4251 | if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) || |
| 4252 | (anchor->module == parent->module)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4253 | /* insert after */ |
| 4254 | insert_after = 1; |
| 4255 | break; |
| 4256 | } |
| 4257 | |
| 4258 | /* we have traversed all the nodes, insert before anchor (as the first node) */ |
| 4259 | } while (anchor->prev->next); |
| 4260 | } |
| 4261 | |
| 4262 | /* insert */ |
| 4263 | if (anchor) { |
| 4264 | if (insert_after) { |
| 4265 | node->next = anchor->next; |
| 4266 | node->prev = anchor; |
| 4267 | anchor->next = node; |
| 4268 | if (node->next) { |
| 4269 | /* middle node */ |
| 4270 | node->next->prev = node; |
| 4271 | } else { |
| 4272 | /* last node */ |
| 4273 | (*children)->prev = node; |
| 4274 | } |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4275 | } else { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4276 | node->next = anchor; |
| 4277 | node->prev = anchor->prev; |
| 4278 | anchor->prev = node; |
| 4279 | if (anchor == *children) { |
| 4280 | /* first node */ |
| 4281 | *children = node; |
| 4282 | } else { |
| 4283 | /* middle node */ |
| 4284 | node->prev->next = node; |
| 4285 | } |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4286 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4287 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4288 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4289 | /* check the name uniqueness (even for an only child, it may be in case) */ |
| 4290 | if (lys_compile_node_uniqness(ctx, parent, node->name, node)) { |
| 4291 | return LY_EEXIST; |
| 4292 | } |
| 4293 | } else { |
| 4294 | /* top-level element */ |
| 4295 | if (!ctx->mod->compiled->data) { |
| 4296 | ctx->mod->compiled->data = node; |
| 4297 | } else { |
| 4298 | /* insert at the end of the module's top-level nodes list */ |
| 4299 | ctx->mod->compiled->data->prev->next = node; |
| 4300 | node->prev = ctx->mod->compiled->data->prev; |
| 4301 | ctx->mod->compiled->data->prev = node; |
| 4302 | } |
| 4303 | |
| 4304 | /* check the name uniqueness on top-level */ |
| 4305 | if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) { |
| 4306 | return LY_EEXIST; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4307 | } |
| 4308 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4309 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4310 | return LY_SUCCESS; |
| 4311 | } |
| 4312 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4313 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4314 | * @brief Prepare the case structure in choice node for the new data node. |
| 4315 | * |
| 4316 | * 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 |
| 4317 | * created in the choice when the first child was processed. |
| 4318 | * |
| 4319 | * @param[in] ctx Compile context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4320 | * @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] | 4321 | * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4322 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4323 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4324 | * @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, |
| 4325 | * 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] | 4326 | */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4327 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4328 | 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] | 4329 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4330 | struct lysp_node *child_p; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4331 | struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4332 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4333 | if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4334 | /* we have to add an implicit case node into the parent choice */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4335 | } else if (pnode->nodetype == LYS_CASE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4336 | /* explicit parent case */ |
| 4337 | LY_LIST_FOR(cs_p->child, child_p) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4338 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4339 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4340 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4341 | LOGINT_RET(ctx->ctx); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4342 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4343 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4344 | return LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4345 | } |
| 4346 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4347 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4348 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4349 | * |
| 4350 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4351 | * the flag to such parents from a mandatory children. |
| 4352 | * |
| 4353 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4354 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4355 | * (mandatory children was removed). |
| 4356 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4357 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4358 | lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add) |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4359 | { |
| 4360 | struct lysc_node *iter; |
| 4361 | |
| 4362 | if (add) { /* set flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4363 | 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] | 4364 | parent = parent->parent) { |
| 4365 | parent->flags |= LYS_MAND_TRUE; |
| 4366 | } |
| 4367 | } else { /* unset flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4368 | 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] | 4369 | 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] | 4370 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4371 | /* there is another mandatory node */ |
| 4372 | return; |
| 4373 | } |
| 4374 | } |
| 4375 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4376 | parent->flags &= ~LYS_MAND_TRUE; |
| 4377 | } |
| 4378 | } |
| 4379 | } |
| 4380 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4381 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4382 | * @brief Compile the parsed augment connecting it into its target. |
| 4383 | * |
| 4384 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4385 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4386 | * are already implemented and compiled. |
| 4387 | * |
| 4388 | * @param[in] ctx Compile context. |
| 4389 | * @param[in] aug_p Parsed augment to compile. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4390 | * @param[in] target Target node of the augment. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4391 | * @return LY_SUCCESS on success. |
| 4392 | * @return LY_EVALID on failure. |
| 4393 | */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4394 | static LY_ERR |
| 4395 | 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] | 4396 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4397 | LY_ERR ret = LY_SUCCESS; |
| 4398 | struct lysp_node *pnode; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4399 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4400 | struct lysc_when **when, *when_shared; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4401 | ly_bool allow_mandatory = 0; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4402 | LY_ARRAY_COUNT_TYPE u; |
| 4403 | struct ly_set child_set = {0}; |
| 4404 | uint32_t i; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4405 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4406 | if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) { |
| 4407 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4408 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4409 | aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4410 | ret = LY_EVALID; |
| 4411 | goto cleanup; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | /* check for mandatory nodes |
| 4415 | * - new cases augmenting some choice can have mandatory nodes |
| 4416 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4417 | */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4418 | if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->mod == target->module)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4419 | allow_mandatory = 1; |
| 4420 | } |
| 4421 | |
| 4422 | when_shared = NULL; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4423 | LY_LIST_FOR(aug_p->child, pnode) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4424 | /* 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^] | 4425 | if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) || |
| 4426 | ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) || |
| 4427 | ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4428 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4429 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4430 | lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4431 | ret = LY_EVALID; |
| 4432 | goto cleanup; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | /* compile the children */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4436 | if (target->nodetype == LYS_CHOICE) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4437 | 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] | 4438 | } else { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4439 | 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] | 4440 | } |
| 4441 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4442 | /* since the augment node is not present in the compiled tree, we need to pass some of its |
| 4443 | * statements to all its children */ |
| 4444 | for (i = 0; i < child_set.count; ++i) { |
| 4445 | node = child_set.snodes[i]; |
| 4446 | if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) { |
| 4447 | node->flags &= ~LYS_MAND_TRUE; |
| 4448 | lys_compile_mandatory_parents(target, 0); |
| 4449 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4450 | "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] | 4451 | ret = LY_EVALID; |
| 4452 | goto cleanup; |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4453 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4454 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4455 | /* pass augment's when to all the children TODO this way even action and notif should have "when" (code below) */ |
| 4456 | if (aug_p->when) { |
| 4457 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, cleanup); |
| 4458 | if (!when_shared) { |
| 4459 | LY_CHECK_GOTO(ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4460 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4461 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4462 | /* do not check "when" semantics in a grouping */ |
| 4463 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->xpath, node, 0, NULL), cleanup); |
| 4464 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4465 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4466 | when_shared = *when; |
| 4467 | } else { |
| 4468 | ++when_shared->refcount; |
| 4469 | (*when) = when_shared; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4470 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4471 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4472 | /* in this case check "when" again for all children because of dummy node check */ |
| 4473 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->xpath, node, 0, NULL), cleanup); |
| 4474 | } |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4475 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4476 | } |
| 4477 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4478 | ly_set_erase(&child_set, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4479 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4480 | |
| 4481 | switch (target->nodetype) { |
| 4482 | case LYS_CONTAINER: |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4483 | COMPILE_OP_ARRAY_GOTO(ctx, aug_p->actions, ((struct lysc_node_container *)target)->actions, target, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4484 | u, lys_compile_action, 0, ret, cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4485 | COMPILE_OP_ARRAY_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container *)target)->notifs, target, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4486 | u, lys_compile_notif, 0, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4487 | break; |
| 4488 | case LYS_LIST: |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4489 | COMPILE_OP_ARRAY_GOTO(ctx, aug_p->actions, ((struct lysc_node_list *)target)->actions, target, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4490 | u, lys_compile_action, 0, ret, cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4491 | COMPILE_OP_ARRAY_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list *)target)->notifs, target, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4492 | u, lys_compile_notif, 0, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4493 | break; |
| 4494 | default: |
| 4495 | if (aug_p->actions) { |
| 4496 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4497 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4498 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4499 | ret = LY_EVALID; |
| 4500 | goto cleanup; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4501 | } |
| 4502 | if (aug_p->notifs) { |
| 4503 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4504 | "Invalid augment of %s node which is not allowed to contain notification node \"%s\".", |
| 4505 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4506 | ret = LY_EVALID; |
| 4507 | goto cleanup; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4508 | } |
| 4509 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4510 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4511 | cleanup: |
| 4512 | ly_set_erase(&child_set, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4513 | return ret; |
| 4514 | } |
| 4515 | |
| 4516 | /** |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4517 | * @brief Find grouping for a uses. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4518 | * |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4519 | * @param[in] ctx Compile context. |
| 4520 | * @param[in] uses_p Parsed uses node. |
| 4521 | * @param[out] gpr_p Found grouping on success. |
| 4522 | * @param[out] grp_mod Module of @p grp_p on success. |
| 4523 | * @return LY_ERR value. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4524 | */ |
| 4525 | static LY_ERR |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4526 | lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p, |
| 4527 | struct lys_module **grp_mod) |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4528 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4529 | struct lysp_node *pnode; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4530 | struct lysp_grp *grp; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4531 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4532 | ly_bool found = 0; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4533 | const char *id, *name, *prefix; |
| 4534 | size_t prefix_len, name_len; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4535 | struct lys_module *mod; |
| 4536 | |
| 4537 | *grp_p = NULL; |
| 4538 | *grp_mod = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4539 | |
| 4540 | /* search for the grouping definition */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4541 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4542 | 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] | 4543 | if (prefix) { |
| 4544 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4545 | if (!mod) { |
| 4546 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4547 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4548 | return LY_EVALID; |
| 4549 | } |
| 4550 | } else { |
| 4551 | mod = ctx->mod_def; |
| 4552 | } |
| 4553 | if (mod == ctx->mod_def) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4554 | for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) { |
| 4555 | grp = (struct lysp_grp *)lysp_node_groupings(pnode); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4556 | LY_ARRAY_FOR(grp, u) { |
| 4557 | if (!strcmp(grp[u].name, name)) { |
| 4558 | grp = &grp[u]; |
| 4559 | found = 1; |
| 4560 | break; |
| 4561 | } |
| 4562 | } |
| 4563 | } |
| 4564 | } |
| 4565 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4566 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4567 | grp = mod->parsed->groupings; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4568 | LY_ARRAY_FOR(grp, u) { |
| 4569 | if (!strcmp(grp[u].name, name)) { |
| 4570 | grp = &grp[u]; |
| 4571 | found = 1; |
| 4572 | break; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4573 | } |
| 4574 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4575 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4576 | /* ... and all the submodules */ |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4577 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4578 | grp = mod->parsed->includes[u].submodule->groupings; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4579 | LY_ARRAY_FOR(grp, v) { |
| 4580 | if (!strcmp(grp[v].name, name)) { |
| 4581 | grp = &grp[v]; |
| 4582 | found = 1; |
| 4583 | break; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4584 | } |
| 4585 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4586 | if (found) { |
| 4587 | break; |
| 4588 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4589 | } |
| 4590 | } |
| 4591 | } |
| 4592 | if (!found) { |
| 4593 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4594 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4595 | return LY_EVALID; |
| 4596 | } |
| 4597 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4598 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4599 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4600 | grp->flags |= LYS_USED_GRP; |
| 4601 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4602 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4603 | *grp_p = grp; |
| 4604 | *grp_mod = mod; |
| 4605 | return LY_SUCCESS; |
| 4606 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4607 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4608 | static const struct lys_module *lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, |
| 4609 | size_t nametest_len, const struct lys_module *local_mod, const char **name, size_t *name_len); |
| 4610 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4611 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4612 | lys_nodeid_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct lys_module **target_mod, |
| 4613 | struct lyxp_expr **expr) |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4614 | { |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4615 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4616 | struct lyxp_expr *e = NULL; |
| 4617 | struct lys_module *tmod = NULL, *mod; |
| 4618 | const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid"; |
| 4619 | uint32_t i; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4620 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4621 | /* parse */ |
| 4622 | ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e); |
| 4623 | if (ret) { |
| 4624 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.", |
| 4625 | nodeid_type, nodeid); |
| 4626 | ret = LY_EVALID; |
| 4627 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4628 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4629 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4630 | if (abs) { |
| 4631 | /* absolute schema nodeid */ |
| 4632 | i = 0; |
| 4633 | } else { |
| 4634 | /* descendant schema nodeid */ |
| 4635 | if (e->tokens[0] != LYXP_TOKEN_NAMETEST) { |
| 4636 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".", |
| 4637 | 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] | 4638 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4639 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4640 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4641 | i = 1; |
| 4642 | } |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4643 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4644 | /* check all the tokens */ |
| 4645 | for ( ; i < e->used; i += 2) { |
| 4646 | if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) { |
| 4647 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".", |
| 4648 | nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]); |
| 4649 | ret = LY_EVALID; |
| 4650 | goto cleanup; |
| 4651 | } else if (e->used == i + 1) { |
| 4652 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4653 | "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr); |
| 4654 | ret = LY_EVALID; |
| 4655 | goto cleanup; |
| 4656 | } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) { |
| 4657 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".", |
| 4658 | nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]); |
| 4659 | ret = LY_EVALID; |
| 4660 | goto cleanup; |
| 4661 | } else if (abs) { |
| 4662 | mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1], |
| 4663 | e->tok_len[i + 1], ctx->mod_def, NULL, NULL); |
| 4664 | LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup); |
| 4665 | |
| 4666 | /* only keep the first module */ |
| 4667 | if (!tmod) { |
| 4668 | tmod = mod; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4669 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4670 | |
| 4671 | /* all the modules must be implemented */ |
| 4672 | if (!mod->implemented) { |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 4673 | ret = lys_set_implemented(mod); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4674 | LY_CHECK_GOTO(ret, cleanup); |
| 4675 | } |
| 4676 | } |
| 4677 | } |
| 4678 | |
| 4679 | cleanup: |
| 4680 | if (ret || !expr) { |
| 4681 | lyxp_expr_free(ctx->ctx, e); |
| 4682 | e = NULL; |
| 4683 | } |
| 4684 | if (expr) { |
| 4685 | *expr = ret ? NULL : e; |
| 4686 | } |
| 4687 | if (target_mod) { |
| 4688 | *target_mod = ret ? NULL : tmod; |
| 4689 | } |
| 4690 | return ret; |
| 4691 | } |
| 4692 | |
| 4693 | /** |
| 4694 | * @brief Check whether 2 schema nodeids match. |
| 4695 | * |
| 4696 | * @param[in] ctx libyang context. |
| 4697 | * @param[in] exp1 First schema nodeid. |
| 4698 | * @param[in] exp1_mod Module of @p exp1 nodes without any prefix. |
| 4699 | * @param[in] exp2 Second schema nodeid. |
| 4700 | * @param[in] exp2_mod Module of @p exp2 nodes without any prefix. |
| 4701 | * @return Whether the schema nodeids match or not. |
| 4702 | */ |
| 4703 | static ly_bool |
| 4704 | lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lys_module *exp1_mod, |
| 4705 | const struct lyxp_expr *exp2, const struct lys_module *exp2_mod) |
| 4706 | { |
| 4707 | uint32_t i; |
| 4708 | const struct lys_module *mod1, *mod2; |
| 4709 | const char *name1, *name2; |
| 4710 | size_t name1_len, name2_len; |
| 4711 | |
| 4712 | if (exp1->used != exp2->used) { |
| 4713 | return 0; |
| 4714 | } |
| 4715 | |
| 4716 | for (i = 0; i < exp1->used; ++i) { |
| 4717 | assert(exp1->tokens[i] == exp2->tokens[i]); |
| 4718 | |
| 4719 | if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) { |
| 4720 | /* check modules of all the nodes in the node ID */ |
| 4721 | mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_mod, |
| 4722 | &name1, &name1_len); |
| 4723 | assert(mod1); |
| 4724 | mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_mod, |
| 4725 | &name2, &name2_len); |
| 4726 | assert(mod2); |
| 4727 | |
| 4728 | /* compare modules */ |
| 4729 | if (mod1 != mod2) { |
| 4730 | return 0; |
| 4731 | } |
| 4732 | |
| 4733 | /* compare names */ |
| 4734 | if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) { |
| 4735 | return 0; |
| 4736 | } |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | return 1; |
| 4741 | } |
| 4742 | |
| 4743 | /** |
| 4744 | * @brief Prepare any uses augments and refines in the context to be applied during uses descendant node compilation. |
| 4745 | * |
| 4746 | * @param[in] ctx Compile context. |
| 4747 | * @param[in] uses_p Parsed uses structure with augments and refines. |
| 4748 | * @param[in] ctx_node Context node of @p uses_p meaning its first data definiition parent. |
| 4749 | * @return LY_ERR value. |
| 4750 | */ |
| 4751 | static LY_ERR |
| 4752 | lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node) |
| 4753 | { |
| 4754 | LY_ERR ret = LY_SUCCESS; |
| 4755 | LY_ARRAY_COUNT_TYPE u; |
| 4756 | struct lyxp_expr *exp = NULL; |
| 4757 | struct lysc_augment *aug; |
| 4758 | struct lysc_refine *rfn; |
| 4759 | struct lysp_refine **new_rfn; |
| 4760 | uint32_t i; |
| 4761 | |
| 4762 | LY_ARRAY_FOR(uses_p->augments, u) { |
| 4763 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4764 | lysc_update_path(ctx, NULL, uses_p->augments[u].nodeid); |
| 4765 | |
| 4766 | /* parse the nodeid */ |
| 4767 | LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, uses_p->augments[u].nodeid, 0, NULL, &exp), cleanup); |
| 4768 | |
| 4769 | /* allocate new compiled augment and store it in the set */ |
| 4770 | aug = calloc(1, sizeof *aug); |
| 4771 | LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 4772 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, LY_SET_OPT_USEASLIST, NULL), cleanup); |
| 4773 | |
| 4774 | aug->nodeid = exp; |
| 4775 | exp = NULL; |
| 4776 | aug->nodeid_ctx_node = ctx_node; |
| 4777 | aug->aug_p = &uses_p->augments[u]; |
| 4778 | |
| 4779 | lysc_update_path(ctx, NULL, NULL); |
| 4780 | lysc_update_path(ctx, NULL, NULL); |
| 4781 | } |
| 4782 | |
| 4783 | LY_ARRAY_FOR(uses_p->refines, u) { |
| 4784 | lysc_update_path(ctx, NULL, "{refine}"); |
| 4785 | lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid); |
| 4786 | |
| 4787 | /* parse the nodeid */ |
| 4788 | LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, uses_p->refines[u].nodeid, 0, NULL, &exp), cleanup); |
| 4789 | |
| 4790 | /* try to find the node in already compiled refines */ |
| 4791 | rfn = NULL; |
| 4792 | for (i = 0; i < ctx->uses_rfns.count; ++i) { |
| 4793 | if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->mod_def, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid, |
| 4794 | ctx->mod_def)) { |
| 4795 | rfn = ctx->uses_rfns.objs[i]; |
| 4796 | break; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4797 | } |
| 4798 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4799 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4800 | if (!rfn) { |
| 4801 | /* allocate new compiled refine */ |
| 4802 | rfn = calloc(1, sizeof *rfn); |
| 4803 | LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 4804 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, LY_SET_OPT_USEASLIST, NULL), cleanup); |
| 4805 | |
| 4806 | rfn->nodeid = exp; |
| 4807 | exp = NULL; |
| 4808 | rfn->nodeid_ctx_node = ctx_node; |
| 4809 | } else { |
| 4810 | /* just free exp */ |
| 4811 | lyxp_expr_free(ctx->ctx, exp); |
| 4812 | exp = NULL; |
| 4813 | } |
| 4814 | |
| 4815 | /* add new parsed refine structure */ |
| 4816 | LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup); |
| 4817 | *new_rfn = &uses_p->refines[u]; |
| 4818 | |
| 4819 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4820 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4821 | } |
| 4822 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4823 | cleanup: |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4824 | lyxp_expr_free(ctx->ctx, exp); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4825 | return ret; |
| 4826 | } |
| 4827 | |
| 4828 | /** |
| 4829 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4830 | * If present, also apply uses's modificators. |
| 4831 | * |
| 4832 | * @param[in] ctx Compile context |
| 4833 | * @param[in] uses_p Parsed uses schema node. |
| 4834 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4835 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4836 | * the compile context. |
| 4837 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4838 | */ |
| 4839 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4840 | 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] | 4841 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4842 | struct lysp_node *pnode; |
| 4843 | struct lysc_node *child; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4844 | struct lysp_grp *grp = NULL; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4845 | uint32_t i, grp_stack_count; |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 4846 | struct lys_module *grp_mod, *mod_old = ctx->mod_def; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4847 | LY_ERR ret = LY_SUCCESS; |
| 4848 | struct lysc_when **when, *when_shared; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4849 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4850 | struct lysc_notif **notifs = NULL; |
| 4851 | struct lysc_action **actions = NULL; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4852 | struct ly_set uses_child_set = {0}; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4853 | |
| 4854 | /* find the referenced grouping */ |
| 4855 | LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod)); |
| 4856 | |
| 4857 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4858 | grp_stack_count = ctx->groupings.count; |
| 4859 | LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL)); |
| 4860 | if (grp_stack_count == ctx->groupings.count) { |
| 4861 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4862 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4863 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4864 | return LY_EVALID; |
| 4865 | } |
| 4866 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4867 | /* check status */ |
| 4868 | ret = lysc_check_status(ctx, uses_p->flags, ctx->mod_def, uses_p->name, grp->flags, grp_mod, grp->name); |
| 4869 | LY_CHECK_GOTO(ret, cleanup); |
| 4870 | |
| 4871 | /* compile any augments and refines so they can be applied during the grouping nodes compilation */ |
| 4872 | ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent); |
| 4873 | LY_CHECK_GOTO(ret, cleanup); |
| 4874 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4875 | /* switch context's mod_def */ |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4876 | ctx->mod_def = grp_mod; |
| 4877 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4878 | /* compile data nodes */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4879 | LY_LIST_FOR(grp->data, pnode) { |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4880 | /* 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] | 4881 | 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] | 4882 | LY_CHECK_GOTO(ret, cleanup); |
| 4883 | } |
| 4884 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4885 | if (child_set) { |
| 4886 | /* add these children to our compiled child_set as well since uses is a schema-only node */ |
| 4887 | 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] | 4888 | } |
| 4889 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4890 | if (uses_p->when) { |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4891 | /* pass uses's when to all the data children, actions and notifications are ignored */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4892 | when_shared = NULL; |
| 4893 | for (i = 0; i < uses_child_set.count; ++i) { |
| 4894 | child = uses_child_set.snodes[i]; |
| 4895 | |
| 4896 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4897 | if (!when_shared) { |
| 4898 | ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when); |
| 4899 | LY_CHECK_GOTO(ret, cleanup); |
| 4900 | |
| 4901 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4902 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4903 | ret = ly_set_add(&ctx->xpath, child, 0, NULL); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4904 | LY_CHECK_GOTO(ret, cleanup); |
| 4905 | } |
| 4906 | |
| 4907 | when_shared = *when; |
| 4908 | } else { |
| 4909 | ++when_shared->refcount; |
| 4910 | (*when) = when_shared; |
| 4911 | |
| 4912 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4913 | /* in this case check "when" again for all children because of dummy node check */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4914 | ret = ly_set_add(&ctx->xpath, child, 0, NULL); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4915 | LY_CHECK_GOTO(ret, cleanup); |
| 4916 | } |
| 4917 | } |
| 4918 | } |
| 4919 | } |
| 4920 | |
| 4921 | /* compile actions */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4922 | if (grp->actions) { |
| 4923 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4924 | if (!actions) { |
| 4925 | 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^] | 4926 | grp->actions[0].name, lys_nodetype2str(grp->actions[0].nodetype), parent->name, |
| 4927 | lys_nodetype2str(parent->nodetype)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4928 | ret = LY_EVALID; |
| 4929 | goto cleanup; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4930 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4931 | COMPILE_OP_ARRAY_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | /* compile notifications */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4935 | if (grp->notifs) { |
| 4936 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4937 | if (!notifs) { |
| 4938 | 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^] | 4939 | grp->notifs[0].name, lys_nodetype2str(grp->notifs[0].nodetype), parent->name, |
| 4940 | lys_nodetype2str(parent->nodetype)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4941 | ret = LY_EVALID; |
| 4942 | goto cleanup; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4943 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4944 | COMPILE_OP_ARRAY_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup); |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4945 | } |
| 4946 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4947 | /* check that all augments were applied */ |
| 4948 | for (i = 0; i < ctx->uses_augs.count; ++i) { |
| 4949 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4950 | "Augment target node \"%s\" in grouping \"%s\" was not found.", |
Michal Vasko | 891d753 | 2020-10-07 09:41:38 +0200 | [diff] [blame] | 4951 | ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4952 | ret = LY_ENOTFOUND; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4953 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4954 | LY_CHECK_GOTO(ret, cleanup); |
| 4955 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4956 | /* check that all refines were applied */ |
| 4957 | for (i = 0; i < ctx->uses_rfns.count; ++i) { |
| 4958 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 4959 | "Refine(s) target node \"%s\" in grouping \"%s\" was not found.", |
Michal Vasko | 891d753 | 2020-10-07 09:41:38 +0200 | [diff] [blame] | 4960 | ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4961 | ret = LY_ENOTFOUND; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4962 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4963 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4964 | |
| 4965 | cleanup: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4966 | /* reload previous context's mod_def */ |
| 4967 | ctx->mod_def = mod_old; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4968 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4969 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 4970 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 4971 | assert(ctx->groupings.count == grp_stack_count); |
| 4972 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 4973 | ly_set_erase(&uses_child_set, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4974 | return ret; |
| 4975 | } |
| 4976 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4977 | static int |
| 4978 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 4979 | { |
| 4980 | struct lysp_node *iter; |
| 4981 | int len = 0; |
| 4982 | |
| 4983 | *path = NULL; |
| 4984 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 4985 | char *s = *path; |
| 4986 | char *id; |
| 4987 | |
| 4988 | switch (iter->nodetype) { |
| 4989 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 4990 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4991 | break; |
| 4992 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 4993 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4994 | break; |
| 4995 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 4996 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4997 | break; |
| 4998 | default: |
| 4999 | id = strdup(iter->name); |
| 5000 | break; |
| 5001 | } |
| 5002 | |
| 5003 | if (!iter->parent) { |
| 5004 | /* print prefix */ |
| 5005 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5006 | } else { |
| 5007 | /* prefix is the same as in parent */ |
| 5008 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5009 | } |
| 5010 | free(s); |
| 5011 | free(id); |
| 5012 | } |
| 5013 | |
| 5014 | if (len < 0) { |
| 5015 | free(*path); |
| 5016 | *path = NULL; |
| 5017 | } else if (len == 0) { |
| 5018 | *path = strdup("/"); |
| 5019 | len = 1; |
| 5020 | } |
| 5021 | return len; |
| 5022 | } |
| 5023 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5024 | /** |
| 5025 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5026 | * |
| 5027 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5028 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5029 | */ |
| 5030 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5031 | 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] | 5032 | { |
| 5033 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5034 | char *path; |
| 5035 | int len; |
| 5036 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5037 | struct lysp_node_uses fake_uses = { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5038 | .parent = pnode, |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5039 | .nodetype = LYS_USES, |
| 5040 | .flags = 0, .next = NULL, |
| 5041 | .name = grp->name, |
| 5042 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5043 | .refines = NULL, .augments = NULL |
| 5044 | }; |
| 5045 | struct lysc_node_container fake_container = { |
| 5046 | .nodetype = LYS_CONTAINER, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5047 | .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5048 | .module = ctx->mod, |
| 5049 | .sp = NULL, .parent = NULL, .next = NULL, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5050 | .prev = (struct lysc_node *)&fake_container, |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5051 | .name = "fake", |
| 5052 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5053 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5054 | }; |
| 5055 | |
| 5056 | if (grp->parent) { |
| 5057 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5058 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5059 | |
| 5060 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5061 | if (len < 0) { |
| 5062 | LOGMEM(ctx->ctx); |
| 5063 | return LY_EMEM; |
| 5064 | } |
| 5065 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5066 | ctx->path_len = (uint32_t)len; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5067 | free(path); |
| 5068 | |
| 5069 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5070 | lysc_update_path(ctx, NULL, grp->name); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5071 | 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] | 5072 | lysc_update_path(ctx, NULL, NULL); |
| 5073 | lysc_update_path(ctx, NULL, NULL); |
| 5074 | |
| 5075 | ctx->path_len = 1; |
| 5076 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5077 | |
| 5078 | /* cleanup */ |
| 5079 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5080 | |
| 5081 | return ret; |
| 5082 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5083 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5084 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5085 | * @brief Set config flags for a node. |
| 5086 | * |
| 5087 | * @param[in] ctx Compile context. |
| 5088 | * @param[in] node Compiled node config to set. |
| 5089 | * @param[in] parent Parent of @p node. |
| 5090 | * @return LY_ERR value. |
| 5091 | */ |
| 5092 | static LY_ERR |
| 5093 | lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent) |
| 5094 | { |
| 5095 | if (node->nodetype == LYS_CASE) { |
| 5096 | /* case never has any config */ |
| 5097 | assert(!(node->flags & LYS_CONFIG_MASK)); |
| 5098 | return LY_SUCCESS; |
| 5099 | } |
| 5100 | |
| 5101 | /* adjust parent to always get the ancestor with config */ |
| 5102 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 5103 | parent = parent->parent; |
| 5104 | assert(parent); |
| 5105 | } |
| 5106 | |
| 5107 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
| 5108 | /* ignore config statements inside RPC/action data */ |
| 5109 | node->flags &= ~LYS_CONFIG_MASK; |
| 5110 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5111 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
| 5112 | /* ignore config statements inside Notification data */ |
| 5113 | node->flags &= ~LYS_CONFIG_MASK; |
| 5114 | node->flags |= LYS_CONFIG_R; |
| 5115 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
| 5116 | /* config not explicitely set, inherit it from parent */ |
| 5117 | if (parent) { |
| 5118 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5119 | } else { |
| 5120 | /* default is config true */ |
| 5121 | node->flags |= LYS_CONFIG_W; |
| 5122 | } |
| 5123 | } else { |
| 5124 | /* config set explicitely */ |
| 5125 | node->flags |= LYS_SET_CONFIG; |
| 5126 | } |
| 5127 | |
| 5128 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5129 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 5130 | "Configuration node cannot be child of any state data node."); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5131 | return LY_EVALID; |
| 5132 | } |
| 5133 | |
| 5134 | return LY_SUCCESS; |
| 5135 | } |
| 5136 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5137 | static LY_ERR |
| 5138 | lysp_ext_dup(const struct ly_ctx *ctx, struct lysp_ext_instance *ext, const struct lysp_ext_instance *orig_ext) |
| 5139 | { |
| 5140 | LY_ERR ret = LY_SUCCESS; |
| 5141 | |
| 5142 | *ext = *orig_ext; |
| 5143 | DUP_STRING(ctx, orig_ext->name, ext->name, ret); |
| 5144 | DUP_STRING(ctx, orig_ext->argument, ext->argument, ret); |
| 5145 | |
| 5146 | return ret; |
| 5147 | } |
| 5148 | |
| 5149 | static LY_ERR |
| 5150 | lysp_restr_dup(const struct ly_ctx *ctx, struct lysp_restr *restr, const struct lysp_restr *orig_restr) |
| 5151 | { |
| 5152 | LY_ERR ret = LY_SUCCESS; |
| 5153 | |
| 5154 | if (orig_restr) { |
| 5155 | DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret); |
| 5156 | restr->arg.mod = orig_restr->arg.mod; |
| 5157 | DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret); |
| 5158 | DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret); |
| 5159 | DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret); |
| 5160 | DUP_STRING(ctx, orig_restr->ref, restr->ref, ret); |
| 5161 | DUP_ARRAY(ctx, orig_restr->exts, restr->exts, lysp_ext_dup); |
| 5162 | } |
| 5163 | |
| 5164 | return ret; |
| 5165 | } |
| 5166 | |
| 5167 | static LY_ERR |
| 5168 | lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str) |
| 5169 | { |
| 5170 | LY_ERR ret = LY_SUCCESS; |
| 5171 | |
| 5172 | DUP_STRING(ctx, *orig_str, *str, ret); |
| 5173 | |
| 5174 | return ret; |
| 5175 | } |
| 5176 | |
| 5177 | static LY_ERR |
| 5178 | lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname) |
| 5179 | { |
| 5180 | LY_ERR ret = LY_SUCCESS; |
| 5181 | |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 5182 | if (!orig_qname->str) { |
| 5183 | return LY_SUCCESS; |
| 5184 | } |
| 5185 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5186 | DUP_STRING(ctx, orig_qname->str, qname->str, ret); |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 5187 | assert(orig_qname->mod); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5188 | qname->mod = orig_qname->mod; |
| 5189 | |
| 5190 | return ret; |
| 5191 | } |
| 5192 | |
| 5193 | static LY_ERR |
| 5194 | lysp_type_enum_dup(const struct ly_ctx *ctx, struct lysp_type_enum *enm, const struct lysp_type_enum *orig_enm) |
| 5195 | { |
| 5196 | LY_ERR ret = LY_SUCCESS; |
| 5197 | |
| 5198 | DUP_STRING(ctx, orig_enm->name, enm->name, ret); |
| 5199 | DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret); |
| 5200 | DUP_STRING(ctx, orig_enm->ref, enm->ref, ret); |
| 5201 | enm->value = orig_enm->value; |
| 5202 | DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup); |
| 5203 | DUP_ARRAY(ctx, orig_enm->exts, enm->exts, lysp_ext_dup); |
| 5204 | enm->flags = orig_enm->flags; |
| 5205 | |
| 5206 | return ret; |
| 5207 | } |
| 5208 | |
| 5209 | static LY_ERR |
| 5210 | lysp_type_dup(const struct ly_ctx *ctx, struct lysp_type *type, const struct lysp_type *orig_type) |
| 5211 | { |
| 5212 | LY_ERR ret = LY_SUCCESS; |
| 5213 | |
| 5214 | DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done); |
| 5215 | |
| 5216 | if (orig_type->range) { |
| 5217 | type->range = calloc(1, sizeof *type->range); |
| 5218 | LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM); |
| 5219 | LY_CHECK_RET(lysp_restr_dup(ctx, type->range, orig_type->range)); |
| 5220 | } |
| 5221 | |
| 5222 | if (orig_type->length) { |
| 5223 | type->length = calloc(1, sizeof *type->length); |
| 5224 | LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM); |
| 5225 | LY_CHECK_RET(lysp_restr_dup(ctx, type->length, orig_type->length)); |
| 5226 | } |
| 5227 | |
| 5228 | DUP_ARRAY(ctx, orig_type->patterns, type->patterns, lysp_restr_dup); |
| 5229 | DUP_ARRAY(ctx, orig_type->enums, type->enums, lysp_type_enum_dup); |
| 5230 | DUP_ARRAY(ctx, orig_type->bits, type->bits, lysp_type_enum_dup); |
| 5231 | LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, &type->path), done); |
| 5232 | DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup); |
| 5233 | DUP_ARRAY(ctx, orig_type->types, type->types, lysp_type_dup); |
| 5234 | DUP_ARRAY(ctx, orig_type->exts, type->exts, lysp_ext_dup); |
| 5235 | |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 5236 | type->mod = orig_type->mod; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5237 | type->compiled = orig_type->compiled; |
| 5238 | |
| 5239 | type->fraction_digits = orig_type->fraction_digits; |
| 5240 | type->require_instance = orig_type->require_instance; |
| 5241 | type->flags = orig_type->flags; |
| 5242 | |
| 5243 | done: |
| 5244 | return ret; |
| 5245 | } |
| 5246 | |
| 5247 | static LY_ERR |
| 5248 | lysp_when_dup(const struct ly_ctx *ctx, struct lysp_when *when, const struct lysp_when *orig_when) |
| 5249 | { |
| 5250 | LY_ERR ret = LY_SUCCESS; |
| 5251 | |
| 5252 | DUP_STRING(ctx, orig_when->cond, when->cond, ret); |
| 5253 | DUP_STRING(ctx, orig_when->dsc, when->dsc, ret); |
| 5254 | DUP_STRING(ctx, orig_when->ref, when->ref, ret); |
| 5255 | DUP_ARRAY(ctx, orig_when->exts, when->exts, lysp_ext_dup); |
| 5256 | |
| 5257 | return ret; |
| 5258 | } |
| 5259 | |
| 5260 | static LY_ERR |
| 5261 | lysp_node_common_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig) |
| 5262 | { |
| 5263 | LY_ERR ret = LY_SUCCESS; |
| 5264 | |
| 5265 | node->parent = NULL; |
| 5266 | node->nodetype = orig->nodetype; |
| 5267 | node->flags = orig->flags; |
| 5268 | node->next = NULL; |
| 5269 | DUP_STRING(ctx, orig->name, node->name, ret); |
| 5270 | DUP_STRING(ctx, orig->dsc, node->dsc, ret); |
| 5271 | DUP_STRING(ctx, orig->ref, node->ref, ret); |
| 5272 | |
| 5273 | if (orig->when) { |
| 5274 | node->when = calloc(1, sizeof *node->when); |
| 5275 | LY_CHECK_ERR_RET(!node->when, LOGMEM(ctx), LY_EMEM); |
| 5276 | LY_CHECK_RET(lysp_when_dup(ctx, node->when, orig->when)); |
| 5277 | } |
| 5278 | |
| 5279 | DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup); |
| 5280 | DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup); |
| 5281 | |
| 5282 | return ret; |
| 5283 | } |
| 5284 | |
| 5285 | static LY_ERR |
| 5286 | lysp_node_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig) |
| 5287 | { |
| 5288 | LY_ERR ret = LY_SUCCESS; |
| 5289 | struct lysp_node_container *cont; |
| 5290 | const struct lysp_node_container *orig_cont; |
| 5291 | struct lysp_node_leaf *leaf; |
| 5292 | const struct lysp_node_leaf *orig_leaf; |
| 5293 | struct lysp_node_leaflist *llist; |
| 5294 | const struct lysp_node_leaflist *orig_llist; |
| 5295 | struct lysp_node_list *list; |
| 5296 | const struct lysp_node_list *orig_list; |
| 5297 | struct lysp_node_choice *choice; |
| 5298 | const struct lysp_node_choice *orig_choice; |
| 5299 | struct lysp_node_case *cas; |
| 5300 | const struct lysp_node_case *orig_cas; |
| 5301 | struct lysp_node_anydata *any; |
| 5302 | const struct lysp_node_anydata *orig_any; |
| 5303 | |
| 5304 | assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA)); |
| 5305 | |
| 5306 | /* common part */ |
| 5307 | LY_CHECK_RET(lysp_node_common_dup(ctx, node, orig)); |
| 5308 | |
| 5309 | /* specific part */ |
| 5310 | switch (node->nodetype) { |
| 5311 | case LYS_CONTAINER: |
| 5312 | cont = (struct lysp_node_container *)node; |
| 5313 | orig_cont = (const struct lysp_node_container *)orig; |
| 5314 | |
| 5315 | DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup); |
| 5316 | DUP_STRING(ctx, orig_cont->presence, cont->presence, ret); |
| 5317 | /* we do not need the rest */ |
| 5318 | break; |
| 5319 | case LYS_LEAF: |
| 5320 | leaf = (struct lysp_node_leaf *)node; |
| 5321 | orig_leaf = (const struct lysp_node_leaf *)orig; |
| 5322 | |
| 5323 | DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup); |
| 5324 | LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type)); |
| 5325 | DUP_STRING(ctx, orig_leaf->units, leaf->units, ret); |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 5326 | LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5327 | break; |
| 5328 | case LYS_LEAFLIST: |
| 5329 | llist = (struct lysp_node_leaflist *)node; |
| 5330 | orig_llist = (const struct lysp_node_leaflist *)orig; |
| 5331 | |
| 5332 | DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup); |
| 5333 | LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type)); |
| 5334 | DUP_STRING(ctx, orig_llist->units, llist->units, ret); |
| 5335 | DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup); |
| 5336 | llist->min = orig_llist->min; |
| 5337 | llist->max = orig_llist->max; |
| 5338 | break; |
| 5339 | case LYS_LIST: |
| 5340 | list = (struct lysp_node_list *)node; |
| 5341 | orig_list = (const struct lysp_node_list *)orig; |
| 5342 | |
| 5343 | DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup); |
| 5344 | DUP_STRING(ctx, orig_list->key, list->key, ret); |
| 5345 | /* we do not need these arrays */ |
| 5346 | DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup); |
| 5347 | list->min = orig_list->min; |
| 5348 | list->max = orig_list->max; |
| 5349 | break; |
| 5350 | case LYS_CHOICE: |
| 5351 | choice = (struct lysp_node_choice *)node; |
| 5352 | orig_choice = (const struct lysp_node_choice *)orig; |
| 5353 | |
| 5354 | /* we do not need children */ |
| 5355 | LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt)); |
| 5356 | break; |
| 5357 | case LYS_CASE: |
| 5358 | cas = (struct lysp_node_case *)node; |
| 5359 | orig_cas = (const struct lysp_node_case *)orig; |
| 5360 | |
| 5361 | /* we do not need children */ |
| 5362 | (void)cas; |
| 5363 | (void)orig_cas; |
| 5364 | break; |
| 5365 | case LYS_ANYDATA: |
| 5366 | case LYS_ANYXML: |
| 5367 | any = (struct lysp_node_anydata *)node; |
| 5368 | orig_any = (const struct lysp_node_anydata *)orig; |
| 5369 | |
| 5370 | DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup); |
| 5371 | break; |
| 5372 | default: |
| 5373 | LOGINT_RET(ctx); |
| 5374 | } |
| 5375 | |
| 5376 | return ret; |
| 5377 | } |
| 5378 | |
| 5379 | static LY_ERR |
| 5380 | lysp_action_inout_dup(const struct ly_ctx *ctx, struct lysp_action_inout *inout, const struct lysp_action_inout *orig) |
| 5381 | { |
| 5382 | inout->parent = NULL; |
| 5383 | inout->nodetype = orig->nodetype; |
| 5384 | DUP_ARRAY(ctx, orig->musts, inout->musts, lysp_restr_dup); |
| 5385 | /* we dot need these arrays */ |
| 5386 | DUP_ARRAY(ctx, orig->exts, inout->exts, lysp_ext_dup); |
| 5387 | |
| 5388 | return LY_SUCCESS; |
| 5389 | } |
| 5390 | |
| 5391 | static LY_ERR |
| 5392 | lysp_action_dup(const struct ly_ctx *ctx, struct lysp_action *act, const struct lysp_action *orig) |
| 5393 | { |
| 5394 | LY_ERR ret = LY_SUCCESS; |
| 5395 | |
| 5396 | act->parent = NULL; |
| 5397 | act->nodetype = orig->nodetype; |
| 5398 | act->flags = orig->flags; |
| 5399 | DUP_STRING(ctx, orig->name, act->name, ret); |
| 5400 | DUP_STRING(ctx, orig->dsc, act->dsc, ret); |
| 5401 | DUP_STRING(ctx, orig->ref, act->ref, ret); |
| 5402 | DUP_ARRAY(ctx, orig->iffeatures, act->iffeatures, lysp_qname_dup); |
| 5403 | |
| 5404 | act->input.nodetype = orig->input.nodetype; |
| 5405 | act->output.nodetype = orig->output.nodetype; |
| 5406 | /* we do not need choldren of in/out */ |
| 5407 | DUP_ARRAY(ctx, orig->exts, act->exts, lysp_ext_dup); |
| 5408 | |
| 5409 | return ret; |
| 5410 | } |
| 5411 | |
| 5412 | static LY_ERR |
| 5413 | lysp_notif_dup(const struct ly_ctx *ctx, struct lysp_notif *notif, const struct lysp_notif *orig) |
| 5414 | { |
| 5415 | LY_ERR ret = LY_SUCCESS; |
| 5416 | |
| 5417 | notif->parent = NULL; |
| 5418 | notif->nodetype = orig->nodetype; |
| 5419 | notif->flags = orig->flags; |
| 5420 | DUP_STRING(ctx, orig->name, notif->name, ret); |
| 5421 | DUP_STRING(ctx, orig->dsc, notif->dsc, ret); |
| 5422 | DUP_STRING(ctx, orig->ref, notif->ref, ret); |
| 5423 | DUP_ARRAY(ctx, orig->iffeatures, notif->iffeatures, lysp_qname_dup); |
| 5424 | DUP_ARRAY(ctx, orig->musts, notif->musts, lysp_restr_dup); |
| 5425 | /* we do not need these arrays */ |
| 5426 | DUP_ARRAY(ctx, orig->exts, notif->exts, lysp_ext_dup); |
| 5427 | |
| 5428 | return ret; |
| 5429 | } |
| 5430 | |
| 5431 | /** |
| 5432 | * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied. |
| 5433 | * |
| 5434 | * @param[in] ctx libyang context. |
| 5435 | * @param[in] pnode Node to duplicate. |
| 5436 | * @param[in] with_links Whether to also copy any links (child, parent pointers). |
| 5437 | * @param[out] dup_p Duplicated parsed node. |
| 5438 | * @return LY_ERR value. |
| 5439 | */ |
| 5440 | static LY_ERR |
| 5441 | lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p) |
| 5442 | { |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5443 | LY_ERR ret = LY_SUCCESS; |
| 5444 | void *mem = NULL; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5445 | |
| 5446 | if (!pnode) { |
| 5447 | *dup_p = NULL; |
| 5448 | return LY_SUCCESS; |
| 5449 | } |
| 5450 | |
| 5451 | switch (pnode->nodetype) { |
| 5452 | case LYS_CONTAINER: |
| 5453 | mem = calloc(1, sizeof(struct lysp_node_container)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5454 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5455 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5456 | break; |
| 5457 | case LYS_LEAF: |
| 5458 | mem = calloc(1, sizeof(struct lysp_node_leaf)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5459 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5460 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5461 | break; |
| 5462 | case LYS_LEAFLIST: |
| 5463 | mem = calloc(1, sizeof(struct lysp_node_leaflist)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5464 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5465 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5466 | break; |
| 5467 | case LYS_LIST: |
| 5468 | mem = calloc(1, sizeof(struct lysp_node_list)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5469 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5470 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5471 | break; |
| 5472 | case LYS_CHOICE: |
| 5473 | mem = calloc(1, sizeof(struct lysp_node_choice)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5474 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5475 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5476 | break; |
| 5477 | case LYS_CASE: |
| 5478 | mem = calloc(1, sizeof(struct lysp_node_case)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5479 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5480 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5481 | break; |
| 5482 | case LYS_ANYDATA: |
| 5483 | case LYS_ANYXML: |
| 5484 | mem = calloc(1, sizeof(struct lysp_node_anydata)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5485 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5486 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5487 | break; |
| 5488 | case LYS_INPUT: |
| 5489 | case LYS_OUTPUT: |
| 5490 | mem = calloc(1, sizeof(struct lysp_action_inout)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5491 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5492 | 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] | 5493 | break; |
| 5494 | case LYS_ACTION: |
| 5495 | case LYS_RPC: |
| 5496 | mem = calloc(1, sizeof(struct lysp_action)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5497 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5498 | 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] | 5499 | break; |
| 5500 | case LYS_NOTIF: |
| 5501 | mem = calloc(1, sizeof(struct lysp_notif)); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5502 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 5503 | 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] | 5504 | break; |
| 5505 | default: |
| 5506 | LOGINT_RET(ctx); |
| 5507 | } |
| 5508 | |
| 5509 | if (with_links) { |
| 5510 | /* copy also parent and child pointers */ |
| 5511 | ((struct lysp_node *)mem)->parent = pnode->parent; |
| 5512 | switch (pnode->nodetype) { |
| 5513 | case LYS_CONTAINER: |
| 5514 | ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child; |
| 5515 | break; |
| 5516 | case LYS_LIST: |
| 5517 | ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child; |
| 5518 | break; |
| 5519 | case LYS_CHOICE: |
| 5520 | ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child; |
| 5521 | break; |
| 5522 | case LYS_CASE: |
| 5523 | ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child; |
| 5524 | break; |
| 5525 | default: |
| 5526 | break; |
| 5527 | } |
| 5528 | } |
| 5529 | |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 5530 | cleanup: |
| 5531 | if (ret) { |
| 5532 | free(mem); |
| 5533 | } else { |
| 5534 | *dup_p = mem; |
| 5535 | } |
| 5536 | return ret; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 5537 | } |
| 5538 | |
| 5539 | #define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \ |
| 5540 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s of %s node - it is not possible to %s \"%s\" property.", \ |
| 5541 | AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\ |
| 5542 | ret = LY_EVALID; \ |
| 5543 | goto cleanup; |
| 5544 | |
| 5545 | #define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \ |
| 5546 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5547 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid %s of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5548 | AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
| 5549 | ret = LY_EVALID; \ |
| 5550 | goto cleanup; \ |
| 5551 | } |
| 5552 | |
| 5553 | /** |
| 5554 | * @brief Apply refine. |
| 5555 | * |
| 5556 | * @param[in] ctx Compile context. |
| 5557 | * @param[in] rfn Refine to apply. |
| 5558 | * @param[in,out] target Refine target. |
| 5559 | * @return LY_ERR value. |
| 5560 | */ |
| 5561 | static LY_ERR |
| 5562 | lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target) |
| 5563 | { |
| 5564 | LY_ERR ret = LY_SUCCESS; |
| 5565 | LY_ARRAY_COUNT_TYPE u; |
| 5566 | struct lysp_qname *qname; |
| 5567 | struct lysp_restr **musts, *must; |
| 5568 | uint32_t *num; |
| 5569 | |
| 5570 | /* default value */ |
| 5571 | if (rfn->dflts) { |
| 5572 | switch (target->nodetype) { |
| 5573 | case LYS_LEAF: |
| 5574 | AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default"); |
| 5575 | |
| 5576 | FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str); |
| 5577 | DUP_STRING_GOTO(ctx->ctx, rfn->dflts[0], ((struct lysp_node_leaf *)target)->dflt.str, ret, cleanup); |
| 5578 | ((struct lysp_node_leaf *)target)->dflt.mod = ctx->mod; |
| 5579 | break; |
| 5580 | case LYS_LEAFLIST: |
| 5581 | if (ctx->mod->version < LYS_VERSION_1_1) { |
| 5582 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5583 | "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
| 5584 | ret = LY_EVALID; |
| 5585 | goto cleanup; |
| 5586 | } |
| 5587 | |
| 5588 | FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free); |
| 5589 | ((struct lysp_node_leaflist *)target)->dflts = NULL; |
| 5590 | LY_ARRAY_FOR(rfn->dflts, u) { |
| 5591 | LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup); |
| 5592 | DUP_STRING_GOTO(ctx->ctx, rfn->dflts[u], qname->str, ret, cleanup); |
| 5593 | qname->mod = ctx->mod; |
| 5594 | } |
| 5595 | break; |
| 5596 | case LYS_CHOICE: |
| 5597 | AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default"); |
| 5598 | |
| 5599 | FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str); |
| 5600 | DUP_STRING_GOTO(ctx->ctx, rfn->dflts[0], ((struct lysp_node_choice *)target)->dflt.str, ret, cleanup); |
| 5601 | ((struct lysp_node_choice *)target)->dflt.mod = ctx->mod; |
| 5602 | break; |
| 5603 | default: |
| 5604 | AMEND_WRONG_NODETYPE("refine", "replace", "default"); |
| 5605 | } |
| 5606 | } |
| 5607 | |
| 5608 | /* description */ |
| 5609 | if (rfn->dsc) { |
| 5610 | FREE_STRING(ctx->ctx, target->dsc); |
| 5611 | DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup); |
| 5612 | } |
| 5613 | |
| 5614 | /* reference */ |
| 5615 | if (rfn->ref) { |
| 5616 | FREE_STRING(ctx->ctx, target->ref); |
| 5617 | DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup); |
| 5618 | } |
| 5619 | |
| 5620 | /* config */ |
| 5621 | if (rfn->flags & LYS_CONFIG_MASK) { |
| 5622 | if (ctx->options & (LYSC_OPT_NOTIFICATION | LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
| 5623 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 5624 | ctx->options & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
| 5625 | } else { |
| 5626 | target->flags &= ~LYS_CONFIG_MASK; |
| 5627 | target->flags |= rfn->flags & LYS_CONFIG_MASK; |
| 5628 | } |
| 5629 | } |
| 5630 | |
| 5631 | /* mandatory */ |
| 5632 | if (rfn->flags & LYS_MAND_MASK) { |
| 5633 | switch (target->nodetype) { |
| 5634 | case LYS_LEAF: |
| 5635 | case LYS_CHOICE: |
| 5636 | case LYS_ANYDATA: |
| 5637 | case LYS_ANYXML: |
| 5638 | break; |
| 5639 | default: |
| 5640 | AMEND_WRONG_NODETYPE("refine", "replace", "mandatory"); |
| 5641 | } |
| 5642 | |
| 5643 | target->flags &= ~LYS_MAND_MASK; |
| 5644 | target->flags |= rfn->flags & LYS_MAND_MASK; |
| 5645 | } |
| 5646 | |
| 5647 | /* presence */ |
| 5648 | if (rfn->presence) { |
| 5649 | switch (target->nodetype) { |
| 5650 | case LYS_CONTAINER: |
| 5651 | break; |
| 5652 | default: |
| 5653 | AMEND_WRONG_NODETYPE("refine", "replace", "presence"); |
| 5654 | } |
| 5655 | |
| 5656 | FREE_STRING(ctx->ctx, ((struct lysp_node_container *)target)->presence); |
| 5657 | DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup); |
| 5658 | } |
| 5659 | |
| 5660 | /* must */ |
| 5661 | if (rfn->musts) { |
| 5662 | switch (target->nodetype) { |
| 5663 | case LYS_CONTAINER: |
| 5664 | case LYS_LIST: |
| 5665 | case LYS_LEAF: |
| 5666 | case LYS_LEAFLIST: |
| 5667 | case LYS_ANYDATA: |
| 5668 | case LYS_ANYXML: |
| 5669 | musts = &((struct lysp_node_container *)target)->musts; |
| 5670 | break; |
| 5671 | default: |
| 5672 | AMEND_WRONG_NODETYPE("refine", "add", "must"); |
| 5673 | } |
| 5674 | |
| 5675 | LY_ARRAY_FOR(rfn->musts, u) { |
| 5676 | LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup); |
| 5677 | LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup); |
| 5678 | } |
| 5679 | } |
| 5680 | |
| 5681 | /* min-elements */ |
| 5682 | if (rfn->flags & LYS_SET_MIN) { |
| 5683 | switch (target->nodetype) { |
| 5684 | case LYS_LEAFLIST: |
| 5685 | num = &((struct lysp_node_leaflist *)target)->min; |
| 5686 | break; |
| 5687 | case LYS_LIST: |
| 5688 | num = &((struct lysp_node_list *)target)->min; |
| 5689 | break; |
| 5690 | default: |
| 5691 | AMEND_WRONG_NODETYPE("refine", "replace", "min-elements"); |
| 5692 | } |
| 5693 | |
| 5694 | *num = rfn->min; |
| 5695 | } |
| 5696 | |
| 5697 | /* max-elements */ |
| 5698 | if (rfn->flags & LYS_SET_MAX) { |
| 5699 | switch (target->nodetype) { |
| 5700 | case LYS_LEAFLIST: |
| 5701 | num = &((struct lysp_node_leaflist *)target)->max; |
| 5702 | break; |
| 5703 | case LYS_LIST: |
| 5704 | num = &((struct lysp_node_list *)target)->max; |
| 5705 | break; |
| 5706 | default: |
| 5707 | AMEND_WRONG_NODETYPE("refine", "replace", "max-elements"); |
| 5708 | } |
| 5709 | |
| 5710 | *num = rfn->max; |
| 5711 | } |
| 5712 | |
| 5713 | /* if-feature */ |
| 5714 | if (rfn->iffeatures) { |
| 5715 | switch (target->nodetype) { |
| 5716 | case LYS_LEAF: |
| 5717 | case LYS_LEAFLIST: |
| 5718 | case LYS_LIST: |
| 5719 | case LYS_CONTAINER: |
| 5720 | case LYS_CHOICE: |
| 5721 | case LYS_CASE: |
| 5722 | case LYS_ANYDATA: |
| 5723 | case LYS_ANYXML: |
| 5724 | break; |
| 5725 | default: |
| 5726 | AMEND_WRONG_NODETYPE("refine", "add", "if-feature"); |
| 5727 | } |
| 5728 | |
| 5729 | LY_ARRAY_FOR(rfn->iffeatures, u) { |
| 5730 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup); |
| 5731 | DUP_STRING_GOTO(ctx->ctx, rfn->iffeatures[u].str, qname->str, ret, cleanup); |
| 5732 | qname->mod = ctx->mod; |
| 5733 | } |
| 5734 | } |
| 5735 | |
| 5736 | /* extension */ |
| 5737 | /* TODO refine extensions */ |
| 5738 | |
| 5739 | cleanup: |
| 5740 | return ret; |
| 5741 | } |
| 5742 | |
| 5743 | /** |
| 5744 | * @brief Apply deviate add. |
| 5745 | * |
| 5746 | * @param[in] ctx Compile context. |
| 5747 | * @param[in] d Deviate add to apply. |
| 5748 | * @param[in,out] target Deviation target. |
| 5749 | * @return LY_ERR value. |
| 5750 | */ |
| 5751 | static LY_ERR |
| 5752 | lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target) |
| 5753 | { |
| 5754 | LY_ERR ret = LY_SUCCESS; |
| 5755 | LY_ARRAY_COUNT_TYPE u; |
| 5756 | struct lysp_qname *qname; |
| 5757 | uint32_t *num; |
| 5758 | struct lysp_restr **musts, *must; |
| 5759 | |
| 5760 | #define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \ |
| 5761 | if (((TYPE)target)->MEMBER) { \ |
| 5762 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5763 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5764 | PROPERTY, ((TYPE)target)->VALUEMEMBER); \ |
| 5765 | ret = LY_EVALID; \ |
| 5766 | goto cleanup; \ |
| 5767 | } |
| 5768 | |
| 5769 | /* [units-stmt] */ |
| 5770 | if (d->units) { |
| 5771 | switch (target->nodetype) { |
| 5772 | case LYS_LEAF: |
| 5773 | case LYS_LEAFLIST: |
| 5774 | break; |
| 5775 | default: |
| 5776 | AMEND_WRONG_NODETYPE("deviation", "add", "units"); |
| 5777 | } |
| 5778 | |
| 5779 | DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units); |
| 5780 | DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup); |
| 5781 | } |
| 5782 | |
| 5783 | /* *must-stmt */ |
| 5784 | if (d->musts) { |
| 5785 | switch (target->nodetype) { |
| 5786 | case LYS_CONTAINER: |
| 5787 | case LYS_LIST: |
| 5788 | case LYS_LEAF: |
| 5789 | case LYS_LEAFLIST: |
| 5790 | case LYS_ANYDATA: |
| 5791 | case LYS_ANYXML: |
| 5792 | musts = &((struct lysp_node_container *)target)->musts; |
| 5793 | break; |
| 5794 | case LYS_NOTIF: |
| 5795 | musts = &((struct lysp_notif *)target)->musts; |
| 5796 | break; |
| 5797 | case LYS_INPUT: |
| 5798 | case LYS_OUTPUT: |
| 5799 | musts = &((struct lysp_action_inout *)target)->musts; |
| 5800 | break; |
| 5801 | default: |
| 5802 | AMEND_WRONG_NODETYPE("deviation", "add", "must"); |
| 5803 | } |
| 5804 | |
| 5805 | LY_ARRAY_FOR(d->musts, u) { |
| 5806 | LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup); |
| 5807 | LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup); |
| 5808 | } |
| 5809 | } |
| 5810 | |
| 5811 | /* *unique-stmt */ |
| 5812 | if (d->uniques) { |
| 5813 | switch (target->nodetype) { |
| 5814 | case LYS_LIST: |
| 5815 | break; |
| 5816 | default: |
| 5817 | AMEND_WRONG_NODETYPE("deviation", "add", "unique"); |
| 5818 | } |
| 5819 | |
| 5820 | LY_ARRAY_FOR(d->uniques, u) { |
| 5821 | LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup); |
| 5822 | DUP_STRING_GOTO(ctx->ctx, d->uniques[u], qname->str, ret, cleanup); |
| 5823 | qname->mod = ctx->mod; |
| 5824 | } |
| 5825 | } |
| 5826 | |
| 5827 | /* *default-stmt */ |
| 5828 | if (d->dflts) { |
| 5829 | switch (target->nodetype) { |
| 5830 | case LYS_LEAF: |
| 5831 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 5832 | DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str); |
| 5833 | |
| 5834 | DUP_STRING_GOTO(ctx->ctx, d->dflts[0], ((struct lysp_node_leaf *)target)->dflt.str, ret, cleanup); |
| 5835 | ((struct lysp_node_leaf *)target)->dflt.mod = ctx->mod; |
| 5836 | break; |
| 5837 | case LYS_LEAFLIST: |
| 5838 | LY_ARRAY_FOR(d->dflts, u) { |
| 5839 | LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup); |
| 5840 | DUP_STRING_GOTO(ctx->ctx, d->dflts[u], qname->str, ret, cleanup); |
| 5841 | qname->mod = ctx->mod; |
| 5842 | } |
| 5843 | break; |
| 5844 | case LYS_CHOICE: |
| 5845 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 5846 | DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str); |
| 5847 | |
| 5848 | DUP_STRING_GOTO(ctx->ctx, d->dflts[0], ((struct lysp_node_choice *)target)->dflt.str, ret, cleanup); |
| 5849 | ((struct lysp_node_choice *)target)->dflt.mod = ctx->mod; |
| 5850 | break; |
| 5851 | default: |
| 5852 | AMEND_WRONG_NODETYPE("deviation", "add", "default"); |
| 5853 | } |
| 5854 | } |
| 5855 | |
| 5856 | /* [config-stmt] */ |
| 5857 | if (d->flags & LYS_CONFIG_MASK) { |
| 5858 | switch (target->nodetype) { |
| 5859 | case LYS_CONTAINER: |
| 5860 | case LYS_LEAF: |
| 5861 | case LYS_LEAFLIST: |
| 5862 | case LYS_LIST: |
| 5863 | case LYS_CHOICE: |
| 5864 | case LYS_ANYDATA: |
| 5865 | case LYS_ANYXML: |
| 5866 | break; |
| 5867 | default: |
| 5868 | AMEND_WRONG_NODETYPE("deviation", "add", "config"); |
| 5869 | } |
| 5870 | |
| 5871 | if (target->flags & LYS_CONFIG_MASK) { |
| 5872 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5873 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5874 | target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5875 | ret = LY_EVALID; |
| 5876 | goto cleanup; |
| 5877 | } |
| 5878 | |
| 5879 | target->flags |= d->flags & LYS_CONFIG_MASK; |
| 5880 | } |
| 5881 | |
| 5882 | /* [mandatory-stmt] */ |
| 5883 | if (d->flags & LYS_MAND_MASK) { |
| 5884 | switch (target->nodetype) { |
| 5885 | case LYS_LEAF: |
| 5886 | case LYS_CHOICE: |
| 5887 | case LYS_ANYDATA: |
| 5888 | case LYS_ANYXML: |
| 5889 | break; |
| 5890 | default: |
| 5891 | AMEND_WRONG_NODETYPE("deviation", "add", "mandatory"); |
| 5892 | } |
| 5893 | |
| 5894 | if (target->flags & LYS_MAND_MASK) { |
| 5895 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5896 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5897 | target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5898 | ret = LY_EVALID; |
| 5899 | goto cleanup; |
| 5900 | } |
| 5901 | |
| 5902 | target->flags |= d->flags & LYS_MAND_MASK; |
| 5903 | } |
| 5904 | |
| 5905 | /* [min-elements-stmt] */ |
| 5906 | if (d->flags & LYS_SET_MIN) { |
| 5907 | switch (target->nodetype) { |
| 5908 | case LYS_LEAFLIST: |
| 5909 | num = &((struct lysp_node_leaflist *)target)->min; |
| 5910 | break; |
| 5911 | case LYS_LIST: |
| 5912 | num = &((struct lysp_node_list *)target)->min; |
| 5913 | break; |
| 5914 | default: |
| 5915 | AMEND_WRONG_NODETYPE("deviation", "add", "min-elements"); |
| 5916 | } |
| 5917 | |
| 5918 | if (target->flags & LYS_SET_MIN) { |
| 5919 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5920 | "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num); |
| 5921 | ret = LY_EVALID; |
| 5922 | goto cleanup; |
| 5923 | } |
| 5924 | |
| 5925 | *num = d->min; |
| 5926 | } |
| 5927 | |
| 5928 | /* [max-elements-stmt] */ |
| 5929 | if (d->flags & LYS_SET_MAX) { |
| 5930 | switch (target->nodetype) { |
| 5931 | case LYS_LEAFLIST: |
| 5932 | num = &((struct lysp_node_leaflist *)target)->max; |
| 5933 | break; |
| 5934 | case LYS_LIST: |
| 5935 | num = &((struct lysp_node_list *)target)->max; |
| 5936 | break; |
| 5937 | default: |
| 5938 | AMEND_WRONG_NODETYPE("deviation", "add", "max-elements"); |
| 5939 | } |
| 5940 | |
| 5941 | if (target->flags & LYS_SET_MAX) { |
| 5942 | if (*num) { |
| 5943 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5944 | "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").", |
| 5945 | *num); |
| 5946 | } else { |
| 5947 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5948 | "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\")."); |
| 5949 | } |
| 5950 | ret = LY_EVALID; |
| 5951 | goto cleanup; |
| 5952 | } |
| 5953 | |
| 5954 | *num = d->max; |
| 5955 | } |
| 5956 | |
| 5957 | cleanup: |
| 5958 | return ret; |
| 5959 | } |
| 5960 | |
| 5961 | /** |
| 5962 | * @brief Apply deviate delete. |
| 5963 | * |
| 5964 | * @param[in] ctx Compile context. |
| 5965 | * @param[in] d Deviate delete to apply. |
| 5966 | * @param[in,out] target Deviation target. |
| 5967 | * @return LY_ERR value. |
| 5968 | */ |
| 5969 | static LY_ERR |
| 5970 | lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target) |
| 5971 | { |
| 5972 | LY_ERR ret = LY_SUCCESS; |
| 5973 | struct lysp_restr **musts; |
| 5974 | LY_ARRAY_COUNT_TYPE u, v; |
| 5975 | struct lysp_qname **uniques, **dflts; |
| 5976 | |
| 5977 | #define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \ |
| 5978 | LY_ARRAY_FOR(d->DEV_ARRAY, u) { \ |
| 5979 | int found = 0; \ |
| 5980 | LY_ARRAY_FOR(ORIG_ARRAY, v) { \ |
| 5981 | if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \ |
| 5982 | found = 1; \ |
| 5983 | break; \ |
| 5984 | } \ |
| 5985 | } \ |
| 5986 | if (!found) { \ |
| 5987 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5988 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5989 | PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \ |
| 5990 | ret = LY_EVALID; \ |
| 5991 | goto cleanup; \ |
| 5992 | } \ |
| 5993 | LY_ARRAY_DECREMENT(ORIG_ARRAY); \ |
| 5994 | FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \ |
| 5995 | memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \ |
| 5996 | } \ |
| 5997 | if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \ |
| 5998 | LY_ARRAY_FREE(ORIG_ARRAY); \ |
| 5999 | ORIG_ARRAY = NULL; \ |
| 6000 | } |
| 6001 | |
| 6002 | #define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 6003 | if (!((TYPE)target)->MEMBER) { \ |
| 6004 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
| 6005 | ret = LY_EVALID; \ |
| 6006 | goto cleanup; \ |
| 6007 | } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \ |
| 6008 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 6009 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \ |
| 6010 | PROPERTY, VALUE, ((TYPE)target)->MEMBER); \ |
| 6011 | ret = LY_EVALID; \ |
| 6012 | goto cleanup; \ |
| 6013 | } |
| 6014 | |
| 6015 | /* [units-stmt] */ |
| 6016 | if (d->units) { |
| 6017 | switch (target->nodetype) { |
| 6018 | case LYS_LEAF: |
| 6019 | case LYS_LEAFLIST: |
| 6020 | break; |
| 6021 | default: |
| 6022 | AMEND_WRONG_NODETYPE("deviation", "delete", "units"); |
| 6023 | } |
| 6024 | |
| 6025 | DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units); |
| 6026 | FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units); |
| 6027 | ((struct lysp_node_leaf *)target)->units = NULL; |
| 6028 | } |
| 6029 | |
| 6030 | /* *must-stmt */ |
| 6031 | if (d->musts) { |
| 6032 | switch (target->nodetype) { |
| 6033 | case LYS_CONTAINER: |
| 6034 | case LYS_LIST: |
| 6035 | case LYS_LEAF: |
| 6036 | case LYS_LEAFLIST: |
| 6037 | case LYS_ANYDATA: |
| 6038 | case LYS_ANYXML: |
| 6039 | musts = &((struct lysp_node_container *)target)->musts; |
| 6040 | break; |
| 6041 | case LYS_NOTIF: |
| 6042 | musts = &((struct lysp_notif *)target)->musts; |
| 6043 | break; |
| 6044 | case LYS_INPUT: |
| 6045 | case LYS_OUTPUT: |
| 6046 | musts = &((struct lysp_action_inout *)target)->musts; |
| 6047 | break; |
| 6048 | default: |
| 6049 | AMEND_WRONG_NODETYPE("deviation", "delete", "must"); |
| 6050 | } |
| 6051 | |
| 6052 | DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must"); |
| 6053 | } |
| 6054 | |
| 6055 | /* *unique-stmt */ |
| 6056 | if (d->uniques) { |
| 6057 | switch (target->nodetype) { |
| 6058 | case LYS_LIST: |
| 6059 | break; |
| 6060 | default: |
| 6061 | AMEND_WRONG_NODETYPE("deviation", "delete", "unique"); |
| 6062 | } |
| 6063 | |
| 6064 | uniques = &((struct lysp_node_list *)target)->uniques; |
| 6065 | DEV_DEL_ARRAY(uniques, *uniques, , .str, lysp_qname_free, "unique"); |
| 6066 | } |
| 6067 | |
| 6068 | /* *default-stmt */ |
| 6069 | if (d->dflts) { |
| 6070 | switch (target->nodetype) { |
| 6071 | case LYS_LEAF: |
| 6072 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 6073 | DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0]); |
| 6074 | |
| 6075 | FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str); |
| 6076 | ((struct lysp_node_leaf *)target)->dflt.str = NULL; |
| 6077 | break; |
| 6078 | case LYS_LEAFLIST: |
| 6079 | dflts = &((struct lysp_node_leaflist *)target)->dflts; |
| 6080 | DEV_DEL_ARRAY(dflts, *dflts, , .str, lysp_qname_free, "default"); |
| 6081 | break; |
| 6082 | case LYS_CHOICE: |
| 6083 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 6084 | DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0]); |
| 6085 | |
| 6086 | FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str); |
| 6087 | ((struct lysp_node_choice *)target)->dflt.str = NULL; |
| 6088 | break; |
| 6089 | default: |
| 6090 | AMEND_WRONG_NODETYPE("deviation", "delete", "default"); |
| 6091 | } |
| 6092 | } |
| 6093 | |
| 6094 | cleanup: |
| 6095 | return ret; |
| 6096 | } |
| 6097 | |
| 6098 | /** |
| 6099 | * @brief Apply deviate replace. |
| 6100 | * |
| 6101 | * @param[in] ctx Compile context. |
| 6102 | * @param[in] d Deviate replace to apply. |
| 6103 | * @param[in,out] target Deviation target. |
| 6104 | * @return LY_ERR value. |
| 6105 | */ |
| 6106 | static LY_ERR |
| 6107 | lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target) |
| 6108 | { |
| 6109 | LY_ERR ret = LY_SUCCESS; |
| 6110 | uint32_t *num; |
| 6111 | |
| 6112 | #define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 6113 | if (!((TYPE)target)->MEMBER) { \ |
| 6114 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
| 6115 | ret = LY_EVALID; \ |
| 6116 | goto cleanup; \ |
| 6117 | } |
| 6118 | |
| 6119 | /* [type-stmt] */ |
| 6120 | if (d->type) { |
| 6121 | switch (target->nodetype) { |
| 6122 | case LYS_LEAF: |
| 6123 | case LYS_LEAFLIST: |
| 6124 | break; |
| 6125 | default: |
| 6126 | AMEND_WRONG_NODETYPE("deviation", "replace", "type"); |
| 6127 | } |
| 6128 | |
| 6129 | lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type); |
| 6130 | lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type); |
| 6131 | } |
| 6132 | |
| 6133 | /* [units-stmt] */ |
| 6134 | if (d->units) { |
| 6135 | switch (target->nodetype) { |
| 6136 | case LYS_LEAF: |
| 6137 | case LYS_LEAFLIST: |
| 6138 | break; |
| 6139 | default: |
| 6140 | AMEND_WRONG_NODETYPE("deviation", "replace", "units"); |
| 6141 | } |
| 6142 | |
| 6143 | DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units); |
| 6144 | FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units); |
| 6145 | DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup); |
| 6146 | } |
| 6147 | |
| 6148 | /* [default-stmt] */ |
| 6149 | if (d->dflt) { |
| 6150 | switch (target->nodetype) { |
| 6151 | case LYS_LEAF: |
| 6152 | DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt); |
| 6153 | |
| 6154 | FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str); |
| 6155 | DUP_STRING_GOTO(ctx->ctx, d->dflt, ((struct lysp_node_leaf *)target)->dflt.str, ret, cleanup); |
| 6156 | ((struct lysp_node_leaf *)target)->dflt.mod = ctx->mod; |
| 6157 | break; |
| 6158 | case LYS_CHOICE: |
| 6159 | DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt); |
| 6160 | |
| 6161 | FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str); |
| 6162 | DUP_STRING_GOTO(ctx->ctx, d->dflt, ((struct lysp_node_choice *)target)->dflt.str, ret, cleanup); |
| 6163 | ((struct lysp_node_choice *)target)->dflt.mod = ctx->mod; |
| 6164 | break; |
| 6165 | default: |
| 6166 | AMEND_WRONG_NODETYPE("deviation", "replace", "default"); |
| 6167 | } |
| 6168 | } |
| 6169 | |
| 6170 | /* [config-stmt] */ |
| 6171 | if (d->flags & LYS_CONFIG_MASK) { |
| 6172 | switch (target->nodetype) { |
| 6173 | case LYS_CONTAINER: |
| 6174 | case LYS_LEAF: |
| 6175 | case LYS_LEAFLIST: |
| 6176 | case LYS_LIST: |
| 6177 | case LYS_CHOICE: |
| 6178 | case LYS_ANYDATA: |
| 6179 | case LYS_ANYXML: |
| 6180 | break; |
| 6181 | default: |
| 6182 | AMEND_WRONG_NODETYPE("deviation", "replace", "config"); |
| 6183 | } |
| 6184 | |
| 6185 | if (!(target->flags & LYS_CONFIG_MASK)) { |
| 6186 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6187 | "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6188 | ret = LY_EVALID; |
| 6189 | goto cleanup; |
| 6190 | } |
| 6191 | |
| 6192 | target->flags &= ~LYS_CONFIG_MASK; |
| 6193 | target->flags |= d->flags & LYS_CONFIG_MASK; |
| 6194 | } |
| 6195 | |
| 6196 | /* [mandatory-stmt] */ |
| 6197 | if (d->flags & LYS_MAND_MASK) { |
| 6198 | switch (target->nodetype) { |
| 6199 | case LYS_LEAF: |
| 6200 | case LYS_CHOICE: |
| 6201 | case LYS_ANYDATA: |
| 6202 | case LYS_ANYXML: |
| 6203 | break; |
| 6204 | default: |
| 6205 | AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory"); |
| 6206 | } |
| 6207 | |
| 6208 | if (!(target->flags & LYS_MAND_MASK)) { |
| 6209 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6210 | "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6211 | ret = LY_EVALID; |
| 6212 | goto cleanup; |
| 6213 | } |
| 6214 | |
| 6215 | target->flags &= ~LYS_MAND_MASK; |
| 6216 | target->flags |= d->flags & LYS_MAND_MASK; |
| 6217 | } |
| 6218 | |
| 6219 | /* [min-elements-stmt] */ |
| 6220 | if (d->flags & LYS_SET_MIN) { |
| 6221 | switch (target->nodetype) { |
| 6222 | case LYS_LEAFLIST: |
| 6223 | num = &((struct lysp_node_leaflist *)target)->min; |
| 6224 | break; |
| 6225 | case LYS_LIST: |
| 6226 | num = &((struct lysp_node_list *)target)->min; |
| 6227 | break; |
| 6228 | default: |
| 6229 | AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements"); |
| 6230 | } |
| 6231 | |
| 6232 | if (!(target->flags & LYS_SET_MIN)) { |
| 6233 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6234 | "Invalid deviation replacing \"min-elements\" property which is not present."); |
| 6235 | ret = LY_EVALID; |
| 6236 | goto cleanup; |
| 6237 | } |
| 6238 | |
| 6239 | *num = d->min; |
| 6240 | } |
| 6241 | |
| 6242 | /* [max-elements-stmt] */ |
| 6243 | if (d->flags & LYS_SET_MAX) { |
| 6244 | switch (target->nodetype) { |
| 6245 | case LYS_LEAFLIST: |
| 6246 | num = &((struct lysp_node_leaflist *)target)->max; |
| 6247 | break; |
| 6248 | case LYS_LIST: |
| 6249 | num = &((struct lysp_node_list *)target)->max; |
| 6250 | break; |
| 6251 | default: |
| 6252 | AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements"); |
| 6253 | } |
| 6254 | |
| 6255 | if (!(target->flags & LYS_SET_MAX)) { |
| 6256 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6257 | "Invalid deviation replacing \"max-elements\" property which is not present."); |
| 6258 | ret = LY_EVALID; |
| 6259 | goto cleanup; |
| 6260 | } |
| 6261 | |
| 6262 | *num = d->max; |
| 6263 | } |
| 6264 | |
| 6265 | cleanup: |
| 6266 | return ret; |
| 6267 | } |
| 6268 | |
| 6269 | /** |
| 6270 | * @brief Get module of a single nodeid node name test. |
| 6271 | * |
| 6272 | * @param[in] ctx libyang context. |
| 6273 | * @param[in] nametest Nametest with an optional prefix. |
| 6274 | * @param[in] nametest_len Length of @p nametest. |
| 6275 | * @param[in] local_mod Module to return in case of no prefix. |
| 6276 | * @param[out] name Optional pointer to the name test without the prefix. |
| 6277 | * @param[out] name_len Length of @p name. |
| 6278 | * @return Resolved module. |
| 6279 | */ |
| 6280 | static const struct lys_module * |
| 6281 | lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len, |
| 6282 | const struct lys_module *local_mod, const char **name, size_t *name_len) |
| 6283 | { |
| 6284 | const struct lys_module *target_mod; |
| 6285 | const char *ptr; |
| 6286 | |
| 6287 | ptr = ly_strnchr(nametest, ':', nametest_len); |
| 6288 | if (ptr) { |
| 6289 | target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_PREF_SCHEMA, (void *)local_mod); |
| 6290 | if (!target_mod) { |
| 6291 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 6292 | "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
| 6293 | nametest_len, nametest, ptr - nametest, nametest, local_mod->name); |
| 6294 | return NULL; |
| 6295 | } |
| 6296 | |
| 6297 | if (name) { |
| 6298 | *name = ptr + 1; |
| 6299 | *name_len = nametest_len - ((ptr - nametest) + 1); |
| 6300 | } |
| 6301 | } else { |
| 6302 | target_mod = local_mod; |
| 6303 | if (name) { |
| 6304 | *name = nametest; |
| 6305 | *name_len = nametest_len; |
| 6306 | } |
| 6307 | } |
| 6308 | |
| 6309 | return target_mod; |
| 6310 | } |
| 6311 | |
| 6312 | /** |
| 6313 | * @brief Check whether a parsed node matches a single schema nodeid name test. |
| 6314 | * |
| 6315 | * @param[in] pnode Parsed node to consider. |
| 6316 | * @param[in] pnode_mod Compiled @p pnode to-be module. |
| 6317 | * @param[in] mod Expected module. |
| 6318 | * @param[in] name Expected name. |
| 6319 | * @param[in] name_len Length of @p name. |
| 6320 | * @return Whether it is a match or not. |
| 6321 | */ |
| 6322 | static ly_bool |
| 6323 | lysp_schema_nodeid_match_pnode(const struct lysp_node *pnode, const struct lys_module *pnode_mod, |
| 6324 | const struct lys_module *mod, const char *name, size_t name_len) |
| 6325 | { |
| 6326 | const char *pname; |
| 6327 | |
| 6328 | /* compare with the module of the node */ |
| 6329 | if (pnode_mod != mod) { |
| 6330 | return 0; |
| 6331 | } |
| 6332 | |
| 6333 | /* compare names */ |
| 6334 | if (pnode->nodetype & (LYS_ACTION | LYS_RPC)) { |
| 6335 | pname = ((struct lysp_action *)pnode)->name; |
| 6336 | } else if (pnode->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6337 | pname = (pnode->nodetype & LYS_INPUT) ? "input" : "output"; |
| 6338 | } else { |
| 6339 | pname = pnode->name; |
| 6340 | } |
| 6341 | if (ly_strncmp(pname, name, name_len)) { |
| 6342 | return 0; |
| 6343 | } |
| 6344 | |
| 6345 | return 1; |
| 6346 | } |
| 6347 | |
| 6348 | /** |
| 6349 | * @brief Check whether a compiled node matches a single schema nodeid name test. |
| 6350 | * |
| 6351 | * @param[in,out] node Compiled node to consider. On a match it is moved to its parent. |
| 6352 | * @param[in] mod Expected module. |
| 6353 | * @param[in] name Expected name. |
| 6354 | * @param[in] name_len Length of @p name. |
| 6355 | * @return Whether it is a match or not. |
| 6356 | */ |
| 6357 | static ly_bool |
| 6358 | lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name, |
| 6359 | size_t name_len) |
| 6360 | { |
| 6361 | const struct lys_module *node_mod; |
| 6362 | const char *node_name; |
| 6363 | |
| 6364 | /* compare with the module of the node */ |
| 6365 | if ((*node)->nodetype == LYS_INPUT) { |
| 6366 | node_mod = ((struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, input)))->module; |
| 6367 | } else if ((*node)->nodetype == LYS_OUTPUT) { |
| 6368 | node_mod = ((struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, output)))->module; |
| 6369 | } else { |
| 6370 | node_mod = (*node)->module; |
| 6371 | } |
| 6372 | if (node_mod != mod) { |
| 6373 | return 0; |
| 6374 | } |
| 6375 | |
| 6376 | /* compare names */ |
| 6377 | if ((*node)->nodetype == LYS_INPUT) { |
| 6378 | node_name = "input"; |
| 6379 | } else if ((*node)->nodetype == LYS_OUTPUT) { |
| 6380 | node_name = "output"; |
| 6381 | } else { |
| 6382 | node_name = (*node)->name; |
| 6383 | } |
| 6384 | if (ly_strncmp(node_name, name, name_len)) { |
| 6385 | return 0; |
| 6386 | } |
| 6387 | |
| 6388 | if ((*node)->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6389 | /* move up from input/output */ |
| 6390 | if ((*node)->nodetype == LYS_INPUT) { |
| 6391 | (*node) = (struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, input)); |
| 6392 | } else { |
| 6393 | (*node) = (struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, output)); |
| 6394 | } |
| 6395 | } else if ((*node)->parent && ((*node)->parent->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 6396 | /* move to the input/output */ |
| 6397 | if ((*node)->flags & LYS_CONFIG_W) { |
| 6398 | *node = (struct lysc_node *)&((struct lysc_action *)(*node)->parent)->input; |
| 6399 | } else { |
| 6400 | *node = (struct lysc_node *)&((struct lysc_action *)(*node)->parent)->output; |
| 6401 | } |
| 6402 | } else { |
| 6403 | /* move to next parent */ |
| 6404 | *node = (*node)->parent; |
| 6405 | } |
| 6406 | |
| 6407 | return 1; |
| 6408 | } |
| 6409 | |
| 6410 | /** |
| 6411 | * @brief Check whether a node matches specific schema nodeid. |
| 6412 | * |
| 6413 | * @param[in] exp Parsed nodeid to match. |
| 6414 | * @param[in] exp_mod Module to use for nodes in @p exp without a prefix. |
| 6415 | * @param[in] ctx_node Initial context node that should match, only for descendant paths. |
| 6416 | * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched. |
| 6417 | * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled. |
| 6418 | * @param[in] pnode_mod Compiled @p pnode to-be module. |
| 6419 | * @return Whether it is a match or not. |
| 6420 | */ |
| 6421 | static ly_bool |
| 6422 | lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lys_module *exp_mod, const struct lysc_node *ctx_node, |
| 6423 | const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod) |
| 6424 | { |
| 6425 | uint32_t i; |
| 6426 | const struct lys_module *mod; |
| 6427 | const char *name; |
| 6428 | size_t name_len; |
| 6429 | |
| 6430 | /* compare last node in the node ID */ |
| 6431 | i = exp->used - 1; |
| 6432 | |
| 6433 | /* get exp node ID module */ |
| 6434 | mod = lys_schema_node_get_module(exp_mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_mod, &name, &name_len); |
| 6435 | assert(mod); |
| 6436 | |
| 6437 | if (pnode) { |
| 6438 | /* compare on the last parsed-only node */ |
| 6439 | if (!lysp_schema_nodeid_match_pnode(pnode, pnode_mod, mod, name, name_len)) { |
| 6440 | return 0; |
| 6441 | } |
| 6442 | } else { |
| 6443 | /* using parent directly */ |
| 6444 | if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) { |
| 6445 | return 0; |
| 6446 | } |
| 6447 | } |
| 6448 | |
| 6449 | /* now compare all the compiled parents */ |
| 6450 | while (i > 1) { |
| 6451 | i -= 2; |
| 6452 | assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST); |
| 6453 | |
| 6454 | if (!parent) { |
| 6455 | /* no more parents but path continues */ |
| 6456 | return 0; |
| 6457 | } |
| 6458 | |
| 6459 | /* get exp node ID module */ |
| 6460 | mod = lys_schema_node_get_module(exp_mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_mod, &name, |
| 6461 | &name_len); |
| 6462 | assert(mod); |
| 6463 | |
| 6464 | /* compare with the parent */ |
| 6465 | if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) { |
| 6466 | return 0; |
| 6467 | } |
| 6468 | } |
| 6469 | |
| 6470 | if (ctx_node && (ctx_node != parent)) { |
| 6471 | /* descendant path has not finished in the context node */ |
| 6472 | return 0; |
| 6473 | } else if (!ctx_node && parent) { |
| 6474 | /* some parent was not matched */ |
| 6475 | return 0; |
| 6476 | } |
| 6477 | |
| 6478 | return 1; |
| 6479 | } |
| 6480 | |
| 6481 | static void |
| 6482 | lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug) |
| 6483 | { |
| 6484 | if (aug) { |
| 6485 | lyxp_expr_free(ctx, aug->nodeid); |
| 6486 | |
| 6487 | free(aug); |
| 6488 | } |
| 6489 | } |
| 6490 | |
| 6491 | static void |
| 6492 | lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev) |
| 6493 | { |
| 6494 | if (dev) { |
| 6495 | lyxp_expr_free(ctx, dev->nodeid); |
| 6496 | LY_ARRAY_FREE(dev->devs); |
| 6497 | LY_ARRAY_FREE(dev->dev_mods); |
| 6498 | |
| 6499 | free(dev); |
| 6500 | } |
| 6501 | } |
| 6502 | |
| 6503 | static void |
| 6504 | lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn) |
| 6505 | { |
| 6506 | if (rfn) { |
| 6507 | lyxp_expr_free(ctx, rfn->nodeid); |
| 6508 | LY_ARRAY_FREE(rfn->rfns); |
| 6509 | |
| 6510 | free(rfn); |
| 6511 | } |
| 6512 | } |
| 6513 | |
| 6514 | static void |
| 6515 | lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode) |
| 6516 | { |
| 6517 | if (!dev_pnode) { |
| 6518 | return; |
| 6519 | } |
| 6520 | |
| 6521 | switch (dev_pnode->nodetype) { |
| 6522 | case LYS_CONTAINER: |
| 6523 | ((struct lysp_node_container *)dev_pnode)->child = NULL; |
| 6524 | break; |
| 6525 | case LYS_LIST: |
| 6526 | ((struct lysp_node_list *)dev_pnode)->child = NULL; |
| 6527 | break; |
| 6528 | case LYS_CHOICE: |
| 6529 | ((struct lysp_node_choice *)dev_pnode)->child = NULL; |
| 6530 | break; |
| 6531 | case LYS_CASE: |
| 6532 | ((struct lysp_node_case *)dev_pnode)->child = NULL; |
| 6533 | break; |
| 6534 | case LYS_LEAF: |
| 6535 | case LYS_LEAFLIST: |
| 6536 | case LYS_ANYXML: |
| 6537 | case LYS_ANYDATA: |
| 6538 | /* no children */ |
| 6539 | break; |
| 6540 | case LYS_NOTIF: |
| 6541 | ((struct lysp_notif *)dev_pnode)->data = NULL; |
| 6542 | lysp_notif_free((struct ly_ctx *)ctx, (struct lysp_notif *)dev_pnode); |
| 6543 | free(dev_pnode); |
| 6544 | return; |
| 6545 | case LYS_RPC: |
| 6546 | case LYS_ACTION: |
| 6547 | ((struct lysp_action *)dev_pnode)->input.data = NULL; |
| 6548 | ((struct lysp_action *)dev_pnode)->output.data = NULL; |
| 6549 | lysp_action_free((struct ly_ctx *)ctx, (struct lysp_action *)dev_pnode); |
| 6550 | free(dev_pnode); |
| 6551 | return; |
| 6552 | case LYS_INPUT: |
| 6553 | case LYS_OUTPUT: |
| 6554 | ((struct lysp_action_inout *)dev_pnode)->data = NULL; |
| 6555 | lysp_action_inout_free((struct ly_ctx *)ctx, (struct lysp_action_inout *)dev_pnode); |
| 6556 | free(dev_pnode); |
| 6557 | return; |
| 6558 | default: |
| 6559 | LOGINT(ctx); |
| 6560 | return; |
| 6561 | } |
| 6562 | |
| 6563 | lysp_node_free((struct ly_ctx *)ctx, dev_pnode); |
| 6564 | } |
| 6565 | |
| 6566 | /** |
| 6567 | * @brief Compile and apply any precompiled deviations and refines targetting a node. |
| 6568 | * |
| 6569 | * @param[in] ctx Compile context. |
| 6570 | * @param[in] pnode Parsed node to consider. |
| 6571 | * @param[in] parent First compiled parent of @p pnode. |
| 6572 | * @param[out] dev_pnode Copy of parsed node @p pnode with deviations and refines, if any. NULL if there are none. |
| 6573 | * @param[out] no_supported Whether a not-supported deviation is defined for the node. |
| 6574 | * @return LY_ERR value. |
| 6575 | */ |
| 6576 | static LY_ERR |
| 6577 | lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent, |
| 6578 | struct lysp_node **dev_pnode, ly_bool *not_supported) |
| 6579 | { |
| 6580 | LY_ERR ret = LY_SUCCESS; |
| 6581 | uint32_t i; |
| 6582 | LY_ARRAY_COUNT_TYPE u; |
| 6583 | struct lys_module *orig_mod = ctx->mod, *orig_mod_def = ctx->mod_def; |
| 6584 | char orig_path[LYSC_CTX_BUFSIZE]; |
| 6585 | struct lysc_refine *rfn; |
| 6586 | struct lysc_deviation *dev; |
| 6587 | struct lysp_deviation *dev_p; |
| 6588 | struct lysp_deviate *d; |
| 6589 | |
| 6590 | *dev_pnode = NULL; |
| 6591 | *not_supported = 0; |
| 6592 | |
| 6593 | for (i = 0; i < ctx->uses_rfns.count; ++i) { |
| 6594 | rfn = ctx->uses_rfns.objs[i]; |
| 6595 | |
| 6596 | if (!lysp_schema_nodeid_match(rfn->nodeid, ctx->mod, rfn->nodeid_ctx_node, parent, pnode, ctx->mod)) { |
| 6597 | /* not our target node */ |
| 6598 | continue; |
| 6599 | } |
| 6600 | |
| 6601 | if (!*dev_pnode) { |
| 6602 | /* first refine on this node, create a copy first */ |
| 6603 | LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup); |
| 6604 | } |
| 6605 | |
| 6606 | /* apply all the refines by changing (the copy of) the parsed node */ |
| 6607 | LY_ARRAY_FOR(rfn->rfns, u) { |
| 6608 | /* apply refine, keep the current path and add to it */ |
| 6609 | lysc_update_path(ctx, NULL, "{refine}"); |
| 6610 | lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid); |
| 6611 | ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode); |
| 6612 | lysc_update_path(ctx, NULL, NULL); |
| 6613 | lysc_update_path(ctx, NULL, NULL); |
| 6614 | LY_CHECK_GOTO(ret, cleanup); |
| 6615 | } |
| 6616 | |
| 6617 | /* refine was applied, remove it */ |
| 6618 | lysc_refine_free(ctx->ctx, rfn); |
| 6619 | ly_set_rm_index(&ctx->uses_rfns, i, NULL); |
| 6620 | |
| 6621 | /* all the refines for one target node are in one structure, we are done */ |
| 6622 | break; |
| 6623 | } |
| 6624 | |
| 6625 | for (i = 0; i < ctx->devs.count; ++i) { |
| 6626 | dev = ctx->devs.objs[i]; |
| 6627 | |
| 6628 | if (!lysp_schema_nodeid_match(dev->nodeid, dev->nodeid_mod, NULL, parent, pnode, ctx->mod_def)) { |
| 6629 | /* not our target node */ |
| 6630 | continue; |
| 6631 | } |
| 6632 | |
| 6633 | if (dev->not_supported) { |
| 6634 | /* it is not supported, no more deviations */ |
| 6635 | *not_supported = 1; |
| 6636 | goto dev_applied; |
| 6637 | } |
| 6638 | |
| 6639 | if (!*dev_pnode) { |
| 6640 | /* first deviation on this node, create a copy first */ |
| 6641 | LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup); |
| 6642 | } |
| 6643 | |
| 6644 | /* apply all the deviates by changing (the copy of) the parsed node */ |
| 6645 | LY_ARRAY_FOR(dev->devs, u) { |
| 6646 | dev_p = dev->devs[u]; |
| 6647 | LY_LIST_FOR(dev_p->deviates, d) { |
| 6648 | /* generate correct path */ |
| 6649 | strcpy(orig_path, ctx->path); |
| 6650 | ctx->path_len = 1; |
| 6651 | ctx->mod = (struct lys_module *)dev->dev_mods[u]; |
| 6652 | ctx->mod_def = (struct lys_module *)dev->dev_mods[u]; |
| 6653 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 6654 | lysc_update_path(ctx, NULL, dev_p->nodeid); |
| 6655 | |
| 6656 | switch (d->mod) { |
| 6657 | case LYS_DEV_ADD: |
| 6658 | ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode); |
| 6659 | break; |
| 6660 | case LYS_DEV_DELETE: |
| 6661 | ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode); |
| 6662 | break; |
| 6663 | case LYS_DEV_REPLACE: |
| 6664 | ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode); |
| 6665 | break; |
| 6666 | default: |
| 6667 | LOGINT(ctx->ctx); |
| 6668 | ret = LY_EINT; |
| 6669 | } |
| 6670 | |
| 6671 | /* restore previous path */ |
| 6672 | strcpy(ctx->path, orig_path); |
| 6673 | ctx->path_len = strlen(ctx->path); |
| 6674 | ctx->mod = orig_mod; |
| 6675 | ctx->mod_def = orig_mod_def; |
| 6676 | |
| 6677 | LY_CHECK_GOTO(ret, cleanup); |
| 6678 | } |
| 6679 | } |
| 6680 | |
| 6681 | dev_applied: |
| 6682 | /* deviation was applied, remove it */ |
| 6683 | lysc_deviation_free(ctx->ctx, dev); |
| 6684 | ly_set_rm_index(&ctx->devs, i, NULL); |
| 6685 | |
| 6686 | /* all the deviations for one target node are in one structure, we are done */ |
| 6687 | break; |
| 6688 | } |
| 6689 | |
| 6690 | cleanup: |
| 6691 | if (ret) { |
| 6692 | lysp_dev_node_free(ctx->ctx, *dev_pnode); |
| 6693 | *dev_pnode = NULL; |
| 6694 | *not_supported = 0; |
| 6695 | } |
| 6696 | return ret; |
| 6697 | } |
| 6698 | |
| 6699 | /** |
| 6700 | * @brief Compile and apply any precompiled top-level or uses augments targetting a node. |
| 6701 | * |
| 6702 | * @param[in] ctx Compile context. |
| 6703 | * @param[in] node Compiled node to consider. |
| 6704 | * @return LY_ERR value. |
| 6705 | */ |
| 6706 | static LY_ERR |
| 6707 | lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node) |
| 6708 | { |
| 6709 | LY_ERR ret = LY_SUCCESS; |
| 6710 | struct lys_module *orig_mod = ctx->mod, *orig_mod_def = ctx->mod_def; |
| 6711 | uint32_t i; |
| 6712 | char orig_path[LYSC_CTX_BUFSIZE]; |
| 6713 | struct lysc_augment *aug; |
| 6714 | |
| 6715 | /* uses augments */ |
| 6716 | for (i = 0; i < ctx->uses_augs.count; ) { |
| 6717 | aug = ctx->uses_augs.objs[i]; |
| 6718 | |
| 6719 | if (!lysp_schema_nodeid_match(aug->nodeid, ctx->mod, aug->nodeid_ctx_node, node, NULL, NULL)) { |
| 6720 | /* not our target node */ |
| 6721 | ++i; |
| 6722 | continue; |
| 6723 | } |
| 6724 | |
| 6725 | /* apply augment, keep the current path and add to it */ |
| 6726 | lysc_update_path(ctx, NULL, "{augment}"); |
| 6727 | lysc_update_path(ctx, NULL, aug->aug_p->nodeid); |
| 6728 | ret = lys_compile_augment(ctx, aug->aug_p, node); |
| 6729 | lysc_update_path(ctx, NULL, NULL); |
| 6730 | lysc_update_path(ctx, NULL, NULL); |
| 6731 | LY_CHECK_GOTO(ret, cleanup); |
| 6732 | |
| 6733 | /* 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] | 6734 | ly_set_rm(&ctx->uses_augs, aug, NULL); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 6735 | lysc_augment_free(ctx->ctx, aug); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6736 | } |
| 6737 | |
| 6738 | /* top-level augments */ |
| 6739 | for (i = 0; i < ctx->augs.count; ) { |
| 6740 | aug = ctx->augs.objs[i]; |
| 6741 | |
| 6742 | if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_mod, NULL, node, NULL, NULL)) { |
| 6743 | /* not our target node */ |
| 6744 | ++i; |
| 6745 | continue; |
| 6746 | } |
| 6747 | |
| 6748 | /* apply augment, use the path and modules from the augment */ |
| 6749 | strcpy(orig_path, ctx->path); |
| 6750 | ctx->path_len = 1; |
| 6751 | lysc_update_path(ctx, NULL, "{augment}"); |
| 6752 | lysc_update_path(ctx, NULL, aug->aug_p->nodeid); |
| 6753 | ctx->mod = (struct lys_module *)aug->nodeid_mod; |
| 6754 | ctx->mod_def = (struct lys_module *)aug->nodeid_mod; |
| 6755 | ret = lys_compile_augment(ctx, aug->aug_p, node); |
| 6756 | strcpy(ctx->path, orig_path); |
| 6757 | ctx->path_len = strlen(ctx->path); |
| 6758 | LY_CHECK_GOTO(ret, cleanup); |
| 6759 | |
| 6760 | /* augment was applied, remove it */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6761 | ly_set_rm(&ctx->augs, aug, NULL); |
Michal Vasko | af70245 | 2020-10-02 09:02:55 +0200 | [diff] [blame] | 6762 | lysc_augment_free(ctx->ctx, aug); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6763 | } |
| 6764 | |
| 6765 | cleanup: |
| 6766 | ctx->mod = orig_mod; |
| 6767 | ctx->mod_def = orig_mod_def; |
| 6768 | return ret; |
| 6769 | } |
| 6770 | |
| 6771 | /** |
| 6772 | * @brief Prepare a top-level augment to be applied during data nodes compilation. |
| 6773 | * |
| 6774 | * @param[in] ctx Compile context. |
| 6775 | * @param[in] aug_p Parsed augment to be applied. |
| 6776 | * @param[in] mod_def Local module for @p aug_p. |
| 6777 | * @return LY_ERR value. |
| 6778 | */ |
| 6779 | static LY_ERR |
| 6780 | lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lys_module *mod_def) |
| 6781 | { |
| 6782 | LY_ERR ret = LY_SUCCESS; |
| 6783 | struct lyxp_expr *exp = NULL; |
| 6784 | struct lysc_augment *aug; |
| 6785 | const struct lys_module *mod; |
| 6786 | |
| 6787 | /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */ |
| 6788 | ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp); |
| 6789 | LY_CHECK_GOTO(ret, cleanup); |
| 6790 | |
| 6791 | mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], mod_def, NULL, NULL); |
| 6792 | LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup); |
| 6793 | if (mod != ctx->mod) { |
| 6794 | /* augment for another module, ignore */ |
| 6795 | goto cleanup; |
| 6796 | } |
| 6797 | |
| 6798 | /* allocate new compiled augment and store it in the set */ |
| 6799 | aug = calloc(1, sizeof *aug); |
| 6800 | LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 6801 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, LY_SET_OPT_USEASLIST, NULL), cleanup); |
| 6802 | |
| 6803 | aug->nodeid = exp; |
| 6804 | exp = NULL; |
| 6805 | aug->nodeid_mod = mod_def; |
| 6806 | aug->aug_p = aug_p; |
| 6807 | |
| 6808 | cleanup: |
| 6809 | lyxp_expr_free(ctx->ctx, exp); |
| 6810 | return ret; |
| 6811 | } |
| 6812 | |
| 6813 | /** |
| 6814 | * @brief Prepare all top-level augments for the current module to be applied during data nodes compilation. |
| 6815 | * |
| 6816 | * @param[in] ctx Compile context. |
| 6817 | * @return LY_ERR value. |
| 6818 | */ |
| 6819 | static LY_ERR |
| 6820 | lys_precompile_own_augments(struct lysc_ctx *ctx) |
| 6821 | { |
| 6822 | LY_ARRAY_COUNT_TYPE u, v, w; |
| 6823 | const struct lys_module *aug_mod; |
| 6824 | |
| 6825 | LY_ARRAY_FOR(ctx->mod->augmented_by, u) { |
| 6826 | aug_mod = ctx->mod->augmented_by[u]; |
| 6827 | |
| 6828 | /* collect all module augments */ |
| 6829 | LY_ARRAY_FOR(aug_mod->parsed->augments, v) { |
| 6830 | LY_CHECK_RET(lys_precompile_own_augment(ctx, &aug_mod->parsed->augments[v], aug_mod)); |
| 6831 | } |
| 6832 | |
| 6833 | /* collect all submodules augments */ |
| 6834 | LY_ARRAY_FOR(aug_mod->parsed->includes, v) { |
| 6835 | LY_ARRAY_FOR(aug_mod->parsed->includes[v].submodule->augments, w) { |
| 6836 | LY_CHECK_RET(lys_precompile_own_augment(ctx, &aug_mod->parsed->includes[v].submodule->augments[w], aug_mod)); |
| 6837 | } |
| 6838 | } |
| 6839 | } |
| 6840 | |
| 6841 | return LY_SUCCESS; |
| 6842 | } |
| 6843 | |
| 6844 | /** |
| 6845 | * @brief Prepare a deviation to be applied during data nodes compilation. |
| 6846 | * |
| 6847 | * @param[in] ctx Compile context. |
| 6848 | * @param[in] dev_p Parsed deviation to be applied. |
| 6849 | * @param[in] mod_def Local module for @p dev_p. |
| 6850 | * @return LY_ERR value. |
| 6851 | */ |
| 6852 | static LY_ERR |
| 6853 | lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lys_module *mod_def) |
| 6854 | { |
| 6855 | LY_ERR ret = LY_SUCCESS; |
| 6856 | struct lysc_deviation *dev = NULL; |
| 6857 | struct lyxp_expr *exp = NULL; |
| 6858 | struct lysp_deviation **new_dev; |
| 6859 | const struct lys_module *mod, **new_dev_mod; |
| 6860 | uint32_t i; |
| 6861 | |
| 6862 | /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */ |
| 6863 | ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp); |
| 6864 | LY_CHECK_GOTO(ret, cleanup); |
| 6865 | |
| 6866 | mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], mod_def, NULL, NULL); |
| 6867 | LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup); |
| 6868 | if (mod != ctx->mod) { |
| 6869 | /* deviation for another module, ignore */ |
| 6870 | goto cleanup; |
| 6871 | } |
| 6872 | |
| 6873 | /* try to find the node in already compiled deviations */ |
| 6874 | for (i = 0; i < ctx->devs.count; ++i) { |
| 6875 | if (lys_abs_schema_nodeid_match(ctx->ctx, exp, mod_def, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid, |
| 6876 | ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid_mod)) { |
| 6877 | dev = ctx->devs.objs[i]; |
| 6878 | break; |
| 6879 | } |
| 6880 | } |
| 6881 | |
| 6882 | if (!dev) { |
| 6883 | /* allocate new compiled deviation */ |
| 6884 | dev = calloc(1, sizeof *dev); |
| 6885 | LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 6886 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, LY_SET_OPT_USEASLIST, NULL), cleanup); |
| 6887 | |
| 6888 | dev->nodeid = exp; |
| 6889 | exp = NULL; |
| 6890 | dev->nodeid_mod = mod_def; |
| 6891 | } |
| 6892 | |
| 6893 | /* add new parsed deviation structure */ |
| 6894 | LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup); |
| 6895 | *new_dev = dev_p; |
| 6896 | LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_mods, new_dev_mod, ret, cleanup); |
| 6897 | *new_dev_mod = mod_def; |
| 6898 | |
| 6899 | cleanup: |
| 6900 | lyxp_expr_free(ctx->ctx, exp); |
| 6901 | return ret; |
| 6902 | } |
| 6903 | |
| 6904 | /** |
| 6905 | * @brief Prepare all deviations for the current module to be applied during data nodes compilation. |
| 6906 | * |
| 6907 | * @param[in] ctx Compile context. |
| 6908 | * @return LY_ERR value. |
| 6909 | */ |
| 6910 | static LY_ERR |
| 6911 | lys_precompile_own_deviations(struct lysc_ctx *ctx) |
| 6912 | { |
| 6913 | LY_ARRAY_COUNT_TYPE u, v, w; |
| 6914 | const struct lys_module *dev_mod; |
| 6915 | struct lysc_deviation *dev; |
| 6916 | struct lysp_deviate *d; |
| 6917 | int not_supported; |
| 6918 | uint32_t i; |
| 6919 | |
| 6920 | LY_ARRAY_FOR(ctx->mod->deviated_by, u) { |
| 6921 | dev_mod = ctx->mod->deviated_by[u]; |
| 6922 | |
| 6923 | /* compile all module deviations */ |
| 6924 | LY_ARRAY_FOR(dev_mod->parsed->deviations, v) { |
| 6925 | LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod)); |
| 6926 | } |
| 6927 | |
| 6928 | /* compile all submodules deviations */ |
| 6929 | LY_ARRAY_FOR(dev_mod->parsed->includes, v) { |
| 6930 | LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) { |
| 6931 | LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w], dev_mod)); |
| 6932 | } |
| 6933 | } |
| 6934 | } |
| 6935 | |
| 6936 | /* set not-supported flags for all the deviations */ |
| 6937 | for (i = 0; i < ctx->devs.count; ++i) { |
| 6938 | dev = ctx->devs.objs[i]; |
| 6939 | not_supported = 0; |
| 6940 | |
| 6941 | LY_ARRAY_FOR(dev->devs, u) { |
| 6942 | LY_LIST_FOR(dev->devs[u]->deviates, d) { |
| 6943 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 6944 | not_supported = 1; |
| 6945 | break; |
| 6946 | } |
| 6947 | } |
| 6948 | if (not_supported) { |
| 6949 | break; |
| 6950 | } |
| 6951 | } |
| 6952 | if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) { |
| 6953 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6954 | "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr); |
| 6955 | return LY_EVALID; |
| 6956 | } |
| 6957 | |
| 6958 | dev->not_supported = not_supported; |
| 6959 | } |
| 6960 | |
| 6961 | return LY_SUCCESS; |
| 6962 | } |
| 6963 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 6964 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6965 | * @brief Compile parsed schema node information. |
| 6966 | * @param[in] ctx Compile context |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6967 | * @param[in] pnode Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6968 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 6969 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 6970 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 6971 | * @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). |
| 6972 | * 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] | 6973 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6974 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6975 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6976 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status, |
| 6977 | struct ly_set *child_set) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6978 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 6979 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 6980 | struct lysc_node *node = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 6981 | struct lysc_when **when; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6982 | struct lysp_node *dev_pnode = NULL, *orig_pnode = pnode; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6983 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6984 | ly_bool not_supported; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 6985 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6986 | 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] | 6987 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6988 | if (pnode->nodetype != LYS_USES) { |
| 6989 | lysc_update_path(ctx, parent, pnode->name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6990 | } else { |
| 6991 | lysc_update_path(ctx, NULL, "{uses}"); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6992 | lysc_update_path(ctx, NULL, pnode->name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6993 | } |
| 6994 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 6995 | switch (pnode->nodetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6996 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6997 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6998 | node_compile_spec = lys_compile_node_container; |
| 6999 | break; |
| 7000 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7001 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7002 | node_compile_spec = lys_compile_node_leaf; |
| 7003 | break; |
| 7004 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7005 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 7006 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7007 | break; |
| 7008 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7009 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 7010 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7011 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7012 | case LYS_CHOICE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7013 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 7014 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7015 | break; |
Michal Vasko | 891d753 | 2020-10-07 09:41:38 +0200 | [diff] [blame] | 7016 | case LYS_CASE: |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7017 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case)); |
| 7018 | node_compile_spec = lys_compile_node_case; |
| 7019 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7020 | case LYS_ANYXML: |
| 7021 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7022 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 7023 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7024 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7025 | case LYS_USES: |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7026 | 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] | 7027 | lysc_update_path(ctx, NULL, NULL); |
| 7028 | lysc_update_path(ctx, NULL, NULL); |
| 7029 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7030 | default: |
| 7031 | LOGINT(ctx->ctx); |
| 7032 | return LY_EINT; |
| 7033 | } |
| 7034 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7035 | |
| 7036 | /* compile any deviations for this node */ |
| 7037 | LY_CHECK_ERR_RET(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, ¬_supported), |
| 7038 | free(node), ret); |
| 7039 | if (not_supported) { |
| 7040 | free(node); |
| 7041 | lysc_update_path(ctx, NULL, NULL); |
| 7042 | return LY_SUCCESS; |
| 7043 | } else if (dev_pnode) { |
| 7044 | pnode = dev_pnode; |
| 7045 | } |
| 7046 | |
| 7047 | node->nodetype = pnode->nodetype; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7048 | node->module = ctx->mod; |
| 7049 | node->prev = node; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7050 | node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7051 | |
| 7052 | /* config */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7053 | ret = lys_compile_config(ctx, node, parent); |
| 7054 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7055 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7056 | /* list ordering */ |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 7057 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 7058 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 7059 | 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] | 7060 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 7061 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 7062 | node->flags &= ~LYS_ORDBY_MASK; |
| 7063 | node->flags |= LYS_ORDBY_SYSTEM; |
| 7064 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 7065 | /* default ordering is system */ |
| 7066 | node->flags |= LYS_ORDBY_SYSTEM; |
| 7067 | } |
| 7068 | } |
| 7069 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7070 | /* status - it is not inherited by specification, but it does not make sense to have |
| 7071 | * 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] | 7072 | 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] | 7073 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7074 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7075 | node->sp = orig_pnode; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7076 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7077 | DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error); |
| 7078 | DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error); |
| 7079 | DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error); |
| 7080 | if (pnode->when) { |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 7081 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7082 | LY_CHECK_GOTO(ret = lys_compile_when(ctx, pnode->when, pnode->flags, node, when), error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7083 | |
| 7084 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 7085 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7086 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->xpath, node, 0, NULL), error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7087 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 7088 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7089 | 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] | 7090 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7091 | /* 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] | 7092 | LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup); |
| 7093 | |
| 7094 | /* connect any augments */ |
| 7095 | LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7096 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7097 | /* nodetype-specific part */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7098 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7099 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7100 | /* final compilation tasks that require the node to be connected */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7101 | 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] | 7102 | if (node->flags & LYS_MAND_TRUE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 7103 | /* inherit LYS_MAND_TRUE in parent containers */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 7104 | lys_compile_mandatory_parents(parent, 1); |
| 7105 | } |
| 7106 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7107 | if (child_set) { |
| 7108 | /* add the new node into set */ |
| 7109 | LY_CHECK_GOTO(ret = ly_set_add(child_set, node, LY_SET_OPT_USEASLIST, NULL), cleanup); |
| 7110 | } |
| 7111 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7112 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7113 | lysp_dev_node_free(ctx->ctx, dev_pnode); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7114 | return LY_SUCCESS; |
| 7115 | |
| 7116 | error: |
| 7117 | lysc_node_free(ctx->ctx, node); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7118 | cleanup: |
| 7119 | if (dev_pnode) { |
| 7120 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_OTHER, "Compilation of a deviated and/or refined node failed."); |
| 7121 | lysp_dev_node_free(ctx->ctx, dev_pnode); |
| 7122 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7123 | return ret; |
| 7124 | } |
| 7125 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7126 | /** |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7127 | * @brief Add a module reference into an array, checks for duplicities. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7128 | * |
| 7129 | * @param[in] ctx Compile context. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7130 | * @param[in] mod Module reference to add. |
| 7131 | * @param[in,out] mod_array Module sized array to add to. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7132 | * @return LY_ERR value. |
| 7133 | */ |
| 7134 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7135 | 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] | 7136 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7137 | LY_ARRAY_COUNT_TYPE u; |
| 7138 | struct lys_module **new_mod; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7139 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7140 | LY_ARRAY_FOR(*mod_array, u) { |
| 7141 | if ((*mod_array)[u] == mod) { |
| 7142 | /* already there */ |
| 7143 | return LY_EEXIST; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7144 | } |
| 7145 | } |
| 7146 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7147 | /* add the new module ref */ |
| 7148 | LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM); |
| 7149 | *new_mod = mod; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7150 | |
| 7151 | return LY_SUCCESS; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7152 | } |
| 7153 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7154 | /** |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7155 | * @brief Compile top-level augments and deviations defined in the current module. |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 7156 | * Generally, just add the module refence to the target modules. But in case |
| 7157 | * of foreign augments, they are directly applied. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7158 | * |
| 7159 | * @param[in] ctx Compile context. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7160 | * @return LY_ERR value. |
| 7161 | */ |
| 7162 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7163 | lys_precompile_augments_deviations(struct lysc_ctx *ctx) |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7164 | { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7165 | LY_ERR ret = LY_SUCCESS; |
| 7166 | LY_ARRAY_COUNT_TYPE u, v; |
| 7167 | const struct lysp_module *mod_p; |
| 7168 | const struct lysc_node *target; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7169 | struct lys_module *mod; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7170 | struct lysp_submodule *submod; |
| 7171 | ly_bool has_dev = 0; |
| 7172 | uint16_t flags; |
| 7173 | uint32_t idx, opt_prev = ctx->options; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7174 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 7175 | for (idx = 0; idx < ctx->ctx->implementing.count; ++idx) { |
| 7176 | if (ctx->mod == ctx->ctx->implementing.objs[idx]) { |
| 7177 | break; |
| 7178 | } |
| 7179 | } |
| 7180 | if (idx == ctx->ctx->implementing.count) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7181 | /* it was already implemented and all the augments and deviations fully applied */ |
| 7182 | return LY_SUCCESS; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7183 | } |
| 7184 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 7185 | mod_p = ctx->mod->parsed; |
| 7186 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7187 | LY_ARRAY_FOR(mod_p->augments, u) { |
| 7188 | lysc_update_path(ctx, NULL, "{augment}"); |
| 7189 | lysc_update_path(ctx, NULL, mod_p->augments[u].nodeid); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7190 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7191 | /* get target module */ |
| 7192 | ret = lys_nodeid_check(ctx, mod_p->augments[u].nodeid, 1, &mod, NULL); |
| 7193 | LY_CHECK_RET(ret); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7194 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7195 | /* add this module into the target module augmented_by, if not there already from previous augments */ |
| 7196 | lys_array_add_mod_ref(ctx, ctx->mod, &mod->augmented_by); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 7197 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7198 | /* if we are compiling this module, we cannot add augments to it yet */ |
| 7199 | if (mod != ctx->mod) { |
| 7200 | /* apply the augment, find the target node first */ |
| 7201 | flags = 0; |
| 7202 | 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] | 7203 | LY_CHECK_RET(ret); |
| 7204 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7205 | /* apply the augment */ |
| 7206 | ctx->options |= flags; |
| 7207 | ret = lys_compile_augment(ctx, &mod_p->augments[u], (struct lysc_node *)target); |
| 7208 | ctx->options = opt_prev; |
| 7209 | LY_CHECK_RET(ret); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7210 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7211 | |
| 7212 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7213 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7214 | } |
| 7215 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7216 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 7217 | /* get target module */ |
| 7218 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 7219 | lysc_update_path(ctx, NULL, mod_p->deviations[u].nodeid); |
| 7220 | ret = lys_nodeid_check(ctx, mod_p->deviations[u].nodeid, 1, &mod, NULL); |
| 7221 | lysc_update_path(ctx, NULL, NULL); |
| 7222 | lysc_update_path(ctx, NULL, NULL); |
| 7223 | LY_CHECK_RET(ret); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 7224 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7225 | /* add this module into the target module deviated_by, if not there already from previous deviations */ |
| 7226 | lys_array_add_mod_ref(ctx, ctx->mod, &mod->deviated_by); |
| 7227 | |
| 7228 | /* new deviation added to the target module */ |
| 7229 | has_dev = 1; |
| 7230 | } |
| 7231 | |
| 7232 | /* the same for augments and deviations in submodules */ |
| 7233 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 7234 | submod = mod_p->includes[v].submodule; |
| 7235 | LY_ARRAY_FOR(submod->augments, u) { |
| 7236 | lysc_update_path(ctx, NULL, "{augment}"); |
| 7237 | lysc_update_path(ctx, NULL, submod->augments[u].nodeid); |
| 7238 | |
| 7239 | ret = lys_nodeid_check(ctx, submod->augments[u].nodeid, 1, &mod, NULL); |
| 7240 | LY_CHECK_RET(ret); |
| 7241 | |
| 7242 | lys_array_add_mod_ref(ctx, ctx->mod, &mod->augmented_by); |
| 7243 | if (mod != ctx->mod) { |
| 7244 | flags = 0; |
| 7245 | ret = lysc_resolve_schema_nodeid(ctx, mod_p->augments[u].nodeid, 0, NULL, ctx->mod_def, 0, &target, &flags); |
| 7246 | LY_CHECK_RET(ret); |
| 7247 | |
| 7248 | ctx->options |= flags; |
| 7249 | ret = lys_compile_augment(ctx, &submod->augments[u], (struct lysc_node *)target); |
| 7250 | ctx->options = opt_prev; |
| 7251 | LY_CHECK_RET(ret); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 7252 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7253 | |
| 7254 | lysc_update_path(ctx, NULL, NULL); |
| 7255 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 7256 | } |
| 7257 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7258 | LY_ARRAY_FOR(submod->deviations, u) { |
| 7259 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 7260 | lysc_update_path(ctx, NULL, submod->deviations[u].nodeid); |
| 7261 | ret = lys_nodeid_check(ctx, submod->deviations[u].nodeid, 1, &mod, NULL); |
| 7262 | lysc_update_path(ctx, NULL, NULL); |
| 7263 | lysc_update_path(ctx, NULL, NULL); |
| 7264 | LY_CHECK_RET(ret); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 7265 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7266 | lys_array_add_mod_ref(ctx, ctx->mod, &mod->deviated_by); |
| 7267 | has_dev = 1; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 7268 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7269 | } |
| 7270 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7271 | if (!has_dev) { |
| 7272 | /* no need to recompile any modules */ |
| 7273 | return LY_SUCCESS; |
| 7274 | } |
| 7275 | |
| 7276 | /* free all the modules in descending order */ |
| 7277 | idx = ctx->ctx->list.count; |
| 7278 | do { |
| 7279 | --idx; |
| 7280 | mod = ctx->ctx->list.objs[idx]; |
| 7281 | /* skip this module */ |
| 7282 | if (mod == mod_p->mod) { |
| 7283 | continue; |
| 7284 | } |
| 7285 | |
| 7286 | if (mod->implemented && mod->compiled) { |
| 7287 | /* keep information about features state in the module */ |
| 7288 | lys_feature_precompile_revert(ctx, mod); |
| 7289 | |
| 7290 | /* free the module */ |
| 7291 | lysc_module_free(mod->compiled, NULL); |
| 7292 | mod->compiled = NULL; |
| 7293 | } |
| 7294 | } while (idx); |
| 7295 | |
| 7296 | /* recompile all the modules in ascending order */ |
| 7297 | for (idx = 0; idx < ctx->ctx->list.count; ++idx) { |
| 7298 | mod = ctx->ctx->list.objs[idx]; |
| 7299 | |
| 7300 | /* skip this module */ |
| 7301 | if (mod == mod_p->mod) { |
| 7302 | continue; |
| 7303 | } |
| 7304 | |
| 7305 | if (mod->implemented) { |
| 7306 | /* compile */ |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 7307 | LY_CHECK_GOTO(ret = lys_compile(mod, 0), cleanup); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7308 | } |
| 7309 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7310 | |
| 7311 | cleanup: |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7312 | return ret; |
| 7313 | } |
| 7314 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7315 | static void * |
| 7316 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 7317 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7318 | for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7319 | if (substmts[u].stmt == stmt) { |
| 7320 | return substmts[u].storage; |
| 7321 | } |
| 7322 | } |
| 7323 | return NULL; |
| 7324 | } |
| 7325 | |
| 7326 | LY_ERR |
| 7327 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 7328 | { |
| 7329 | LY_ERR ret = LY_EVALID, r; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7330 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7331 | struct lysp_stmt *stmt; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7332 | struct lysp_qname qname; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7333 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7334 | |
| 7335 | /* check for invalid substatements */ |
| 7336 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 7337 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 7338 | continue; |
| 7339 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7340 | for (u = 0; substmts[u].stmt; ++u) { |
| 7341 | if (substmts[u].stmt == stmt->kw) { |
| 7342 | break; |
| 7343 | } |
| 7344 | } |
| 7345 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7346 | 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^] | 7347 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7348 | goto cleanup; |
| 7349 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7350 | } |
| 7351 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7352 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 7353 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7354 | /* keep order of the processing the same as the order in the defined substmts, |
| 7355 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 7356 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7357 | ly_bool stmt_present = 0; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7358 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7359 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 7360 | if (substmts[u].stmt != stmt->kw) { |
| 7361 | continue; |
| 7362 | } |
| 7363 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7364 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7365 | if (substmts[u].storage) { |
| 7366 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7367 | case LY_STMT_STATUS: |
| 7368 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 7369 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 7370 | break; |
| 7371 | case LY_STMT_UNITS: { |
| 7372 | const char **units; |
| 7373 | |
| 7374 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 7375 | /* single item */ |
| 7376 | if (*((const char **)substmts[u].storage)) { |
| 7377 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 7378 | goto cleanup; |
| 7379 | } |
| 7380 | units = (const char **)substmts[u].storage; |
| 7381 | } else { |
| 7382 | /* sized array */ |
| 7383 | const char ***units_array = (const char ***)substmts[u].storage; |
| 7384 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 7385 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7386 | r = lydict_insert(ctx->ctx, stmt->arg, 0, units); |
| 7387 | LY_CHECK_ERR_GOTO(r, ret = r, cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7388 | break; |
| 7389 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7390 | case LY_STMT_TYPE: { |
| 7391 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 7392 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 7393 | |
| 7394 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 7395 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7396 | if (*(struct lysc_type **)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7397 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 7398 | goto cleanup; |
| 7399 | } |
| 7400 | compiled = substmts[u].storage; |
| 7401 | } else { |
| 7402 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7403 | struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7404 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7405 | compiled = (void *)type; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7406 | } |
| 7407 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7408 | 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] | 7409 | 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^] | 7410 | flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type **)compiled, |
| 7411 | 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] | 7412 | lysp_type_free(ctx->ctx, parsed); |
| 7413 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7414 | break; |
| 7415 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7416 | case LY_STMT_IF_FEATURE: { |
| 7417 | struct lysc_iffeature *iff = NULL; |
| 7418 | |
| 7419 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 7420 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7421 | if (((struct lysc_iffeature *)substmts[u].storage)->features) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7422 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 7423 | goto cleanup; |
| 7424 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7425 | iff = (struct lysc_iffeature *)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7426 | } else { |
| 7427 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7428 | struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7429 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 7430 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7431 | qname.str = stmt->arg; |
| 7432 | qname.mod = ctx->mod_def; |
| 7433 | 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] | 7434 | break; |
| 7435 | } |
| 7436 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 7437 | * 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] | 7438 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 7439 | 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^] | 7440 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7441 | goto cleanup; |
| 7442 | } |
| 7443 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7444 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7445 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 7446 | 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] | 7447 | 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^] | 7448 | 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] | 7449 | goto cleanup; |
| 7450 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7451 | } |
| 7452 | |
| 7453 | ret = LY_SUCCESS; |
| 7454 | |
| 7455 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7456 | return ret; |
| 7457 | } |
| 7458 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7459 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7460 | * @brief Check when for cyclic dependencies. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7461 | * |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7462 | * @param[in] set Set with all the referenced nodes. |
| 7463 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 7464 | * @return LY_ERR value |
| 7465 | */ |
| 7466 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7467 | 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] | 7468 | { |
| 7469 | struct lyxp_set tmp_set; |
| 7470 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7471 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7472 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7473 | struct lysc_when *when; |
| 7474 | LY_ERR ret = LY_SUCCESS; |
| 7475 | |
| 7476 | memset(&tmp_set, 0, sizeof tmp_set); |
| 7477 | |
| 7478 | /* prepare in_ctx of the set */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 7479 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7480 | xp_scnode = &set->val.scnodes[i]; |
| 7481 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7482 | if (xp_scnode->in_ctx != -1) { |
| 7483 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7484 | xp_scnode->in_ctx = 1; |
| 7485 | } |
| 7486 | } |
| 7487 | |
| 7488 | for (i = 0; i < set->used; ++i) { |
| 7489 | xp_scnode = &set->val.scnodes[i]; |
| 7490 | if (xp_scnode->in_ctx != 1) { |
| 7491 | /* already checked */ |
| 7492 | continue; |
| 7493 | } |
| 7494 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 7495 | if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) || |
| 7496 | !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7497 | /* no when to check */ |
| 7498 | xp_scnode->in_ctx = 0; |
| 7499 | continue; |
| 7500 | } |
| 7501 | |
| 7502 | node = xp_scnode->scnode; |
| 7503 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7504 | LY_ARRAY_FOR(node->when, u) { |
| 7505 | when = node->when[u]; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 7506 | ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 7507 | 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] | 7508 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7509 | 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] | 7510 | goto cleanup; |
| 7511 | } |
| 7512 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7513 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7514 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7515 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7516 | /* try to find this node in our set */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 7517 | uint32_t idx; |
| 7518 | 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] | 7519 | 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] | 7520 | ret = LY_EVALID; |
| 7521 | goto cleanup; |
| 7522 | } |
| 7523 | |
| 7524 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7525 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7526 | } else { |
| 7527 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7528 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7529 | } |
| 7530 | } |
| 7531 | |
| 7532 | /* merge this set into the global when set */ |
| 7533 | lyxp_set_scnode_merge(set, &tmp_set); |
| 7534 | } |
| 7535 | |
| 7536 | /* check when of non-data parents as well */ |
| 7537 | node = node->parent; |
| 7538 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 7539 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7540 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 7541 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7542 | } |
| 7543 | |
| 7544 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7545 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7546 | return ret; |
| 7547 | } |
| 7548 | |
| 7549 | /** |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7550 | * @brief Check when/must expressions of a node on a complete compiled schema tree. |
| 7551 | * |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7552 | * @param[in] ctx Compile context. |
| 7553 | * @param[in] node Node to check. |
| 7554 | * @return LY_ERR value |
| 7555 | */ |
| 7556 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7557 | 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] | 7558 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7559 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7560 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7561 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7562 | uint32_t opts; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7563 | ly_bool input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7564 | struct lysc_when **when = NULL; |
| 7565 | struct lysc_must *musts = NULL; |
| 7566 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7567 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7568 | |
| 7569 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7570 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7571 | if (node->flags & LYS_CONFIG_R) { |
Michal Vasko | 2b7e558 | 2020-10-07 12:31:23 +0200 | [diff] [blame] | 7572 | 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] | 7573 | if (op) { |
| 7574 | /* we are actually in output */ |
| 7575 | opts = LYXP_SCNODE_OUTPUT; |
| 7576 | } |
| 7577 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7578 | |
| 7579 | switch (node->nodetype) { |
| 7580 | case LYS_CONTAINER: |
| 7581 | when = ((struct lysc_node_container *)node)->when; |
| 7582 | musts = ((struct lysc_node_container *)node)->musts; |
| 7583 | break; |
| 7584 | case LYS_CHOICE: |
| 7585 | when = ((struct lysc_node_choice *)node)->when; |
| 7586 | break; |
| 7587 | case LYS_LEAF: |
| 7588 | when = ((struct lysc_node_leaf *)node)->when; |
| 7589 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 7590 | break; |
| 7591 | case LYS_LEAFLIST: |
| 7592 | when = ((struct lysc_node_leaflist *)node)->when; |
| 7593 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 7594 | break; |
| 7595 | case LYS_LIST: |
| 7596 | when = ((struct lysc_node_list *)node)->when; |
| 7597 | musts = ((struct lysc_node_list *)node)->musts; |
| 7598 | break; |
| 7599 | case LYS_ANYXML: |
| 7600 | case LYS_ANYDATA: |
| 7601 | when = ((struct lysc_node_anydata *)node)->when; |
| 7602 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 7603 | break; |
| 7604 | case LYS_CASE: |
| 7605 | when = ((struct lysc_node_case *)node)->when; |
| 7606 | break; |
| 7607 | case LYS_NOTIF: |
| 7608 | musts = ((struct lysc_notif *)node)->musts; |
| 7609 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7610 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7611 | case LYS_ACTION: |
| 7612 | /* first process input musts */ |
| 7613 | musts = ((struct lysc_action *)node)->input.musts; |
| 7614 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7615 | default: |
| 7616 | /* nothing to check */ |
| 7617 | break; |
| 7618 | } |
| 7619 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7620 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7621 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 7622 | 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^] | 7623 | 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] | 7624 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7625 | 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] | 7626 | goto cleanup; |
| 7627 | } |
| 7628 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7629 | ctx->path[0] = '\0'; |
| 7630 | 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] | 7631 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7632 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7633 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 7634 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7635 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7636 | /* 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] | 7637 | 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^] | 7638 | schema->name); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7639 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7640 | |
| 7641 | /* check dummy node accessing */ |
| 7642 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7643 | 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] | 7644 | ret = LY_EVALID; |
| 7645 | goto cleanup; |
| 7646 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7647 | } |
| 7648 | } |
| 7649 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7650 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7651 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7652 | LY_CHECK_GOTO(ret, cleanup); |
| 7653 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7654 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7655 | } |
| 7656 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7657 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7658 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7659 | LY_ARRAY_FOR(musts, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 7660 | 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] | 7661 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7662 | 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] | 7663 | goto cleanup; |
| 7664 | } |
| 7665 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7666 | ctx->path[0] = '\0'; |
| 7667 | 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] | 7668 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7669 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7670 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7671 | /* 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] | 7672 | 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^] | 7673 | 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] | 7674 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7675 | } |
| 7676 | } |
| 7677 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7678 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7679 | } |
| 7680 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7681 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7682 | /* now check output musts */ |
| 7683 | input_done = 1; |
| 7684 | musts = ((struct lysc_action *)node)->output.musts; |
| 7685 | opts = LYXP_SCNODE_OUTPUT; |
| 7686 | goto check_musts; |
| 7687 | } |
| 7688 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7689 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7690 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7691 | return ret; |
| 7692 | } |
| 7693 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7694 | /** |
| 7695 | * @brief Check leafref for its target existence on a complete compiled schema tree. |
| 7696 | * |
| 7697 | * @param[in] ctx Compile context. |
| 7698 | * @param[in] node Context node for the leafref. |
| 7699 | * @param[in] lref Leafref to resolve. |
| 7700 | * @return LY_ERR value. |
| 7701 | */ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7702 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7703 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 7704 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7705 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7706 | struct ly_path *p; |
| 7707 | struct lysc_type *type; |
| 7708 | |
| 7709 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7710 | |
| 7711 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 7712 | 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^] | 7713 | lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, |
| 7714 | LY_PREF_SCHEMA, (void *)lref->path_mod, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7715 | |
| 7716 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7717 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7718 | ly_path_free(node->module->ctx, p); |
| 7719 | |
| 7720 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7721 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 7722 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 7723 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7724 | return LY_EVALID; |
| 7725 | } |
| 7726 | |
| 7727 | /* check status */ |
| 7728 | ctx->path[0] = '\0'; |
| 7729 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7730 | ctx->path_len = strlen(ctx->path); |
| 7731 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 7732 | return LY_EVALID; |
| 7733 | } |
| 7734 | ctx->path_len = 1; |
| 7735 | ctx->path[1] = '\0'; |
| 7736 | |
| 7737 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7738 | if (lref->require_instance) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 7739 | 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] | 7740 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7741 | 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^] | 7742 | " 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] | 7743 | return LY_EVALID; |
| 7744 | } |
| 7745 | } |
| 7746 | |
| 7747 | /* store the target's type and check for circular chain of leafrefs */ |
| 7748 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 7749 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 7750 | if (type == (struct lysc_type *)lref) { |
| 7751 | /* circular chain detected */ |
| 7752 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 7753 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7754 | return LY_EVALID; |
| 7755 | } |
| 7756 | } |
| 7757 | |
| 7758 | /* check if leafref and its target are under common if-features */ |
| 7759 | if (lys_compile_leafref_features_validate(node, target)) { |
| 7760 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 7761 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7762 | " features applicable to the leafref itself.", lref->path->expr); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7763 | return LY_EVALID; |
| 7764 | } |
| 7765 | |
| 7766 | return LY_SUCCESS; |
| 7767 | } |
| 7768 | |
| 7769 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7770 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7771 | { |
| 7772 | struct lysc_ext_instance *ext; |
| 7773 | struct lysp_ext_instance *ext_p = NULL; |
| 7774 | struct lysp_stmt *stmt; |
| 7775 | const struct lys_module *ext_mod; |
| 7776 | LY_ERR ret = LY_SUCCESS; |
| 7777 | |
| 7778 | /* create the parsed extension instance manually */ |
| 7779 | ext_p = calloc(1, sizeof *ext_p); |
| 7780 | 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] | 7781 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup); |
| 7782 | 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] | 7783 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7784 | ext_p->insubstmt_index = 0; |
| 7785 | |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 7786 | ext_p->child = stmt = calloc(1, sizeof *ext_p->child); |
| 7787 | LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7788 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup); |
| 7789 | 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] | 7790 | stmt->kw = LY_STMT_TYPE; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7791 | |
| 7792 | /* allocate new extension instance */ |
| 7793 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7794 | |
| 7795 | /* manually get extension definition module */ |
| 7796 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7797 | |
| 7798 | /* compile the extension instance */ |
| 7799 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7800 | |
| 7801 | cleanup: |
| 7802 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7803 | free(ext_p); |
| 7804 | return ret; |
| 7805 | } |
| 7806 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7807 | /** |
| 7808 | * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree. |
| 7809 | * |
| 7810 | * @param[in] ctx Compile context. |
| 7811 | * @param[in] node Leaf or leaf-list to compile the default value(s) for. |
| 7812 | * @param[in] type Type of the default value. |
| 7813 | * @param[in] dflt Default value. |
| 7814 | * @param[in] dflt_mod Local module for @p dflt. |
| 7815 | * @param[in,out] storage Storage for the compiled default value. |
| 7816 | * @return LY_ERR value. |
| 7817 | */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7818 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7819 | 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] | 7820 | const struct lys_module *dflt_mod, struct lyd_value *storage) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7821 | { |
| 7822 | LY_ERR ret; |
| 7823 | struct ly_err_item *err = NULL; |
| 7824 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7825 | 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^] | 7826 | node, storage, &err); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7827 | if (ret == LY_EINCOMPLETE) { |
| 7828 | /* we have no data so we will not be resolving it */ |
| 7829 | ret = LY_SUCCESS; |
| 7830 | } |
| 7831 | |
| 7832 | if (ret) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7833 | ctx->path[0] = '\0'; |
| 7834 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7835 | if (err) { |
| 7836 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 7837 | "Invalid default - value does not fit the type (%s).", err->msg); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7838 | ly_err_free(err); |
| 7839 | } else { |
| 7840 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame^] | 7841 | "Invalid default - value does not fit the type."); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7842 | } |
| 7843 | return ret; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7844 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 7845 | |
| 7846 | ++((struct lysc_type *)storage->realtype)->refcount; |
| 7847 | return LY_SUCCESS; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7848 | } |
| 7849 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7850 | /** |
| 7851 | * @brief Compile default value of a leaf expecting a complete compiled schema tree. |
| 7852 | * |
| 7853 | * @param[in] ctx Compile context. |
| 7854 | * @param[in] leaf Leaf that the default value is for. |
| 7855 | * @param[in] dflt Default value to compile. |
| 7856 | * @return LY_ERR value. |
| 7857 | */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7858 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7859 | 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] | 7860 | { |
| 7861 | LY_ERR ret; |
| 7862 | |
| 7863 | assert(!leaf->dflt); |
| 7864 | |
| 7865 | if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) { |
| 7866 | /* ignore default values for keys and mandatory leaves */ |
| 7867 | return LY_SUCCESS; |
| 7868 | } |
| 7869 | |
| 7870 | /* allocate the default value */ |
| 7871 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 7872 | LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM); |
| 7873 | |
| 7874 | /* store the default value */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7875 | 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] | 7876 | if (ret) { |
| 7877 | free(leaf->dflt); |
| 7878 | leaf->dflt = NULL; |
| 7879 | } |
| 7880 | |
| 7881 | return ret; |
| 7882 | } |
| 7883 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7884 | /** |
| 7885 | * @brief Compile default values of a leaf-list expecting a complete compiled schema tree. |
| 7886 | * |
| 7887 | * @param[in] ctx Compile context. |
| 7888 | * @param[in] llist Leaf-list that the default value(s) are for. |
| 7889 | * @param[in] dflt Default value to compile, in case of a single value. |
| 7890 | * @param[in] dflts Sized array of default values, in case of more values. |
| 7891 | * @return LY_ERR value. |
| 7892 | */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7893 | static LY_ERR |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7894 | lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt, |
| 7895 | struct lysp_qname *dflts) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7896 | { |
| 7897 | LY_ERR ret; |
| 7898 | LY_ARRAY_COUNT_TYPE orig_count, u, v; |
| 7899 | |
| 7900 | assert(dflt || dflts); |
| 7901 | |
| 7902 | if (llist->dflts) { |
| 7903 | /* there were already some defaults and we are adding new by deviations */ |
| 7904 | assert(dflts); |
| 7905 | orig_count = LY_ARRAY_COUNT(llist->dflts); |
| 7906 | } else { |
| 7907 | orig_count = 0; |
| 7908 | } |
| 7909 | |
| 7910 | /* allocate new items */ |
| 7911 | if (dflts) { |
| 7912 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM); |
| 7913 | } else { |
| 7914 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM); |
| 7915 | } |
| 7916 | |
| 7917 | /* fill each new default value */ |
| 7918 | if (dflts) { |
| 7919 | LY_ARRAY_FOR(dflts, u) { |
| 7920 | llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7921 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u].str, dflts[u].mod, |
| 7922 | llist->dflts[orig_count + u]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7923 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret); |
| 7924 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7925 | } |
| 7926 | } else { |
| 7927 | llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7928 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt->str, dflt->mod, |
| 7929 | llist->dflts[orig_count]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7930 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret); |
| 7931 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7932 | } |
| 7933 | |
| 7934 | /* check default value uniqueness */ |
| 7935 | if (llist->flags & LYS_CONFIG_W) { |
| 7936 | /* configuration data values must be unique - so check the default values */ |
| 7937 | for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) { |
| 7938 | for (v = 0; v < u; ++v) { |
| 7939 | 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] | 7940 | lysc_update_path(ctx, llist->parent, llist->name); |
| 7941 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7942 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", |
| 7943 | llist->dflts[u]->canonical); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7944 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7945 | return LY_EVALID; |
| 7946 | } |
| 7947 | } |
| 7948 | } |
| 7949 | } |
| 7950 | |
| 7951 | return LY_SUCCESS; |
| 7952 | } |
| 7953 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7954 | /** |
| 7955 | * @brief Finish compilation of all the unres sets of a compile context. |
| 7956 | * |
| 7957 | * @param[in] ctx Compile context with unres sets. |
| 7958 | * @return LY_ERR value. |
| 7959 | */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7960 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7961 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7962 | { |
| 7963 | struct lysc_node *node; |
| 7964 | struct lysc_type *type, *typeiter; |
| 7965 | struct lysc_type_leafref *lref; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 7966 | struct lysc_augment *aug; |
| 7967 | struct lysc_deviation *dev; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7968 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7969 | uint32_t i; |
| 7970 | |
| 7971 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7972 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7973 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7974 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7975 | node = ctx->leafrefs.objs[i]; |
| 7976 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7977 | type = ((struct lysc_node_leaf *)node)->type; |
| 7978 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7979 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7980 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7981 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7982 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7983 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7984 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7985 | } |
| 7986 | } |
| 7987 | } |
| 7988 | } |
| 7989 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7990 | /* store pointer to the real type */ |
| 7991 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7992 | if (type->basetype == LY_TYPE_LEAFREF) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7993 | for (typeiter = ((struct lysc_type_leafref *)type)->realtype; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7994 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7995 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 7996 | ((struct lysc_type_leafref *)type)->realtype = typeiter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7997 | } else if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7998 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7999 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 8000 | 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] | 8001 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 8002 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 8003 | ((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] | 8004 | } |
| 8005 | } |
| 8006 | } |
| 8007 | } |
| 8008 | |
| 8009 | /* check xpath */ |
| 8010 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 8011 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 8012 | } |
| 8013 | |
| 8014 | /* finish incomplete default values compilation */ |
| 8015 | for (i = 0; i < ctx->dflts.count; ++i) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8016 | struct lysc_unres_dflt *r = ctx->dflts.objs[i]; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 8017 | if (r->leaf->nodetype == LYS_LEAF) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8018 | 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] | 8019 | } else { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8020 | 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] | 8021 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8022 | } |
| 8023 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8024 | /* check that all augments were applied */ |
| 8025 | for (i = 0; i < ctx->augs.count; ++i) { |
| 8026 | aug = ctx->augs.objs[i]; |
| 8027 | LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 8028 | "Augment target node \"%s\" from module \"%s\" was not found.", aug->nodeid->expr, |
| 8029 | aug->nodeid_mod->name); |
| 8030 | } |
| 8031 | if (ctx->augs.count) { |
| 8032 | return LY_ENOTFOUND; |
| 8033 | } |
| 8034 | |
| 8035 | /* check that all deviations were applied */ |
| 8036 | for (i = 0; i < ctx->devs.count; ++i) { |
| 8037 | dev = ctx->devs.objs[i]; |
| 8038 | LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 8039 | "Deviation(s) target node \"%s\" from module \"%s\" was not found.", dev->nodeid->expr, |
| 8040 | dev->nodeid_mod->name); |
| 8041 | } |
| 8042 | if (ctx->devs.count) { |
| 8043 | return LY_ENOTFOUND; |
| 8044 | } |
| 8045 | |
| 8046 | return LY_SUCCESS; |
| 8047 | } |
| 8048 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 8049 | void |
| 8050 | 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] | 8051 | { |
| 8052 | uint32_t i; |
| 8053 | LY_ARRAY_COUNT_TYPE u, count; |
| 8054 | struct lys_module *m; |
| 8055 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 8056 | for (i = 0; i < ctx->list.count; ++i) { |
| 8057 | m = ctx->list.objs[i]; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8058 | |
| 8059 | if (m->augmented_by) { |
| 8060 | count = LY_ARRAY_COUNT(m->augmented_by); |
| 8061 | for (u = 0; u < count; ++u) { |
| 8062 | if (m->augmented_by[u] == mod) { |
| 8063 | /* keep the order */ |
| 8064 | if (u < count - 1) { |
| 8065 | memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by); |
| 8066 | } |
| 8067 | LY_ARRAY_DECREMENT(m->augmented_by); |
| 8068 | break; |
| 8069 | } |
| 8070 | } |
| 8071 | if (!LY_ARRAY_COUNT(m->augmented_by)) { |
| 8072 | LY_ARRAY_FREE(m->augmented_by); |
| 8073 | m->augmented_by = NULL; |
| 8074 | } |
| 8075 | } |
| 8076 | |
| 8077 | if (m->deviated_by) { |
| 8078 | count = LY_ARRAY_COUNT(m->deviated_by); |
| 8079 | for (u = 0; u < count; ++u) { |
| 8080 | if (m->deviated_by[u] == mod) { |
| 8081 | /* keep the order */ |
| 8082 | if (u < count - 1) { |
| 8083 | memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by); |
| 8084 | } |
| 8085 | LY_ARRAY_DECREMENT(m->deviated_by); |
| 8086 | break; |
| 8087 | } |
| 8088 | } |
| 8089 | if (!LY_ARRAY_COUNT(m->deviated_by)) { |
| 8090 | LY_ARRAY_FREE(m->deviated_by); |
| 8091 | m->deviated_by = NULL; |
| 8092 | } |
| 8093 | } |
| 8094 | } |
| 8095 | } |
| 8096 | |
| 8097 | /** |
| 8098 | * @brief Compile features in the current module and all its submodules. |
| 8099 | * |
| 8100 | * @param[in] ctx Compile context. |
| 8101 | * @return LY_ERR value. |
| 8102 | */ |
| 8103 | static LY_ERR |
| 8104 | lys_compile_features(struct lysc_ctx *ctx) |
| 8105 | { |
| 8106 | struct lysp_submodule *submod; |
| 8107 | LY_ARRAY_COUNT_TYPE u, v; |
| 8108 | |
| 8109 | if (!ctx->mod->features) { |
| 8110 | /* features are compiled directly into the module structure, |
| 8111 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
| 8112 | LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, ctx->mod->parsed->features, &ctx->mod->features)); |
| 8113 | LY_ARRAY_FOR(ctx->mod->parsed->includes, v) { |
| 8114 | submod = ctx->mod->parsed->includes[v].submodule; |
| 8115 | LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, submod->features, &ctx->mod->features)); |
| 8116 | } |
| 8117 | } |
| 8118 | |
| 8119 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 8120 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 8121 | * are present. */ |
| 8122 | LY_ARRAY_FOR(ctx->mod->parsed->features, u) { |
| 8123 | LY_CHECK_RET(lys_feature_precompile_finish(ctx, &ctx->mod->parsed->features[u], ctx->mod->features)); |
| 8124 | } |
| 8125 | |
| 8126 | lysc_update_path(ctx, NULL, "{submodule}"); |
| 8127 | LY_ARRAY_FOR(ctx->mod->parsed->includes, v) { |
| 8128 | submod = ctx->mod->parsed->includes[v].submodule; |
| 8129 | |
| 8130 | lysc_update_path(ctx, NULL, submod->name); |
| 8131 | LY_ARRAY_FOR(submod->features, u) { |
| 8132 | LY_CHECK_RET(lys_feature_precompile_finish(ctx, &submod->features[u], ctx->mod->features)); |
| 8133 | } |
| 8134 | lysc_update_path(ctx, NULL, NULL); |
| 8135 | } |
| 8136 | lysc_update_path(ctx, NULL, NULL); |
| 8137 | |
| 8138 | return LY_SUCCESS; |
| 8139 | } |
| 8140 | |
| 8141 | /** |
| 8142 | * @brief Compile identites in the current module and all its submodules. |
| 8143 | * |
| 8144 | * @param[in] ctx Compile context. |
| 8145 | * @return LY_ERR value. |
| 8146 | */ |
| 8147 | static LY_ERR |
| 8148 | lys_compile_identities(struct lysc_ctx *ctx) |
| 8149 | { |
| 8150 | struct lysp_submodule *submod; |
| 8151 | LY_ARRAY_COUNT_TYPE u; |
| 8152 | |
| 8153 | if (!ctx->mod->identities) { |
| 8154 | LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, ctx->mod->parsed->identities, &ctx->mod->identities)); |
| 8155 | LY_ARRAY_FOR(ctx->mod->parsed->includes, u) { |
| 8156 | submod = ctx->mod->parsed->includes[u].submodule; |
| 8157 | LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, submod->identities, &ctx->mod->identities)); |
| 8158 | } |
| 8159 | } |
| 8160 | |
| 8161 | if (ctx->mod->parsed->identities) { |
| 8162 | LY_CHECK_RET(lys_compile_identities_derived(ctx, ctx->mod->parsed->identities, ctx->mod->identities)); |
| 8163 | } |
| 8164 | lysc_update_path(ctx, NULL, "{submodule}"); |
| 8165 | LY_ARRAY_FOR(ctx->mod->parsed->includes, u) { |
| 8166 | |
| 8167 | submod = ctx->mod->parsed->includes[u].submodule; |
| 8168 | if (submod->identities) { |
| 8169 | lysc_update_path(ctx, NULL, submod->name); |
| 8170 | LY_CHECK_RET(lys_compile_identities_derived(ctx, submod->identities, ctx->mod->identities)); |
| 8171 | lysc_update_path(ctx, NULL, NULL); |
| 8172 | } |
| 8173 | } |
| 8174 | lysc_update_path(ctx, NULL, NULL); |
| 8175 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8176 | return LY_SUCCESS; |
| 8177 | } |
| 8178 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8179 | LY_ERR |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8180 | lys_compile(struct lys_module *mod, uint32_t options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8181 | { |
| 8182 | struct lysc_ctx ctx = {0}; |
| 8183 | struct lysc_module *mod_c; |
| 8184 | struct lysp_module *sp; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8185 | struct lysp_submodule *submod; |
| 8186 | struct lysp_node *pnode; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 8187 | struct lysp_grp *grps; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 8188 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 8189 | uint32_t i; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 8190 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8191 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8192 | 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] | 8193 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8194 | if (!mod->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 8195 | /* just imported modules are not compiled */ |
| 8196 | return LY_SUCCESS; |
| 8197 | } |
| 8198 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8199 | /* context will be changed */ |
| 8200 | ++mod->ctx->module_set_id; |
| 8201 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8202 | sp = mod->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8203 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8204 | ctx.ctx = mod->ctx; |
| 8205 | ctx.mod = mod; |
| 8206 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 8207 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 8208 | ctx.path_len = 1; |
| 8209 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8210 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8211 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 8212 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 8213 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8214 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8215 | /* process imports */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 8216 | LY_ARRAY_FOR(sp->imports, u) { |
| 8217 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 8218 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 8219 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8220 | /* features */ |
| 8221 | LY_CHECK_GOTO(ret = lys_compile_features(&ctx), error); |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 8222 | |
| 8223 | /* identities, work similarly to features with the precompilation */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8224 | LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), error); |
| 8225 | |
| 8226 | /* augments and deviations */ |
| 8227 | LY_CHECK_GOTO(ret = lys_precompile_augments_deviations(&ctx), error); |
| 8228 | |
| 8229 | /* compile augments and deviations of our module from other modules so they can be applied during compilation */ |
| 8230 | LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), error); |
| 8231 | LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8232 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 8233 | /* data nodes */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8234 | LY_LIST_FOR(sp->data, pnode) { |
| 8235 | 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] | 8236 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 8237 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8238 | /* top-level RPCs and notifications */ |
| 8239 | COMPILE_OP_ARRAY_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 8240 | 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] | 8241 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8242 | /* extension instances */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 8243 | 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] | 8244 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8245 | /* the same for submodules */ |
| 8246 | LY_ARRAY_FOR(sp->includes, u) { |
| 8247 | submod = sp->includes[u].submodule; |
| 8248 | LY_LIST_FOR(submod->data, pnode) { |
| 8249 | ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL); |
| 8250 | LY_CHECK_GOTO(ret, error); |
| 8251 | } |
| 8252 | |
| 8253 | COMPILE_OP_ARRAY_GOTO(&ctx, submod->rpcs, mod_c->rpcs, NULL, v, lys_compile_action, 0, ret, error); |
| 8254 | COMPILE_OP_ARRAY_GOTO(&ctx, submod->notifs, mod_c->notifs, NULL, v, lys_compile_notif, 0, ret, error); |
| 8255 | |
| 8256 | COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error); |
| 8257 | } |
| 8258 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8259 | /* finish compilation for all unresolved items in the context */ |
| 8260 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 8261 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 8262 | /* validate non-instantiated groupings from the parsed schema, |
| 8263 | * without it we would accept even the schemas with invalid grouping specification */ |
| 8264 | ctx.options |= LYSC_OPT_GROUPING; |
| 8265 | LY_ARRAY_FOR(sp->groupings, u) { |
| 8266 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8267 | 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] | 8268 | } |
| 8269 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8270 | LY_LIST_FOR(sp->data, pnode) { |
| 8271 | grps = (struct lysp_grp *)lysp_node_groupings(pnode); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 8272 | LY_ARRAY_FOR(grps, u) { |
| 8273 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8274 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 8275 | } |
| 8276 | } |
| 8277 | } |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8278 | LY_ARRAY_FOR(sp->includes, u) { |
| 8279 | submod = sp->includes[u].submodule; |
| 8280 | LY_ARRAY_FOR(submod->groupings, u) { |
| 8281 | if (!(submod->groupings[u].flags & LYS_USED_GRP)) { |
| 8282 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &submod->groupings[u]), error); |
| 8283 | } |
| 8284 | } |
| 8285 | LY_LIST_FOR(submod->data, pnode) { |
| 8286 | grps = (struct lysp_grp *)lysp_node_groupings(pnode); |
| 8287 | LY_ARRAY_FOR(grps, u) { |
| 8288 | if (!(grps[u].flags & LYS_USED_GRP)) { |
| 8289 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error); |
| 8290 | } |
| 8291 | } |
| 8292 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 8293 | } |
| 8294 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 8295 | #if 0 |
| 8296 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 8297 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 8298 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 8299 | * the anotation definitions available in the internal schema structure. */ |
| 8300 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 8301 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 8302 | lys_free(mod, NULL, 1, 1); |
| 8303 | return NULL; |
| 8304 | } |
| 8305 | } |
| 8306 | #endif |
| 8307 | |
| 8308 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8309 | if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
| 8310 | 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] | 8311 | } |
| 8312 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8313 | /* there can be no leftover deviations */ |
| 8314 | LY_CHECK_ERR_GOTO(ctx.devs.count, LOGINT(ctx.ctx); ret = LY_EINT, error); |
| 8315 | |
| 8316 | for (i = 0; i < ctx.dflts.count; ++i) { |
| 8317 | lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]); |
| 8318 | } |
| 8319 | ly_set_erase(&ctx.dflts, NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8320 | ly_set_erase(&ctx.xpath, NULL); |
| 8321 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 8322 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 8323 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8324 | ly_set_erase(&ctx.augs, NULL); |
| 8325 | ly_set_erase(&ctx.devs, NULL); |
| 8326 | ly_set_erase(&ctx.uses_augs, NULL); |
| 8327 | ly_set_erase(&ctx.uses_rfns, NULL); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 8328 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 8329 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8330 | lysp_module_free(mod->parsed); |
| 8331 | mod->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8332 | } |
| 8333 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8334 | return LY_SUCCESS; |
| 8335 | |
| 8336 | error: |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 8337 | lys_precompile_augments_deviations_revert(ctx.ctx, mod); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8338 | lys_feature_precompile_revert(&ctx, mod); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8339 | for (i = 0; i < ctx.dflts.count; ++i) { |
| 8340 | lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]); |
| 8341 | } |
| 8342 | ly_set_erase(&ctx.dflts, NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8343 | ly_set_erase(&ctx.xpath, NULL); |
| 8344 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 8345 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 8346 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 8347 | for (i = 0; i < ctx.augs.count; ++i) { |
| 8348 | lysc_augment_free(ctx.ctx, ctx.augs.objs[i]); |
| 8349 | } |
| 8350 | ly_set_erase(&ctx.augs, NULL); |
| 8351 | for (i = 0; i < ctx.devs.count; ++i) { |
| 8352 | lysc_deviation_free(ctx.ctx, ctx.devs.objs[i]); |
| 8353 | } |
| 8354 | ly_set_erase(&ctx.devs, NULL); |
| 8355 | for (i = 0; i < ctx.uses_augs.count; ++i) { |
| 8356 | lysc_augment_free(ctx.ctx, ctx.uses_augs.objs[i]); |
| 8357 | } |
| 8358 | ly_set_erase(&ctx.uses_augs, NULL); |
| 8359 | for (i = 0; i < ctx.uses_rfns.count; ++i) { |
| 8360 | lysc_refine_free(ctx.ctx, ctx.uses_rfns.objs[i]); |
| 8361 | } |
| 8362 | ly_set_erase(&ctx.uses_rfns, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8363 | lysc_module_free(mod_c, NULL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 8364 | mod->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 8365 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 8366 | return ret; |
| 8367 | } |