Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 24 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 26 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "dict.h" |
| 29 | #include "log.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 30 | #include "path.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 31 | #include "parser.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 32 | #include "parser_schema.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 33 | #include "plugins_exts.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "plugins_types.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 35 | #include "plugins_exts_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "set.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | #include "tree.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 38 | #include "tree_data.h" |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 39 | #include "tree_data_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 40 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 41 | #include "tree_schema_internal.h" |
| 42 | #include "xpath.h" |
| 43 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 44 | static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, |
| 45 | void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod); |
| 46 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 47 | /** |
| 48 | * @brief Duplicate string into dictionary |
| 49 | * @param[in] CTX libyang context of the dictionary. |
| 50 | * @param[in] ORIG String to duplicate. |
| 51 | * @param[out] DUP Where to store the result. |
| 52 | */ |
| 53 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 54 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 55 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 56 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 57 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 58 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 59 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 60 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 61 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 62 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 63 | } \ |
| 64 | } |
| 65 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 66 | #define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 67 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 68 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 69 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 70 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 71 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 72 | RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 73 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 74 | } \ |
| 75 | } |
| 76 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 77 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 78 | if (EXTS_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 79 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \ |
| 80 | for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 81 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 82 | RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 83 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 84 | } \ |
| 85 | } |
| 86 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 87 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 88 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 89 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 90 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 91 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 92 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 93 | RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 94 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 95 | } \ |
| 96 | } |
| 97 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 98 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 99 | if (MEMBER_P) { \ |
| 100 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 101 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 102 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 103 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 104 | } |
| 105 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 106 | #define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 107 | if (MEMBER_P) { \ |
| 108 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 109 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 110 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 111 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 112 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 113 | } |
| 114 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 115 | #define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 116 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 117 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 118 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 119 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 120 | return LY_EVALID; \ |
| 121 | } \ |
| 122 | } \ |
| 123 | } |
| 124 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 125 | #define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 126 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 127 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \ |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 128 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \ |
| 129 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 130 | return LY_EVALID; \ |
| 131 | } \ |
| 132 | } \ |
| 133 | } |
| 134 | |
| 135 | struct lysc_ext * |
| 136 | lysc_ext_dup(struct lysc_ext *orig) |
| 137 | { |
| 138 | ++orig->refcount; |
| 139 | return orig; |
| 140 | } |
| 141 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 142 | static struct lysc_ext_instance * |
| 143 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 144 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 145 | /* TODO - extensions, increase refcount */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 146 | (void) ctx; |
| 147 | (void) orig; |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 151 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 152 | * @brief Add record into the compile context's list of incomplete default values. |
| 153 | * @param[in] ctx Compile context with the incomplete default values list. |
| 154 | * @param[in] context_node Context schema node to store in the record. |
| 155 | * @param[in] dflt Incomplete default value to store in the record. |
| 156 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 157 | * @return LY_EMEM in case of memory allocation failure. |
| 158 | * @return LY_SUCCESS |
| 159 | */ |
| 160 | static LY_ERR |
| 161 | lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod) |
| 162 | { |
| 163 | struct lysc_incomplete_dflt *r; |
| 164 | r = malloc(sizeof *r); |
| 165 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 166 | r->context_node = context_node; |
| 167 | r->dflt = dflt; |
| 168 | r->dflt_mod = dflt_mod; |
| 169 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 170 | |
| 171 | return LY_SUCCESS; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 176 | * @param[in] ctx Compile context with the incomplete default values list. |
| 177 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 178 | */ |
| 179 | static void |
| 180 | lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt) |
| 181 | { |
| 182 | unsigned int u; |
| 183 | struct lysc_incomplete_dflt *r; |
| 184 | |
| 185 | for (u = 0; u < ctx->dflts.count; ++u) { |
| 186 | r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u]; |
| 187 | if (r->dflt == dflt) { |
| 188 | free(ctx->dflts.objs[u]); |
| 189 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 190 | --ctx->dflts.count; |
| 191 | return; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 196 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 197 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 198 | { |
| 199 | int len; |
| 200 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 201 | |
| 202 | if (!name) { |
| 203 | /* removing last path segment */ |
| 204 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 205 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 206 | if (ctx->path[ctx->path_len] == '=') { |
| 207 | ctx->path[ctx->path_len++] = '}'; |
| 208 | } else { |
| 209 | /* not a top-level special tag, remove also preceiding '/' */ |
| 210 | goto remove_nodelevel; |
| 211 | } |
| 212 | } else { |
| 213 | remove_nodelevel: |
| 214 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 215 | if (ctx->path_len == 0) { |
| 216 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 217 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | /* set new terminating NULL-byte */ |
| 221 | ctx->path[ctx->path_len] = '\0'; |
| 222 | } else { |
| 223 | if (ctx->path_len > 1) { |
| 224 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 225 | /* extension of the special tag */ |
| 226 | nextlevel = 2; |
| 227 | --ctx->path_len; |
| 228 | } else { |
| 229 | /* there is already some path, so add next level */ |
| 230 | nextlevel = 1; |
| 231 | } |
| 232 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 233 | |
| 234 | if (nextlevel != 2) { |
| 235 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 236 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 237 | 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] | 238 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 239 | 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] | 240 | } |
| 241 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 242 | 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] | 243 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 244 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 245 | /* output truncated */ |
| 246 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 247 | } else { |
| 248 | ctx->path_len += len; |
| 249 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 254 | * @brief Duplicate the compiled pattern structure. |
| 255 | * |
| 256 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 257 | * |
| 258 | * @param[in] orig The pattern structure to duplicate. |
| 259 | * @return The duplicated structure to use. |
| 260 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 261 | static struct lysc_pattern* |
| 262 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 263 | { |
| 264 | ++orig->refcount; |
| 265 | return orig; |
| 266 | } |
| 267 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 268 | /** |
| 269 | * @brief Duplicate the array of compiled patterns. |
| 270 | * |
| 271 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 272 | * |
| 273 | * @param[in] ctx Libyang context for logging. |
| 274 | * @param[in] orig The patterns sized array to duplicate. |
| 275 | * @return New sized array as a copy of @p orig. |
| 276 | * @return NULL in case of memory allocation error. |
| 277 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 278 | static struct lysc_pattern** |
| 279 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 280 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 281 | struct lysc_pattern **dup = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 282 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 283 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 284 | assert(orig); |
| 285 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 286 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 287 | LY_ARRAY_FOR(orig, u) { |
| 288 | dup[u] = lysc_pattern_dup(orig[u]); |
| 289 | LY_ARRAY_INCREMENT(dup); |
| 290 | } |
| 291 | return dup; |
| 292 | } |
| 293 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 294 | /** |
| 295 | * @brief Duplicate compiled range structure. |
| 296 | * |
| 297 | * @param[in] ctx Libyang context for logging. |
| 298 | * @param[in] orig The range structure to be duplicated. |
| 299 | * @return New compiled range structure as a copy of @p orig. |
| 300 | * @return NULL in case of memory allocation error. |
| 301 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 302 | struct lysc_range* |
| 303 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 304 | { |
| 305 | struct lysc_range *dup; |
| 306 | LY_ERR ret; |
| 307 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 308 | assert(orig); |
| 309 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 310 | dup = calloc(1, sizeof *dup); |
| 311 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 312 | if (orig->parts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 313 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup); |
| 314 | LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts); |
| 315 | 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] | 316 | } |
| 317 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 318 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 319 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 320 | |
| 321 | return dup; |
| 322 | cleanup: |
| 323 | free(dup); |
| 324 | (void) ret; /* set but not used due to the return type */ |
| 325 | return NULL; |
| 326 | } |
| 327 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 328 | /** |
| 329 | * @brief Stack for processing if-feature expressions. |
| 330 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 331 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 332 | int size; /**< number of items in the stack */ |
| 333 | int index; /**< first empty item */ |
| 334 | 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] | 335 | }; |
| 336 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 337 | /** |
| 338 | * @brief Add @ref ifftokens into the stack. |
| 339 | * @param[in] stack The if-feature stack to use. |
| 340 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 341 | * @return LY_EMEM in case of memory allocation error |
| 342 | * @return LY_ESUCCESS if the value successfully stored. |
| 343 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 344 | static LY_ERR |
| 345 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 346 | { |
| 347 | if (stack->index == stack->size) { |
| 348 | stack->size += 4; |
| 349 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 350 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 351 | } |
| 352 | stack->stack[stack->index++] = value; |
| 353 | return LY_SUCCESS; |
| 354 | } |
| 355 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 356 | /** |
| 357 | * @brief Get (and remove) the last item form the stack. |
| 358 | * @param[in] stack The if-feature stack to use. |
| 359 | * @return The value from the top of the stack. |
| 360 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 361 | static uint8_t |
| 362 | iff_stack_pop(struct iff_stack *stack) |
| 363 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 364 | assert(stack && stack->index); |
| 365 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 366 | stack->index--; |
| 367 | return stack->stack[stack->index]; |
| 368 | } |
| 369 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 370 | /** |
| 371 | * @brief Clean up the stack. |
| 372 | * @param[in] stack The if-feature stack to use. |
| 373 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 374 | static void |
| 375 | iff_stack_clean(struct iff_stack *stack) |
| 376 | { |
| 377 | stack->size = 0; |
| 378 | free(stack->stack); |
| 379 | } |
| 380 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 381 | /** |
| 382 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 383 | * (libyang format of the if-feature expression). |
| 384 | * @param[in,out] list The 2bits array to modify. |
| 385 | * @param[in] op The operand (@ref ifftokens) to store. |
| 386 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 387 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 388 | static void |
| 389 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 390 | { |
| 391 | uint8_t *item; |
| 392 | uint8_t mask = 3; |
| 393 | |
| 394 | assert(pos >= 0); |
| 395 | assert(op <= 3); /* max 2 bits */ |
| 396 | |
| 397 | item = &list[pos / 4]; |
| 398 | mask = mask << 2 * (pos % 4); |
| 399 | *item = (*item) & ~mask; |
| 400 | *item = (*item) | (op << 2 * (pos % 4)); |
| 401 | } |
| 402 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 403 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 404 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 405 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 406 | /** |
| 407 | * @brief Find a feature of the given name and referenced in the given module. |
| 408 | * |
| 409 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 410 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 411 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 412 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 413 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 414 | * assigned till that time will be still valid. |
| 415 | * |
| 416 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 417 | * @param[in] name Name of the feature including possible prefix. |
| 418 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 419 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 420 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 421 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 422 | lys_feature_find(struct lys_module *mod, const char *name, size_t len) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 423 | { |
| 424 | size_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 425 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 426 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 427 | |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 428 | assert(mod); |
| 429 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 430 | for (i = 0; i < len; ++i) { |
| 431 | if (name[i] == ':') { |
| 432 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 433 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 434 | LY_CHECK_RET(!mod, NULL); |
| 435 | |
| 436 | name = &name[i + 1]; |
| 437 | len = len - i - 1; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 442 | if (mod->implemented) { |
| 443 | /* module is implemented so there is already the compiled schema */ |
| 444 | flist = mod->compiled->features; |
| 445 | } else { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 446 | flist = mod->dis_features; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 447 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 448 | LY_ARRAY_FOR(flist, u) { |
| 449 | f = &flist[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 450 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 451 | return f; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return NULL; |
| 456 | } |
| 457 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 458 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 459 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 460 | */ |
| 461 | static LY_ERR |
| 462 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 463 | { |
| 464 | LY_ERR ret = LY_SUCCESS; |
| 465 | |
| 466 | if (!ext_p->compiled) { |
| 467 | lysc_update_path(ctx, NULL, "{extension}"); |
| 468 | lysc_update_path(ctx, NULL, ext_p->name); |
| 469 | |
| 470 | /* compile the extension definition */ |
| 471 | ext_p->compiled = calloc(1, sizeof **ext); |
| 472 | ext_p->compiled->refcount = 1; |
| 473 | DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name); |
| 474 | DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument); |
| 475 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 476 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 477 | |
| 478 | lysc_update_path(ctx, NULL, NULL); |
| 479 | lysc_update_path(ctx, NULL, NULL); |
| 480 | |
| 481 | /* find extension definition plugin */ |
| 482 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 483 | } |
| 484 | |
| 485 | *ext = lysc_ext_dup(ext_p->compiled); |
| 486 | |
| 487 | done: |
| 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 492 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 493 | * |
| 494 | * @param[in] ctx Compilation context. |
| 495 | * @param[in] ext_p Parsed extension instance. |
| 496 | * @param[in,out] ext Prepared compiled extension instance. |
| 497 | * @param[in] parent Extension instance parent. |
| 498 | * @param[in] parent_type Extension instance parent type. |
| 499 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 500 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 501 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 502 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
| 503 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 504 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 505 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 506 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 507 | size_t u; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 508 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 509 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 510 | |
| 511 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 512 | ext->insubstmt = ext_p->insubstmt; |
| 513 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 514 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 515 | ext->parent = parent; |
| 516 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 517 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 518 | 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] | 519 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 520 | /* get module where the extension definition should be placed */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 521 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u); |
| 522 | if (ext_p->yin) { |
| 523 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 524 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 525 | * YANG (import) prefix must be done here. */ |
| 526 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 527 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 528 | u = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 529 | } else { |
| 530 | assert(ctx->mod_def->parsed); |
| 531 | LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) { |
| 532 | 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] | 533 | char *s; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 534 | LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1, |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 535 | ret = LY_EMEM, cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 536 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 537 | u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | if (!prefixed_name) { |
| 543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 544 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 545 | goto cleanup; |
| 546 | } |
| 547 | } else { |
| 548 | prefixed_name = ext_p->name; |
| 549 | } |
| 550 | lysc_update_path(ctx, NULL, prefixed_name); |
| 551 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 552 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 553 | 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] | 554 | if (!ext_mod) { |
| 555 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 556 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 557 | goto cleanup; |
| 558 | } else if (!ext_mod->parsed->extensions) { |
| 559 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 560 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 561 | prefixed_name, ext_mod->name); |
| 562 | goto cleanup; |
| 563 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 564 | } |
| 565 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 566 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 567 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 568 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 569 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 570 | /* compile extension definition and assign it */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 571 | LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 572 | break; |
| 573 | } |
| 574 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 575 | if (!ext->def) { |
| 576 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 577 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 578 | goto cleanup; |
| 579 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 580 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 581 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 582 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 583 | * 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] | 584 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 585 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 586 | struct lysp_stmt *stmt = NULL; |
| 587 | |
| 588 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 589 | /* ... argument was the first XML child element */ |
| 590 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 591 | /* TODO check namespace of the statement */ |
| 592 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 593 | stmt = ext_p->child; |
| 594 | } |
| 595 | } |
| 596 | } else { |
| 597 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 598 | * with LYS_YIN_ATTR flag */ |
| 599 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 600 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 601 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 602 | break; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | if (!stmt) { |
| 607 | /* missing extension's argument */ |
| 608 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 609 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 610 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 611 | |
| 612 | } |
| 613 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 614 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 615 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 616 | if (prefixed_name != ext_p->name) { |
| 617 | lydict_remove(ctx->ctx, ext_p->name); |
| 618 | ext_p->name = prefixed_name; |
| 619 | if (!ext_p->argument && ext->argument) { |
| 620 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 621 | } |
| 622 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 623 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 624 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 625 | if (ext->argument) { |
| 626 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 627 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 628 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 629 | if (ext->argument) { |
| 630 | lysc_update_path(ctx, NULL, NULL); |
| 631 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 632 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 633 | ext_p->compiled = ext; |
| 634 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 635 | ret = LY_SUCCESS; |
| 636 | cleanup: |
| 637 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 638 | lydict_remove(ctx->ctx, prefixed_name); |
| 639 | } |
| 640 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 641 | lysc_update_path(ctx, NULL, NULL); |
| 642 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 643 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 644 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 648 | * @brief Compile information from the if-feature statement |
| 649 | * @param[in] ctx Compile context. |
| 650 | * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 651 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 652 | * @return LY_ERR value. |
| 653 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 654 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 655 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 656 | { |
| 657 | const char *c = *value; |
| 658 | int r, rc = EXIT_FAILURE; |
| 659 | int i, j, last_not, checkversion = 0; |
| 660 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 661 | uint8_t op; |
| 662 | struct iff_stack stack = {0, 0, NULL}; |
| 663 | struct lysc_feature *f; |
| 664 | |
| 665 | assert(c); |
| 666 | |
| 667 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 668 | for (i = j = last_not = 0; c[i]; i++) { |
| 669 | if (c[i] == '(') { |
| 670 | j++; |
| 671 | checkversion = 1; |
| 672 | continue; |
| 673 | } else if (c[i] == ')') { |
| 674 | j--; |
| 675 | continue; |
| 676 | } else if (isspace(c[i])) { |
| 677 | checkversion = 1; |
| 678 | continue; |
| 679 | } |
| 680 | |
| 681 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 682 | int sp; |
| 683 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 684 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 685 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 686 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 687 | return LY_EVALID; |
| 688 | } else if (!isspace(c[i + r])) { |
| 689 | /* feature name starting with the not/and/or */ |
| 690 | last_not = 0; |
| 691 | f_size++; |
| 692 | } else if (c[i] == 'n') { /* not operation */ |
| 693 | if (last_not) { |
| 694 | /* double not */ |
| 695 | expr_size = expr_size - 2; |
| 696 | last_not = 0; |
| 697 | } else { |
| 698 | last_not = 1; |
| 699 | } |
| 700 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 701 | if (f_exp != f_size) { |
| 702 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 703 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 704 | return LY_EVALID; |
| 705 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 706 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 707 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 708 | /* not a not operation */ |
| 709 | last_not = 0; |
| 710 | } |
| 711 | i += r; |
| 712 | } else { |
| 713 | f_size++; |
| 714 | last_not = 0; |
| 715 | } |
| 716 | expr_size++; |
| 717 | |
| 718 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 719 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 720 | i--; |
| 721 | break; |
| 722 | } |
| 723 | i++; |
| 724 | } |
| 725 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 726 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 727 | /* not matching count of ( and ) */ |
| 728 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 729 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 730 | return LY_EVALID; |
| 731 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 732 | if (f_exp != f_size) { |
| 733 | /* features do not match the needed arguments for the logical operations */ |
| 734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 735 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 736 | "the required number of operands for the operations.", *value); |
| 737 | return LY_EVALID; |
| 738 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 739 | |
| 740 | if (checkversion || expr_size > 1) { |
| 741 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 742 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 744 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 745 | return LY_EVALID; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /* allocate the memory */ |
| 750 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 751 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 752 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 753 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 754 | |
| 755 | stack.size = expr_size; |
| 756 | f_size--; expr_size--; /* used as indexes from now */ |
| 757 | |
| 758 | for (i--; i >= 0; i--) { |
| 759 | if (c[i] == ')') { |
| 760 | /* push it on stack */ |
| 761 | iff_stack_push(&stack, LYS_IFF_RP); |
| 762 | continue; |
| 763 | } else if (c[i] == '(') { |
| 764 | /* pop from the stack into result all operators until ) */ |
| 765 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 766 | iff_setop(iff->expr, op, expr_size--); |
| 767 | } |
| 768 | continue; |
| 769 | } else if (isspace(c[i])) { |
| 770 | continue; |
| 771 | } |
| 772 | |
| 773 | /* end of operator or operand -> find beginning and get what is it */ |
| 774 | j = i + 1; |
| 775 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 776 | i--; |
| 777 | } |
| 778 | i++; /* go back by one step */ |
| 779 | |
| 780 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 781 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 782 | /* double not */ |
| 783 | iff_stack_pop(&stack); |
| 784 | } else { |
| 785 | /* not has the highest priority, so do not pop from the stack |
| 786 | * as in case of AND and OR */ |
| 787 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 788 | } |
| 789 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 790 | /* as for OR - pop from the stack all operators with the same or higher |
| 791 | * priority and store them to the result, then push the AND to the stack */ |
| 792 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 793 | op = iff_stack_pop(&stack); |
| 794 | iff_setop(iff->expr, op, expr_size--); |
| 795 | } |
| 796 | iff_stack_push(&stack, LYS_IFF_AND); |
| 797 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 798 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 799 | op = iff_stack_pop(&stack); |
| 800 | iff_setop(iff->expr, op, expr_size--); |
| 801 | } |
| 802 | iff_stack_push(&stack, LYS_IFF_OR); |
| 803 | } else { |
| 804 | /* feature name, length is j - i */ |
| 805 | |
| 806 | /* add it to the expression */ |
| 807 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 808 | |
| 809 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 810 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 811 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 812 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 813 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 814 | iff->features[f_size] = f; |
| 815 | LY_ARRAY_INCREMENT(iff->features); |
| 816 | f_size--; |
| 817 | } |
| 818 | } |
| 819 | while (stack.index) { |
| 820 | op = iff_stack_pop(&stack); |
| 821 | iff_setop(iff->expr, op, expr_size--); |
| 822 | } |
| 823 | |
| 824 | if (++expr_size || ++f_size) { |
| 825 | /* not all expected operators and operands found */ |
| 826 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 827 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 828 | rc = LY_EINT; |
| 829 | } else { |
| 830 | rc = LY_SUCCESS; |
| 831 | } |
| 832 | |
| 833 | error: |
| 834 | /* cleanup */ |
| 835 | iff_stack_clean(&stack); |
| 836 | |
| 837 | return rc; |
| 838 | } |
| 839 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 840 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 841 | * @brief Get the XPath context node for the given schema node. |
| 842 | * @param[in] start The schema node where the XPath expression appears. |
| 843 | * @return The context node to evaluate XPath expression in given schema node. |
| 844 | * @return NULL in case the context node is the root node. |
| 845 | */ |
| 846 | static struct lysc_node * |
| 847 | lysc_xpath_context(struct lysc_node *start) |
| 848 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 849 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 850 | start = start->parent); |
| 851 | return start; |
| 852 | } |
| 853 | |
| 854 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 855 | * @brief Compile information from the when statement |
| 856 | * @param[in] ctx Compile context. |
| 857 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 858 | * @param[in] flags Flags of the node with the "when" defiition. |
| 859 | * @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] | 860 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 861 | * @return LY_ERR value. |
| 862 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 863 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 864 | 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] | 865 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 866 | LY_ERR ret = LY_SUCCESS; |
| 867 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 868 | *when = calloc(1, sizeof **when); |
| 869 | (*when)->refcount = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 870 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 871 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 872 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 873 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 874 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 875 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 876 | 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] | 877 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 878 | |
| 879 | done: |
| 880 | return ret; |
| 881 | } |
| 882 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 883 | /** |
| 884 | * @brief Compile information from the must statement |
| 885 | * @param[in] ctx Compile context. |
| 886 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 887 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 888 | * @return LY_ERR value. |
| 889 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 890 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 891 | 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] | 892 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 893 | LY_ERR ret = LY_SUCCESS; |
| 894 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 895 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 896 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 897 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 898 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 899 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 900 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 901 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 902 | 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] | 903 | |
| 904 | done: |
| 905 | return ret; |
| 906 | } |
| 907 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 908 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 909 | * @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] | 910 | * @param[in] ctx Compile context. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 911 | * @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] | 912 | * @return LY_ERR value. |
| 913 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 914 | static LY_ERR |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 915 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 916 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 917 | const struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 918 | LY_ERR ret = LY_SUCCESS; |
| 919 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 920 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 921 | * 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] | 922 | * 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] | 923 | if (!imp_p->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 924 | /* try to use filepath if present */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 925 | if (imp_p->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 926 | struct ly_in *in; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 927 | if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 928 | LOGINT(ctx->ctx); |
| 929 | } else { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 930 | LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)], |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 931 | ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 932 | if (mod != imp_p->module) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 933 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 934 | imp_p->module->filepath, imp_p->module->name); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 935 | mod = NULL; |
| 936 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 937 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 938 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 939 | } |
| 940 | if (!mod) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 941 | 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] | 942 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 943 | imp_p->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 944 | return LY_ENOTFOUND; |
| 945 | } |
| 946 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 947 | } |
| 948 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 949 | return ret; |
| 950 | } |
| 951 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 952 | LY_ERR |
| 953 | lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 954 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 955 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 956 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 957 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 958 | LY_ERR ret = LY_SUCCESS; |
| 959 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 960 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 961 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 962 | if (!ctx_sc) { |
| 963 | context.ctx = ctx; |
| 964 | context.mod = module; |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 965 | context.mod_def = module; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 966 | context.path_len = 1; |
| 967 | context.path[0] = '/'; |
| 968 | ctx_sc = &context; |
| 969 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 970 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 971 | if (!identities_p) { |
| 972 | return LY_SUCCESS; |
| 973 | } |
| 974 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 975 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 979 | 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] | 980 | LY_ARRAY_FOR(identities_p, u) { |
| 981 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 982 | |
| 983 | LY_ARRAY_INCREMENT(*identities); |
| 984 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name); |
| 985 | DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name); |
| 986 | DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc); |
| 987 | DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref); |
| 988 | (*identities)[offset + u].module = ctx_sc->mod; |
| 989 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
| 990 | lys_compile_iffeature, ret, done); |
| 991 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 992 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
| 993 | LYEXT_PAR_IDENT, ret, done); |
| 994 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 995 | |
| 996 | lysc_update_path(ctx_sc, NULL, NULL); |
| 997 | } |
| 998 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 999 | done: |
| 1000 | return ret; |
| 1001 | } |
| 1002 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1003 | /** |
| 1004 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1005 | * |
| 1006 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1007 | * |
| 1008 | * @param[in] ctx Compile context for logging. |
| 1009 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1010 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1011 | * @return LY_SUCCESS if everything is ok. |
| 1012 | * @return LY_EVALID if the identity is derived from itself. |
| 1013 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1014 | static LY_ERR |
| 1015 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1016 | { |
| 1017 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1018 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1019 | struct ly_set recursion = {0}; |
| 1020 | struct lysc_ident *drv; |
| 1021 | |
| 1022 | if (!derived) { |
| 1023 | return LY_SUCCESS; |
| 1024 | } |
| 1025 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1026 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1027 | if (ident == derived[u]) { |
| 1028 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1029 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1030 | goto cleanup; |
| 1031 | } |
| 1032 | ly_set_add(&recursion, derived[u], 0); |
| 1033 | } |
| 1034 | |
| 1035 | for (v = 0; v < recursion.count; ++v) { |
| 1036 | drv = recursion.objs[v]; |
| 1037 | if (!drv->derived) { |
| 1038 | continue; |
| 1039 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1040 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1041 | if (ident == drv->derived[u]) { |
| 1042 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1043 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1044 | goto cleanup; |
| 1045 | } |
| 1046 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1047 | } |
| 1048 | } |
| 1049 | ret = LY_SUCCESS; |
| 1050 | |
| 1051 | cleanup: |
| 1052 | ly_set_erase(&recursion, NULL); |
| 1053 | return ret; |
| 1054 | } |
| 1055 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1056 | /** |
| 1057 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1058 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1059 | * 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] | 1060 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1061 | * to distinguish these two use cases. |
| 1062 | * |
| 1063 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1064 | * @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] | 1065 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1066 | * @param[in] bases Array of bases of identityref to fill in. |
| 1067 | * @return LY_ERR value. |
| 1068 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1069 | static LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1070 | lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, |
| 1071 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1072 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1073 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1074 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1075 | struct lys_module *mod; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1076 | struct lysc_ident **idref, *identities; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1077 | |
| 1078 | assert(ident || bases); |
| 1079 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1080 | if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1081 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1082 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1083 | return LY_EVALID; |
| 1084 | } |
| 1085 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1086 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1087 | s = strchr(bases_p[u], ':'); |
| 1088 | if (s) { |
| 1089 | /* prefixed identity */ |
| 1090 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1091 | 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] | 1092 | } else { |
| 1093 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1094 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1095 | } |
| 1096 | if (!mod) { |
| 1097 | if (ident) { |
| 1098 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1099 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1100 | } else { |
| 1101 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1102 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1103 | } |
| 1104 | return LY_EVALID; |
| 1105 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1106 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1107 | idref = NULL; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1108 | if (mod->compiled) { |
| 1109 | identities = mod->compiled->identities; |
| 1110 | } else { |
| 1111 | identities = mod->dis_identities; |
| 1112 | } |
| 1113 | LY_ARRAY_FOR(identities, v) { |
| 1114 | if (!strcmp(name, identities[v].name)) { |
| 1115 | if (ident) { |
| 1116 | if (ident == &identities[v]) { |
| 1117 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1118 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1119 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1120 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1121 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived)); |
| 1122 | /* we have match! store the backlink */ |
| 1123 | LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM); |
| 1124 | *idref = ident; |
| 1125 | } else { |
| 1126 | /* we have match! store the found identity */ |
| 1127 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 1128 | *idref = &identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1129 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1130 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | if (!idref || !(*idref)) { |
| 1134 | if (ident) { |
| 1135 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1136 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1137 | } else { |
| 1138 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1139 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1140 | } |
| 1141 | return LY_EVALID; |
| 1142 | } |
| 1143 | } |
| 1144 | return LY_SUCCESS; |
| 1145 | } |
| 1146 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1147 | /** |
| 1148 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1149 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1150 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1151 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1152 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1153 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1154 | static LY_ERR |
| 1155 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1156 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1157 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1158 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1159 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1160 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1161 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1162 | continue; |
| 1163 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1164 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1165 | 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] | 1166 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1167 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1168 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1169 | return LY_SUCCESS; |
| 1170 | } |
| 1171 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1172 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1173 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 1174 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1175 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1176 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1177 | struct lysc_ctx context = {0}; |
| 1178 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1179 | assert(ctx_sc || ctx); |
| 1180 | |
| 1181 | if (!ctx_sc) { |
| 1182 | context.ctx = ctx; |
| 1183 | context.mod = module; |
| 1184 | context.path_len = 1; |
| 1185 | context.path[0] = '/'; |
| 1186 | ctx_sc = &context; |
| 1187 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1188 | |
| 1189 | if (!features_p) { |
| 1190 | return LY_SUCCESS; |
| 1191 | } |
| 1192 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1193 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1194 | } |
| 1195 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1196 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1197 | 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] | 1198 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1199 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1200 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1201 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1202 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1203 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1204 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1205 | DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1206 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1207 | (*features)[offset + u].module = ctx_sc->mod; |
| 1208 | |
| 1209 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1210 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1211 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1212 | |
| 1213 | return LY_SUCCESS; |
| 1214 | } |
| 1215 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1216 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1217 | * @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] | 1218 | * |
| 1219 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1220 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1221 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1222 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1223 | * being currently processed). |
| 1224 | * @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] | 1225 | * @return LY_SUCCESS if everything is ok. |
| 1226 | * @return LY_EVALID if the feature references indirectly itself. |
| 1227 | */ |
| 1228 | static LY_ERR |
| 1229 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1230 | { |
| 1231 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1232 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1233 | struct ly_set recursion = {0}; |
| 1234 | struct lysc_feature *drv; |
| 1235 | |
| 1236 | if (!depfeatures) { |
| 1237 | return LY_SUCCESS; |
| 1238 | } |
| 1239 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1240 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1241 | if (feature == depfeatures[u]) { |
| 1242 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1243 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1244 | goto cleanup; |
| 1245 | } |
| 1246 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1247 | } |
| 1248 | |
| 1249 | for (v = 0; v < recursion.count; ++v) { |
| 1250 | drv = recursion.objs[v]; |
| 1251 | if (!drv->depfeatures) { |
| 1252 | continue; |
| 1253 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1254 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1255 | if (feature == drv->depfeatures[u]) { |
| 1256 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1257 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1258 | goto cleanup; |
| 1259 | } |
| 1260 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1261 | } |
| 1262 | } |
| 1263 | ret = LY_SUCCESS; |
| 1264 | |
| 1265 | cleanup: |
| 1266 | ly_set_erase(&recursion, NULL); |
| 1267 | return ret; |
| 1268 | } |
| 1269 | |
| 1270 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1271 | * @brief Create pre-compiled features array. |
| 1272 | * |
| 1273 | * See lys_feature_precompile() for more details. |
| 1274 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1275 | * @param[in] ctx Compile context. |
| 1276 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1277 | * @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] | 1278 | * @return LY_ERR value. |
| 1279 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1280 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1281 | 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] | 1282 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1283 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1284 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1285 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1286 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1287 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1288 | /* find the preprecompiled feature */ |
| 1289 | LY_ARRAY_FOR(features, x) { |
| 1290 | if (strcmp(features[x].name, feature_p->name)) { |
| 1291 | continue; |
| 1292 | } |
| 1293 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1294 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1295 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1296 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1297 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1298 | 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] | 1299 | 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] | 1300 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1301 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1302 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1303 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1304 | /* check for circular dependency - direct reference first,... */ |
| 1305 | if (feature == feature->iffeatures[u].features[v]) { |
| 1306 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1307 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1308 | return LY_EVALID; |
| 1309 | } |
| 1310 | /* ... and indirect circular reference */ |
| 1311 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1312 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1313 | /* add itself into the dependants list */ |
| 1314 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1315 | *df = feature; |
| 1316 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1317 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1318 | } |
| 1319 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1320 | lysc_update_path(ctx, NULL, NULL); |
| 1321 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1322 | done: |
| 1323 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1324 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1325 | |
| 1326 | LOGINT(ctx->ctx); |
| 1327 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1330 | /** |
| 1331 | * @brief Revert compiled list of features back to the precompiled state. |
| 1332 | * |
| 1333 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1334 | * The features are supposed to be stored again as dis_features in ::lys_module structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1335 | * |
| 1336 | * @param[in] ctx Compilation context. |
| 1337 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1338 | * and supposed to hold the dis_features list. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1339 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1340 | static void |
| 1341 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1342 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1343 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1344 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 1345 | if (mod->compiled) { |
| 1346 | /* keep the dis_features list until the complete lys_module is freed */ |
| 1347 | mod->dis_features = mod->compiled->features; |
| 1348 | mod->compiled->features = NULL; |
| 1349 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1350 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1351 | /* 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] | 1352 | * which may points into the data being freed here */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1353 | LY_ARRAY_FOR(mod->dis_features, u) { |
| 1354 | LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) { |
| 1355 | lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1356 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1357 | LY_ARRAY_FREE(mod->dis_features[u].iffeatures); |
| 1358 | mod->dis_features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1359 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1360 | LY_ARRAY_FOR(mod->dis_features[u].exts, v) { |
| 1361 | lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1362 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1363 | LY_ARRAY_FREE(mod->dis_features[u].exts); |
| 1364 | mod->dis_features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1368 | /** |
| 1369 | * @brief Validate and normalize numeric value from a range definition. |
| 1370 | * @param[in] ctx Compile context. |
| 1371 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1372 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1373 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1374 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1375 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1376 | * @param[in] value String value of the range boundary. |
| 1377 | * @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. |
| 1378 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1379 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1380 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1381 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1382 | 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] | 1383 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1384 | size_t fraction = 0, size; |
| 1385 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1386 | *len = 0; |
| 1387 | |
| 1388 | assert(value); |
| 1389 | /* parse value */ |
| 1390 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1391 | return LY_EVALID; |
| 1392 | } |
| 1393 | |
| 1394 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1395 | ++(*len); |
| 1396 | } |
| 1397 | |
| 1398 | while (isdigit(value[*len])) { |
| 1399 | ++(*len); |
| 1400 | } |
| 1401 | |
| 1402 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1403 | if (basetype == LY_TYPE_DEC64) { |
| 1404 | goto decimal; |
| 1405 | } else { |
| 1406 | *valcopy = strndup(value, *len); |
| 1407 | return LY_SUCCESS; |
| 1408 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1409 | } |
| 1410 | fraction = *len; |
| 1411 | |
| 1412 | ++(*len); |
| 1413 | while (isdigit(value[*len])) { |
| 1414 | ++(*len); |
| 1415 | } |
| 1416 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1417 | if (basetype == LY_TYPE_DEC64) { |
| 1418 | decimal: |
| 1419 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1420 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1421 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1422 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1423 | *len, value, frdigits); |
| 1424 | return LY_EINVAL; |
| 1425 | } |
| 1426 | if (fraction) { |
| 1427 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1428 | } else { |
| 1429 | size = (*len) + frdigits + 1; |
| 1430 | } |
| 1431 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1432 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1433 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1434 | (*valcopy)[size - 1] = '\0'; |
| 1435 | if (fraction) { |
| 1436 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1437 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1438 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1439 | } else { |
| 1440 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1441 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1442 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1443 | } |
| 1444 | return LY_SUCCESS; |
| 1445 | } |
| 1446 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1447 | /** |
| 1448 | * @brief Check that values in range are in ascendant order. |
| 1449 | * @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] | 1450 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1451 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1452 | * @param[in] value Current value to check. |
| 1453 | * @param[in] prev_value The last seen value. |
| 1454 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1455 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1456 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1457 | range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1458 | { |
| 1459 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1460 | 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] | 1461 | return LY_EEXIST; |
| 1462 | } |
| 1463 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1464 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1465 | return LY_EEXIST; |
| 1466 | } |
| 1467 | } |
| 1468 | return LY_SUCCESS; |
| 1469 | } |
| 1470 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1471 | /** |
| 1472 | * @brief Set min/max value of the range part. |
| 1473 | * @param[in] ctx Compile context. |
| 1474 | * @param[in] part Range part structure to fill. |
| 1475 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1476 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1477 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1478 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1479 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1480 | * @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] | 1481 | * @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] | 1482 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1483 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1484 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1485 | * frdigits value), LY_EMEM. |
| 1486 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1487 | static LY_ERR |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 1488 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, |
| 1489 | int first, int length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1490 | { |
| 1491 | LY_ERR ret = LY_SUCCESS; |
| 1492 | char *valcopy = NULL; |
| 1493 | size_t len; |
| 1494 | |
| 1495 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1496 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1497 | LY_CHECK_GOTO(ret, finalize); |
| 1498 | } |
| 1499 | if (!valcopy && base_range) { |
| 1500 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1501 | 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] | 1502 | } else { |
| 1503 | part->min_64 = base_range->parts[0].min_64; |
| 1504 | } |
| 1505 | if (!first) { |
| 1506 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1507 | } |
| 1508 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1512 | case LY_TYPE_INT8: /* range */ |
| 1513 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1514 | 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] | 1515 | } else if (max) { |
| 1516 | part->max_64 = INT64_C(127); |
| 1517 | } else { |
| 1518 | part->min_64 = INT64_C(-128); |
| 1519 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1520 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1521 | 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] | 1522 | } |
| 1523 | break; |
| 1524 | case LY_TYPE_INT16: /* range */ |
| 1525 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1526 | 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] | 1527 | } else if (max) { |
| 1528 | part->max_64 = INT64_C(32767); |
| 1529 | } else { |
| 1530 | part->min_64 = INT64_C(-32768); |
| 1531 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1532 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1533 | 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] | 1534 | } |
| 1535 | break; |
| 1536 | case LY_TYPE_INT32: /* range */ |
| 1537 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1538 | 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] | 1539 | } else if (max) { |
| 1540 | part->max_64 = INT64_C(2147483647); |
| 1541 | } else { |
| 1542 | part->min_64 = INT64_C(-2147483648); |
| 1543 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1544 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1545 | 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] | 1546 | } |
| 1547 | break; |
| 1548 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1549 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1550 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1551 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1552 | max ? &part->max_64 : &part->min_64); |
| 1553 | } else if (max) { |
| 1554 | part->max_64 = INT64_C(9223372036854775807); |
| 1555 | } else { |
| 1556 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1557 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1558 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1559 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1560 | } |
| 1561 | break; |
| 1562 | case LY_TYPE_UINT8: /* range */ |
| 1563 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1564 | 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] | 1565 | } else if (max) { |
| 1566 | part->max_u64 = UINT64_C(255); |
| 1567 | } else { |
| 1568 | part->min_u64 = UINT64_C(0); |
| 1569 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1570 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1571 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1572 | } |
| 1573 | break; |
| 1574 | case LY_TYPE_UINT16: /* range */ |
| 1575 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1576 | 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] | 1577 | } else if (max) { |
| 1578 | part->max_u64 = UINT64_C(65535); |
| 1579 | } else { |
| 1580 | part->min_u64 = UINT64_C(0); |
| 1581 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1582 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1583 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1584 | } |
| 1585 | break; |
| 1586 | case LY_TYPE_UINT32: /* range */ |
| 1587 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1588 | 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] | 1589 | } else if (max) { |
| 1590 | part->max_u64 = UINT64_C(4294967295); |
| 1591 | } else { |
| 1592 | part->min_u64 = UINT64_C(0); |
| 1593 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1594 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1595 | 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] | 1596 | } |
| 1597 | break; |
| 1598 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1599 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1600 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1601 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1602 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1603 | } else if (max) { |
| 1604 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1605 | } else { |
| 1606 | part->min_u64 = UINT64_C(0); |
| 1607 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1608 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1609 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1610 | } |
| 1611 | break; |
| 1612 | default: |
| 1613 | LOGINT(ctx->ctx); |
| 1614 | ret = LY_EINT; |
| 1615 | } |
| 1616 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1617 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1618 | if (ret == LY_EDENIED) { |
| 1619 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1620 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1621 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1622 | } else if (ret == LY_EVALID) { |
| 1623 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1624 | "Invalid %s restriction - invalid value \"%s\".", |
| 1625 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1626 | } else if (ret == LY_EEXIST) { |
| 1627 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1628 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1629 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1630 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1631 | } else if (!ret && value) { |
| 1632 | *value = *value + len; |
| 1633 | } |
| 1634 | free(valcopy); |
| 1635 | return ret; |
| 1636 | } |
| 1637 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1638 | /** |
| 1639 | * @brief Compile the parsed range restriction. |
| 1640 | * @param[in] ctx Compile context. |
| 1641 | * @param[in] range_p Parsed range structure to compile. |
| 1642 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1643 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1644 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1645 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1646 | * range restriction must be more restrictive than the base_range. |
| 1647 | * @param[in,out] range Pointer to the created current range structure. |
| 1648 | * @return LY_ERR value. |
| 1649 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1650 | static LY_ERR |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 1651 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, |
| 1652 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1653 | { |
| 1654 | LY_ERR ret = LY_EVALID; |
| 1655 | const char *expr; |
| 1656 | struct lysc_range_part *parts = NULL, *part; |
| 1657 | int range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1658 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1659 | |
| 1660 | assert(range); |
| 1661 | assert(range_p); |
| 1662 | |
| 1663 | expr = range_p->arg; |
| 1664 | while(1) { |
| 1665 | if (isspace(*expr)) { |
| 1666 | ++expr; |
| 1667 | } else if (*expr == '\0') { |
| 1668 | if (range_expected) { |
| 1669 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1670 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1671 | length_restr ? "length" : "range", range_p->arg); |
| 1672 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1673 | } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1674 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1675 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1676 | length_restr ? "length" : "range", range_p->arg); |
| 1677 | goto cleanup; |
| 1678 | } |
| 1679 | parts_done++; |
| 1680 | break; |
| 1681 | } else if (!strncmp(expr, "min", 3)) { |
| 1682 | if (parts) { |
| 1683 | /* min cannot be used elsewhere than in the first part */ |
| 1684 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1685 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1686 | expr - range_p->arg, range_p->arg); |
| 1687 | goto cleanup; |
| 1688 | } |
| 1689 | expr += 3; |
| 1690 | |
| 1691 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1692 | 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] | 1693 | part->max_64 = part->min_64; |
| 1694 | } else if (*expr == '|') { |
| 1695 | if (!parts || range_expected) { |
| 1696 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1697 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1698 | goto cleanup; |
| 1699 | } |
| 1700 | expr++; |
| 1701 | parts_done++; |
| 1702 | /* process next part of the expression */ |
| 1703 | } else if (!strncmp(expr, "..", 2)) { |
| 1704 | expr += 2; |
| 1705 | while (isspace(*expr)) { |
| 1706 | expr++; |
| 1707 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1708 | if (!parts || LY_ARRAY_COUNT(parts) == parts_done) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1709 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1710 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1711 | goto cleanup; |
| 1712 | } |
| 1713 | /* continue expecting the upper boundary */ |
| 1714 | range_expected = 1; |
| 1715 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1716 | /* number */ |
| 1717 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1718 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1719 | 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] | 1720 | range_expected = 0; |
| 1721 | } else { |
| 1722 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1723 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1724 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1725 | part->max_64 = part->min_64; |
| 1726 | } |
| 1727 | |
| 1728 | /* continue with possible another expression part */ |
| 1729 | } else if (!strncmp(expr, "max", 3)) { |
| 1730 | expr += 3; |
| 1731 | while (isspace(*expr)) { |
| 1732 | expr++; |
| 1733 | } |
| 1734 | if (*expr != '\0') { |
| 1735 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1736 | length_restr ? "length" : "range", expr); |
| 1737 | goto cleanup; |
| 1738 | } |
| 1739 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1740 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1741 | 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] | 1742 | range_expected = 0; |
| 1743 | } else { |
| 1744 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1745 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1746 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1747 | part->min_64 = part->max_64; |
| 1748 | } |
| 1749 | } else { |
| 1750 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1751 | length_restr ? "length" : "range", expr); |
| 1752 | goto cleanup; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | /* check with the previous range/length restriction */ |
| 1757 | if (base_range) { |
| 1758 | switch (basetype) { |
| 1759 | case LY_TYPE_BINARY: |
| 1760 | case LY_TYPE_UINT8: |
| 1761 | case LY_TYPE_UINT16: |
| 1762 | case LY_TYPE_UINT32: |
| 1763 | case LY_TYPE_UINT64: |
| 1764 | case LY_TYPE_STRING: |
| 1765 | uns = 1; |
| 1766 | break; |
| 1767 | case LY_TYPE_DEC64: |
| 1768 | case LY_TYPE_INT8: |
| 1769 | case LY_TYPE_INT16: |
| 1770 | case LY_TYPE_INT32: |
| 1771 | case LY_TYPE_INT64: |
| 1772 | uns = 0; |
| 1773 | break; |
| 1774 | default: |
| 1775 | LOGINT(ctx->ctx); |
| 1776 | ret = LY_EINT; |
| 1777 | goto cleanup; |
| 1778 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1779 | for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1780 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1781 | goto baseerror; |
| 1782 | } |
| 1783 | /* current lower bound is not lower than the base */ |
| 1784 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1785 | /* base has single value */ |
| 1786 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1787 | /* both lower bounds are the same */ |
| 1788 | if (parts[u].min_64 != parts[u].max_64) { |
| 1789 | /* current continues with a range */ |
| 1790 | goto baseerror; |
| 1791 | } else { |
| 1792 | /* equal single values, move both forward */ |
| 1793 | ++v; |
| 1794 | continue; |
| 1795 | } |
| 1796 | } else { |
| 1797 | /* base is single value lower than current range, so the |
| 1798 | * value from base range is removed in the current, |
| 1799 | * move only base and repeat checking */ |
| 1800 | ++v; |
| 1801 | --u; |
| 1802 | continue; |
| 1803 | } |
| 1804 | } else { |
| 1805 | /* base is the range */ |
| 1806 | if (parts[u].min_64 == parts[u].max_64) { |
| 1807 | /* current is a single value */ |
| 1808 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1809 | /* current is behind the base range, so base range is omitted, |
| 1810 | * move the base and keep the current for further check */ |
| 1811 | ++v; |
| 1812 | --u; |
| 1813 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1814 | continue; |
| 1815 | } else { |
| 1816 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1817 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1818 | /* higher bound is higher than the current higher bound */ |
| 1819 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1820 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1821 | * continue with the same current, but move the base */ |
| 1822 | --u; |
| 1823 | ++v; |
| 1824 | continue; |
| 1825 | } |
| 1826 | /* current range starts within the base range but end behind it */ |
| 1827 | goto baseerror; |
| 1828 | } else { |
| 1829 | /* current range is smaller than the base, |
| 1830 | * move current, but stay with the base */ |
| 1831 | continue; |
| 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | } |
| 1836 | if (u != parts_done) { |
| 1837 | baseerror: |
| 1838 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1839 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1840 | length_restr ? "length" : "range", range_p->arg); |
| 1841 | goto cleanup; |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | if (!(*range)) { |
| 1846 | *range = calloc(1, sizeof **range); |
| 1847 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1848 | } |
| 1849 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1850 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1851 | if (range_p->eapptag) { |
| 1852 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1853 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1854 | } |
| 1855 | if (range_p->emsg) { |
| 1856 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1857 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1858 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1859 | if (range_p->dsc) { |
| 1860 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1861 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1862 | } |
| 1863 | if (range_p->ref) { |
| 1864 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1865 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1866 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1867 | /* extensions are taken only from the last range by the caller */ |
| 1868 | |
| 1869 | (*range)->parts = parts; |
| 1870 | parts = NULL; |
| 1871 | ret = LY_SUCCESS; |
| 1872 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1873 | LY_ARRAY_FREE(parts); |
| 1874 | |
| 1875 | return ret; |
| 1876 | } |
| 1877 | |
| 1878 | /** |
| 1879 | * @brief Checks pattern syntax. |
| 1880 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1881 | * @param[in] ctx Context. |
| 1882 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1883 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1884 | * @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] | 1885 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1886 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1887 | LY_ERR |
| 1888 | 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] | 1889 | { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1890 | int idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1891 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1892 | int err_code; |
| 1893 | const char *orig_ptr; |
| 1894 | PCRE2_SIZE err_offset; |
| 1895 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1896 | #define URANGE_LEN 19 |
| 1897 | char *ublock2urange[][2] = { |
| 1898 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1899 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1900 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1901 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1902 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1903 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1904 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1905 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1906 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1907 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1908 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1909 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1910 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1911 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1912 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1913 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1914 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1915 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1916 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1917 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1918 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1919 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1920 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1921 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1922 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1923 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1924 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1925 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1926 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1927 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1928 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1929 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1930 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1931 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1932 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1933 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1934 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1935 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1936 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1937 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1938 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1939 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1940 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1941 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1942 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1943 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1944 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1945 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1946 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1947 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1948 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1949 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1950 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1951 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1952 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1953 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1954 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1955 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1956 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1957 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1958 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1959 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1960 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1961 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1962 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1963 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1964 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1965 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1966 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1967 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1968 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1969 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1970 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1971 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1972 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1973 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1974 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1975 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1976 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1977 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1978 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1979 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1980 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1981 | {NULL, NULL} |
| 1982 | }; |
| 1983 | |
| 1984 | /* adjust the expression to a Perl equivalent |
| 1985 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1986 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1987 | /* allocate space for the transformed pattern */ |
| 1988 | size = strlen(pattern) + 1; |
| 1989 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1990 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1991 | perl_regex[0] = '\0'; |
| 1992 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1993 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 1994 | brack = 0; |
| 1995 | idx = 0; |
| 1996 | orig_ptr = pattern; |
| 1997 | while (orig_ptr[0]) { |
| 1998 | switch (orig_ptr[0]) { |
| 1999 | case '$': |
| 2000 | case '^': |
| 2001 | if (!brack) { |
| 2002 | /* make space for the extra character */ |
| 2003 | ++size; |
| 2004 | perl_regex = ly_realloc(perl_regex, size); |
| 2005 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2006 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2007 | /* print escape slash */ |
| 2008 | perl_regex[idx] = '\\'; |
| 2009 | ++idx; |
| 2010 | } |
| 2011 | break; |
| 2012 | case '[': |
| 2013 | /* must not be escaped */ |
| 2014 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2015 | ++brack; |
| 2016 | } |
| 2017 | break; |
| 2018 | case ']': |
| 2019 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2020 | /* pattern was checked and compiled already */ |
| 2021 | assert(brack); |
| 2022 | --brack; |
| 2023 | } |
| 2024 | break; |
| 2025 | default: |
| 2026 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2027 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2028 | |
| 2029 | /* copy char */ |
| 2030 | perl_regex[idx] = orig_ptr[0]; |
| 2031 | |
| 2032 | ++idx; |
| 2033 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2034 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2035 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2036 | |
| 2037 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2038 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2039 | start = ptr - perl_regex; |
| 2040 | |
| 2041 | ptr = strchr(ptr, '}'); |
| 2042 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2043 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2044 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2045 | free(perl_regex); |
| 2046 | return LY_EVALID; |
| 2047 | } |
| 2048 | end = (ptr - perl_regex) + 1; |
| 2049 | |
| 2050 | /* need more space */ |
| 2051 | if (end - start < URANGE_LEN) { |
| 2052 | 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] | 2053 | 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] | 2054 | } |
| 2055 | |
| 2056 | /* find our range */ |
| 2057 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2058 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2059 | break; |
| 2060 | } |
| 2061 | } |
| 2062 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2064 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2065 | free(perl_regex); |
| 2066 | return LY_EVALID; |
| 2067 | } |
| 2068 | |
| 2069 | /* 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] | 2070 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2071 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2072 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2073 | } |
| 2074 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2075 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2076 | } |
| 2077 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2078 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2079 | /* skip brackets */ |
| 2080 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2081 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2082 | } else { |
| 2083 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2084 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2089 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2090 | PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE, |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2091 | &err_code, &err_offset, NULL); |
| 2092 | if (!code_local) { |
| 2093 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2094 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2095 | 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] | 2096 | free(perl_regex); |
| 2097 | return LY_EVALID; |
| 2098 | } |
| 2099 | free(perl_regex); |
| 2100 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2101 | if (code) { |
| 2102 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2103 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2104 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | return LY_SUCCESS; |
| 2108 | |
| 2109 | #undef URANGE_LEN |
| 2110 | } |
| 2111 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2112 | /** |
| 2113 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2114 | * @param[in] ctx Compile context. |
| 2115 | * @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] | 2116 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2117 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2118 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2119 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2120 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2121 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2122 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2123 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2124 | { |
| 2125 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2126 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2127 | LY_ERR ret = LY_SUCCESS; |
| 2128 | |
| 2129 | /* first, copy the patterns from the base type */ |
| 2130 | if (base_patterns) { |
| 2131 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2132 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2133 | } |
| 2134 | |
| 2135 | LY_ARRAY_FOR(patterns_p, u) { |
| 2136 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2137 | *pattern = calloc(1, sizeof **pattern); |
| 2138 | ++(*pattern)->refcount; |
| 2139 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2140 | ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2141 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2142 | |
| 2143 | if (patterns_p[u].arg[0] == 0x15) { |
| 2144 | (*pattern)->inverted = 1; |
| 2145 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2146 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2147 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2148 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2149 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2150 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2151 | 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] | 2152 | } |
| 2153 | done: |
| 2154 | return ret; |
| 2155 | } |
| 2156 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2157 | /** |
| 2158 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2159 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2160 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2161 | 0 /* LY_TYPE_UNKNOWN */, |
| 2162 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2163 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2164 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2165 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2166 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2167 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2168 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2169 | 0 /* LY_TYPE_BOOL */, |
| 2170 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2171 | 0 /* LY_TYPE_EMPTY */, |
| 2172 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2173 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2174 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2175 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2176 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2177 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2178 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2179 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2180 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2181 | }; |
| 2182 | |
| 2183 | /** |
| 2184 | * @brief stringification of the YANG built-in data types |
| 2185 | */ |
| 2186 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2187 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2188 | "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2189 | }; |
| 2190 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2191 | /** |
| 2192 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2193 | * @param[in] ctx Compile context. |
| 2194 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2195 | * @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] | 2196 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2197 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2198 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2199 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2200 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2201 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2202 | 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] | 2203 | { |
| 2204 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2205 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2206 | int32_t value = 0; |
| 2207 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2208 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2209 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2210 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2211 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2212 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2213 | return LY_EVALID; |
| 2214 | } |
| 2215 | |
| 2216 | LY_ARRAY_FOR(enums_p, u) { |
| 2217 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2218 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2219 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2220 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2221 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2222 | if (base_enums) { |
| 2223 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2224 | LY_ARRAY_FOR(base_enums, v) { |
| 2225 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2226 | break; |
| 2227 | } |
| 2228 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2229 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2230 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2231 | "Invalid %s - derived type adds new item \"%s\".", |
| 2232 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2233 | return LY_EVALID; |
| 2234 | } |
| 2235 | match = v; |
| 2236 | } |
| 2237 | |
| 2238 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2239 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2240 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2241 | e->value = (int32_t)enums_p[u].value; |
| 2242 | if (!u || e->value >= value) { |
| 2243 | value = e->value + 1; |
| 2244 | } |
| 2245 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2246 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2247 | if (e->value == (*enums)[v].value) { |
| 2248 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2249 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2250 | e->value, e->name, (*enums)[v].name); |
| 2251 | return LY_EVALID; |
| 2252 | } |
| 2253 | } |
| 2254 | } else if (base_enums) { |
| 2255 | /* inherit the assigned value */ |
| 2256 | e->value = base_enums[match].value; |
| 2257 | if (!u || e->value >= value) { |
| 2258 | value = e->value + 1; |
| 2259 | } |
| 2260 | } else { |
| 2261 | /* assign value automatically */ |
| 2262 | if (u && value == INT32_MIN) { |
| 2263 | /* counter overflow */ |
| 2264 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2265 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2266 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2267 | return LY_EVALID; |
| 2268 | } |
| 2269 | e->value = value++; |
| 2270 | } |
| 2271 | } else { /* LY_TYPE_BITS */ |
| 2272 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2273 | e->value = (int32_t)enums_p[u].value; |
| 2274 | if (!u || (uint32_t)e->value >= position) { |
| 2275 | position = (uint32_t)e->value + 1; |
| 2276 | } |
| 2277 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2278 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2279 | if (e->value == (*enums)[v].value) { |
| 2280 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2281 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2282 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2283 | return LY_EVALID; |
| 2284 | } |
| 2285 | } |
| 2286 | } else if (base_enums) { |
| 2287 | /* inherit the assigned value */ |
| 2288 | e->value = base_enums[match].value; |
| 2289 | if (!u || (uint32_t)e->value >= position) { |
| 2290 | position = (uint32_t)e->value + 1; |
| 2291 | } |
| 2292 | } else { |
| 2293 | /* assign value automatically */ |
| 2294 | if (u && position == 0) { |
| 2295 | /* counter overflow */ |
| 2296 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2297 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2298 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2299 | return LY_EVALID; |
| 2300 | } |
| 2301 | e->value = position++; |
| 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | if (base_enums) { |
| 2306 | /* the assigned values must not change from the derived type */ |
| 2307 | if (e->value != base_enums[match].value) { |
| 2308 | if (basetype == LY_TYPE_ENUM) { |
| 2309 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2310 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2311 | e->name, base_enums[match].value, e->value); |
| 2312 | } else { |
| 2313 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2314 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2315 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2316 | } |
| 2317 | return LY_EVALID; |
| 2318 | } |
| 2319 | } |
| 2320 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2321 | 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] | 2322 | 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] | 2323 | |
| 2324 | if (basetype == LY_TYPE_BITS) { |
| 2325 | /* keep bits ordered by position */ |
| 2326 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2327 | if (v != u) { |
| 2328 | memcpy(&storage, e, sizeof *e); |
| 2329 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2330 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2331 | } |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | done: |
| 2336 | return ret; |
| 2337 | } |
| 2338 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2339 | /** |
| 2340 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2341 | * |
| 2342 | * path-arg = absolute-path / relative-path |
| 2343 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2344 | * relative-path = 1*(".." "/") descendant-path |
| 2345 | * |
| 2346 | * @param[in,out] path Path to parse. |
| 2347 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2348 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2349 | * @param[out] name Name of the token. |
| 2350 | * @param[out] nam_len Length of the name. |
| 2351 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2352 | * must not be changed between consecutive calls. -1 if the |
| 2353 | * path is absolute. |
| 2354 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2355 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2356 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2357 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2358 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2359 | int *parent_times, int *has_predicate) |
| 2360 | { |
| 2361 | int par_times = 0; |
| 2362 | |
| 2363 | assert(path && *path); |
| 2364 | assert(parent_times); |
| 2365 | assert(prefix); |
| 2366 | assert(prefix_len); |
| 2367 | assert(name); |
| 2368 | assert(name_len); |
| 2369 | assert(has_predicate); |
| 2370 | |
| 2371 | *prefix = NULL; |
| 2372 | *prefix_len = 0; |
| 2373 | *name = NULL; |
| 2374 | *name_len = 0; |
| 2375 | *has_predicate = 0; |
| 2376 | |
| 2377 | if (!*parent_times) { |
| 2378 | if (!strncmp(*path, "..", 2)) { |
| 2379 | *path += 2; |
| 2380 | ++par_times; |
| 2381 | while (!strncmp(*path, "/..", 3)) { |
| 2382 | *path += 3; |
| 2383 | ++par_times; |
| 2384 | } |
| 2385 | } |
| 2386 | if (par_times) { |
| 2387 | *parent_times = par_times; |
| 2388 | } else { |
| 2389 | *parent_times = -1; |
| 2390 | } |
| 2391 | } |
| 2392 | |
| 2393 | if (**path != '/') { |
| 2394 | return LY_EINVAL; |
| 2395 | } |
| 2396 | /* skip '/' */ |
| 2397 | ++(*path); |
| 2398 | |
| 2399 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2400 | 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] | 2401 | |
| 2402 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2403 | /* path continues by another token or this is the last token */ |
| 2404 | return LY_SUCCESS; |
| 2405 | } else if ((*path)[0] != '[') { |
| 2406 | /* unexpected character */ |
| 2407 | return LY_EINVAL; |
| 2408 | } else { |
| 2409 | /* predicate starting with [ */ |
| 2410 | *has_predicate = 1; |
| 2411 | return LY_SUCCESS; |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2416 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2417 | * |
| 2418 | * The set of features used for target must be a subset of features used for the leafref. |
| 2419 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2420 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2421 | * |
| 2422 | * @param[in] refnode The leafref node. |
| 2423 | * @param[in] target Tha target node of the leafref. |
| 2424 | * @return LY_SUCCESS or LY_EVALID; |
| 2425 | */ |
| 2426 | static LY_ERR |
| 2427 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2428 | { |
| 2429 | LY_ERR ret = LY_EVALID; |
| 2430 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2431 | LY_ARRAY_COUNT_TYPE u, v, count; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2432 | struct ly_set features = {0}; |
| 2433 | |
| 2434 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2435 | if (iter->iffeatures) { |
| 2436 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2437 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2438 | LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2439 | } |
| 2440 | } |
| 2441 | } |
| 2442 | } |
| 2443 | |
| 2444 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2445 | * So when adding features applicable to the target into the features set, we should not be |
| 2446 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2447 | * to the leafref itself. */ |
| 2448 | count = features.count; |
| 2449 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2450 | if (iter->iffeatures) { |
| 2451 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2452 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2453 | if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) { |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2454 | /* new feature was added (or LY_EMEM) */ |
| 2455 | goto cleanup; |
| 2456 | } |
| 2457 | } |
| 2458 | } |
| 2459 | } |
| 2460 | } |
| 2461 | ret = LY_SUCCESS; |
| 2462 | |
| 2463 | cleanup: |
| 2464 | ly_set_erase(&features, NULL); |
| 2465 | return ret; |
| 2466 | } |
| 2467 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2468 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2469 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2470 | struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2471 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2472 | /** |
| 2473 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2474 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2475 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2476 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2477 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2478 | * @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] | 2479 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2480 | * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2481 | * @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] | 2482 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2483 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2484 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2485 | * @return LY_ERR value. |
| 2486 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2487 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2488 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2489 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2490 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2491 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2492 | { |
| 2493 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2494 | struct lysc_type_bin *bin; |
| 2495 | struct lysc_type_num *num; |
| 2496 | struct lysc_type_str *str; |
| 2497 | struct lysc_type_bits *bits; |
| 2498 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2499 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2500 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2501 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2502 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2503 | |
| 2504 | switch (basetype) { |
| 2505 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2506 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2507 | |
| 2508 | /* 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] | 2509 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2510 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2511 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2512 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2513 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | if (tpdfname) { |
| 2518 | type_p->compiled = *type; |
| 2519 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2520 | } |
| 2521 | break; |
| 2522 | case LY_TYPE_BITS: |
| 2523 | /* RFC 7950 9.7 - bits */ |
| 2524 | bits = (struct lysc_type_bits*)(*type); |
| 2525 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2526 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2527 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2528 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2529 | } |
| 2530 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2531 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2532 | /* 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] | 2533 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2534 | 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] | 2535 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2536 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2537 | free(*type); |
| 2538 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2539 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2540 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | if (tpdfname) { |
| 2544 | type_p->compiled = *type; |
| 2545 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2546 | } |
| 2547 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2548 | case LY_TYPE_DEC64: |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2549 | dec = (struct lysc_type_dec *)(*type); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2550 | |
| 2551 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2552 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2553 | if (!type_p->fraction_digits) { |
| 2554 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2555 | 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] | 2556 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2557 | 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] | 2558 | free(*type); |
| 2559 | *type = NULL; |
| 2560 | } |
| 2561 | return LY_EVALID; |
| 2562 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2563 | dec->fraction_digits = type_p->fraction_digits; |
| 2564 | } else { |
| 2565 | if (type_p->fraction_digits) { |
| 2566 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
| 2567 | if (tpdfname) { |
| 2568 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2569 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
| 2570 | tpdfname); |
| 2571 | } else { |
| 2572 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2573 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
| 2574 | free(*type); |
| 2575 | *type = NULL; |
| 2576 | } |
| 2577 | return LY_EVALID; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2578 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2579 | dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2580 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2581 | |
| 2582 | /* RFC 7950 9.2.4 - range */ |
| 2583 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2584 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2585 | base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2586 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2587 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2588 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | if (tpdfname) { |
| 2592 | type_p->compiled = *type; |
| 2593 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2594 | } |
| 2595 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2596 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2597 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2598 | |
| 2599 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2600 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2601 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2602 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2603 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2604 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2605 | } |
| 2606 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2607 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2608 | } |
| 2609 | |
| 2610 | /* RFC 7950 9.4.5 - pattern */ |
| 2611 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2612 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2613 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2614 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2615 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2616 | } |
| 2617 | |
| 2618 | if (tpdfname) { |
| 2619 | type_p->compiled = *type; |
| 2620 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2621 | } |
| 2622 | break; |
| 2623 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2624 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2625 | |
| 2626 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2627 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2628 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2629 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2630 | } |
| 2631 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2632 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2633 | /* 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] | 2634 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2635 | 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] | 2636 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2637 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2638 | free(*type); |
| 2639 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2640 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2641 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | if (tpdfname) { |
| 2645 | type_p->compiled = *type; |
| 2646 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2647 | } |
| 2648 | break; |
| 2649 | case LY_TYPE_INT8: |
| 2650 | case LY_TYPE_UINT8: |
| 2651 | case LY_TYPE_INT16: |
| 2652 | case LY_TYPE_UINT16: |
| 2653 | case LY_TYPE_INT32: |
| 2654 | case LY_TYPE_UINT32: |
| 2655 | case LY_TYPE_INT64: |
| 2656 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2657 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2658 | |
| 2659 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2660 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2661 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2662 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2663 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2664 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | if (tpdfname) { |
| 2669 | type_p->compiled = *type; |
| 2670 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2671 | } |
| 2672 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2673 | case LY_TYPE_IDENT: |
| 2674 | idref = (struct lysc_type_identityref*)(*type); |
| 2675 | |
| 2676 | /* RFC 7950 9.10.2 - base */ |
| 2677 | if (type_p->bases) { |
| 2678 | if (base) { |
| 2679 | /* only the directly derived identityrefs can contain base specification */ |
| 2680 | if (tpdfname) { |
| 2681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2682 | "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.", |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2683 | tpdfname); |
| 2684 | } else { |
| 2685 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2686 | "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] | 2687 | free(*type); |
| 2688 | *type = NULL; |
| 2689 | } |
| 2690 | return LY_EVALID; |
| 2691 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2692 | LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | if (!base && !type_p->flags) { |
| 2696 | /* type derived from identityref built-in type must contain at least one base */ |
| 2697 | if (tpdfname) { |
| 2698 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2699 | } else { |
| 2700 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2701 | free(*type); |
| 2702 | *type = NULL; |
| 2703 | } |
| 2704 | return LY_EVALID; |
| 2705 | } |
| 2706 | |
| 2707 | if (tpdfname) { |
| 2708 | type_p->compiled = *type; |
| 2709 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2710 | } |
| 2711 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2712 | case LY_TYPE_LEAFREF: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2713 | lref = (struct lysc_type_leafref*)*type; |
| 2714 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2715 | /* RFC 7950 9.9.3 - require-instance */ |
| 2716 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2717 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2718 | if (tpdfname) { |
| 2719 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2720 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2721 | } else { |
| 2722 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2723 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2724 | free(*type); |
| 2725 | *type = NULL; |
| 2726 | } |
| 2727 | return LY_EVALID; |
| 2728 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2729 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2730 | } else if (base) { |
| 2731 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2732 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2733 | } else { |
| 2734 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2735 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2736 | } |
| 2737 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2738 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2739 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2740 | } else if (base) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2741 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path); |
| 2742 | lref->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2743 | } else if (tpdfname) { |
| 2744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2745 | return LY_EVALID; |
| 2746 | } else { |
| 2747 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2748 | free(*type); |
| 2749 | *type = NULL; |
| 2750 | return LY_EVALID; |
| 2751 | } |
| 2752 | if (tpdfname) { |
| 2753 | type_p->compiled = *type; |
| 2754 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2755 | } |
| 2756 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2757 | case LY_TYPE_INST: |
| 2758 | /* RFC 7950 9.9.3 - require-instance */ |
| 2759 | if (type_p->flags & LYS_SET_REQINST) { |
| 2760 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2761 | } else { |
| 2762 | /* default is true */ |
| 2763 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2764 | } |
| 2765 | |
| 2766 | if (tpdfname) { |
| 2767 | type_p->compiled = *type; |
| 2768 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2769 | } |
| 2770 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2771 | case LY_TYPE_UNION: |
| 2772 | un = (struct lysc_type_union*)(*type); |
| 2773 | |
| 2774 | /* RFC 7950 7.4 - type */ |
| 2775 | if (type_p->types) { |
| 2776 | if (base) { |
| 2777 | /* only the directly derived union can contain types specification */ |
| 2778 | if (tpdfname) { |
| 2779 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2780 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2781 | tpdfname); |
| 2782 | } else { |
| 2783 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2784 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2785 | free(*type); |
| 2786 | *type = NULL; |
| 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 | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2793 | LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, |
| 2794 | &type_p->types[u], &un->types[u + additional], 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 | fd69e1d | 2020-07-03 11:57:17 +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, |
Radek Krejci | c4fa029 | 2020-05-14 10:54:49 +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)); |
| 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; |
| 2810 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path); |
| 2811 | lref->refcount = 1; |
| 2812 | lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2813 | lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 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 */ |
| 2827 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 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", ""); |
| 2840 | free(*type); |
| 2841 | *type = NULL; |
| 2842 | } |
| 2843 | return LY_EVALID; |
| 2844 | } |
| 2845 | |
| 2846 | if (tpdfname) { |
| 2847 | type_p->compiled = *type; |
| 2848 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2849 | } |
| 2850 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2851 | case LY_TYPE_BOOL: |
| 2852 | case LY_TYPE_EMPTY: |
| 2853 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2854 | break; |
| 2855 | } |
| 2856 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2857 | done: |
| 2858 | return ret; |
| 2859 | } |
| 2860 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2861 | /** |
| 2862 | * @brief Compile information about the leaf/leaf-list's type. |
| 2863 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2864 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2865 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2866 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2867 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2868 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2869 | * @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] | 2870 | * @param[out] units Storage for inheriting units value from the typedefs the current type derives from. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2871 | * @return LY_ERR value. |
| 2872 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2873 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2874 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2875 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2876 | struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2877 | { |
| 2878 | LY_ERR ret = LY_SUCCESS; |
| 2879 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2880 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2881 | struct type_context { |
| 2882 | const struct lysp_tpdf *tpdf; |
| 2883 | struct lysp_node *node; |
| 2884 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2885 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2886 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2887 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2888 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2889 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2890 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2891 | |
| 2892 | (*type) = NULL; |
| 2893 | |
| 2894 | tctx = calloc(1, sizeof *tctx); |
| 2895 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2896 | for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2897 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2898 | ret == LY_SUCCESS; |
| 2899 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2900 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2901 | if (basetype) { |
| 2902 | break; |
| 2903 | } |
| 2904 | |
| 2905 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2906 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2907 | tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2908 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2909 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2910 | if (units && !*units) { |
| 2911 | /* inherit units */ |
| 2912 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2913 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2914 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2915 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2916 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2917 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2918 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2919 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2920 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2921 | break; |
| 2922 | } |
| 2923 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2924 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2925 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2926 | * 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] | 2927 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2928 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2929 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2930 | dummyloops = 1; |
| 2931 | goto preparenext; |
| 2932 | } else { |
| 2933 | tctx = NULL; |
| 2934 | break; |
| 2935 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2936 | } |
| 2937 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2938 | /* circular typedef reference detection */ |
| 2939 | for (u = 0; u < tpdf_chain.count; u++) { |
| 2940 | /* local part */ |
| 2941 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 2942 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2943 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2944 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2945 | free(tctx); |
| 2946 | ret = LY_EVALID; |
| 2947 | goto cleanup; |
| 2948 | } |
| 2949 | } |
| 2950 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 2951 | /* global part for unions corner case */ |
| 2952 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 2953 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2954 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2955 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2956 | free(tctx); |
| 2957 | ret = LY_EVALID; |
| 2958 | goto cleanup; |
| 2959 | } |
| 2960 | } |
| 2961 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2962 | /* store information for the following processing */ |
| 2963 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2964 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2965 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2966 | /* prepare next loop */ |
| 2967 | tctx_prev = tctx; |
| 2968 | tctx = calloc(1, sizeof *tctx); |
| 2969 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2970 | } |
| 2971 | free(tctx); |
| 2972 | |
| 2973 | /* allocate type according to the basetype */ |
| 2974 | switch (basetype) { |
| 2975 | case LY_TYPE_BINARY: |
| 2976 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2977 | break; |
| 2978 | case LY_TYPE_BITS: |
| 2979 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2980 | break; |
| 2981 | case LY_TYPE_BOOL: |
| 2982 | case LY_TYPE_EMPTY: |
| 2983 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2984 | break; |
| 2985 | case LY_TYPE_DEC64: |
| 2986 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2987 | break; |
| 2988 | case LY_TYPE_ENUM: |
| 2989 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2990 | break; |
| 2991 | case LY_TYPE_IDENT: |
| 2992 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2993 | break; |
| 2994 | case LY_TYPE_INST: |
| 2995 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2996 | break; |
| 2997 | case LY_TYPE_LEAFREF: |
| 2998 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2999 | break; |
| 3000 | case LY_TYPE_STRING: |
| 3001 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3002 | break; |
| 3003 | case LY_TYPE_UNION: |
| 3004 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3005 | break; |
| 3006 | case LY_TYPE_INT8: |
| 3007 | case LY_TYPE_UINT8: |
| 3008 | case LY_TYPE_INT16: |
| 3009 | case LY_TYPE_UINT16: |
| 3010 | case LY_TYPE_INT32: |
| 3011 | case LY_TYPE_UINT32: |
| 3012 | case LY_TYPE_INT64: |
| 3013 | case LY_TYPE_UINT64: |
| 3014 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3015 | break; |
| 3016 | case LY_TYPE_UNKNOWN: |
| 3017 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3018 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3019 | ret = LY_EVALID; |
| 3020 | goto cleanup; |
| 3021 | } |
| 3022 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3023 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3024 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3025 | ly_data_type2str[basetype]); |
| 3026 | free(*type); |
| 3027 | (*type) = NULL; |
| 3028 | ret = LY_EVALID; |
| 3029 | goto cleanup; |
| 3030 | } |
| 3031 | |
| 3032 | /* get restrictions from the referred typedefs */ |
| 3033 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3034 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3035 | |
| 3036 | /* remember the typedef context for circular check */ |
| 3037 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3038 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3039 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3040 | base = tctx->tpdf->type.compiled; |
| 3041 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3042 | } 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] | 3043 | /* no change, just use the type information from the base */ |
| 3044 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3045 | ++base->refcount; |
| 3046 | continue; |
| 3047 | } |
| 3048 | |
| 3049 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3050 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3051 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3052 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3053 | ret = LY_EVALID; |
| 3054 | goto cleanup; |
| 3055 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3056 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3057 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3058 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3059 | ret = LY_EVALID; |
| 3060 | goto cleanup; |
| 3061 | } |
| 3062 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3063 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3064 | /* TODO user type plugins */ |
| 3065 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3066 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3067 | ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf*)tctx->tpdf)->type, |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 3068 | basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3069 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3070 | LY_CHECK_GOTO(ret, cleanup); |
| 3071 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3072 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3073 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3074 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3075 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3076 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3077 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3078 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3079 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3080 | /* TODO user type plugins */ |
| 3081 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3082 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3083 | ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3084 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3085 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3086 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3087 | free(*type); |
| 3088 | (*type) = base; |
| 3089 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3090 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3091 | if (dflt && !(*type)->dflt) { |
| 3092 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3093 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3094 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3095 | (*type)->dflt->realtype = (*type); |
| 3096 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3097 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3098 | LY_PREF_SCHEMA, (*type)->dflt_mod, NULL, NULL, (*type)->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3099 | if (err) { |
| 3100 | ly_err_print(err); |
| 3101 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3102 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3103 | ly_err_free(err); |
| 3104 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3105 | if (ret == LY_EINCOMPLETE) { |
| 3106 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3107 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3108 | |
| 3109 | /* but in general result is so far ok */ |
| 3110 | ret = LY_SUCCESS; |
| 3111 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3112 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3113 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3114 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3115 | 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] | 3116 | |
| 3117 | cleanup: |
| 3118 | ly_set_erase(&tpdf_chain, free); |
| 3119 | return ret; |
| 3120 | } |
| 3121 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3122 | /** |
| 3123 | * @brief Compile status information of the given node. |
| 3124 | * |
| 3125 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3126 | * has the status correctly set during the compilation. |
| 3127 | * |
| 3128 | * @param[in] ctx Compile context |
| 3129 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3130 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3131 | * the compatibility with the parent's status value. |
| 3132 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3133 | * @return LY_ERR value. |
| 3134 | */ |
| 3135 | static LY_ERR |
| 3136 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3137 | { |
| 3138 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3139 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3140 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3141 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3142 | if ((parent_flags & 0x3) != 0x3) { |
| 3143 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3144 | * combination of bits (0x3) which marks the uses_status value */ |
| 3145 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3146 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3147 | } |
| 3148 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3149 | } else { |
| 3150 | (*node_flags) |= LYS_STATUS_CURR; |
| 3151 | } |
| 3152 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3153 | /* check status compatibility with the parent */ |
| 3154 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3155 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3156 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3157 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3158 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3159 | } else { /* LYS_STATUS_DEPRC */ |
| 3160 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3161 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3162 | } |
| 3163 | return LY_EVALID; |
| 3164 | } |
| 3165 | } |
| 3166 | return LY_SUCCESS; |
| 3167 | } |
| 3168 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3169 | /** |
| 3170 | * @brief Check uniqness of the node/action/notification name. |
| 3171 | * |
| 3172 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3173 | * structures, but they share the namespace so we need to check their name collisions. |
| 3174 | * |
| 3175 | * @param[in] ctx Compile context. |
| 3176 | * @param[in] children List (linked list) of data nodes to go through. |
| 3177 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3178 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3179 | * @param[in] name Name of the item to find in the given lists. |
| 3180 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3181 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3182 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3183 | */ |
| 3184 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3185 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions, |
| 3186 | const struct lysc_notif *notifs, const char *name, void *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3187 | { |
| 3188 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3189 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3190 | |
| 3191 | LY_LIST_FOR(children, iter) { |
| 3192 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3193 | goto error; |
| 3194 | } |
| 3195 | } |
| 3196 | LY_ARRAY_FOR(actions, u) { |
| 3197 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3198 | goto error; |
| 3199 | } |
| 3200 | } |
| 3201 | LY_ARRAY_FOR(notifs, u) { |
| 3202 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3203 | goto error; |
| 3204 | } |
| 3205 | } |
| 3206 | return LY_SUCCESS; |
| 3207 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3208 | 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] | 3209 | return LY_EEXIST; |
| 3210 | } |
| 3211 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3212 | static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3213 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3214 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3215 | * @brief Compile parsed RPC/action schema node information. |
| 3216 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3217 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3218 | * @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] | 3219 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3220 | * @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). |
| 3221 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3222 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3223 | */ |
| 3224 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3225 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3226 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3227 | { |
| 3228 | LY_ERR ret = LY_SUCCESS; |
| 3229 | struct lysp_node *child_p; |
| 3230 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3231 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3232 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3233 | lysc_update_path(ctx, parent, action_p->name); |
| 3234 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3235 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3236 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3237 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3238 | action_p->name, action)) { |
| 3239 | return LY_EVALID; |
| 3240 | } |
| 3241 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3242 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3244 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3245 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3246 | return LY_EVALID; |
| 3247 | } |
| 3248 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3249 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3250 | action->module = ctx->mod; |
| 3251 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3252 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3253 | action->sp = action_p; |
| 3254 | } |
| 3255 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3256 | |
| 3257 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3258 | * 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] | 3259 | 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] | 3260 | |
| 3261 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3262 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3263 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3264 | 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] | 3265 | 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] | 3266 | |
| 3267 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3268 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3269 | COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3270 | COMPILE_EXTS_GOTO(ctx, action_p->input.exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3271 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3272 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3273 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3274 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3275 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3276 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3277 | |
| 3278 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3279 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3280 | COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3281 | COMPILE_EXTS_GOTO(ctx, action_p->output.exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3282 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3283 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3284 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3285 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3286 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3287 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3288 | |
| 3289 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3290 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3291 | ly_set_add(&ctx->xpath, action, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3292 | } |
| 3293 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3294 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3295 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3296 | return ret; |
| 3297 | } |
| 3298 | |
| 3299 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3300 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3301 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3302 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3303 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3304 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3305 | * @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] | 3306 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3307 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3308 | */ |
| 3309 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3310 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3311 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3312 | { |
| 3313 | LY_ERR ret = LY_SUCCESS; |
| 3314 | struct lysp_node *child_p; |
| 3315 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3316 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3317 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3318 | lysc_update_path(ctx, parent, notif_p->name); |
| 3319 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3320 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3321 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3322 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3323 | notif_p->name, notif)) { |
| 3324 | return LY_EVALID; |
| 3325 | } |
| 3326 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3327 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3328 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3329 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3330 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3331 | return LY_EVALID; |
| 3332 | } |
| 3333 | |
| 3334 | notif->nodetype = LYS_NOTIF; |
| 3335 | notif->module = ctx->mod; |
| 3336 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3337 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3338 | notif->sp = notif_p; |
| 3339 | } |
| 3340 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3341 | |
| 3342 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3343 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3344 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3345 | |
| 3346 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3347 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3348 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3349 | 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] | 3350 | 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] | 3351 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3352 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3353 | ly_set_add(&ctx->xpath, notif, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3354 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3355 | 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] | 3356 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3357 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3358 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3359 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3360 | } |
| 3361 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3362 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3363 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3364 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3365 | return ret; |
| 3366 | } |
| 3367 | |
| 3368 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3369 | * @brief Compile parsed container node information. |
| 3370 | * @param[in] ctx Compile context |
| 3371 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3372 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3373 | * is enriched with the container-specific information. |
| 3374 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3375 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3376 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3377 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3378 | { |
| 3379 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3380 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3381 | struct lysp_node *child_p; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3382 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3383 | LY_ERR ret = LY_SUCCESS; |
| 3384 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3385 | if (cont_p->presence) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3386 | /* explicit presence */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3387 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3388 | } else if (cont_p->musts) { |
| 3389 | /* container with a must condition */ |
Radek Krejci | 175f25b | 2020-08-13 12:02:36 +0200 | [diff] [blame] | 3390 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name); |
| 3391 | cont->flags |= LYS_PRESENCE; |
| 3392 | } else if (cont_p->when) { |
| 3393 | /* container with a when condition */ |
| 3394 | 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] | 3395 | cont->flags |= LYS_PRESENCE; |
| 3396 | } else if (cont_p->parent) { |
| 3397 | if (cont_p->parent->nodetype == LYS_CHOICE) { |
| 3398 | /* 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] | 3399 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".", |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3400 | cont_p->name, cont_p->parent->name); |
| 3401 | cont->flags |= LYS_PRESENCE; |
| 3402 | } else if ((cont_p->parent->nodetype == LYS_CASE) |
| 3403 | && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) { |
| 3404 | /* 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] | 3405 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".", |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3406 | cont_p->name, cont_p->parent->name); |
| 3407 | cont->flags |= LYS_PRESENCE; |
| 3408 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3409 | } |
| 3410 | |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3411 | /* more cases when the container has meaning but is kept NP for convenience: |
| 3412 | * - when condition |
| 3413 | * - direct child action/notification |
| 3414 | */ |
| 3415 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3416 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3417 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3418 | } |
| 3419 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3420 | 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] | 3421 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3422 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3423 | ly_set_add(&ctx->xpath, cont, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3424 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3425 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3426 | COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3427 | |
| 3428 | done: |
| 3429 | return ret; |
| 3430 | } |
| 3431 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3432 | /* |
| 3433 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3434 | * @param[in] ctx Compile context. |
| 3435 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3436 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3437 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3438 | * @return LY_ERR value. |
| 3439 | */ |
| 3440 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3441 | lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, struct lysc_node_leaf *leaf) |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3442 | { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3443 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3444 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3445 | LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type, |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3446 | leaf->units ? NULL : &leaf->units)); |
| 3447 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3448 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3449 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3450 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3451 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3452 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3453 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3454 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3455 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3456 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3457 | } |
| 3458 | } else { |
| 3459 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3460 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3461 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3462 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3463 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3464 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3465 | } |
| 3466 | } |
| 3467 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3468 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3469 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3470 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3471 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3472 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3473 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3474 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3475 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3476 | } |
| 3477 | } |
| 3478 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3479 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3480 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3481 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3482 | return LY_EVALID; |
| 3483 | } |
| 3484 | } |
| 3485 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3486 | return LY_SUCCESS; |
| 3487 | } |
| 3488 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3489 | /** |
| 3490 | * @brief Compile parsed leaf node information. |
| 3491 | * @param[in] ctx Compile context |
| 3492 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3493 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3494 | * is enriched with the leaf-specific information. |
| 3495 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3496 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3497 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3498 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3499 | { |
| 3500 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3501 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3502 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3503 | LY_ERR ret = LY_SUCCESS; |
| 3504 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3505 | 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] | 3506 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3507 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3508 | ly_set_add(&ctx->xpath, leaf, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3509 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3510 | if (leaf_p->units) { |
| 3511 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3512 | leaf->flags |= LYS_SET_UNITS; |
| 3513 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3514 | |
| 3515 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3516 | leaf->dflt = (void*)leaf_p->dflt; |
| 3517 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3518 | if (ret) { |
| 3519 | leaf->dflt = NULL; |
| 3520 | return ret; |
| 3521 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3522 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3523 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3524 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3525 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3526 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3527 | leaf->dflt->realtype = leaf->type; |
| 3528 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3529 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3530 | LY_PREF_SCHEMA, leaf->dflt_mod, node, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3531 | leaf->dflt->realtype->refcount++; |
| 3532 | if (err) { |
| 3533 | ly_err_print(err); |
| 3534 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3535 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3536 | ly_err_free(err); |
| 3537 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3538 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3539 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3540 | LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod)); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3541 | |
| 3542 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3543 | ret = LY_SUCCESS; |
| 3544 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3545 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3546 | leaf->flags |= LYS_SET_DFLT; |
| 3547 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3548 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3549 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3550 | done: |
| 3551 | return ret; |
| 3552 | } |
| 3553 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3554 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3555 | * @brief Compile parsed leaf-list node information. |
| 3556 | * @param[in] ctx Compile context |
| 3557 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3558 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3559 | * is enriched with the leaf-list-specific information. |
| 3560 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3561 | */ |
| 3562 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3563 | lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3564 | { |
| 3565 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3566 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3567 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3568 | LY_ERR ret = LY_SUCCESS; |
| 3569 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3570 | 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] | 3571 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3572 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3573 | ly_set_add(&ctx->xpath, llist, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3574 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3575 | if (llist_p->units) { |
| 3576 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3577 | llist->flags |= LYS_SET_UNITS; |
| 3578 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3579 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3580 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3581 | llist->dflts = (void*)llist_p->dflts; |
| 3582 | ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist); |
Michal Vasko | 6a044b2 | 2020-01-15 12:25:39 +0100 | [diff] [blame] | 3583 | if (ret != LY_SUCCESS) { |
| 3584 | /* make sure the defaults are freed correctly */ |
| 3585 | if (llist_p->dflts) { |
| 3586 | llist->dflts = NULL; |
| 3587 | } |
| 3588 | return ret; |
| 3589 | } |
| 3590 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3591 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3592 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3593 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done); |
| 3594 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(llist_p->dflts), ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3595 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3596 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3597 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3598 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3599 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3600 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3601 | llist->dflts[u]->realtype = llist->type; |
| 3602 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3603 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3604 | LY_PREF_SCHEMA, llist->dflts_mods[u], node, NULL, llist->dflts[u], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3605 | llist->dflts[u]->realtype->refcount++; |
| 3606 | if (err) { |
| 3607 | ly_err_print(err); |
| 3608 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3609 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3610 | ly_err_free(err); |
| 3611 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3612 | if (ret == LY_EINCOMPLETE) { |
| 3613 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3614 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3615 | |
| 3616 | /* but in general result is so far ok */ |
| 3617 | ret = LY_SUCCESS; |
| 3618 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3619 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3620 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3621 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3622 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3623 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3624 | /* configuration data values must be unique - so check the default values */ |
| 3625 | LY_ARRAY_FOR(llist->dflts, u) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3626 | for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3627 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3628 | int dynamic = 0; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3629 | const char *val = llist->type->plugin->print(llist->dflts[v], LY_PREF_SCHEMA, llist->dflts_mods[v], |
| 3630 | &dynamic); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3631 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3632 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3633 | if (dynamic) { |
| 3634 | free((char*)val); |
| 3635 | } |
| 3636 | return LY_EVALID; |
| 3637 | } |
| 3638 | } |
| 3639 | } |
| 3640 | } |
| 3641 | |
| 3642 | /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3643 | |
| 3644 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3645 | if (llist->min) { |
| 3646 | llist->flags |= LYS_MAND_TRUE; |
| 3647 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3648 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3649 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3650 | done: |
| 3651 | return ret; |
| 3652 | } |
| 3653 | |
| 3654 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3655 | * @brief Compile information about list's uniques. |
| 3656 | * @param[in] ctx Compile context. |
| 3657 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3658 | * @param[in] uniques Sized array list of unique statements. |
| 3659 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3660 | * @return LY_ERR value. |
| 3661 | */ |
| 3662 | static LY_ERR |
| 3663 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3664 | { |
| 3665 | LY_ERR ret = LY_SUCCESS; |
| 3666 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3667 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3668 | const char *keystr, *delim; |
| 3669 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3670 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3671 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3672 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3673 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3674 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3675 | config = -1; |
| 3676 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3677 | keystr = uniques[v]; |
| 3678 | while (keystr) { |
| 3679 | delim = strpbrk(keystr, " \t\n"); |
| 3680 | if (delim) { |
| 3681 | len = delim - keystr; |
| 3682 | while (isspace(*delim)) { |
| 3683 | ++delim; |
| 3684 | } |
| 3685 | } else { |
| 3686 | len = strlen(keystr); |
| 3687 | } |
| 3688 | |
| 3689 | /* unique node must be present */ |
| 3690 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3691 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3692 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3693 | if (ret != LY_SUCCESS) { |
| 3694 | if (ret == LY_EDENIED) { |
| 3695 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3696 | "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.", |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3697 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3698 | } |
| 3699 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3700 | } else if (flags) { |
| 3701 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3702 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3703 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3704 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3705 | } |
| 3706 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3707 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3708 | /* all referenced leafs must be of the same config type */ |
| 3709 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3710 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3711 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3712 | return LY_EVALID; |
| 3713 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3714 | config = 1; |
| 3715 | } else { /* LYS_CONFIG_R */ |
| 3716 | config = 0; |
| 3717 | } |
| 3718 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3719 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3720 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3721 | if (parent->nodetype == LYS_LIST) { |
| 3722 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3723 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3724 | return LY_EVALID; |
| 3725 | } |
| 3726 | } |
| 3727 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3728 | /* check status */ |
| 3729 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3730 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3731 | |
| 3732 | /* mark leaf as unique */ |
| 3733 | (*key)->flags |= LYS_UNIQUE; |
| 3734 | |
| 3735 | /* next unique value in line */ |
| 3736 | keystr = delim; |
| 3737 | } |
| 3738 | /* next unique definition */ |
| 3739 | } |
| 3740 | |
| 3741 | return LY_SUCCESS; |
| 3742 | } |
| 3743 | |
| 3744 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3745 | * @brief Compile parsed list node information. |
| 3746 | * @param[in] ctx Compile context |
| 3747 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3748 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3749 | * is enriched with the list-specific information. |
| 3750 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3751 | */ |
| 3752 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3753 | lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3754 | { |
| 3755 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3756 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3757 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3758 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3759 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3760 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3761 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3762 | LY_ERR ret = LY_SUCCESS; |
| 3763 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3764 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3765 | if (list->min) { |
| 3766 | list->flags |= LYS_MAND_TRUE; |
| 3767 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3768 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3769 | |
| 3770 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3771 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3772 | } |
| 3773 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3774 | 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] | 3775 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3776 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3777 | ly_set_add(&ctx->xpath, list, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3778 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3779 | |
| 3780 | /* keys */ |
| 3781 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3782 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3783 | return LY_EVALID; |
| 3784 | } |
| 3785 | |
| 3786 | /* find all the keys (must be direct children) */ |
| 3787 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3788 | if (!keystr) { |
| 3789 | /* keyless list */ |
| 3790 | list->flags |= LYS_KEYLESS; |
| 3791 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3792 | while (keystr) { |
| 3793 | delim = strpbrk(keystr, " \t\n"); |
| 3794 | if (delim) { |
| 3795 | len = delim - keystr; |
| 3796 | while (isspace(*delim)) { |
| 3797 | ++delim; |
| 3798 | } |
| 3799 | } else { |
| 3800 | len = strlen(keystr); |
| 3801 | } |
| 3802 | |
| 3803 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3804 | 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] | 3805 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3806 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3807 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3808 | return LY_EVALID; |
| 3809 | } |
| 3810 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3811 | if (key->flags & LYS_KEY) { |
| 3812 | /* the node was already marked as a key */ |
| 3813 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3814 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3815 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3816 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3817 | |
| 3818 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3819 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3820 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3821 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3822 | return LY_EVALID; |
| 3823 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3824 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3825 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3826 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3827 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3828 | "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] | 3829 | return LY_EVALID; |
| 3830 | } |
| 3831 | } else { |
| 3832 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3833 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3834 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3835 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3836 | return LY_EVALID; |
| 3837 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3838 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3839 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3840 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3841 | return LY_EVALID; |
| 3842 | } |
| 3843 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3844 | |
| 3845 | /* check status */ |
| 3846 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3847 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3848 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3849 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3850 | if (key->dflt) { |
| 3851 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3852 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3853 | free(key->dflt); |
| 3854 | key->dflt = NULL; |
| 3855 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3856 | } |
| 3857 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3858 | key->flags |= LYS_KEY; |
| 3859 | |
| 3860 | /* move it to the correct position */ |
| 3861 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3862 | /* fix links in closest previous siblings of the key */ |
| 3863 | if (key->next) { |
| 3864 | key->next->prev = key->prev; |
| 3865 | } else { |
| 3866 | /* last child */ |
| 3867 | list->child->prev = key->prev; |
| 3868 | } |
| 3869 | if (key->prev->next) { |
| 3870 | key->prev->next = key->next; |
| 3871 | } |
| 3872 | /* fix links in the key */ |
| 3873 | if (prev_key) { |
| 3874 | key->prev = (struct lysc_node*)prev_key; |
| 3875 | key->next = prev_key->next; |
| 3876 | } else { |
| 3877 | key->prev = list->child->prev; |
| 3878 | key->next = list->child; |
| 3879 | } |
| 3880 | /* fix links in closes future siblings of the key */ |
| 3881 | if (prev_key) { |
| 3882 | if (prev_key->next) { |
| 3883 | prev_key->next->prev = (struct lysc_node*)key; |
| 3884 | } else { |
| 3885 | list->child->prev = (struct lysc_node*)key; |
| 3886 | } |
| 3887 | prev_key->next = (struct lysc_node*)key; |
| 3888 | } else { |
| 3889 | list->child->prev = (struct lysc_node*)key; |
| 3890 | } |
| 3891 | /* fix links in parent */ |
| 3892 | if (!key->prev->next) { |
| 3893 | list->child = (struct lysc_node*)key; |
| 3894 | } |
| 3895 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3896 | |
| 3897 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3898 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3899 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3900 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3901 | } |
| 3902 | |
| 3903 | /* uniques */ |
| 3904 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3905 | LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3906 | } |
| 3907 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3908 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3909 | COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3910 | |
| 3911 | done: |
| 3912 | return ret; |
| 3913 | } |
| 3914 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3915 | /** |
| 3916 | * @brief Do some checks and set the default choice's case. |
| 3917 | * |
| 3918 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3919 | * |
| 3920 | * @param[in] ctx Compile context. |
| 3921 | * @param[in] dflt Name of the default branch. Can contain even the prefix, but it make sense only in case it is the prefix of the module itself, |
| 3922 | * not the reference to the imported module. |
| 3923 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3924 | * @return LY_ERR value. |
| 3925 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3926 | static LY_ERR |
| 3927 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3928 | { |
| 3929 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3930 | const char *prefix = NULL, *name; |
| 3931 | size_t prefix_len = 0; |
| 3932 | |
| 3933 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3934 | name = strchr(dflt, ':'); |
| 3935 | if (name) { |
| 3936 | prefix = dflt; |
| 3937 | prefix_len = name - prefix; |
| 3938 | ++name; |
| 3939 | } else { |
| 3940 | name = dflt; |
| 3941 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3942 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3943 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3944 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3945 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3946 | prefix_len, prefix); |
| 3947 | return LY_EVALID; |
| 3948 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3949 | ch->dflt = (struct lysc_node_case*)lys_find_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3950 | if (!ch->dflt) { |
| 3951 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3952 | "Default case \"%s\" not found.", dflt); |
| 3953 | return LY_EVALID; |
| 3954 | } |
| 3955 | /* no mandatory nodes directly under the default case */ |
| 3956 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3957 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3958 | break; |
| 3959 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3960 | if (iter->flags & LYS_MAND_TRUE) { |
| 3961 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3962 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3963 | return LY_EVALID; |
| 3964 | } |
| 3965 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3966 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3967 | return LY_SUCCESS; |
| 3968 | } |
| 3969 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3970 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3971 | lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3972 | { |
| 3973 | struct lys_module *mod; |
| 3974 | const char *prefix = NULL, *name; |
| 3975 | size_t prefix_len = 0; |
| 3976 | struct lysc_node_case *cs; |
| 3977 | struct lysc_node *node; |
| 3978 | |
| 3979 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3980 | name = strchr(dflt, ':'); |
| 3981 | if (name) { |
| 3982 | prefix = dflt; |
| 3983 | prefix_len = name - prefix; |
| 3984 | ++name; |
| 3985 | } else { |
| 3986 | name = dflt; |
| 3987 | } |
| 3988 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3989 | if (prefix) { |
| 3990 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3991 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3992 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3993 | "The prefix does not match any imported module of the deviation module.", dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3994 | return LY_EVALID; |
| 3995 | } |
| 3996 | } else { |
| 3997 | mod = ctx->mod; |
| 3998 | } |
| 3999 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4000 | cs = (struct lysc_node_case*)lys_find_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4001 | if (!cs) { |
| 4002 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4003 | "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4004 | return LY_EVALID; |
| 4005 | } |
| 4006 | |
| 4007 | /* check that there is no mandatory node */ |
| 4008 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4009 | if (node->parent != (struct lysc_node*)cs) { |
| 4010 | break; |
| 4011 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4012 | if (node->flags & LYS_MAND_TRUE) { |
| 4013 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4014 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4015 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4016 | return LY_EVALID; |
| 4017 | } |
| 4018 | } |
| 4019 | |
| 4020 | /* set the default case in choice */ |
| 4021 | ch->dflt = cs; |
| 4022 | cs->flags |= LYS_SET_DFLT; |
| 4023 | |
| 4024 | return LY_SUCCESS; |
| 4025 | } |
| 4026 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4027 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4028 | * @brief Compile parsed choice node information. |
| 4029 | * @param[in] ctx Compile context |
| 4030 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4031 | * @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] | 4032 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4033 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4034 | */ |
| 4035 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4036 | lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4037 | { |
| 4038 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4039 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4040 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4041 | LY_ERR ret = LY_SUCCESS; |
| 4042 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4043 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4044 | if (child_p->nodetype == LYS_CASE) { |
| 4045 | LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4046 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4047 | } |
| 4048 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4049 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4050 | } |
| 4051 | } |
| 4052 | |
| 4053 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4054 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4055 | 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] | 4056 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4057 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4058 | return ret; |
| 4059 | } |
| 4060 | |
| 4061 | /** |
| 4062 | * @brief Compile parsed anydata or anyxml node information. |
| 4063 | * @param[in] ctx Compile context |
| 4064 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4065 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4066 | * is enriched with the any-specific information. |
| 4067 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4068 | */ |
| 4069 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4070 | lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4071 | { |
| 4072 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4073 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4074 | unsigned int u; |
| 4075 | LY_ERR ret = LY_SUCCESS; |
| 4076 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4077 | 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] | 4078 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4079 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4080 | ly_set_add(&ctx->xpath, any, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4081 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4082 | |
| 4083 | if (any->flags & LYS_CONFIG_W) { |
Radek Krejci | 5c4ed7b | 2020-08-12 11:29:44 +0200 | [diff] [blame] | 4084 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s", |
| 4085 | 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] | 4086 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4087 | done: |
| 4088 | return ret; |
| 4089 | } |
| 4090 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4091 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4092 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4093 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4094 | * |
| 4095 | * @param[in] ctx Compile context |
| 4096 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4097 | * the choice itself is expected instead of a specific case node. |
| 4098 | * @param[in] node Schema node to connect into the list. |
| 4099 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4100 | * 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] | 4101 | */ |
| 4102 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4103 | 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] | 4104 | { |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4105 | struct lysc_node **children, *start, *end; |
| 4106 | const struct lys_module *mod; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4107 | |
| 4108 | if (node->nodetype == LYS_CASE) { |
| 4109 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4110 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4111 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4112 | } |
| 4113 | if (children) { |
| 4114 | if (!(*children)) { |
| 4115 | /* first child */ |
| 4116 | *children = node; |
| 4117 | } else if (*children != node) { |
| 4118 | /* by the condition in previous branch we cover the choice/case children |
| 4119 | * - the children list is shared by the choice and the the first case, in addition |
| 4120 | * the first child of each case must be referenced from the case node. So the node is |
| 4121 | * actually always already inserted in case it is the first children - so here such |
| 4122 | * a situation actually corresponds to the first branch */ |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4123 | if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module) |
| 4124 | && (strcmp((*children)->prev->module->name, node->module->name) > 0)) { |
| 4125 | /* some augments are already connected and we are connecting new ones, |
| 4126 | * keep module name order and insert the node into the children list */ |
| 4127 | end = (*children); |
| 4128 | do { |
| 4129 | end = end->prev; |
| 4130 | mod = end->module; |
| 4131 | while (end->prev->module == mod) { |
| 4132 | end = end->prev; |
| 4133 | } |
| 4134 | } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module) |
| 4135 | && (strcmp(mod->name, node->module->name) > 0)); |
| 4136 | |
| 4137 | /* we have the last existing node after our node, easily get the first before and connect it */ |
| 4138 | start = end->prev; |
| 4139 | start->next = node; |
| 4140 | node->next = end; |
| 4141 | end->prev = node; |
| 4142 | node->prev = start; |
| 4143 | } else { |
| 4144 | /* insert at the end of the parent's children list */ |
| 4145 | (*children)->prev->next = node; |
| 4146 | node->prev = (*children)->prev; |
| 4147 | (*children)->prev = node; |
| 4148 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4149 | |
| 4150 | /* check the name uniqueness */ |
| 4151 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4152 | lysc_node_notifs(parent), node->name, node)) { |
| 4153 | return LY_EEXIST; |
| 4154 | } |
| 4155 | } |
| 4156 | } |
| 4157 | return LY_SUCCESS; |
| 4158 | } |
| 4159 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4160 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4161 | * @brief Prepare the case structure in choice node for the new data node. |
| 4162 | * |
| 4163 | * 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 |
| 4164 | * created in the choice when the first child was processed. |
| 4165 | * |
| 4166 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4167 | * @param[in] node_p Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case, |
Radek Krejci | 3942480 | 2020-08-12 09:31:00 +0200 | [diff] [blame] | 4168 | * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4169 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4170 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4171 | * @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, |
| 4172 | * 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] | 4173 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4174 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4175 | lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node_choice *ch, struct lysc_node *child) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4176 | { |
| 4177 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4178 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4179 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4180 | unsigned int u; |
| 4181 | LY_ERR ret; |
| 4182 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4183 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4184 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4185 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4186 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4187 | return NULL; \ |
| 4188 | } \ |
| 4189 | } |
| 4190 | |
Radek Krejci | 3942480 | 2020-08-12 09:31:00 +0200 | [diff] [blame] | 4191 | if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4192 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4193 | |
| 4194 | /* we have to add an implicit case node into the parent choice */ |
| 4195 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4196 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4197 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4198 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4199 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4200 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4201 | /* the case is already present since the child is not its first children */ |
| 4202 | return (struct lysc_node_case*)ch->cases->prev; |
| 4203 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4204 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4205 | |
| 4206 | /* explicit parent case is not present (this is its first child) */ |
| 4207 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4208 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4209 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4210 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4211 | cs->sp = node_p; |
| 4212 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4213 | /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */ |
Radek Krejci | bae745f | 2019-04-09 16:28:00 +0200 | [diff] [blame] | 4214 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4215 | |
| 4216 | if (node_p->when) { |
| 4217 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4218 | ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4219 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4220 | |
| 4221 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4222 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4223 | ly_set_add(&ctx->xpath, cs, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4224 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4225 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4226 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4227 | } else { |
| 4228 | LOGINT(ctx->ctx); |
| 4229 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4230 | } |
| 4231 | cs->module = ctx->mod; |
| 4232 | cs->prev = (struct lysc_node*)cs; |
| 4233 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4234 | lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4235 | cs->child = child; |
| 4236 | |
| 4237 | return cs; |
| 4238 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4239 | if (cs) { |
| 4240 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4241 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4242 | return NULL; |
| 4243 | |
| 4244 | #undef UNIQUE_CHECK |
| 4245 | } |
| 4246 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4247 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4248 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4249 | * |
| 4250 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4251 | * @param[in] node Target node where the config is supposed to be changed. |
| 4252 | * @param[in] config_flag Node's config flag to be applied to the @p node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4253 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4254 | * done only on the node for which the refine was created. The function applies also recursively to apply the config change |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4255 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4256 | * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging). |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4257 | * @return LY_ERR value. |
| 4258 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4259 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4260 | lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4261 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4262 | { |
| 4263 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4264 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4265 | |
| 4266 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4267 | /* nothing to do */ |
| 4268 | return LY_SUCCESS; |
| 4269 | } |
| 4270 | |
| 4271 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4272 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4273 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4274 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4275 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4276 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4277 | return LY_EVALID; |
| 4278 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4279 | node->flags |= LYS_SET_CONFIG; |
| 4280 | } else { |
| 4281 | if (node->flags & LYS_SET_CONFIG) { |
| 4282 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4283 | /* setting config flags, but have node with explicit config true */ |
| 4284 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4285 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4286 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4287 | return LY_EVALID; |
| 4288 | } |
| 4289 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4290 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4291 | return LY_SUCCESS; |
| 4292 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4293 | } |
| 4294 | node->flags &= ~LYS_CONFIG_MASK; |
| 4295 | node->flags |= config; |
| 4296 | |
| 4297 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4298 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4299 | LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4300 | } |
| 4301 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4302 | return LY_SUCCESS; |
| 4303 | } |
| 4304 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4305 | /** |
| 4306 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4307 | * |
| 4308 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4309 | * the flag to such parents from a mandatory children. |
| 4310 | * |
| 4311 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4312 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4313 | * (mandatory children was removed). |
| 4314 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4315 | void |
| 4316 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4317 | { |
| 4318 | struct lysc_node *iter; |
| 4319 | |
| 4320 | if (add) { /* set flag */ |
| 4321 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4322 | parent = parent->parent) { |
| 4323 | parent->flags |= LYS_MAND_TRUE; |
| 4324 | } |
| 4325 | } else { /* unset flag */ |
| 4326 | for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4327 | 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] | 4328 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4329 | /* there is another mandatory node */ |
| 4330 | return; |
| 4331 | } |
| 4332 | } |
| 4333 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4334 | parent->flags &= ~LYS_MAND_TRUE; |
| 4335 | } |
| 4336 | } |
| 4337 | } |
| 4338 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4339 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4340 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4341 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4342 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4343 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4344 | */ |
| 4345 | static void |
| 4346 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4347 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4348 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4349 | size_t len; |
| 4350 | |
| 4351 | len = strlen(aug_p->nodeid); |
| 4352 | LY_ARRAY_FOR(result, v) { |
| 4353 | if (strlen(result[v]->nodeid) <= len) { |
| 4354 | continue; |
| 4355 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4356 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4357 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4358 | memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4359 | break; |
| 4360 | } |
| 4361 | } |
| 4362 | result[v] = aug_p; |
| 4363 | LY_ARRAY_INCREMENT(result); |
| 4364 | } |
| 4365 | |
| 4366 | /** |
| 4367 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4368 | * |
| 4369 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4370 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4371 | * |
| 4372 | * @param[in] ctx Compile context. |
| 4373 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4374 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4375 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4376 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4377 | * @return LY_ERR value. |
| 4378 | */ |
| 4379 | LY_ERR |
| 4380 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4381 | { |
| 4382 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4383 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4384 | |
| 4385 | assert(augments); |
| 4386 | |
| 4387 | /* get count of the augments in module and all its submodules */ |
| 4388 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4389 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4390 | } |
| 4391 | LY_ARRAY_FOR(inc_p, u) { |
| 4392 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4393 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4394 | } |
| 4395 | } |
| 4396 | |
| 4397 | if (!count) { |
| 4398 | *augments = NULL; |
| 4399 | return LY_SUCCESS; |
| 4400 | } |
| 4401 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4402 | |
| 4403 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4404 | * together, so there can be even /z/y betwwen them. */ |
| 4405 | LY_ARRAY_FOR(aug_p, u) { |
| 4406 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4407 | } |
| 4408 | LY_ARRAY_FOR(inc_p, u) { |
| 4409 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4410 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4411 | } |
| 4412 | } |
| 4413 | |
| 4414 | *augments = result; |
| 4415 | return LY_SUCCESS; |
| 4416 | } |
| 4417 | |
| 4418 | /** |
| 4419 | * @brief Compile the parsed augment connecting it into its target. |
| 4420 | * |
| 4421 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4422 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4423 | * are already implemented and compiled. |
| 4424 | * |
| 4425 | * @param[in] ctx Compile context. |
| 4426 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4427 | * @param[in] parent Parent node to provide the augment's context. It is NULL for the top level augments and a node holding uses's |
| 4428 | * children in case of the augmenting uses data. |
| 4429 | * @return LY_SUCCESS on success. |
| 4430 | * @return LY_EVALID on failure. |
| 4431 | */ |
| 4432 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4433 | lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent) |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4434 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4435 | LY_ERR ret = LY_SUCCESS, rc; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4436 | struct lysp_node *node_p, *case_node_p; |
| 4437 | struct lysc_node *target; /* target target of the augment */ |
| 4438 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4439 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4440 | struct lys_module **aug_mod; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4441 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4442 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4443 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4444 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4445 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4446 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4447 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4448 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4449 | ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def, |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4450 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4451 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4452 | if (ret != LY_SUCCESS) { |
| 4453 | if (ret == LY_EDENIED) { |
| 4454 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4455 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4456 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4457 | } |
| 4458 | return LY_EVALID; |
| 4459 | } |
| 4460 | |
| 4461 | /* check for mandatory nodes |
| 4462 | * - new cases augmenting some choice can have mandatory nodes |
| 4463 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4464 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4465 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4466 | allow_mandatory = 1; |
| 4467 | } |
| 4468 | |
| 4469 | when_shared = NULL; |
| 4470 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4471 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4472 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4473 | && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) |
Radek Krejci | 2d56a89 | 2019-02-19 09:05:26 +0100 | [diff] [blame] | 4474 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4475 | && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4476 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4477 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4478 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4479 | return LY_EVALID; |
| 4480 | } |
| 4481 | |
| 4482 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4483 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4484 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4485 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4486 | } else { |
| 4487 | LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4488 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4489 | } |
| 4490 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4491 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4492 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4493 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4494 | * here we gets the last created node as last children of our parent */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4495 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4496 | /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4497 | for (node = (struct lysc_node*)lysc_node_children(target, flags); node->next && node->next->parent == node->parent; node = node->next); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4498 | } else if (target->nodetype == LYS_CHOICE) { |
| 4499 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4500 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4501 | } else { |
| 4502 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4503 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4504 | if (!node) { |
| 4505 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4506 | break; |
| 4507 | } |
| 4508 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4509 | } |
| 4510 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4511 | if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4512 | node->flags &= ~LYS_MAND_TRUE; |
| 4513 | lys_compile_mandatory_parents(target, 0); |
| 4514 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4515 | "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4516 | return LY_EVALID; |
| 4517 | } |
| 4518 | |
| 4519 | /* pass augment's when to all the children */ |
| 4520 | if (aug_p->when) { |
| 4521 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4522 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4523 | ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4524 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4525 | |
| 4526 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4527 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4528 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4529 | } |
| 4530 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4531 | when_shared = *when; |
| 4532 | } else { |
| 4533 | ++when_shared->refcount; |
| 4534 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4535 | |
| 4536 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4537 | /* in this case check "when" again for all children because of dummy node check */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4538 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4539 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4540 | } |
| 4541 | } |
| 4542 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4543 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4544 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4545 | switch (target->nodetype) { |
| 4546 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4547 | COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4548 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4549 | COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4550 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4551 | break; |
| 4552 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4553 | COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4554 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4555 | COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4556 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4557 | break; |
| 4558 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4559 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4560 | if (aug_p->actions) { |
| 4561 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4562 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4563 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4564 | return LY_EVALID; |
| 4565 | } |
| 4566 | if (aug_p->notifs) { |
| 4567 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4568 | "Invalid augment of %s node which is not allowed to contain notification node \"%s\".", |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4569 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4570 | return LY_EVALID; |
| 4571 | } |
| 4572 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4573 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4574 | /* add this module into the target module augmented_by, if not there already */ |
| 4575 | rc = LY_SUCCESS; |
| 4576 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4577 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4578 | rc = LY_EEXIST; |
| 4579 | break; |
| 4580 | } |
| 4581 | } |
| 4582 | if (!rc) { |
| 4583 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4584 | *aug_mod = ctx->mod; |
| 4585 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4586 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4587 | lysc_update_path(ctx, NULL, NULL); |
| 4588 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4589 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4590 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4591 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4592 | return ret; |
| 4593 | } |
| 4594 | |
| 4595 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4596 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4597 | * |
| 4598 | * @param[in] ctx Compile context. |
| 4599 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4600 | * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node. |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4601 | * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging). |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4602 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4603 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4604 | * the tests are skipped here, but they are supposed to be performed after all the deviations are applied. |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4605 | * @return LY_ERR value. |
| 4606 | */ |
| 4607 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4608 | lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, int refine_flag) |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4609 | { |
| 4610 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4611 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4612 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4613 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4614 | return LY_EVALID; |
| 4615 | } |
| 4616 | |
| 4617 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4618 | /* check if node has default value */ |
| 4619 | if (node->nodetype & LYS_LEAF) { |
| 4620 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4621 | if (refine_flag) { |
| 4622 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4623 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4624 | return LY_EVALID; |
| 4625 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4626 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4627 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4628 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4629 | |
| 4630 | /* update the list of incomplete default values if needed */ |
| 4631 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4632 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4633 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4634 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4635 | free(leaf->dflt); |
| 4636 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4637 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4638 | } |
| 4639 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4640 | if (refine_flag) { |
| 4641 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4642 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4643 | return LY_EVALID; |
| 4644 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4645 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4646 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4647 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid refine of mandatory under the default case."); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4648 | return LY_EVALID; |
| 4649 | } |
| 4650 | |
| 4651 | node->flags &= ~LYS_MAND_FALSE; |
| 4652 | node->flags |= LYS_MAND_TRUE; |
| 4653 | lys_compile_mandatory_parents(node->parent, 1); |
| 4654 | } else { |
| 4655 | /* make mandatory false */ |
| 4656 | node->flags &= ~LYS_MAND_TRUE; |
| 4657 | node->flags |= LYS_MAND_FALSE; |
| 4658 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4659 | if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt && ((struct lysc_node_leaf*)node)->type->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4660 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4661 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4662 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4663 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4664 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4665 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4666 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4667 | } |
| 4668 | } |
| 4669 | return LY_SUCCESS; |
| 4670 | } |
| 4671 | |
| 4672 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4673 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4674 | * If present, also apply uses's modificators. |
| 4675 | * |
| 4676 | * @param[in] ctx Compile context |
| 4677 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4678 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4679 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4680 | * the compile context. |
| 4681 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4682 | */ |
| 4683 | static LY_ERR |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4684 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p) |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4685 | { |
| 4686 | struct lysp_node *node_p; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4687 | struct lysc_node *node, *child, *iter; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4688 | /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4689 | struct lysc_node_container context_node_fake = |
| 4690 | {.nodetype = LYS_CONTAINER, |
| 4691 | .module = ctx->mod, |
| 4692 | .flags = parent ? parent->flags : 0, |
| 4693 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4694 | .prev = (struct lysc_node*)&context_node_fake, |
| 4695 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4696 | struct lysp_grp *grp = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4697 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4698 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4699 | int found; |
| 4700 | const char *id, *name, *prefix; |
| 4701 | size_t prefix_len, name_len; |
| 4702 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4703 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4704 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4705 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4706 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4707 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4708 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4709 | struct lysp_augment **augments = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4710 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4711 | struct lysc_notif **notifs = NULL; |
| 4712 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4713 | |
| 4714 | /* search for the grouping definition */ |
| 4715 | found = 0; |
| 4716 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4717 | 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] | 4718 | if (prefix) { |
| 4719 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4720 | if (!mod) { |
| 4721 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4722 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4723 | return LY_EVALID; |
| 4724 | } |
| 4725 | } else { |
| 4726 | mod = ctx->mod_def; |
| 4727 | } |
| 4728 | if (mod == ctx->mod_def) { |
| 4729 | for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4730 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4731 | LY_ARRAY_FOR(grp, u) { |
| 4732 | if (!strcmp(grp[u].name, name)) { |
| 4733 | grp = &grp[u]; |
| 4734 | found = 1; |
| 4735 | break; |
| 4736 | } |
| 4737 | } |
| 4738 | } |
| 4739 | } |
| 4740 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4741 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4742 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4743 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4744 | for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4745 | if (!strcmp(grp[u].name, name)) { |
| 4746 | grp = &grp[u]; |
| 4747 | found = 1; |
| 4748 | } |
| 4749 | } |
| 4750 | } |
| 4751 | if (!found && mod->parsed->includes) { |
| 4752 | /* ... and all the submodules */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4753 | for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4754 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4755 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4756 | for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4757 | if (!strcmp(grp[v].name, name)) { |
| 4758 | grp = &grp[v]; |
| 4759 | found = 1; |
| 4760 | } |
| 4761 | } |
| 4762 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4763 | } |
| 4764 | } |
| 4765 | } |
| 4766 | if (!found) { |
| 4767 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4768 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4769 | return LY_EVALID; |
| 4770 | } |
| 4771 | |
| 4772 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4773 | grp_stack_count = ctx->groupings.count; |
| 4774 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4775 | if (grp_stack_count == ctx->groupings.count) { |
| 4776 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4777 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4778 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4779 | return LY_EVALID; |
| 4780 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4781 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4782 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4783 | grp->flags |= LYS_USED_GRP; |
| 4784 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4785 | |
| 4786 | /* switch context's mod_def */ |
| 4787 | mod_old = ctx->mod_def; |
| 4788 | ctx->mod_def = mod; |
| 4789 | |
| 4790 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4791 | LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), cleanup); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4792 | |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4793 | /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly |
| 4794 | * applu refine and augment only to the nodes from the uses */ |
| 4795 | if (parent) { |
| 4796 | if (parent->nodetype == LYS_CASE) { |
| 4797 | child = (struct lysc_node*)lysc_node_children(parent->parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4798 | } else { |
| 4799 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4800 | } |
| 4801 | } else if (ctx->mod->compiled->data) { |
| 4802 | child = ctx->mod->compiled->data; |
| 4803 | } else { |
| 4804 | child = NULL; |
| 4805 | } |
| 4806 | /* remember the last child */ |
| 4807 | if (child) { |
| 4808 | child = child->prev; |
| 4809 | } |
| 4810 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4811 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4812 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4813 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4814 | LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4815 | } |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4816 | |
| 4817 | /* split the children and add the uses's data into the fake context node */ |
| 4818 | if (child) { |
| 4819 | context_node_fake.child = child->next; |
| 4820 | } else if (parent) { |
| 4821 | context_node_fake.child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4822 | } else if (ctx->mod->compiled->data) { |
| 4823 | context_node_fake.child = ctx->mod->compiled->data; |
| 4824 | } |
| 4825 | if (context_node_fake.child) { |
| 4826 | /* remember child as the last data node added by grouping to fix the list later */ |
| 4827 | child = context_node_fake.child->prev; |
| 4828 | context_node_fake.child->prev = NULL; |
| 4829 | } |
| 4830 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4831 | when_shared = NULL; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4832 | LY_LIST_FOR(context_node_fake.child, iter) { |
| 4833 | iter->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4834 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4835 | /* pass uses's when to all the data children, actions and notifications are ignored */ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4836 | if (uses_p->when) { |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4837 | LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4838 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4839 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 4840 | |
| 4841 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4842 | /* do not check "when" semantics in a grouping */ |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4843 | ly_set_add(&ctx->xpath, iter, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4844 | } |
| 4845 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4846 | when_shared = *when; |
| 4847 | } else { |
| 4848 | ++when_shared->refcount; |
| 4849 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4850 | |
| 4851 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4852 | /* in this case check "when" again for all children because of dummy node check */ |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4853 | ly_set_add(&ctx->xpath, iter, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4854 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4855 | } |
| 4856 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4857 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4858 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4859 | /* compile actions */ |
| 4860 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4861 | if (actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4862 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4863 | COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4864 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4865 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4866 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 4867 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 4868 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4869 | } |
| 4870 | } |
| 4871 | |
| 4872 | /* compile notifications */ |
| 4873 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4874 | if (notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4875 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4876 | COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4877 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4878 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4879 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 4880 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 4881 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4882 | } |
| 4883 | } |
| 4884 | |
| 4885 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4886 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4887 | LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4888 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4889 | LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4890 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4891 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4892 | /* reload previous context's mod_def */ |
| 4893 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4894 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4895 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4896 | /* apply refine */ |
| 4897 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4898 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4899 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4900 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, ctx->mod, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4901 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4902 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4903 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4904 | |
| 4905 | /* default value */ |
| 4906 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4907 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4908 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4909 | "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.", |
| 4910 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4911 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4912 | } |
| 4913 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4914 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4915 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4916 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4917 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4918 | } |
| 4919 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4920 | struct ly_err_item *err = NULL; |
| 4921 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4922 | if (leaf->dflt) { |
| 4923 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4924 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4925 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4926 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4927 | } else { |
| 4928 | /* prepare a new one */ |
| 4929 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4930 | leaf->dflt->realtype = leaf->type; |
| 4931 | } |
| 4932 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4933 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4934 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4935 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4936 | LY_PREF_SCHEMA, leaf->dflt_mod, node, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4937 | leaf->dflt->realtype->refcount++; |
| 4938 | if (err) { |
| 4939 | ly_err_print(err); |
| 4940 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4941 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4942 | ly_err_free(err); |
| 4943 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4944 | if (rc == LY_EINCOMPLETE) { |
| 4945 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4946 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4947 | |
| 4948 | /* but in general result is so far ok */ |
| 4949 | rc = LY_SUCCESS; |
| 4950 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4951 | LY_CHECK_GOTO(rc, cleanup); |
| 4952 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4953 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4954 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4955 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4956 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4957 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4958 | "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4959 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4960 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4961 | |
| 4962 | /* remove previous set of default values */ |
| 4963 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4964 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4965 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 4966 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 4967 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4968 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4969 | LY_ARRAY_FREE(llist->dflts); |
| 4970 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4971 | LY_ARRAY_FREE(llist->dflts_mods); |
| 4972 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4973 | |
| 4974 | /* create the new set of the default values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4975 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup); |
| 4976 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4977 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4978 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4979 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 4980 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4981 | LY_ARRAY_INCREMENT(llist->dflts); |
| 4982 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 4983 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4984 | rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]), |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4985 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
| 4986 | LY_PREF_SCHEMA, llist->dflts_mods[u], node, NULL, llist->dflts[u], |
| 4987 | NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4988 | llist->dflts[u]->realtype->refcount++; |
| 4989 | if (err) { |
| 4990 | ly_err_print(err); |
| 4991 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4992 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 4993 | ly_err_free(err); |
| 4994 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4995 | if (rc == LY_EINCOMPLETE) { |
| 4996 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4997 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4998 | |
| 4999 | /* but in general result is so far ok */ |
| 5000 | rc = LY_SUCCESS; |
| 5001 | } |
| 5002 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5003 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5004 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5005 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5006 | if (((struct lysc_node_choice*)node)->dflt) { |
| 5007 | /* unset LYS_SET_DFLT from the current default case */ |
| 5008 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 5009 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5010 | LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5011 | } |
| 5012 | } |
| 5013 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5014 | /* description */ |
| 5015 | if (rfn->dsc) { |
| 5016 | FREE_STRING(ctx->ctx, node->dsc); |
| 5017 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 5018 | } |
| 5019 | |
| 5020 | /* reference */ |
| 5021 | if (rfn->ref) { |
| 5022 | FREE_STRING(ctx->ctx, node->ref); |
| 5023 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 5024 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5025 | |
| 5026 | /* config */ |
| 5027 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5028 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5029 | LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5030 | } else { |
| 5031 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5032 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5033 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5034 | } |
| 5035 | |
| 5036 | /* mandatory */ |
| 5037 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5038 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5039 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5040 | |
| 5041 | /* presence */ |
| 5042 | if (rfn->presence) { |
| 5043 | if (node->nodetype != LYS_CONTAINER) { |
| 5044 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5045 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5046 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5047 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5048 | } |
| 5049 | node->flags |= LYS_PRESENCE; |
| 5050 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5051 | |
| 5052 | /* must */ |
| 5053 | if (rfn->musts) { |
| 5054 | switch (node->nodetype) { |
| 5055 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5056 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5057 | break; |
| 5058 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5059 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5060 | break; |
| 5061 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5062 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5063 | break; |
| 5064 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5065 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5066 | break; |
| 5067 | case LYS_ANYXML: |
| 5068 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5069 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5070 | break; |
| 5071 | default: |
| 5072 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5073 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5074 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5075 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5076 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5077 | ly_set_add(&ctx->xpath, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5078 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5079 | |
| 5080 | /* min/max-elements */ |
| 5081 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5082 | switch (node->nodetype) { |
| 5083 | case LYS_LEAFLIST: |
| 5084 | if (rfn->flags & LYS_SET_MAX) { |
| 5085 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5086 | } |
| 5087 | if (rfn->flags & LYS_SET_MIN) { |
| 5088 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5089 | if (rfn->min) { |
| 5090 | node->flags |= LYS_MAND_TRUE; |
| 5091 | lys_compile_mandatory_parents(node->parent, 1); |
| 5092 | } else { |
| 5093 | node->flags &= ~LYS_MAND_TRUE; |
| 5094 | lys_compile_mandatory_parents(node->parent, 0); |
| 5095 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5096 | } |
| 5097 | break; |
| 5098 | case LYS_LIST: |
| 5099 | if (rfn->flags & LYS_SET_MAX) { |
| 5100 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5101 | } |
| 5102 | if (rfn->flags & LYS_SET_MIN) { |
| 5103 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5104 | if (rfn->min) { |
| 5105 | node->flags |= LYS_MAND_TRUE; |
| 5106 | lys_compile_mandatory_parents(node->parent, 1); |
| 5107 | } else { |
| 5108 | node->flags &= ~LYS_MAND_TRUE; |
| 5109 | lys_compile_mandatory_parents(node->parent, 0); |
| 5110 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5111 | } |
| 5112 | break; |
| 5113 | default: |
| 5114 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5115 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5116 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5117 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5118 | } |
| 5119 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5120 | |
| 5121 | /* if-feature */ |
| 5122 | if (rfn->iffeatures) { |
| 5123 | /* any node in compiled tree can get additional if-feature, so do not check nodetype */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5124 | COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5125 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5126 | |
| 5127 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5128 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5129 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5130 | /* do some additional checks of the changed nodes when all the refines are applied */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5131 | for (uint32_t i = 0; i < refined.count; ++i) { |
| 5132 | node = (struct lysc_node*)refined.objs[i]; |
| 5133 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5134 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5135 | |
| 5136 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5137 | if ((node->flags & LYS_MAND_TRUE) && |
| 5138 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5139 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5140 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5141 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5142 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5143 | } |
| 5144 | |
| 5145 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5146 | if (node->nodetype == LYS_LIST) { |
| 5147 | min = ((struct lysc_node_list*)node)->min; |
| 5148 | max = ((struct lysc_node_list*)node)->max; |
| 5149 | } else { |
| 5150 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5151 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5152 | } |
| 5153 | if (min > max) { |
| 5154 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5155 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5156 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5157 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5158 | } |
| 5159 | } |
| 5160 | } |
| 5161 | |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5162 | if (first_p) { |
| 5163 | *first_p = context_node_fake.child; |
| 5164 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5165 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5166 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5167 | |
| 5168 | cleanup: |
| 5169 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5170 | if (context_node_fake.child) { |
| 5171 | context_node_fake.child->prev = child; |
| 5172 | } |
| 5173 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5174 | child->parent = parent; |
| 5175 | } |
| 5176 | |
| 5177 | if (uses_p->augments || uses_p->refines) { |
| 5178 | /* return back actions and notifications in case they were separated for augment/refine processing */ |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5179 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5180 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5181 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5182 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5183 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5184 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5185 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5186 | } |
| 5187 | } |
| 5188 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5189 | /* reload previous context's mod_def */ |
| 5190 | ctx->mod_def = mod_old; |
| 5191 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5192 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5193 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5194 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5195 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5196 | |
| 5197 | return ret; |
| 5198 | } |
| 5199 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5200 | static int |
| 5201 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5202 | { |
| 5203 | struct lysp_node *iter; |
| 5204 | int len = 0; |
| 5205 | |
| 5206 | *path = NULL; |
| 5207 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5208 | char *s = *path; |
| 5209 | char *id; |
| 5210 | |
| 5211 | switch (iter->nodetype) { |
| 5212 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5213 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5214 | break; |
| 5215 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5216 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5217 | break; |
| 5218 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5219 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5220 | break; |
| 5221 | default: |
| 5222 | id = strdup(iter->name); |
| 5223 | break; |
| 5224 | } |
| 5225 | |
| 5226 | if (!iter->parent) { |
| 5227 | /* print prefix */ |
| 5228 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5229 | } else { |
| 5230 | /* prefix is the same as in parent */ |
| 5231 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5232 | } |
| 5233 | free(s); |
| 5234 | free(id); |
| 5235 | } |
| 5236 | |
| 5237 | if (len < 0) { |
| 5238 | free(*path); |
| 5239 | *path = NULL; |
| 5240 | } else if (len == 0) { |
| 5241 | *path = strdup("/"); |
| 5242 | len = 1; |
| 5243 | } |
| 5244 | return len; |
| 5245 | } |
| 5246 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5247 | /** |
| 5248 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5249 | * |
| 5250 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5251 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5252 | */ |
| 5253 | static LY_ERR |
| 5254 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5255 | { |
| 5256 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5257 | char *path; |
| 5258 | int len; |
| 5259 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5260 | struct lysp_node_uses fake_uses = { |
| 5261 | .parent = node_p, |
| 5262 | .nodetype = LYS_USES, |
| 5263 | .flags = 0, .next = NULL, |
| 5264 | .name = grp->name, |
| 5265 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5266 | .refines = NULL, .augments = NULL |
| 5267 | }; |
| 5268 | struct lysc_node_container fake_container = { |
| 5269 | .nodetype = LYS_CONTAINER, |
| 5270 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5271 | .module = ctx->mod, |
| 5272 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5273 | .prev = (struct lysc_node*)&fake_container, |
| 5274 | .name = "fake", |
| 5275 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5276 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5277 | }; |
| 5278 | |
| 5279 | if (grp->parent) { |
| 5280 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5281 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5282 | |
| 5283 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5284 | if (len < 0) { |
| 5285 | LOGMEM(ctx->ctx); |
| 5286 | return LY_EMEM; |
| 5287 | } |
| 5288 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5289 | ctx->path_len = (uint16_t)len; |
| 5290 | free(path); |
| 5291 | |
| 5292 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5293 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5294 | 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] | 5295 | lysc_update_path(ctx, NULL, NULL); |
| 5296 | lysc_update_path(ctx, NULL, NULL); |
| 5297 | |
| 5298 | ctx->path_len = 1; |
| 5299 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5300 | |
| 5301 | /* cleanup */ |
| 5302 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5303 | |
| 5304 | return ret; |
| 5305 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5306 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5307 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5308 | * @brief Compile parsed schema node information. |
| 5309 | * @param[in] ctx Compile context |
| 5310 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5311 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5312 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5313 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5314 | * @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). |
| 5315 | * 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] | 5316 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5317 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5318 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5319 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5320 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5321 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5322 | struct lysc_node *node = NULL; |
| 5323 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5324 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5325 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5326 | 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] | 5327 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5328 | if (node_p->nodetype != LYS_USES) { |
| 5329 | lysc_update_path(ctx, parent, node_p->name); |
| 5330 | } else { |
| 5331 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5332 | lysc_update_path(ctx, NULL, node_p->name); |
| 5333 | } |
| 5334 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5335 | switch (node_p->nodetype) { |
| 5336 | case LYS_CONTAINER: |
| 5337 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5338 | node_compile_spec = lys_compile_node_container; |
| 5339 | break; |
| 5340 | case LYS_LEAF: |
| 5341 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5342 | node_compile_spec = lys_compile_node_leaf; |
| 5343 | break; |
| 5344 | case LYS_LIST: |
| 5345 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5346 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5347 | break; |
| 5348 | case LYS_LEAFLIST: |
| 5349 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5350 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5351 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5352 | case LYS_CHOICE: |
| 5353 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5354 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5355 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5356 | case LYS_ANYXML: |
| 5357 | case LYS_ANYDATA: |
| 5358 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5359 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5360 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5361 | case LYS_USES: |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5362 | if (parent && parent->nodetype == LYS_CHOICE) { |
| 5363 | assert(node_p->parent->nodetype == LYS_CASE); |
| 5364 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5365 | cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, NULL); |
| 5366 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
| 5367 | } |
| 5368 | |
| 5369 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, cs ? (struct lysc_node*)cs : parent, &node); |
| 5370 | if (cs) { |
| 5371 | cs->child = node; |
| 5372 | lysc_update_path(ctx, NULL, NULL); |
| 5373 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5374 | lysc_update_path(ctx, NULL, NULL); |
| 5375 | lysc_update_path(ctx, NULL, NULL); |
| 5376 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5377 | default: |
| 5378 | LOGINT(ctx->ctx); |
| 5379 | return LY_EINT; |
| 5380 | } |
| 5381 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5382 | node->nodetype = node_p->nodetype; |
| 5383 | node->module = ctx->mod; |
| 5384 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5385 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5386 | |
| 5387 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5388 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5389 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5390 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5391 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5392 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5393 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5394 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5395 | node->flags |= LYS_CONFIG_R; |
| 5396 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5397 | /* config not explicitely set, inherit it from parent */ |
| 5398 | if (parent) { |
| 5399 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5400 | } else { |
| 5401 | /* default is config true */ |
| 5402 | node->flags |= LYS_CONFIG_W; |
| 5403 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5404 | } else { |
| 5405 | /* config set explicitely */ |
| 5406 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5407 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5408 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5409 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5410 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5411 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5412 | goto error; |
| 5413 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5414 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5415 | /* *list ordering */ |
| 5416 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5417 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5418 | LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).", |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5419 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5420 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5421 | node->flags &= ~LYS_ORDBY_MASK; |
| 5422 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5423 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5424 | /* default ordering is system */ |
| 5425 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5426 | } |
| 5427 | } |
| 5428 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5429 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5430 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5431 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5432 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5433 | * the parent is choice (parent here) or some case (so we have to get its flags to check) */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5434 | 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] | 5435 | } |
| 5436 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5437 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5438 | node->sp = node_p; |
| 5439 | } |
| 5440 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5441 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5442 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5443 | if (node_p->when) { |
| 5444 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5445 | LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5446 | |
| 5447 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5448 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5449 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5450 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5451 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5452 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5453 | |
| 5454 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5455 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5456 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5457 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5458 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5459 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5460 | if (node->flags & LYS_MAND_TRUE) { |
| 5461 | lys_compile_mandatory_parents(parent, 1); |
| 5462 | } |
| 5463 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5464 | lysc_update_path(ctx, NULL, NULL); |
| 5465 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5466 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5467 | if (parent) { |
| 5468 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5469 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5470 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5471 | } else { |
| 5472 | lysc_update_path(ctx, parent, node->name); |
| 5473 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5474 | cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5475 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5476 | if (uses_status) { |
| 5477 | |
| 5478 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5479 | /* the postponed status check of the node and its real parent - in case of implicit case, |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5480 | * it directly gets the same status flags as the choice; |
| 5481 | * uses_status cannot be applied here since uses cannot be child statement of choice */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5482 | LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5483 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5484 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5485 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5486 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5487 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5488 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5489 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5490 | |
| 5491 | if (parent->nodetype == LYS_CHOICE) { |
| 5492 | lysc_update_path(ctx, NULL, NULL); |
| 5493 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5494 | } else { |
| 5495 | /* top-level element */ |
| 5496 | if (!ctx->mod->compiled->data) { |
| 5497 | ctx->mod->compiled->data = node; |
| 5498 | } else { |
| 5499 | /* insert at the end of the module's top-level nodes list */ |
| 5500 | ctx->mod->compiled->data->prev->next = node; |
| 5501 | node->prev = ctx->mod->compiled->data->prev; |
| 5502 | ctx->mod->compiled->data->prev = node; |
| 5503 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5504 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5505 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5506 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5507 | return LY_EVALID; |
| 5508 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5509 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5510 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5511 | |
| 5512 | return LY_SUCCESS; |
| 5513 | |
| 5514 | error: |
| 5515 | lysc_node_free(ctx->ctx, node); |
| 5516 | return ret; |
| 5517 | } |
| 5518 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5519 | static void |
| 5520 | lysc_disconnect(struct lysc_node *node) |
| 5521 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5522 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5523 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame^] | 5524 | struct lysc_module *modc = node->module->compiled; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5525 | int remove_cs = 0; |
| 5526 | |
| 5527 | parent = node->parent; |
| 5528 | |
| 5529 | /* parent's first child */ |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame^] | 5530 | if (parent && parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5531 | cs = (struct lysc_node_case*)node; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame^] | 5532 | } else if (parent && parent->nodetype == LYS_CASE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5533 | /* disconnecting some node in a case */ |
| 5534 | cs = (struct lysc_node_case*)parent; |
| 5535 | parent = cs->parent; |
| 5536 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5537 | if (child == node) { |
| 5538 | if (cs->child == child) { |
| 5539 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5540 | /* case with a single child -> remove also the case */ |
| 5541 | child->parent = NULL; |
| 5542 | remove_cs = 1; |
| 5543 | } else { |
| 5544 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5545 | } |
| 5546 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5547 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5548 | } |
| 5549 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5550 | if (!remove_cs) { |
| 5551 | cs = NULL; |
| 5552 | } |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame^] | 5553 | } else if (parent && lysc_node_children(parent, node->flags) == node) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5554 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame^] | 5555 | } else if (modc->data == node) { |
| 5556 | modc->data = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5557 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5558 | if (cs) { |
| 5559 | if (remove_cs) { |
| 5560 | /* cs has only one child which is being also removed */ |
| 5561 | lysc_disconnect((struct lysc_node*)cs); |
| 5562 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5563 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5564 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5565 | /* default case removed */ |
| 5566 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5567 | } |
| 5568 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5569 | /* first case removed */ |
| 5570 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5571 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5572 | if (cs->child) { |
| 5573 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5574 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5575 | prev = cs->child->prev; |
| 5576 | } /* else all the children are under a single case */ |
| 5577 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5578 | if (child->parent != (struct lysc_node*)cs) { |
| 5579 | break; |
| 5580 | } |
| 5581 | lysc_node_free(node->module->ctx, child); |
| 5582 | } |
| 5583 | if (prev) { |
| 5584 | if (prev->next) { |
| 5585 | prev->next = child; |
| 5586 | } |
| 5587 | if (child) { |
| 5588 | child->prev = prev; |
| 5589 | } else { |
| 5590 | /* link from the first child under the cases */ |
| 5591 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5592 | } |
| 5593 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5594 | } |
| 5595 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5596 | } |
| 5597 | |
| 5598 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5599 | if (node->prev->next) { |
| 5600 | node->prev->next = node->next; |
| 5601 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5602 | if (node->next) { |
| 5603 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5604 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame^] | 5605 | if (parent) { |
| 5606 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
| 5607 | } else { |
| 5608 | child = modc->data; |
| 5609 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5610 | if (child) { |
| 5611 | child->prev = node->prev; |
| 5612 | } |
| 5613 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5614 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5615 | } |
| 5616 | } |
| 5617 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5618 | struct lysc_deviation { |
| 5619 | const char *nodeid; |
| 5620 | struct lysc_node *target; /* target node of the deviation */ |
| 5621 | struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */ |
| 5622 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
| 5623 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5624 | }; |
| 5625 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5626 | /* MACROS for deviates checking */ |
| 5627 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5628 | if (!(target->nodetype & (NODETYPES))) { \ |
| 5629 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\ |
| 5630 | goto cleanup; \ |
| 5631 | } |
| 5632 | |
| 5633 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5634 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5635 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5636 | lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
| 5637 | goto cleanup; \ |
| 5638 | } |
| 5639 | |
| 5640 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5641 | if (!((TYPE)target)->MEMBER || COND) { \ |
| 5642 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
| 5643 | goto cleanup; \ |
| 5644 | } |
| 5645 | |
| 5646 | /** |
| 5647 | * @brief Apply deviate add. |
| 5648 | * |
| 5649 | * @param[in] ctx Compile context. |
| 5650 | * @param[in] target Deviation target. |
| 5651 | * @param[in] dev_flags Internal deviation flags. |
| 5652 | * @param[in] d Deviate add to apply. |
| 5653 | * @param[out] dflt Added default value. |
| 5654 | * @return LY_ERR value. |
| 5655 | */ |
| 5656 | static LY_ERR |
| 5657 | lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysc_node *target, int dev_flags, struct lysp_deviate_add *d, |
| 5658 | const char **dflt) |
| 5659 | { |
| 5660 | LY_ERR ret = LY_EVALID, rc; |
| 5661 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 5662 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 5663 | struct ly_err_item *err = NULL; |
| 5664 | LY_ARRAY_COUNT_TYPE x, y; |
| 5665 | |
| 5666 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5667 | if (((TYPE)target)->MEMBER COND) { \ |
| 5668 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5669 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5670 | PROPERTY, ((TYPE)target)->MEMBER); \ |
| 5671 | goto cleanup; \ |
| 5672 | } |
| 5673 | |
| 5674 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
| 5675 | if (((TYPE)target)->MEMBER && (COND)) { \ |
| 5676 | int dynamic_ = 0; const char *val_; \ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5677 | val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \ |
| 5678 | ((TYPE)target)->VALUEMODMEMBER, &dynamic_); \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5679 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5680 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5681 | if (dynamic_) {free((void*)val_);} \ |
| 5682 | goto cleanup; \ |
| 5683 | } |
| 5684 | |
| 5685 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
| 5686 | if (((TYPE)target)->MEMBER && (COND)) { \ |
| 5687 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5688 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5689 | PROPERTY, ((TYPE)target)->VALUEMEMBER); \ |
| 5690 | goto cleanup; \ |
| 5691 | } |
| 5692 | |
| 5693 | /* [units-stmt] */ |
| 5694 | if (d->units) { |
| 5695 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5696 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units); |
| 5697 | |
| 5698 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
| 5699 | DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units); |
| 5700 | } |
| 5701 | |
| 5702 | /* *must-stmt */ |
| 5703 | if (d->musts) { |
| 5704 | switch (target->nodetype) { |
| 5705 | case LYS_CONTAINER: |
| 5706 | case LYS_LIST: |
| 5707 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts, |
| 5708 | x, lys_compile_must, ret, cleanup); |
| 5709 | break; |
| 5710 | case LYS_LEAF: |
| 5711 | case LYS_LEAFLIST: |
| 5712 | case LYS_ANYDATA: |
| 5713 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts, |
| 5714 | x, lys_compile_must, ret, cleanup); |
| 5715 | break; |
| 5716 | case LYS_NOTIF: |
| 5717 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts, |
| 5718 | x, lys_compile_must, ret, cleanup); |
| 5719 | break; |
| 5720 | case LYS_RPC: |
| 5721 | case LYS_ACTION: |
| 5722 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
| 5723 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts, |
| 5724 | x, lys_compile_must, ret, cleanup); |
| 5725 | break; |
| 5726 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
| 5727 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts, |
| 5728 | x, lys_compile_must, ret, cleanup); |
| 5729 | break; |
| 5730 | } |
| 5731 | /* fall through */ |
| 5732 | default: |
| 5733 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5734 | lys_nodetype2str(target->nodetype), "add", "must"); |
| 5735 | goto cleanup; |
| 5736 | } |
| 5737 | ly_set_add(&ctx->xpath, target, 0); |
| 5738 | } |
| 5739 | |
| 5740 | /* *unique-stmt */ |
| 5741 | if (d->uniques) { |
| 5742 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5743 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup); |
| 5744 | } |
| 5745 | |
| 5746 | /* *default-stmt */ |
| 5747 | if (d->dflts) { |
| 5748 | switch (target->nodetype) { |
| 5749 | case LYS_LEAF: |
| 5750 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
| 5751 | DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt, dflt_mod); |
| 5752 | if (leaf->dflt) { |
| 5753 | /* first, remove the default value taken from the type */ |
| 5754 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 5755 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5756 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5757 | } else { |
| 5758 | /* prepare new default value storage */ |
| 5759 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 5760 | } |
| 5761 | *dflt = d->dflts[0]; |
| 5762 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5763 | |
| 5764 | /* mark the new default values as leaf's own */ |
| 5765 | target->flags |= LYS_SET_DFLT; |
| 5766 | break; |
| 5767 | case LYS_LEAFLIST: |
| 5768 | if (llist->dflts && !(target->flags & LYS_SET_DFLT)) { |
| 5769 | /* first, remove the default value taken from the type */ |
| 5770 | LY_ARRAY_FOR(llist->dflts, x) { |
| 5771 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
| 5772 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5773 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5774 | free(llist->dflts[x]); |
| 5775 | } |
| 5776 | LY_ARRAY_FREE(llist->dflts); |
| 5777 | llist->dflts = NULL; |
| 5778 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5779 | llist->dflts_mods = NULL; |
| 5780 | } |
| 5781 | /* add new default value(s) */ |
| 5782 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d->dflts), ret, cleanup); |
| 5783 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d->dflts), ret, cleanup); |
| 5784 | for (x = y = LY_ARRAY_COUNT(llist->dflts); |
| 5785 | x < LY_ARRAY_COUNT(d->dflts) + y; ++x) { |
| 5786 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5787 | llist->dflts_mods[x] = ctx->mod_def; |
| 5788 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5789 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5790 | llist->dflts[x]->realtype = llist->type; |
| 5791 | rc = llist->type->plugin->store(ctx->ctx, llist->type, d->dflts[x - y], strlen(d->dflts[x - y]), |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5792 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
| 5793 | LY_PREF_SCHEMA, llist->dflts_mods[x], target, NULL, llist->dflts[x], NULL, &err); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5794 | llist->dflts[x]->realtype->refcount++; |
| 5795 | if (err) { |
| 5796 | ly_err_print(err); |
| 5797 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5798 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5799 | d->dflts[x - y], err->msg); |
| 5800 | ly_err_free(err); |
| 5801 | } |
| 5802 | if (rc == LY_EINCOMPLETE) { |
| 5803 | /* postpone default compilation when the tree is complete */ |
| 5804 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5805 | |
| 5806 | /* but in general result is so far ok */ |
| 5807 | rc = LY_SUCCESS; |
| 5808 | } |
| 5809 | LY_CHECK_GOTO(rc, cleanup); |
| 5810 | } |
| 5811 | /* mark the new default values as leaf-list's own */ |
| 5812 | target->flags |= LYS_SET_DFLT; |
| 5813 | break; |
| 5814 | case LYS_CHOICE: |
| 5815 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
| 5816 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name); |
| 5817 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5818 | * to allow making the default case even the augmented case from the deviating module */ |
| 5819 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) { |
| 5820 | goto cleanup; |
| 5821 | } |
| 5822 | break; |
| 5823 | default: |
| 5824 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5825 | lys_nodetype2str(target->nodetype), "add", "default"); |
| 5826 | goto cleanup; |
| 5827 | } |
| 5828 | } |
| 5829 | |
| 5830 | /* [config-stmt] */ |
| 5831 | if (d->flags & LYS_CONFIG_MASK) { |
| 5832 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 5833 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5834 | lys_nodetype2str(target->nodetype), "add", "config"); |
| 5835 | goto cleanup; |
| 5836 | } |
| 5837 | if (dev_flags) { |
| 5838 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 5839 | dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
| 5840 | } |
| 5841 | if (target->flags & LYS_SET_CONFIG) { |
| 5842 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5843 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5844 | target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5845 | goto cleanup; |
| 5846 | } |
| 5847 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 5848 | } |
| 5849 | |
| 5850 | /* [mandatory-stmt] */ |
| 5851 | if (d->flags & LYS_MAND_MASK) { |
| 5852 | if (target->flags & LYS_MAND_MASK) { |
| 5853 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5854 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5855 | target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5856 | goto cleanup; |
| 5857 | } |
| 5858 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 5859 | } |
| 5860 | |
| 5861 | /* [min-elements-stmt] */ |
| 5862 | if (d->flags & LYS_SET_MIN) { |
| 5863 | if (target->nodetype == LYS_LEAFLIST) { |
| 5864 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 5865 | /* change value */ |
| 5866 | ((struct lysc_node_leaflist*)target)->min = d->min; |
| 5867 | } else if (target->nodetype == LYS_LIST) { |
| 5868 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 5869 | /* change value */ |
| 5870 | ((struct lysc_node_list*)target)->min = d->min; |
| 5871 | } else { |
| 5872 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5873 | lys_nodetype2str(target->nodetype), "add", "min-elements"); |
| 5874 | goto cleanup; |
| 5875 | } |
| 5876 | if (d->min) { |
| 5877 | target->flags |= LYS_MAND_TRUE; |
| 5878 | } |
| 5879 | } |
| 5880 | |
| 5881 | /* [max-elements-stmt] */ |
| 5882 | if (d->flags & LYS_SET_MAX) { |
| 5883 | if (target->nodetype == LYS_LEAFLIST) { |
| 5884 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 5885 | /* change value */ |
| 5886 | ((struct lysc_node_leaflist*)target)->max = d->max ? d->max : (uint32_t)-1; |
| 5887 | } else if (target->nodetype == LYS_LIST) { |
| 5888 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 5889 | /* change value */ |
| 5890 | ((struct lysc_node_list*)target)->max = d->max ? d->max : (uint32_t)-1; |
| 5891 | } else { |
| 5892 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5893 | lys_nodetype2str(target->nodetype), "add", "max-elements"); |
| 5894 | goto cleanup; |
| 5895 | } |
| 5896 | } |
| 5897 | |
| 5898 | ret = LY_SUCCESS; |
| 5899 | |
| 5900 | cleanup: |
| 5901 | return ret; |
| 5902 | } |
| 5903 | |
| 5904 | /** |
| 5905 | * @brief Apply deviate delete. |
| 5906 | * |
| 5907 | * @param[in] ctx Compile context. |
| 5908 | * @param[in] target Deviation target. |
| 5909 | * @param[in] dev_flags Internal deviation flags. |
| 5910 | * @param[in] d Deviate delete to apply. |
| 5911 | * @return LY_ERR value. |
| 5912 | */ |
| 5913 | static LY_ERR |
| 5914 | lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysc_node *target, int dev_flags, struct lysp_deviate_del *d) |
| 5915 | { |
| 5916 | LY_ERR ret = LY_EVALID; |
| 5917 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 5918 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 5919 | struct lysc_node_list *list = (struct lysc_node_list *)target; |
| 5920 | LY_ARRAY_COUNT_TYPE x, y, z; |
| 5921 | int i; |
| 5922 | size_t prefix_len, name_len; |
| 5923 | const char *prefix, *name, *nodeid, *dflt; |
| 5924 | struct lys_module *mod; |
| 5925 | |
| 5926 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5927 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \ |
| 5928 | LY_ARRAY_FOR(d->ARRAY_DEV, x) { \ |
| 5929 | LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \ |
| 5930 | if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \ |
| 5931 | } \ |
| 5932 | if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5933 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5934 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5935 | PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \ |
| 5936 | goto cleanup; \ |
| 5937 | } \ |
| 5938 | LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \ |
| 5939 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \ |
| 5940 | memmove(&((TYPE)target)->ARRAY_TRG[y], \ |
| 5941 | &((TYPE)target)->ARRAY_TRG[y + 1], \ |
| 5942 | (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \ |
| 5943 | } \ |
| 5944 | if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5945 | LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \ |
| 5946 | ((TYPE)target)->ARRAY_TRG = NULL; \ |
| 5947 | } |
| 5948 | |
| 5949 | /* [units-stmt] */ |
| 5950 | if (d->units) { |
| 5951 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
| 5952 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d->units); |
| 5953 | if (strcmp(((struct lysc_node_leaf*)target)->units, d->units)) { |
| 5954 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5955 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5956 | d->units, ((struct lysc_node_leaf*)target)->units); |
| 5957 | goto cleanup; |
| 5958 | } |
| 5959 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)target)->units); |
| 5960 | ((struct lysc_node_leaf*)target)->units = NULL; |
| 5961 | } |
| 5962 | |
| 5963 | /* *must-stmt */ |
| 5964 | if (d->musts) { |
| 5965 | switch (target->nodetype) { |
| 5966 | case LYS_CONTAINER: |
| 5967 | case LYS_LIST: |
| 5968 | DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5969 | break; |
| 5970 | case LYS_LEAF: |
| 5971 | case LYS_LEAFLIST: |
| 5972 | case LYS_ANYDATA: |
| 5973 | DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5974 | break; |
| 5975 | case LYS_NOTIF: |
| 5976 | DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5977 | break; |
| 5978 | case LYS_RPC: |
| 5979 | case LYS_ACTION: |
| 5980 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
| 5981 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5982 | break; |
| 5983 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
| 5984 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5985 | break; |
| 5986 | } |
| 5987 | /* fall through */ |
| 5988 | default: |
| 5989 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5990 | lys_nodetype2str(target->nodetype), "delete", "must"); |
| 5991 | goto cleanup; |
| 5992 | } |
| 5993 | } |
| 5994 | |
| 5995 | /* *unique-stmt */ |
| 5996 | if (d->uniques) { |
| 5997 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5998 | LY_ARRAY_FOR(d->uniques, x) { |
| 5999 | LY_ARRAY_FOR(list->uniques, z) { |
| 6000 | for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6001 | nodeid = strpbrk(name, " \t\n"); |
| 6002 | if (nodeid) { |
| 6003 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
| 6004 | break; |
| 6005 | } |
| 6006 | while (isspace(*nodeid)) { |
| 6007 | ++nodeid; |
| 6008 | } |
| 6009 | } else { |
| 6010 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6011 | break; |
| 6012 | } |
| 6013 | } |
| 6014 | } |
| 6015 | if (!name) { |
| 6016 | /* complete match - remove the unique */ |
| 6017 | LY_ARRAY_DECREMENT(list->uniques); |
| 6018 | LY_ARRAY_FREE(list->uniques[z]); |
| 6019 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques)); |
| 6020 | --z; |
| 6021 | break; |
| 6022 | } |
| 6023 | } |
| 6024 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
| 6025 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6026 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6027 | d->uniques[x]); |
| 6028 | goto cleanup; |
| 6029 | } |
| 6030 | } |
| 6031 | if (!LY_ARRAY_COUNT(list->uniques)) { |
| 6032 | LY_ARRAY_FREE(list->uniques); |
| 6033 | list->uniques = NULL; |
| 6034 | } |
| 6035 | } |
| 6036 | |
| 6037 | /* *default-stmt */ |
| 6038 | if (d->dflts) { |
| 6039 | switch (target->nodetype) { |
| 6040 | case LYS_LEAF: |
| 6041 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
| 6042 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(target->flags & LYS_SET_DFLT), |
| 6043 | dflt, "deleting", "default", d->dflts[0]); |
| 6044 | |
| 6045 | /* check that the values matches */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6046 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, leaf->dflt_mod, &i); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6047 | if (strcmp(dflt, d->dflts[0])) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6048 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6049 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6050 | d->dflts[0], dflt); |
Michal Vasko | 9acaf49 | 2020-08-13 09:05:58 +0200 | [diff] [blame] | 6051 | if (i) { |
| 6052 | free((char*)dflt); |
| 6053 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6054 | goto cleanup; |
| 6055 | } |
| 6056 | if (i) { |
| 6057 | free((char*)dflt); |
| 6058 | } |
| 6059 | |
| 6060 | /* update the list of incomplete default values if needed */ |
| 6061 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6062 | |
| 6063 | /* remove the default specification */ |
| 6064 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6065 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6066 | free(leaf->dflt); |
| 6067 | leaf->dflt = NULL; |
| 6068 | leaf->dflt_mod = NULL; |
| 6069 | target->flags &= ~LYS_SET_DFLT; |
| 6070 | break; |
| 6071 | case LYS_LEAFLIST: |
| 6072 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d->dflts[0]); |
| 6073 | LY_ARRAY_FOR(d->dflts, x) { |
| 6074 | LY_ARRAY_FOR(llist->dflts, y) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6075 | dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, llist->dflts_mods[y], &i); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6076 | if (!strcmp(dflt, d->dflts[x])) { |
| 6077 | if (i) { |
| 6078 | free((char*)dflt); |
| 6079 | } |
| 6080 | break; |
| 6081 | } |
| 6082 | if (i) { |
| 6083 | free((char*)dflt); |
| 6084 | } |
| 6085 | } |
| 6086 | if (y == LY_ARRAY_COUNT(llist->dflts)) { |
| 6087 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6088 | "which does not match any of the target's property values.", d->dflts[x]); |
| 6089 | goto cleanup; |
| 6090 | } |
| 6091 | |
| 6092 | /* update the list of incomplete default values if needed */ |
| 6093 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6094 | |
| 6095 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
| 6096 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6097 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6098 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6099 | free(llist->dflts[y]); |
| 6100 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 6101 | memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_COUNT(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods)); |
| 6102 | } |
| 6103 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
| 6104 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6105 | llist->dflts_mods = NULL; |
| 6106 | LY_ARRAY_FREE(llist->dflts); |
| 6107 | llist->dflts = NULL; |
| 6108 | llist->flags &= ~LYS_SET_DFLT; |
| 6109 | } |
| 6110 | break; |
| 6111 | case LYS_CHOICE: |
| 6112 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
| 6113 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d->dflts[0]); |
| 6114 | nodeid = d->dflts[0]; |
| 6115 | LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 6116 | if (prefix) { |
| 6117 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6118 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6119 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
| 6120 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6121 | "The prefix does not match any imported module of the deviation module.", d->dflts[0]); |
| 6122 | goto cleanup; |
| 6123 | } |
| 6124 | if (mod != ((struct lysc_node_choice*)target)->dflt->module) { |
| 6125 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
| 6126 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6127 | "The prefix does not match the default case's module.", d->dflts[0]); |
| 6128 | goto cleanup; |
| 6129 | } |
| 6130 | } |
| 6131 | /* else { |
| 6132 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6133 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6134 | * the module of the default case, just matching its name */ |
| 6135 | if (strcmp(name, ((struct lysc_node_choice*)target)->dflt->name)) { |
| 6136 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6137 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6138 | d->dflts[0], ((struct lysc_node_choice*)target)->dflt->name); |
| 6139 | goto cleanup; |
| 6140 | } |
| 6141 | ((struct lysc_node_choice*)target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6142 | ((struct lysc_node_choice*)target)->dflt = NULL; |
| 6143 | break; |
| 6144 | default: |
| 6145 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6146 | lys_nodetype2str(target->nodetype), "delete", "default"); |
| 6147 | goto cleanup; |
| 6148 | } |
| 6149 | } |
| 6150 | |
| 6151 | ret = LY_SUCCESS; |
| 6152 | |
| 6153 | cleanup: |
| 6154 | return ret; |
| 6155 | } |
| 6156 | |
| 6157 | /** |
| 6158 | * @brief Apply deviate replace. |
| 6159 | * |
| 6160 | * @param[in] ctx Compile context. |
| 6161 | * @param[in] target Deviation target. |
| 6162 | * @param[in] d Deviate replace to apply. |
| 6163 | * @param[out] dflt Added default value. |
| 6164 | * @param[out] changed_type Whether the target type was changed. |
| 6165 | * @return LY_ERR value. |
| 6166 | */ |
| 6167 | static LY_ERR |
| 6168 | lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d, const char **dflt, |
| 6169 | int *changed_type) |
| 6170 | { |
| 6171 | LY_ERR ret = LY_EVALID; |
| 6172 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6173 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6174 | LY_ARRAY_COUNT_TYPE x; |
| 6175 | |
| 6176 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 6177 | if (!(((TYPE)target)->MEMBER COND)) { \ |
| 6178 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 6179 | "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \ |
| 6180 | goto cleanup; \ |
| 6181 | } |
| 6182 | |
| 6183 | /* [type-stmt] */ |
| 6184 | if (d->type) { |
| 6185 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6186 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6187 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type); |
| 6188 | |
| 6189 | if (leaf->dflt && !(target->flags & LYS_SET_DFLT)) { |
| 6190 | /* the target has default from the previous type - remove it */ |
| 6191 | if (target->nodetype == LYS_LEAF) { |
| 6192 | /* update the list of incomplete default values if needed */ |
| 6193 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6194 | |
| 6195 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6196 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6197 | free(leaf->dflt); |
| 6198 | leaf->dflt = NULL; |
| 6199 | leaf->dflt_mod = NULL; |
| 6200 | } else { /* LYS_LEAFLIST */ |
| 6201 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6202 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
| 6203 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6204 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6205 | free(llist->dflts[x]); |
| 6206 | } |
| 6207 | LY_ARRAY_FREE(llist->dflts); |
| 6208 | llist->dflts = NULL; |
| 6209 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6210 | llist->dflts_mods = NULL; |
| 6211 | } |
| 6212 | } |
| 6213 | if (!leaf->dflt) { |
| 6214 | /* there is no default value, do not set changed_type after type compilation |
| 6215 | * which is used to recompile the default value */ |
| 6216 | *changed_type = -1; |
| 6217 | } |
| 6218 | LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d->type, leaf), cleanup); |
| 6219 | (*changed_type)++; |
| 6220 | } |
| 6221 | |
| 6222 | /* [units-stmt] */ |
| 6223 | if (d->units) { |
| 6224 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6225 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS), |
| 6226 | units, "replacing", "units", d->units); |
| 6227 | |
| 6228 | lydict_remove(ctx->ctx, leaf->units); |
| 6229 | DUP_STRING(ctx->ctx, d->units, leaf->units); |
| 6230 | } |
| 6231 | |
| 6232 | /* [default-stmt] */ |
| 6233 | if (d->dflt) { |
| 6234 | switch (target->nodetype) { |
| 6235 | case LYS_LEAF: |
| 6236 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), |
| 6237 | dflt, "replacing", "default", d->dflt); |
| 6238 | /* first, remove the default value taken from the type */ |
| 6239 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6240 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6241 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6242 | *dflt = d->dflt; |
| 6243 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 6244 | break; |
| 6245 | case LYS_CHOICE: |
| 6246 | DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt); |
| 6247 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) { |
| 6248 | goto cleanup; |
| 6249 | } |
| 6250 | break; |
| 6251 | default: |
| 6252 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6253 | lys_nodetype2str(target->nodetype), "replace", "default"); |
| 6254 | goto cleanup; |
| 6255 | } |
| 6256 | } |
| 6257 | |
| 6258 | /* [config-stmt] */ |
| 6259 | if (d->flags & LYS_CONFIG_MASK) { |
| 6260 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 6261 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6262 | lys_nodetype2str(target->nodetype), "replace", "config"); |
| 6263 | goto cleanup; |
| 6264 | } |
| 6265 | if (!(target->flags & LYS_SET_CONFIG)) { |
| 6266 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6267 | "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6268 | goto cleanup; |
| 6269 | } |
| 6270 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 6271 | } |
| 6272 | |
| 6273 | /* [mandatory-stmt] */ |
| 6274 | if (d->flags & LYS_MAND_MASK) { |
| 6275 | if (!(target->flags & LYS_MAND_MASK)) { |
| 6276 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6277 | "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6278 | goto cleanup; |
| 6279 | } |
| 6280 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 6281 | } |
| 6282 | |
| 6283 | /* [min-elements-stmt] */ |
| 6284 | if (d->flags & LYS_SET_MIN) { |
| 6285 | if (target->nodetype == LYS_LEAFLIST) { |
| 6286 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 6287 | /* change value */ |
| 6288 | ((struct lysc_node_leaflist *)target)->min = d->min; |
| 6289 | } else if (target->nodetype == LYS_LIST) { |
| 6290 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 6291 | /* change value */ |
| 6292 | ((struct lysc_node_list *)target)->min = d->min; |
| 6293 | } else { |
| 6294 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6295 | lys_nodetype2str(target->nodetype), "replace", "min-elements"); |
| 6296 | goto cleanup; |
| 6297 | } |
| 6298 | if (d->min) { |
| 6299 | target->flags |= LYS_MAND_TRUE; |
| 6300 | } |
| 6301 | } |
| 6302 | |
| 6303 | /* [max-elements-stmt] */ |
| 6304 | if (d->flags & LYS_SET_MAX) { |
| 6305 | if (target->nodetype == LYS_LEAFLIST) { |
| 6306 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 6307 | /* change value */ |
| 6308 | ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6309 | } else if (target->nodetype == LYS_LIST) { |
| 6310 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 6311 | /* change value */ |
| 6312 | ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6313 | } else { |
| 6314 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6315 | lys_nodetype2str(target->nodetype), "replace", "max-elements"); |
| 6316 | goto cleanup; |
| 6317 | } |
| 6318 | } |
| 6319 | |
| 6320 | ret = LY_SUCCESS; |
| 6321 | |
| 6322 | cleanup: |
| 6323 | return ret; |
| 6324 | } |
| 6325 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6326 | /** |
| 6327 | * @brief Apply all deviations of one target node. |
| 6328 | * |
| 6329 | * @param[in] ctx Compile context. |
| 6330 | * @param[in] dev Deviation structure to apply. |
| 6331 | * @return LY_ERR value. |
| 6332 | */ |
| 6333 | static LY_ERR |
| 6334 | lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6335 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6336 | LY_ERR ret = LY_EVALID, rc; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6337 | struct lysc_node *target = dev->target; |
| 6338 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6339 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6340 | struct ly_err_item *err = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6341 | struct lysc_action *rpcs; |
| 6342 | struct lysc_notif *notifs; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6343 | struct lysp_deviate *d; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6344 | LY_ARRAY_COUNT_TYPE v, x; |
| 6345 | int changed_type = 0; |
| 6346 | const char *dflt = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6347 | uint32_t min, max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6348 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6349 | lysc_update_path(ctx, NULL, dev->nodeid); |
| 6350 | |
| 6351 | /* not-supported */ |
| 6352 | if (dev->not_supported) { |
| 6353 | if (LY_ARRAY_COUNT(dev->deviates) > 1) { |
| 6354 | LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.", |
| 6355 | LY_ARRAY_COUNT(dev->deviates), dev->nodeid); |
| 6356 | } |
| 6357 | |
| 6358 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6359 | if (target->parent) { \ |
| 6360 | ARRAY = (TYPE*)GETFUNC(target->parent); \ |
| 6361 | } else { \ |
| 6362 | ARRAY = target->module->compiled->ARRAY; \ |
| 6363 | } \ |
| 6364 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6365 | if (&ARRAY[x] == (TYPE*)target) { break; } \ |
| 6366 | } \ |
| 6367 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
| 6368 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6369 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6370 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6371 | } |
| 6372 | |
| 6373 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 6374 | if (dev->flags & LYSC_OPT_RPC_INPUT) { |
| 6375 | /* remove RPC's/action's input */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6376 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input); |
| 6377 | memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input); |
| 6378 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free); |
| 6379 | ((struct lysc_action *)target)->input_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6380 | } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6381 | /* remove RPC's/action's output */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6382 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output); |
| 6383 | memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output); |
| 6384 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free); |
| 6385 | ((struct lysc_action *)target)->output_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6386 | } else { |
| 6387 | /* remove RPC/action */ |
| 6388 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
| 6389 | } |
| 6390 | } else if (target->nodetype == LYS_NOTIF) { |
| 6391 | /* remove Notification */ |
| 6392 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
| 6393 | } else { |
| 6394 | /* remove the target node */ |
| 6395 | lysc_disconnect(target); |
| 6396 | lysc_node_free(ctx->ctx, target); |
| 6397 | } |
| 6398 | |
| 6399 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6400 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
| 6401 | return LY_SUCCESS; |
| 6402 | } |
| 6403 | |
| 6404 | /* list of deviates (not-supported is not present in the list) */ |
| 6405 | LY_ARRAY_FOR(dev->deviates, v) { |
| 6406 | d = dev->deviates[v]; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6407 | switch (d->mod) { |
| 6408 | case LYS_DEV_ADD: |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6409 | LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d, &dflt), cleanup); |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6410 | break; |
| 6411 | case LYS_DEV_DELETE: |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6412 | LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup); |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6413 | break; |
| 6414 | case LYS_DEV_REPLACE: |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6415 | LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d, &dflt, &changed_type), cleanup); |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6416 | break; |
| 6417 | default: |
| 6418 | LOGINT(ctx->ctx); |
| 6419 | goto cleanup; |
| 6420 | } |
| 6421 | } |
| 6422 | |
| 6423 | /* final check when all deviations of a single target node are applied */ |
| 6424 | |
| 6425 | /* check min-max compatibility */ |
| 6426 | if (target->nodetype == LYS_LEAFLIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6427 | min = ((struct lysc_node_leaflist *)target)->min; |
| 6428 | max = ((struct lysc_node_leaflist *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6429 | } else if (target->nodetype == LYS_LIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6430 | min = ((struct lysc_node_list *)target)->min; |
| 6431 | max = ((struct lysc_node_list *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6432 | } else { |
| 6433 | min = max = 0; |
| 6434 | } |
| 6435 | if (min > max) { |
| 6436 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
| 6437 | "after deviation: min value %u is bigger than max value %u.", min, max); |
| 6438 | goto cleanup; |
| 6439 | } |
| 6440 | |
| 6441 | if (dflt) { |
| 6442 | /* parse added/changed default value after possible change of the type */ |
| 6443 | leaf->dflt_mod = ctx->mod_def; |
| 6444 | leaf->dflt->realtype = leaf->type; |
| 6445 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6446 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6447 | LY_PREF_SCHEMA, leaf->dflt_mod, target, NULL, leaf->dflt, NULL, &err); |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6448 | leaf->dflt->realtype->refcount++; |
| 6449 | if (err) { |
| 6450 | ly_err_print(err); |
| 6451 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6452 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6453 | ly_err_free(err); |
| 6454 | } |
| 6455 | if (rc == LY_EINCOMPLETE) { |
| 6456 | /* postpone default compilation when the tree is complete */ |
| 6457 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6458 | |
| 6459 | /* but in general result is so far ok */ |
| 6460 | rc = LY_SUCCESS; |
| 6461 | } |
| 6462 | LY_CHECK_GOTO(rc, cleanup); |
| 6463 | } else if (changed_type) { |
| 6464 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6465 | int dynamic; |
| 6466 | if (target->nodetype == LYS_LEAF) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6467 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, leaf->dflt_mod, &dynamic); |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6468 | |
| 6469 | /* update the list of incomplete default values if needed */ |
| 6470 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6471 | |
| 6472 | /* remove the previous default */ |
| 6473 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6474 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6475 | leaf->dflt->realtype = leaf->type; |
| 6476 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6477 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6478 | LY_PREF_SCHEMA, leaf->dflt_mod, target, NULL, leaf->dflt, NULL, &err); |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6479 | leaf->dflt->realtype->refcount++; |
| 6480 | if (err) { |
| 6481 | ly_err_print(err); |
| 6482 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6483 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6484 | ly_err_free(err); |
| 6485 | } |
| 6486 | if (dynamic) { |
| 6487 | free((void*)dflt); |
| 6488 | } |
| 6489 | dflt = NULL; |
| 6490 | if (rc == LY_EINCOMPLETE) { |
| 6491 | /* postpone default compilation when the tree is complete */ |
| 6492 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6493 | |
| 6494 | /* but in general result is so far ok */ |
| 6495 | rc = LY_SUCCESS; |
| 6496 | } |
| 6497 | LY_CHECK_GOTO(rc, cleanup); |
| 6498 | } else { /* LYS_LEAFLIST */ |
| 6499 | LY_ARRAY_FOR(llist->dflts, x) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6500 | dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_SCHEMA, llist->dflts_mods[x], &dynamic); |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6501 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6502 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6503 | llist->dflts[x]->realtype = llist->type; |
| 6504 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6505 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6506 | LY_PREF_SCHEMA, llist->dflts_mods[x], target, NULL, llist->dflts[x], NULL, &err); |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6507 | llist->dflts[x]->realtype->refcount++; |
| 6508 | if (err) { |
| 6509 | ly_err_print(err); |
| 6510 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6511 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6512 | dflt, err->msg); |
| 6513 | ly_err_free(err); |
| 6514 | } |
| 6515 | if (dynamic) { |
| 6516 | free((void*)dflt); |
| 6517 | } |
| 6518 | dflt = NULL; |
| 6519 | if (rc == LY_EINCOMPLETE) { |
| 6520 | /* postpone default compilation when the tree is complete */ |
| 6521 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6522 | |
| 6523 | /* but in general result is so far ok */ |
| 6524 | rc = LY_SUCCESS; |
| 6525 | } |
| 6526 | LY_CHECK_GOTO(rc, cleanup); |
| 6527 | } |
| 6528 | } |
| 6529 | } |
| 6530 | |
| 6531 | /* check mandatory - default compatibility */ |
| 6532 | if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT) |
| 6533 | && (target->flags & LYS_MAND_TRUE)) { |
| 6534 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6535 | "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype)); |
| 6536 | goto cleanup; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6537 | } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice* )target)->dflt |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6538 | && (target->flags & LYS_MAND_TRUE)) { |
| 6539 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice."); |
| 6540 | goto cleanup; |
| 6541 | } |
| 6542 | if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) { |
| 6543 | /* mandatory node under a default case */ |
| 6544 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6545 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6546 | lys_nodetype2str(target->nodetype), target->name, target->parent->name); |
| 6547 | goto cleanup; |
| 6548 | } |
| 6549 | |
| 6550 | /* success */ |
| 6551 | ret = LY_SUCCESS; |
| 6552 | |
| 6553 | cleanup: |
| 6554 | lysc_update_path(ctx, NULL, NULL); |
| 6555 | return ret; |
| 6556 | } |
| 6557 | |
| 6558 | LY_ERR |
| 6559 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
| 6560 | { |
| 6561 | LY_ERR ret = LY_EVALID; |
| 6562 | struct ly_set devs_p = {0}; |
| 6563 | struct ly_set targets = {0}; |
| 6564 | struct lysc_node *target; /* target target of the deviation */ |
| 6565 | struct lysp_deviation *dev; |
| 6566 | struct lysp_deviate *d, **dp_new; |
| 6567 | LY_ARRAY_COUNT_TYPE u, v; |
| 6568 | struct lysc_deviation **devs = NULL; |
| 6569 | struct lys_module *target_mod, **dev_mod; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6570 | uint16_t flags; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6571 | int i; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6572 | |
| 6573 | /* get all deviations from the module and all its submodules ... */ |
| 6574 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 6575 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 6576 | } |
| 6577 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 6578 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 6579 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 6580 | } |
| 6581 | } |
| 6582 | if (!devs_p.count) { |
| 6583 | /* nothing to do */ |
| 6584 | return LY_SUCCESS; |
| 6585 | } |
| 6586 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6587 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 6588 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6589 | /* ... and group them by the target node */ |
| 6590 | devs = calloc(devs_p.count, sizeof *devs); |
| 6591 | for (u = 0; u < devs_p.count; ++u) { |
| 6592 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6593 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6594 | |
| 6595 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6596 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 6597 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6598 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6599 | /* move the target pointer to input/output to make them different from the action and |
| 6600 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 6601 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 6602 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 6603 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 6604 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 6605 | flags |= LYSC_OPT_INTERNAL; |
| 6606 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 6607 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 6608 | flags |= LYSC_OPT_INTERNAL; |
| 6609 | } |
| 6610 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6611 | /* insert into the set of targets with duplicity detection */ |
| 6612 | i = ly_set_add(&targets, target, 0); |
| 6613 | if (!devs[i]) { |
| 6614 | /* new record */ |
| 6615 | devs[i] = calloc(1, sizeof **devs); |
| 6616 | devs[i]->target = target; |
| 6617 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6618 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6619 | } |
| 6620 | /* add deviates into the deviation's list of deviates */ |
| 6621 | for (d = dev->deviates; d; d = d->next) { |
| 6622 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 6623 | *dp_new = d; |
| 6624 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 6625 | devs[i]->not_supported = 1; |
| 6626 | } |
| 6627 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6628 | |
| 6629 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6630 | } |
| 6631 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6632 | /* apply deviations */ |
| 6633 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6634 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 6635 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 6636 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6637 | devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input)); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6638 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6639 | devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output)); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6640 | } |
| 6641 | } |
| 6642 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6643 | /* remember target module (the target node may be removed) */ |
| 6644 | target_mod = devs[u]->target->module; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6645 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6646 | /* apply the deviation */ |
| 6647 | LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6648 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6649 | /* add this module into the target module deviated_by, if not there already */ |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6650 | i = 0; |
| 6651 | LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) { |
| 6652 | if (target_mod->compiled->deviated_by[v] == mod_p->mod) { |
| 6653 | i = 1; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6654 | break; |
| 6655 | } |
| 6656 | } |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6657 | if (!i) { |
| 6658 | LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup); |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6659 | *dev_mod = mod_p->mod; |
| 6660 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6661 | } |
| 6662 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6663 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6664 | ret = LY_SUCCESS; |
| 6665 | |
| 6666 | cleanup: |
| 6667 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6668 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6669 | free(devs[u]); |
| 6670 | } |
| 6671 | free(devs); |
| 6672 | ly_set_erase(&targets, NULL); |
| 6673 | ly_set_erase(&devs_p, NULL); |
| 6674 | |
| 6675 | return ret; |
| 6676 | } |
| 6677 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6678 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6679 | * @brief Compile the given YANG submodule into the main module. |
| 6680 | * @param[in] ctx Compile context |
| 6681 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6682 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6683 | */ |
| 6684 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6685 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6686 | { |
| 6687 | unsigned int u; |
| 6688 | LY_ERR ret = LY_SUCCESS; |
| 6689 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6690 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6691 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6692 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6693 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6694 | if (!mainmod->mod->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6695 | /* features are compiled directly into the compiled module structure, |
| 6696 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6697 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6698 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6699 | LY_CHECK_GOTO(ret, error); |
| 6700 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6701 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6702 | if (!mainmod->mod->dis_identities) { |
| 6703 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities); |
| 6704 | LY_CHECK_GOTO(ret, error); |
| 6705 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6706 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6707 | /* data nodes */ |
| 6708 | LY_LIST_FOR(submod->data, node_p) { |
| 6709 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6710 | LY_CHECK_GOTO(ret, error); |
| 6711 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6712 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6713 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6714 | COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6715 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6716 | error: |
| 6717 | return ret; |
| 6718 | } |
| 6719 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6720 | static void * |
| 6721 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6722 | { |
| 6723 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6724 | if (substmts[u].stmt == stmt) { |
| 6725 | return substmts[u].storage; |
| 6726 | } |
| 6727 | } |
| 6728 | return NULL; |
| 6729 | } |
| 6730 | |
| 6731 | LY_ERR |
| 6732 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6733 | { |
| 6734 | LY_ERR ret = LY_EVALID, r; |
| 6735 | unsigned int u; |
| 6736 | struct lysp_stmt *stmt; |
| 6737 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6738 | |
| 6739 | /* check for invalid substatements */ |
| 6740 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6741 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6742 | continue; |
| 6743 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6744 | for (u = 0; substmts[u].stmt; ++u) { |
| 6745 | if (substmts[u].stmt == stmt->kw) { |
| 6746 | break; |
| 6747 | } |
| 6748 | } |
| 6749 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6750 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6751 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6752 | goto cleanup; |
| 6753 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6754 | } |
| 6755 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6756 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6757 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6758 | /* keep order of the processing the same as the order in the defined substmts, |
| 6759 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6760 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6761 | int stmt_present = 0; |
| 6762 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6763 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6764 | if (substmts[u].stmt != stmt->kw) { |
| 6765 | continue; |
| 6766 | } |
| 6767 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6768 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6769 | if (substmts[u].storage) { |
| 6770 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6771 | case LY_STMT_STATUS: |
| 6772 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6773 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6774 | break; |
| 6775 | case LY_STMT_UNITS: { |
| 6776 | const char **units; |
| 6777 | |
| 6778 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6779 | /* single item */ |
| 6780 | if (*((const char **)substmts[u].storage)) { |
| 6781 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6782 | goto cleanup; |
| 6783 | } |
| 6784 | units = (const char **)substmts[u].storage; |
| 6785 | } else { |
| 6786 | /* sized array */ |
| 6787 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6788 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6789 | } |
| 6790 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6791 | break; |
| 6792 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6793 | case LY_STMT_TYPE: { |
| 6794 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6795 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6796 | |
| 6797 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6798 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6799 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6800 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6801 | goto cleanup; |
| 6802 | } |
| 6803 | compiled = substmts[u].storage; |
| 6804 | } else { |
| 6805 | /* sized array */ |
| 6806 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6807 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6808 | compiled = (void*)type; |
| 6809 | } |
| 6810 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6811 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6812 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6813 | flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled, |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 6814 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6815 | lysp_type_free(ctx->ctx, parsed); |
| 6816 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6817 | break; |
| 6818 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6819 | case LY_STMT_IF_FEATURE: { |
| 6820 | struct lysc_iffeature *iff = NULL; |
| 6821 | |
| 6822 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6823 | /* single item */ |
| 6824 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6825 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6826 | goto cleanup; |
| 6827 | } |
| 6828 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6829 | } else { |
| 6830 | /* sized array */ |
| 6831 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6832 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6833 | } |
| 6834 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6835 | break; |
| 6836 | } |
| 6837 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6838 | * 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] | 6839 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6840 | 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.", |
| 6841 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6842 | goto cleanup; |
| 6843 | } |
| 6844 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6845 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6846 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6847 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6848 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6849 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6850 | goto cleanup; |
| 6851 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6852 | } |
| 6853 | |
| 6854 | ret = LY_SUCCESS; |
| 6855 | |
| 6856 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6857 | return ret; |
| 6858 | } |
| 6859 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6860 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6861 | * @brief Check when for cyclic dependencies. |
| 6862 | * @param[in] set Set with all the referenced nodes. |
| 6863 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6864 | * @return LY_ERR value |
| 6865 | */ |
| 6866 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6867 | 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] | 6868 | { |
| 6869 | struct lyxp_set tmp_set; |
| 6870 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6871 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6872 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6873 | int idx; |
| 6874 | struct lysc_when *when; |
| 6875 | LY_ERR ret = LY_SUCCESS; |
| 6876 | |
| 6877 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6878 | |
| 6879 | /* prepare in_ctx of the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6880 | for ( i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6881 | xp_scnode = &set->val.scnodes[i]; |
| 6882 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6883 | if (xp_scnode->in_ctx != -1) { |
| 6884 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6885 | xp_scnode->in_ctx = 1; |
| 6886 | } |
| 6887 | } |
| 6888 | |
| 6889 | for (i = 0; i < set->used; ++i) { |
| 6890 | xp_scnode = &set->val.scnodes[i]; |
| 6891 | if (xp_scnode->in_ctx != 1) { |
| 6892 | /* already checked */ |
| 6893 | continue; |
| 6894 | } |
| 6895 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6896 | if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
Michal Vasko | 2ff7efe | 2019-12-10 14:50:59 +0100 | [diff] [blame] | 6897 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6898 | /* no when to check */ |
| 6899 | xp_scnode->in_ctx = 0; |
| 6900 | continue; |
| 6901 | } |
| 6902 | |
| 6903 | node = xp_scnode->scnode; |
| 6904 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6905 | LY_ARRAY_FOR(node->when, u) { |
| 6906 | when = node->when[u]; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6907 | ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6908 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6909 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6910 | 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] | 6911 | goto cleanup; |
| 6912 | } |
| 6913 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6914 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6915 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6916 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6917 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6918 | idx = lyxp_set_scnode_dup_node_check(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6919 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6920 | 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] | 6921 | ret = LY_EVALID; |
| 6922 | goto cleanup; |
| 6923 | } |
| 6924 | |
| 6925 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6926 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6927 | } else { |
| 6928 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6929 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6930 | } |
| 6931 | } |
| 6932 | |
| 6933 | /* merge this set into the global when set */ |
| 6934 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6935 | } |
| 6936 | |
| 6937 | /* check when of non-data parents as well */ |
| 6938 | node = node->parent; |
| 6939 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6940 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6941 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6942 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6943 | } |
| 6944 | |
| 6945 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6946 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6947 | return ret; |
| 6948 | } |
| 6949 | |
| 6950 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6951 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6952 | * @param[in] ctx Compile context. |
| 6953 | * @param[in] node Node to check. |
| 6954 | * @return LY_ERR value |
| 6955 | */ |
| 6956 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6957 | 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] | 6958 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6959 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6960 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6961 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6962 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6963 | struct lysc_when **when = NULL; |
| 6964 | struct lysc_must *musts = NULL; |
| 6965 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6966 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6967 | |
| 6968 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6969 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6970 | if (node->flags & LYS_CONFIG_R) { |
| 6971 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent); |
| 6972 | if (op) { |
| 6973 | /* we are actually in output */ |
| 6974 | opts = LYXP_SCNODE_OUTPUT; |
| 6975 | } |
| 6976 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6977 | |
| 6978 | switch (node->nodetype) { |
| 6979 | case LYS_CONTAINER: |
| 6980 | when = ((struct lysc_node_container *)node)->when; |
| 6981 | musts = ((struct lysc_node_container *)node)->musts; |
| 6982 | break; |
| 6983 | case LYS_CHOICE: |
| 6984 | when = ((struct lysc_node_choice *)node)->when; |
| 6985 | break; |
| 6986 | case LYS_LEAF: |
| 6987 | when = ((struct lysc_node_leaf *)node)->when; |
| 6988 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6989 | break; |
| 6990 | case LYS_LEAFLIST: |
| 6991 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6992 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6993 | break; |
| 6994 | case LYS_LIST: |
| 6995 | when = ((struct lysc_node_list *)node)->when; |
| 6996 | musts = ((struct lysc_node_list *)node)->musts; |
| 6997 | break; |
| 6998 | case LYS_ANYXML: |
| 6999 | case LYS_ANYDATA: |
| 7000 | when = ((struct lysc_node_anydata *)node)->when; |
| 7001 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 7002 | break; |
| 7003 | case LYS_CASE: |
| 7004 | when = ((struct lysc_node_case *)node)->when; |
| 7005 | break; |
| 7006 | case LYS_NOTIF: |
| 7007 | musts = ((struct lysc_notif *)node)->musts; |
| 7008 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7009 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7010 | case LYS_ACTION: |
| 7011 | /* first process input musts */ |
| 7012 | musts = ((struct lysc_action *)node)->input.musts; |
| 7013 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7014 | default: |
| 7015 | /* nothing to check */ |
| 7016 | break; |
| 7017 | } |
| 7018 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7019 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7020 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 7021 | ret = lyxp_atomize(when[u]->cond, LY_PREF_SCHEMA, when[u]->module, when[u]->context ? when[u]->context : node, |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7022 | 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] | 7023 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7024 | 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] | 7025 | goto cleanup; |
| 7026 | } |
| 7027 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7028 | ctx->path[0] = '\0'; |
| 7029 | 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] | 7030 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7031 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7032 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 7033 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7034 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7035 | /* 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] | 7036 | ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7037 | schema->name); |
| 7038 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7039 | |
| 7040 | /* check dummy node accessing */ |
| 7041 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7042 | 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] | 7043 | ret = LY_EVALID; |
| 7044 | goto cleanup; |
| 7045 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7046 | } |
| 7047 | } |
| 7048 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7049 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7050 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7051 | LY_CHECK_GOTO(ret, cleanup); |
| 7052 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7053 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7054 | } |
| 7055 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7056 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7057 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7058 | LY_ARRAY_FOR(musts, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 7059 | 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] | 7060 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7061 | 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] | 7062 | goto cleanup; |
| 7063 | } |
| 7064 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7065 | ctx->path[0] = '\0'; |
| 7066 | 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] | 7067 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7068 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7069 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7070 | /* 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] | 7071 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 7072 | 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] | 7073 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7074 | } |
| 7075 | } |
| 7076 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7077 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7078 | } |
| 7079 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7080 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7081 | /* now check output musts */ |
| 7082 | input_done = 1; |
| 7083 | musts = ((struct lysc_action *)node)->output.musts; |
| 7084 | opts = LYXP_SCNODE_OUTPUT; |
| 7085 | goto check_musts; |
| 7086 | } |
| 7087 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7088 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7089 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7090 | return ret; |
| 7091 | } |
| 7092 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7093 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7094 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 7095 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7096 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7097 | struct ly_path *p; |
| 7098 | struct lysc_type *type; |
| 7099 | |
| 7100 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7101 | |
| 7102 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 7103 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 7104 | lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 7105 | LY_PREF_SCHEMA, lref->path_context, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7106 | |
| 7107 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7108 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7109 | ly_path_free(node->module->ctx, p); |
| 7110 | |
| 7111 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7112 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7113 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 7114 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 7115 | return LY_EVALID; |
| 7116 | } |
| 7117 | |
| 7118 | /* check status */ |
| 7119 | ctx->path[0] = '\0'; |
| 7120 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7121 | ctx->path_len = strlen(ctx->path); |
| 7122 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 7123 | return LY_EVALID; |
| 7124 | } |
| 7125 | ctx->path_len = 1; |
| 7126 | ctx->path[1] = '\0'; |
| 7127 | |
| 7128 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7129 | if (lref->require_instance) { |
| 7130 | for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent); |
| 7131 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7132 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 7133 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 7134 | return LY_EVALID; |
| 7135 | } |
| 7136 | } |
| 7137 | |
| 7138 | /* store the target's type and check for circular chain of leafrefs */ |
| 7139 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 7140 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 7141 | if (type == (struct lysc_type *)lref) { |
| 7142 | /* circular chain detected */ |
| 7143 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7144 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 7145 | return LY_EVALID; |
| 7146 | } |
| 7147 | } |
| 7148 | |
| 7149 | /* check if leafref and its target are under common if-features */ |
| 7150 | if (lys_compile_leafref_features_validate(node, target)) { |
| 7151 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7152 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7153 | " features applicable to the leafref itself.", lref->path->expr); |
| 7154 | return LY_EVALID; |
| 7155 | } |
| 7156 | |
| 7157 | return LY_SUCCESS; |
| 7158 | } |
| 7159 | |
| 7160 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7161 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7162 | { |
| 7163 | struct lysc_ext_instance *ext; |
| 7164 | struct lysp_ext_instance *ext_p = NULL; |
| 7165 | struct lysp_stmt *stmt; |
| 7166 | const struct lys_module *ext_mod; |
| 7167 | LY_ERR ret = LY_SUCCESS; |
| 7168 | |
| 7169 | /* create the parsed extension instance manually */ |
| 7170 | ext_p = calloc(1, sizeof *ext_p); |
| 7171 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 7172 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 7173 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 7174 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7175 | ext_p->insubstmt_index = 0; |
| 7176 | |
| 7177 | stmt = calloc(1, sizeof *ext_p->child); |
| 7178 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 7179 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 7180 | stmt->kw = LY_STMT_TYPE; |
| 7181 | ext_p->child = stmt; |
| 7182 | |
| 7183 | /* allocate new extension instance */ |
| 7184 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7185 | |
| 7186 | /* manually get extension definition module */ |
| 7187 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7188 | |
| 7189 | /* compile the extension instance */ |
| 7190 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7191 | |
| 7192 | cleanup: |
| 7193 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7194 | free(ext_p); |
| 7195 | return ret; |
| 7196 | } |
| 7197 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7198 | static LY_ERR |
| 7199 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7200 | { |
| 7201 | struct lysc_node *node; |
| 7202 | struct lysc_type *type, *typeiter; |
| 7203 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7204 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7205 | uint32_t i; |
| 7206 | |
| 7207 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7208 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7209 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7210 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7211 | node = ctx->leafrefs.objs[i]; |
| 7212 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7213 | type = ((struct lysc_node_leaf *)node)->type; |
| 7214 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7215 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7216 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7217 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7218 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7219 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7220 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7221 | } |
| 7222 | } |
| 7223 | } |
| 7224 | } |
| 7225 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7226 | /* store pointer to the real type */ |
| 7227 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7228 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7229 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7230 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7231 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7232 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
| 7233 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7234 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7235 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7236 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7237 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7238 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7239 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7240 | } |
| 7241 | } |
| 7242 | } |
| 7243 | } |
| 7244 | |
| 7245 | /* check xpath */ |
| 7246 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7247 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7248 | } |
| 7249 | |
| 7250 | /* finish incomplete default values compilation */ |
| 7251 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 7252 | struct ly_err_item *err = NULL; |
| 7253 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
| 7254 | LY_ERR ret; |
| 7255 | ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original), |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 7256 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, |
| 7257 | LY_PREF_SCHEMA, r->dflt_mod, r->context_node, NULL, r->dflt, NULL, &err); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7258 | if (err) { |
| 7259 | ly_err_print(err); |
| 7260 | ctx->path[0] = '\0'; |
| 7261 | lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7262 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7263 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7264 | ly_err_free(err); |
| 7265 | } |
| 7266 | LY_CHECK_RET(ret); |
| 7267 | } |
| 7268 | |
| 7269 | return LY_SUCCESS; |
| 7270 | } |
| 7271 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7272 | LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7273 | lys_compile(struct lys_module **mod, int options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7274 | { |
| 7275 | struct lysc_ctx ctx = {0}; |
| 7276 | struct lysc_module *mod_c; |
| 7277 | struct lysp_module *sp; |
| 7278 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7279 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7280 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7281 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7282 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7283 | uint32_t i; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7284 | uint16_t compile_id; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7285 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7286 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7287 | LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7288 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7289 | if (!(*mod)->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7290 | /* just imported modules are not compiled */ |
| 7291 | return LY_SUCCESS; |
| 7292 | } |
| 7293 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7294 | compile_id = ++(*mod)->ctx->module_set_id; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7295 | sp = (*mod)->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7296 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7297 | ctx.ctx = (*mod)->ctx; |
| 7298 | ctx.mod = *mod; |
| 7299 | ctx.mod_def = *mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7300 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7301 | ctx.path_len = 1; |
| 7302 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7303 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7304 | (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7305 | LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM); |
| 7306 | mod_c->mod = *mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7307 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7308 | LY_ARRAY_FOR(sp->imports, u) { |
| 7309 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 7310 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7311 | LY_ARRAY_FOR(sp->includes, u) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7312 | LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7313 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7314 | |
| 7315 | /* features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7316 | if ((*mod)->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7317 | /* there is already precompiled array of features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7318 | mod_c->features = (*mod)->dis_features; |
| 7319 | (*mod)->dis_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7320 | } else { |
| 7321 | /* features are compiled directly into the compiled module structure, |
| 7322 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7323 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7324 | LY_CHECK_GOTO(ret, error); |
| 7325 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7326 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7327 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7328 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7329 | * are present. */ |
| 7330 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7331 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7332 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7333 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7334 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7335 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7336 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7337 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7338 | ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7339 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7340 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7341 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7342 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7343 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7344 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7345 | /* identities, work similarly to features with the precompilation */ |
| 7346 | if ((*mod)->dis_identities) { |
| 7347 | mod_c->identities = (*mod)->dis_identities; |
| 7348 | (*mod)->dis_identities = NULL; |
| 7349 | } else { |
| 7350 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities); |
| 7351 | LY_CHECK_GOTO(ret, error); |
| 7352 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7353 | if (sp->identities) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7354 | LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7355 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7356 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7357 | LY_ARRAY_FOR(sp->includes, v) { |
| 7358 | if (sp->includes[v].submodule->identities) { |
| 7359 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7360 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities); |
| 7361 | LY_CHECK_GOTO(ret, error); |
| 7362 | lysc_update_path(&ctx, NULL, NULL); |
| 7363 | } |
| 7364 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7365 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7366 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7367 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7368 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7369 | LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7370 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7371 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7372 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7373 | COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7374 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7375 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7376 | LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7377 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7378 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7379 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7380 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7381 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7382 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7383 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7384 | /* extension instances TODO cover extension instances from submodules */ |
| 7385 | 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] | 7386 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7387 | /* finish compilation for all unresolved items in the context */ |
| 7388 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7389 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7390 | /* validate non-instantiated groupings from the parsed schema, |
| 7391 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7392 | ctx.options |= LYSC_OPT_GROUPING; |
| 7393 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7394 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7395 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7396 | } |
| 7397 | } |
| 7398 | LY_LIST_FOR(sp->data, node_p) { |
| 7399 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7400 | LY_ARRAY_FOR(grps, u) { |
| 7401 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7402 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7403 | } |
| 7404 | } |
| 7405 | } |
| 7406 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7407 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7408 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7409 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7410 | } |
| 7411 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7412 | #if 0 |
| 7413 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7414 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7415 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7416 | * the anotation definitions available in the internal schema structure. */ |
| 7417 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7418 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7419 | lys_free(mod, NULL, 1, 1); |
| 7420 | return NULL; |
| 7421 | } |
| 7422 | } |
| 7423 | #endif |
| 7424 | |
| 7425 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7426 | if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) { |
| 7427 | 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] | 7428 | } |
| 7429 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7430 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7431 | ly_set_erase(&ctx.xpath, NULL); |
| 7432 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7433 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7434 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7435 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7436 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7437 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7438 | lysp_module_free((*mod)->parsed); |
| 7439 | (*mod)->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7440 | } |
| 7441 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7442 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7443 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7444 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7445 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7446 | if (m->implemented > 1) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7447 | m->implemented = 1; |
| 7448 | } |
| 7449 | } |
| 7450 | } |
| 7451 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7452 | return LY_SUCCESS; |
| 7453 | |
| 7454 | error: |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7455 | lys_feature_precompile_revert(&ctx, *mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7456 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | ly_set_erase(&ctx.xpath, NULL); |
| 7458 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7459 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7460 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7461 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7462 | lysc_module_free(mod_c, NULL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7463 | (*mod)->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7464 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7465 | /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency |
| 7466 | * of this module, since this module can be also compiled from dependency, there can be some other modules being |
| 7467 | * processed and we are going to get back to them via stack, so freeing them is not a good idea. */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7468 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7469 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7470 | if ((m->implemented >= compile_id) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7471 | /* revert features list to the precompiled state */ |
| 7472 | lys_feature_precompile_revert(&ctx, m); |
| 7473 | /* mark module as imported-only / not-implemented */ |
| 7474 | m->implemented = 0; |
| 7475 | /* free the compiled version of the module */ |
| 7476 | lysc_module_free(m->compiled, NULL); |
| 7477 | m->compiled = NULL; |
| 7478 | } |
| 7479 | } |
| 7480 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7481 | /* remove the module itself from the context and free it */ |
| 7482 | ly_set_rm(&ctx.ctx->list, *mod, NULL); |
| 7483 | lys_module_free(*mod, NULL); |
| 7484 | *mod = NULL; |
| 7485 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7486 | return ret; |
| 7487 | } |