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 | |
| 15 | #include "common.h" |
| 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 | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 25 | #include "dict.h" |
| 26 | #include "log.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 27 | #include "plugins_exts.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "set.h" |
| 29 | #include "plugins_types.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 30 | #include "plugins_exts_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | #include "tree.h" |
| 32 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 33 | #include "tree_schema_internal.h" |
| 34 | #include "xpath.h" |
| 35 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 36 | static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, |
| 37 | void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod); |
| 38 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 39 | /** |
| 40 | * @brief Duplicate string into dictionary |
| 41 | * @param[in] CTX libyang context of the dictionary. |
| 42 | * @param[in] ORIG String to duplicate. |
| 43 | * @param[out] DUP Where to store the result. |
| 44 | */ |
| 45 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 46 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 47 | #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] | 48 | if (ARRAY_P) { \ |
| 49 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 50 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 51 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 52 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 53 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 54 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 55 | } \ |
| 56 | } |
| 57 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 58 | #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] | 59 | if (ARRAY_P) { \ |
| 60 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 61 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 62 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 63 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 64 | 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] | 65 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 66 | } \ |
| 67 | } |
| 68 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 69 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 70 | if (EXTS_P) { \ |
| 71 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 72 | for (LY_ARRAY_SIZE_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 73 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 74 | 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] | 75 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 76 | } \ |
| 77 | } |
| 78 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 79 | #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] | 80 | if (ARRAY_P) { \ |
| 81 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 82 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 83 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 84 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 85 | 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] | 86 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 87 | } \ |
| 88 | } |
| 89 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 90 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 91 | if (MEMBER_P) { \ |
| 92 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 93 | 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] | 94 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 95 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 96 | } |
| 97 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 98 | #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] | 99 | if (MEMBER_P) { \ |
| 100 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 101 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 102 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 103 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 104 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 105 | } |
| 106 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 107 | #define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 108 | if (ARRAY) { \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 109 | for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 110 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 111 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 112 | return LY_EVALID; \ |
| 113 | } \ |
| 114 | } \ |
| 115 | } |
| 116 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 117 | #define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 118 | if (ARRAY) { \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 119 | for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 120 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \ |
| 121 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 122 | return LY_EVALID; \ |
| 123 | } \ |
| 124 | } \ |
| 125 | } |
| 126 | |
| 127 | struct lysc_ext * |
| 128 | lysc_ext_dup(struct lysc_ext *orig) |
| 129 | { |
| 130 | ++orig->refcount; |
| 131 | return orig; |
| 132 | } |
| 133 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 134 | static struct lysc_ext_instance * |
| 135 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 136 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 137 | /* TODO - extensions, increase refcount */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 138 | (void) ctx; |
| 139 | (void) orig; |
| 140 | return NULL; |
| 141 | } |
| 142 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 143 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 144 | * @brief Add record into the compile context's list of incomplete default values. |
| 145 | * @param[in] ctx Compile context with the incomplete default values list. |
| 146 | * @param[in] context_node Context schema node to store in the record. |
| 147 | * @param[in] dflt Incomplete default value to store in the record. |
| 148 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 149 | * @return LY_EMEM in case of memory allocation failure. |
| 150 | * @return LY_SUCCESS |
| 151 | */ |
| 152 | static LY_ERR |
| 153 | lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod) |
| 154 | { |
| 155 | struct lysc_incomplete_dflt *r; |
| 156 | r = malloc(sizeof *r); |
| 157 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 158 | r->context_node = context_node; |
| 159 | r->dflt = dflt; |
| 160 | r->dflt_mod = dflt_mod; |
| 161 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 162 | |
| 163 | return LY_SUCCESS; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 168 | * @param[in] ctx Compile context with the incomplete default values list. |
| 169 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 170 | */ |
| 171 | static void |
| 172 | lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt) |
| 173 | { |
| 174 | unsigned int u; |
| 175 | struct lysc_incomplete_dflt *r; |
| 176 | |
| 177 | for (u = 0; u < ctx->dflts.count; ++u) { |
| 178 | r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u]; |
| 179 | if (r->dflt == dflt) { |
| 180 | free(ctx->dflts.objs[u]); |
| 181 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 182 | --ctx->dflts.count; |
| 183 | return; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 188 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 189 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 190 | { |
| 191 | int len; |
| 192 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 193 | |
| 194 | if (!name) { |
| 195 | /* removing last path segment */ |
| 196 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 197 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 198 | if (ctx->path[ctx->path_len] == '=') { |
| 199 | ctx->path[ctx->path_len++] = '}'; |
| 200 | } else { |
| 201 | /* not a top-level special tag, remove also preceiding '/' */ |
| 202 | goto remove_nodelevel; |
| 203 | } |
| 204 | } else { |
| 205 | remove_nodelevel: |
| 206 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 207 | if (ctx->path_len == 0) { |
| 208 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 209 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | /* set new terminating NULL-byte */ |
| 213 | ctx->path[ctx->path_len] = '\0'; |
| 214 | } else { |
| 215 | if (ctx->path_len > 1) { |
| 216 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 217 | /* extension of the special tag */ |
| 218 | nextlevel = 2; |
| 219 | --ctx->path_len; |
| 220 | } else { |
| 221 | /* there is already some path, so add next level */ |
| 222 | nextlevel = 1; |
| 223 | } |
| 224 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 225 | |
| 226 | if (nextlevel != 2) { |
| 227 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 228 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 229 | 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] | 230 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 231 | 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] | 232 | } |
| 233 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 234 | 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] | 235 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 236 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 237 | /* output truncated */ |
| 238 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 239 | } else { |
| 240 | ctx->path_len += len; |
| 241 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 246 | * @brief Duplicate the compiled pattern structure. |
| 247 | * |
| 248 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 249 | * |
| 250 | * @param[in] orig The pattern structure to duplicate. |
| 251 | * @return The duplicated structure to use. |
| 252 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 253 | static struct lysc_pattern* |
| 254 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 255 | { |
| 256 | ++orig->refcount; |
| 257 | return orig; |
| 258 | } |
| 259 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 260 | /** |
| 261 | * @brief Duplicate the array of compiled patterns. |
| 262 | * |
| 263 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 264 | * |
| 265 | * @param[in] ctx Libyang context for logging. |
| 266 | * @param[in] orig The patterns sized array to duplicate. |
| 267 | * @return New sized array as a copy of @p orig. |
| 268 | * @return NULL in case of memory allocation error. |
| 269 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 270 | static struct lysc_pattern** |
| 271 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 272 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 273 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 274 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 275 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 276 | assert(orig); |
| 277 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 278 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 279 | LY_ARRAY_FOR(orig, u) { |
| 280 | dup[u] = lysc_pattern_dup(orig[u]); |
| 281 | LY_ARRAY_INCREMENT(dup); |
| 282 | } |
| 283 | return dup; |
| 284 | } |
| 285 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 286 | /** |
| 287 | * @brief Duplicate compiled range structure. |
| 288 | * |
| 289 | * @param[in] ctx Libyang context for logging. |
| 290 | * @param[in] orig The range structure to be duplicated. |
| 291 | * @return New compiled range structure as a copy of @p orig. |
| 292 | * @return NULL in case of memory allocation error. |
| 293 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 294 | struct lysc_range* |
| 295 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 296 | { |
| 297 | struct lysc_range *dup; |
| 298 | LY_ERR ret; |
| 299 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 300 | assert(orig); |
| 301 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 302 | dup = calloc(1, sizeof *dup); |
| 303 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 304 | if (orig->parts) { |
| 305 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 306 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 307 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 308 | } |
| 309 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 310 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 311 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 312 | |
| 313 | return dup; |
| 314 | cleanup: |
| 315 | free(dup); |
| 316 | (void) ret; /* set but not used due to the return type */ |
| 317 | return NULL; |
| 318 | } |
| 319 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 320 | /** |
| 321 | * @brief Stack for processing if-feature expressions. |
| 322 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 323 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 324 | int size; /**< number of items in the stack */ |
| 325 | int index; /**< first empty item */ |
| 326 | 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] | 327 | }; |
| 328 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 329 | /** |
| 330 | * @brief Add @ref ifftokens into the stack. |
| 331 | * @param[in] stack The if-feature stack to use. |
| 332 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 333 | * @return LY_EMEM in case of memory allocation error |
| 334 | * @return LY_ESUCCESS if the value successfully stored. |
| 335 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 336 | static LY_ERR |
| 337 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 338 | { |
| 339 | if (stack->index == stack->size) { |
| 340 | stack->size += 4; |
| 341 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 342 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 343 | } |
| 344 | stack->stack[stack->index++] = value; |
| 345 | return LY_SUCCESS; |
| 346 | } |
| 347 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 348 | /** |
| 349 | * @brief Get (and remove) the last item form the stack. |
| 350 | * @param[in] stack The if-feature stack to use. |
| 351 | * @return The value from the top of the stack. |
| 352 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 353 | static uint8_t |
| 354 | iff_stack_pop(struct iff_stack *stack) |
| 355 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 356 | assert(stack && stack->index); |
| 357 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 358 | stack->index--; |
| 359 | return stack->stack[stack->index]; |
| 360 | } |
| 361 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 362 | /** |
| 363 | * @brief Clean up the stack. |
| 364 | * @param[in] stack The if-feature stack to use. |
| 365 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 366 | static void |
| 367 | iff_stack_clean(struct iff_stack *stack) |
| 368 | { |
| 369 | stack->size = 0; |
| 370 | free(stack->stack); |
| 371 | } |
| 372 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 373 | /** |
| 374 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 375 | * (libyang format of the if-feature expression). |
| 376 | * @param[in,out] list The 2bits array to modify. |
| 377 | * @param[in] op The operand (@ref ifftokens) to store. |
| 378 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 379 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 380 | static void |
| 381 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 382 | { |
| 383 | uint8_t *item; |
| 384 | uint8_t mask = 3; |
| 385 | |
| 386 | assert(pos >= 0); |
| 387 | assert(op <= 3); /* max 2 bits */ |
| 388 | |
| 389 | item = &list[pos / 4]; |
| 390 | mask = mask << 2 * (pos % 4); |
| 391 | *item = (*item) & ~mask; |
| 392 | *item = (*item) | (op << 2 * (pos % 4)); |
| 393 | } |
| 394 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 395 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 396 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 397 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 398 | /** |
| 399 | * @brief Find a feature of the given name and referenced in the given module. |
| 400 | * |
| 401 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 402 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 403 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 404 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 405 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 406 | * assigned till that time will be still valid. |
| 407 | * |
| 408 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 409 | * @param[in] name Name of the feature including possible prefix. |
| 410 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 411 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 412 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 413 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 414 | 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] | 415 | { |
| 416 | size_t i; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 417 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 418 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 419 | |
| 420 | for (i = 0; i < len; ++i) { |
| 421 | if (name[i] == ':') { |
| 422 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 423 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 424 | LY_CHECK_RET(!mod, NULL); |
| 425 | |
| 426 | name = &name[i + 1]; |
| 427 | len = len - i - 1; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 432 | if (mod->implemented) { |
| 433 | /* module is implemented so there is already the compiled schema */ |
| 434 | flist = mod->compiled->features; |
| 435 | } else { |
| 436 | flist = mod->off_features; |
| 437 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 438 | LY_ARRAY_FOR(flist, u) { |
| 439 | f = &flist[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 440 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 441 | return f; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return NULL; |
| 446 | } |
| 447 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 448 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 449 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 450 | */ |
| 451 | static LY_ERR |
| 452 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 453 | { |
| 454 | LY_ERR ret = LY_SUCCESS; |
| 455 | |
| 456 | if (!ext_p->compiled) { |
| 457 | lysc_update_path(ctx, NULL, "{extension}"); |
| 458 | lysc_update_path(ctx, NULL, ext_p->name); |
| 459 | |
| 460 | /* compile the extension definition */ |
| 461 | ext_p->compiled = calloc(1, sizeof **ext); |
| 462 | ext_p->compiled->refcount = 1; |
| 463 | DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name); |
| 464 | DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument); |
| 465 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 466 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 467 | |
| 468 | lysc_update_path(ctx, NULL, NULL); |
| 469 | lysc_update_path(ctx, NULL, NULL); |
| 470 | |
| 471 | /* find extension definition plugin */ |
| 472 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 473 | } |
| 474 | |
| 475 | *ext = lysc_ext_dup(ext_p->compiled); |
| 476 | |
| 477 | done: |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 482 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 483 | * |
| 484 | * @param[in] ctx Compilation context. |
| 485 | * @param[in] ext_p Parsed extension instance. |
| 486 | * @param[in,out] ext Prepared compiled extension instance. |
| 487 | * @param[in] parent Extension instance parent. |
| 488 | * @param[in] parent_type Extension instance parent type. |
| 489 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 490 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 491 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 492 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
| 493 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 494 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 495 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 496 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 497 | size_t u; |
| 498 | LY_ARRAY_SIZE_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 499 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 500 | |
| 501 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 502 | ext->insubstmt = ext_p->insubstmt; |
| 503 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 504 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 505 | ext->parent = parent; |
| 506 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 507 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 508 | 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] | 509 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 510 | /* get module where the extension definition should be placed */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 511 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u); |
| 512 | if (ext_p->yin) { |
| 513 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 514 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 515 | * YANG (import) prefix must be done here. */ |
| 516 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 517 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 518 | u = 0; |
| 519 | } else if (ctx->mod_def->compiled) { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 520 | LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) { |
| 521 | if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) { |
| 522 | char *s; |
| 523 | asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]); |
| 524 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
| 525 | u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */ |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | if (!prefixed_name) { |
| 531 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 532 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 533 | goto cleanup; |
| 534 | } |
| 535 | } else { |
| 536 | prefixed_name = ext_p->name; |
| 537 | } |
| 538 | lysc_update_path(ctx, NULL, prefixed_name); |
| 539 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 540 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 541 | 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] | 542 | if (!ext_mod) { |
| 543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 544 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 545 | goto cleanup; |
| 546 | } else if (!ext_mod->parsed->extensions) { |
| 547 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 548 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 549 | prefixed_name, ext_mod->name); |
| 550 | goto cleanup; |
| 551 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 552 | } |
| 553 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 554 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 555 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 556 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 557 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 558 | /* compile extension definition and assign it */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 559 | 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] | 560 | break; |
| 561 | } |
| 562 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 563 | if (!ext->def) { |
| 564 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 565 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 566 | goto cleanup; |
| 567 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 568 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 569 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 570 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 571 | * 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] | 572 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 573 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 574 | struct lysp_stmt *stmt = NULL; |
| 575 | |
| 576 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 577 | /* ... argument was the first XML child element */ |
| 578 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 579 | /* TODO check namespace of the statement */ |
| 580 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 581 | stmt = ext_p->child; |
| 582 | } |
| 583 | } |
| 584 | } else { |
| 585 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 586 | * with LYS_YIN_ATTR flag */ |
| 587 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 588 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 589 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 590 | break; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | if (!stmt) { |
| 595 | /* missing extension's argument */ |
| 596 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 597 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 598 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 599 | |
| 600 | } |
| 601 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 602 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 603 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 604 | if (prefixed_name != ext_p->name) { |
| 605 | lydict_remove(ctx->ctx, ext_p->name); |
| 606 | ext_p->name = prefixed_name; |
| 607 | if (!ext_p->argument && ext->argument) { |
| 608 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 609 | } |
| 610 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 611 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 612 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 613 | if (ext->argument) { |
| 614 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 615 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 616 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 617 | if (ext->argument) { |
| 618 | lysc_update_path(ctx, NULL, NULL); |
| 619 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 620 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 621 | ext_p->compiled = ext; |
| 622 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 623 | ret = LY_SUCCESS; |
| 624 | cleanup: |
| 625 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 626 | lydict_remove(ctx->ctx, prefixed_name); |
| 627 | } |
| 628 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 629 | lysc_update_path(ctx, NULL, NULL); |
| 630 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 631 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 632 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 636 | * @brief Compile information from the if-feature statement |
| 637 | * @param[in] ctx Compile context. |
| 638 | * @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] | 639 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 640 | * @return LY_ERR value. |
| 641 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 642 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 643 | 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] | 644 | { |
| 645 | const char *c = *value; |
| 646 | int r, rc = EXIT_FAILURE; |
| 647 | int i, j, last_not, checkversion = 0; |
| 648 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 649 | uint8_t op; |
| 650 | struct iff_stack stack = {0, 0, NULL}; |
| 651 | struct lysc_feature *f; |
| 652 | |
| 653 | assert(c); |
| 654 | |
| 655 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 656 | for (i = j = last_not = 0; c[i]; i++) { |
| 657 | if (c[i] == '(') { |
| 658 | j++; |
| 659 | checkversion = 1; |
| 660 | continue; |
| 661 | } else if (c[i] == ')') { |
| 662 | j--; |
| 663 | continue; |
| 664 | } else if (isspace(c[i])) { |
| 665 | checkversion = 1; |
| 666 | continue; |
| 667 | } |
| 668 | |
| 669 | 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] | 670 | int sp; |
| 671 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 672 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 673 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 674 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 675 | return LY_EVALID; |
| 676 | } else if (!isspace(c[i + r])) { |
| 677 | /* feature name starting with the not/and/or */ |
| 678 | last_not = 0; |
| 679 | f_size++; |
| 680 | } else if (c[i] == 'n') { /* not operation */ |
| 681 | if (last_not) { |
| 682 | /* double not */ |
| 683 | expr_size = expr_size - 2; |
| 684 | last_not = 0; |
| 685 | } else { |
| 686 | last_not = 1; |
| 687 | } |
| 688 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 689 | if (f_exp != f_size) { |
| 690 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 691 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 692 | return LY_EVALID; |
| 693 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 694 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 695 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 696 | /* not a not operation */ |
| 697 | last_not = 0; |
| 698 | } |
| 699 | i += r; |
| 700 | } else { |
| 701 | f_size++; |
| 702 | last_not = 0; |
| 703 | } |
| 704 | expr_size++; |
| 705 | |
| 706 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 707 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 708 | i--; |
| 709 | break; |
| 710 | } |
| 711 | i++; |
| 712 | } |
| 713 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 714 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 715 | /* not matching count of ( and ) */ |
| 716 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 717 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 718 | return LY_EVALID; |
| 719 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 720 | if (f_exp != f_size) { |
| 721 | /* features do not match the needed arguments for the logical operations */ |
| 722 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 723 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 724 | "the required number of operands for the operations.", *value); |
| 725 | return LY_EVALID; |
| 726 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 727 | |
| 728 | if (checkversion || expr_size > 1) { |
| 729 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 730 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 731 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 732 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 733 | return LY_EVALID; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /* allocate the memory */ |
| 738 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 739 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 740 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 741 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 742 | |
| 743 | stack.size = expr_size; |
| 744 | f_size--; expr_size--; /* used as indexes from now */ |
| 745 | |
| 746 | for (i--; i >= 0; i--) { |
| 747 | if (c[i] == ')') { |
| 748 | /* push it on stack */ |
| 749 | iff_stack_push(&stack, LYS_IFF_RP); |
| 750 | continue; |
| 751 | } else if (c[i] == '(') { |
| 752 | /* pop from the stack into result all operators until ) */ |
| 753 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 754 | iff_setop(iff->expr, op, expr_size--); |
| 755 | } |
| 756 | continue; |
| 757 | } else if (isspace(c[i])) { |
| 758 | continue; |
| 759 | } |
| 760 | |
| 761 | /* end of operator or operand -> find beginning and get what is it */ |
| 762 | j = i + 1; |
| 763 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 764 | i--; |
| 765 | } |
| 766 | i++; /* go back by one step */ |
| 767 | |
| 768 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 769 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 770 | /* double not */ |
| 771 | iff_stack_pop(&stack); |
| 772 | } else { |
| 773 | /* not has the highest priority, so do not pop from the stack |
| 774 | * as in case of AND and OR */ |
| 775 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 776 | } |
| 777 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 778 | /* as for OR - pop from the stack all operators with the same or higher |
| 779 | * priority and store them to the result, then push the AND to the stack */ |
| 780 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 781 | op = iff_stack_pop(&stack); |
| 782 | iff_setop(iff->expr, op, expr_size--); |
| 783 | } |
| 784 | iff_stack_push(&stack, LYS_IFF_AND); |
| 785 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 786 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 787 | op = iff_stack_pop(&stack); |
| 788 | iff_setop(iff->expr, op, expr_size--); |
| 789 | } |
| 790 | iff_stack_push(&stack, LYS_IFF_OR); |
| 791 | } else { |
| 792 | /* feature name, length is j - i */ |
| 793 | |
| 794 | /* add it to the expression */ |
| 795 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 796 | |
| 797 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 798 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 799 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 800 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 801 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 802 | iff->features[f_size] = f; |
| 803 | LY_ARRAY_INCREMENT(iff->features); |
| 804 | f_size--; |
| 805 | } |
| 806 | } |
| 807 | while (stack.index) { |
| 808 | op = iff_stack_pop(&stack); |
| 809 | iff_setop(iff->expr, op, expr_size--); |
| 810 | } |
| 811 | |
| 812 | if (++expr_size || ++f_size) { |
| 813 | /* not all expected operators and operands found */ |
| 814 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 815 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 816 | rc = LY_EINT; |
| 817 | } else { |
| 818 | rc = LY_SUCCESS; |
| 819 | } |
| 820 | |
| 821 | error: |
| 822 | /* cleanup */ |
| 823 | iff_stack_clean(&stack); |
| 824 | |
| 825 | return rc; |
| 826 | } |
| 827 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 828 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 829 | * @brief Get the XPath context node for the given schema node. |
| 830 | * @param[in] start The schema node where the XPath expression appears. |
| 831 | * @return The context node to evaluate XPath expression in given schema node. |
| 832 | * @return NULL in case the context node is the root node. |
| 833 | */ |
| 834 | static struct lysc_node * |
| 835 | lysc_xpath_context(struct lysc_node *start) |
| 836 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 837 | 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] | 838 | start = start->parent); |
| 839 | return start; |
| 840 | } |
| 841 | |
| 842 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 843 | * @brief Compile information from the when statement |
| 844 | * @param[in] ctx Compile context. |
| 845 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 846 | * @param[in] flags Flags of the node with the "when" defiition. |
| 847 | * @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] | 848 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 849 | * @return LY_ERR value. |
| 850 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 851 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 852 | 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] | 853 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 854 | LY_ERR ret = LY_SUCCESS; |
| 855 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 856 | *when = calloc(1, sizeof **when); |
| 857 | (*when)->refcount = 1; |
| 858 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 859 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 860 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 861 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 862 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 863 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 864 | 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] | 865 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 866 | |
| 867 | done: |
| 868 | return ret; |
| 869 | } |
| 870 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 871 | /** |
| 872 | * @brief Compile information from the must statement |
| 873 | * @param[in] ctx Compile context. |
| 874 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 875 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 876 | * @return LY_ERR value. |
| 877 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 878 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 879 | 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] | 880 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 881 | LY_ERR ret = LY_SUCCESS; |
| 882 | |
| 883 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 884 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 885 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 886 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 887 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 888 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 889 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 890 | 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] | 891 | |
| 892 | done: |
| 893 | return ret; |
| 894 | } |
| 895 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 896 | /** |
| 897 | * @brief Compile information from the import statement |
| 898 | * @param[in] ctx Compile context. |
| 899 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 900 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 901 | * @return LY_ERR value. |
| 902 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 903 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 904 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 905 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 906 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 907 | LY_ERR ret = LY_SUCCESS; |
| 908 | |
| 909 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 910 | COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 911 | imp->module = imp_p->module; |
| 912 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 913 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 914 | * 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] | 915 | * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 916 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 917 | /* try to use filepath if present */ |
| 918 | if (imp->module->filepath) { |
| 919 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 920 | !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 921 | if (mod != imp->module) { |
| 922 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 923 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 924 | mod = NULL; |
| 925 | } |
| 926 | } |
| 927 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 928 | if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 929 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 930 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 931 | return LY_ENOTFOUND; |
| 932 | } |
| 933 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | done: |
| 937 | return ret; |
| 938 | } |
| 939 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 940 | /** |
| 941 | * @brief Compile information from the identity statement |
| 942 | * |
| 943 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 944 | * |
| 945 | * @param[in] ctx Compile context. |
| 946 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 947 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 948 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 949 | * @return LY_ERR value. |
| 950 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 951 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 952 | lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, struct lysc_ident *idents, struct lysc_ident *ident) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 953 | { |
| 954 | unsigned int u; |
| 955 | LY_ERR ret = LY_SUCCESS; |
| 956 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 957 | lysc_update_path(ctx, NULL, ident_p->name); |
| 958 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 959 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 960 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 961 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 962 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 963 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 964 | COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 965 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 966 | COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 967 | ident->flags = ident_p->flags; |
| 968 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 969 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 970 | done: |
| 971 | return ret; |
| 972 | } |
| 973 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 974 | /** |
| 975 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 976 | * |
| 977 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 978 | * |
| 979 | * @param[in] ctx Compile context for logging. |
| 980 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 981 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 982 | * @return LY_SUCCESS if everything is ok. |
| 983 | * @return LY_EVALID if the identity is derived from itself. |
| 984 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 985 | static LY_ERR |
| 986 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 987 | { |
| 988 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 989 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 990 | struct ly_set recursion = {0}; |
| 991 | struct lysc_ident *drv; |
| 992 | |
| 993 | if (!derived) { |
| 994 | return LY_SUCCESS; |
| 995 | } |
| 996 | |
| 997 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 998 | if (ident == derived[u]) { |
| 999 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1000 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1001 | goto cleanup; |
| 1002 | } |
| 1003 | ly_set_add(&recursion, derived[u], 0); |
| 1004 | } |
| 1005 | |
| 1006 | for (v = 0; v < recursion.count; ++v) { |
| 1007 | drv = recursion.objs[v]; |
| 1008 | if (!drv->derived) { |
| 1009 | continue; |
| 1010 | } |
| 1011 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 1012 | if (ident == drv->derived[u]) { |
| 1013 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1014 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1015 | goto cleanup; |
| 1016 | } |
| 1017 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1018 | } |
| 1019 | } |
| 1020 | ret = LY_SUCCESS; |
| 1021 | |
| 1022 | cleanup: |
| 1023 | ly_set_erase(&recursion, NULL); |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1027 | /** |
| 1028 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1029 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1030 | * 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] | 1031 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1032 | * to distinguish these two use cases. |
| 1033 | * |
| 1034 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1035 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 1036 | * @param[in] ident Referencing identity to work with. |
| 1037 | * @param[in] bases Array of bases of identityref to fill in. |
| 1038 | * @return LY_ERR value. |
| 1039 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1040 | static LY_ERR |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1041 | lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1042 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1043 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1044 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1045 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1046 | struct lysc_ident **idref; |
| 1047 | |
| 1048 | assert(ident || bases); |
| 1049 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1050 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1051 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1052 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1053 | return LY_EVALID; |
| 1054 | } |
| 1055 | |
| 1056 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 1057 | s = strchr(bases_p[u], ':'); |
| 1058 | if (s) { |
| 1059 | /* prefixed identity */ |
| 1060 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1061 | 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] | 1062 | } else { |
| 1063 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1064 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1065 | } |
| 1066 | if (!mod) { |
| 1067 | if (ident) { |
| 1068 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1069 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1070 | } else { |
| 1071 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1072 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1073 | } |
| 1074 | return LY_EVALID; |
| 1075 | } |
| 1076 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1077 | if (mod->compiled && mod->compiled->identities) { |
| 1078 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 1079 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1080 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1081 | if (ident == &mod->compiled->identities[v]) { |
| 1082 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1083 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1084 | return LY_EVALID; |
| 1085 | } |
| 1086 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1087 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1088 | LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1089 | *idref = ident; |
| 1090 | } else { |
| 1091 | /* we have match! store the found identity */ |
| 1092 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1093 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1094 | } |
| 1095 | break; |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | if (!idref || !(*idref)) { |
| 1100 | if (ident) { |
| 1101 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1102 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1103 | } else { |
| 1104 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1105 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1106 | } |
| 1107 | return LY_EVALID; |
| 1108 | } |
| 1109 | } |
| 1110 | return LY_SUCCESS; |
| 1111 | } |
| 1112 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1113 | /** |
| 1114 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1115 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1116 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1117 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1118 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1119 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1120 | static LY_ERR |
| 1121 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1122 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1123 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1124 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1125 | for (u = 0; u < LY_ARRAY_SIZE(idents_p); ++u) { |
| 1126 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1127 | continue; |
| 1128 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1129 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1130 | 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] | 1131 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1132 | } |
| 1133 | return LY_SUCCESS; |
| 1134 | } |
| 1135 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1136 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1137 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1138 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1139 | LY_ARRAY_SIZE_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1140 | struct lysc_ctx context = {0}; |
| 1141 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1142 | assert(ctx_sc || ctx); |
| 1143 | |
| 1144 | if (!ctx_sc) { |
| 1145 | context.ctx = ctx; |
| 1146 | context.mod = module; |
| 1147 | context.path_len = 1; |
| 1148 | context.path[0] = '/'; |
| 1149 | ctx_sc = &context; |
| 1150 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1151 | |
| 1152 | if (!features_p) { |
| 1153 | return LY_SUCCESS; |
| 1154 | } |
| 1155 | if (*features) { |
| 1156 | offset = LY_ARRAY_SIZE(*features); |
| 1157 | } |
| 1158 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1159 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 1160 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1161 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1162 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1163 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1164 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1165 | 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] | 1166 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1167 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1168 | 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] | 1169 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1170 | (*features)[offset + u].module = ctx_sc->mod; |
| 1171 | |
| 1172 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1173 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1174 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1175 | |
| 1176 | return LY_SUCCESS; |
| 1177 | } |
| 1178 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1179 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1180 | * @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] | 1181 | * |
| 1182 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1183 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1184 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1185 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1186 | * being currently processed). |
| 1187 | * @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] | 1188 | * @return LY_SUCCESS if everything is ok. |
| 1189 | * @return LY_EVALID if the feature references indirectly itself. |
| 1190 | */ |
| 1191 | static LY_ERR |
| 1192 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1193 | { |
| 1194 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1195 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1196 | struct ly_set recursion = {0}; |
| 1197 | struct lysc_feature *drv; |
| 1198 | |
| 1199 | if (!depfeatures) { |
| 1200 | return LY_SUCCESS; |
| 1201 | } |
| 1202 | |
| 1203 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1204 | if (feature == depfeatures[u]) { |
| 1205 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1206 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1207 | goto cleanup; |
| 1208 | } |
| 1209 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1210 | } |
| 1211 | |
| 1212 | for (v = 0; v < recursion.count; ++v) { |
| 1213 | drv = recursion.objs[v]; |
| 1214 | if (!drv->depfeatures) { |
| 1215 | continue; |
| 1216 | } |
| 1217 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1218 | if (feature == drv->depfeatures[u]) { |
| 1219 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1220 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1221 | goto cleanup; |
| 1222 | } |
| 1223 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1224 | } |
| 1225 | } |
| 1226 | ret = LY_SUCCESS; |
| 1227 | |
| 1228 | cleanup: |
| 1229 | ly_set_erase(&recursion, NULL); |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
| 1233 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1234 | * @brief Create pre-compiled features array. |
| 1235 | * |
| 1236 | * See lys_feature_precompile() for more details. |
| 1237 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1238 | * @param[in] ctx Compile context. |
| 1239 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1240 | * @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] | 1241 | * @return LY_ERR value. |
| 1242 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1243 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1244 | 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] | 1245 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1246 | LY_ARRAY_SIZE_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1247 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1248 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1249 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1250 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1251 | /* find the preprecompiled feature */ |
| 1252 | LY_ARRAY_FOR(features, x) { |
| 1253 | if (strcmp(features[x].name, feature_p->name)) { |
| 1254 | continue; |
| 1255 | } |
| 1256 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1257 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1258 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1259 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1260 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1261 | 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] | 1262 | 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] | 1263 | if (feature->iffeatures) { |
| 1264 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1265 | if (feature->iffeatures[u].features) { |
| 1266 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1267 | /* check for circular dependency - direct reference first,... */ |
| 1268 | if (feature == feature->iffeatures[u].features[v]) { |
| 1269 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1270 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1271 | return LY_EVALID; |
| 1272 | } |
| 1273 | /* ... and indirect circular reference */ |
| 1274 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1275 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1276 | /* add itself into the dependants list */ |
| 1277 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1278 | *df = feature; |
| 1279 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1280 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1281 | } |
| 1282 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1283 | lysc_update_path(ctx, NULL, NULL); |
| 1284 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1285 | done: |
| 1286 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1287 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1288 | |
| 1289 | LOGINT(ctx->ctx); |
| 1290 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1291 | } |
| 1292 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1293 | /** |
| 1294 | * @brief Revert compiled list of features back to the precompiled state. |
| 1295 | * |
| 1296 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1297 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1298 | * |
| 1299 | * @param[in] ctx Compilation context. |
| 1300 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1301 | * and supposed to hold the off_features list. |
| 1302 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1303 | static void |
| 1304 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1305 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1306 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1307 | |
| 1308 | /* keep the off_features list until the complete lys_module is freed */ |
| 1309 | mod->off_features = mod->compiled->features; |
| 1310 | mod->compiled->features = NULL; |
| 1311 | |
| 1312 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1313 | * which may points into the data being freed here */ |
| 1314 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1315 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1316 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1317 | } |
| 1318 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1319 | mod->off_features[u].iffeatures = NULL; |
| 1320 | |
| 1321 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1322 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1323 | } |
| 1324 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1325 | mod->off_features[u].exts = NULL; |
| 1326 | } |
| 1327 | } |
| 1328 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1329 | /** |
| 1330 | * @brief Validate and normalize numeric value from a range definition. |
| 1331 | * @param[in] ctx Compile context. |
| 1332 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1333 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1334 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1335 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1336 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1337 | * @param[in] value String value of the range boundary. |
| 1338 | * @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. |
| 1339 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1340 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1341 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1342 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1343 | 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] | 1344 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1345 | size_t fraction = 0, size; |
| 1346 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1347 | *len = 0; |
| 1348 | |
| 1349 | assert(value); |
| 1350 | /* parse value */ |
| 1351 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1352 | return LY_EVALID; |
| 1353 | } |
| 1354 | |
| 1355 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1356 | ++(*len); |
| 1357 | } |
| 1358 | |
| 1359 | while (isdigit(value[*len])) { |
| 1360 | ++(*len); |
| 1361 | } |
| 1362 | |
| 1363 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1364 | if (basetype == LY_TYPE_DEC64) { |
| 1365 | goto decimal; |
| 1366 | } else { |
| 1367 | *valcopy = strndup(value, *len); |
| 1368 | return LY_SUCCESS; |
| 1369 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1370 | } |
| 1371 | fraction = *len; |
| 1372 | |
| 1373 | ++(*len); |
| 1374 | while (isdigit(value[*len])) { |
| 1375 | ++(*len); |
| 1376 | } |
| 1377 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1378 | if (basetype == LY_TYPE_DEC64) { |
| 1379 | decimal: |
| 1380 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1381 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1382 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1383 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1384 | *len, value, frdigits); |
| 1385 | return LY_EINVAL; |
| 1386 | } |
| 1387 | if (fraction) { |
| 1388 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1389 | } else { |
| 1390 | size = (*len) + frdigits + 1; |
| 1391 | } |
| 1392 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1393 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1394 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1395 | (*valcopy)[size - 1] = '\0'; |
| 1396 | if (fraction) { |
| 1397 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1398 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1399 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1400 | } else { |
| 1401 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1402 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1403 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1404 | } |
| 1405 | return LY_SUCCESS; |
| 1406 | } |
| 1407 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1408 | /** |
| 1409 | * @brief Check that values in range are in ascendant order. |
| 1410 | * @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] | 1411 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1412 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1413 | * @param[in] value Current value to check. |
| 1414 | * @param[in] prev_value The last seen value. |
| 1415 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1416 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1417 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1418 | 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] | 1419 | { |
| 1420 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1421 | 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] | 1422 | return LY_EEXIST; |
| 1423 | } |
| 1424 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1425 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1426 | return LY_EEXIST; |
| 1427 | } |
| 1428 | } |
| 1429 | return LY_SUCCESS; |
| 1430 | } |
| 1431 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1432 | /** |
| 1433 | * @brief Set min/max value of the range part. |
| 1434 | * @param[in] ctx Compile context. |
| 1435 | * @param[in] part Range part structure to fill. |
| 1436 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1437 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1438 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1439 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1440 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1441 | * @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] | 1442 | * @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] | 1443 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1444 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1445 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1446 | * frdigits value), LY_EMEM. |
| 1447 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1448 | static LY_ERR |
| 1449 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1450 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1451 | { |
| 1452 | LY_ERR ret = LY_SUCCESS; |
| 1453 | char *valcopy = NULL; |
| 1454 | size_t len; |
| 1455 | |
| 1456 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1457 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1458 | LY_CHECK_GOTO(ret, finalize); |
| 1459 | } |
| 1460 | if (!valcopy && base_range) { |
| 1461 | if (max) { |
| 1462 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1463 | } else { |
| 1464 | part->min_64 = base_range->parts[0].min_64; |
| 1465 | } |
| 1466 | if (!first) { |
| 1467 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1468 | } |
| 1469 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1473 | case LY_TYPE_INT8: /* range */ |
| 1474 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1475 | 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] | 1476 | } else if (max) { |
| 1477 | part->max_64 = INT64_C(127); |
| 1478 | } else { |
| 1479 | part->min_64 = INT64_C(-128); |
| 1480 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1481 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1482 | 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] | 1483 | } |
| 1484 | break; |
| 1485 | case LY_TYPE_INT16: /* range */ |
| 1486 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1487 | 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] | 1488 | } else if (max) { |
| 1489 | part->max_64 = INT64_C(32767); |
| 1490 | } else { |
| 1491 | part->min_64 = INT64_C(-32768); |
| 1492 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1493 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1494 | 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] | 1495 | } |
| 1496 | break; |
| 1497 | case LY_TYPE_INT32: /* range */ |
| 1498 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1499 | 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] | 1500 | } else if (max) { |
| 1501 | part->max_64 = INT64_C(2147483647); |
| 1502 | } else { |
| 1503 | part->min_64 = INT64_C(-2147483648); |
| 1504 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1505 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1506 | 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] | 1507 | } |
| 1508 | break; |
| 1509 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1510 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1511 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1512 | 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] | 1513 | max ? &part->max_64 : &part->min_64); |
| 1514 | } else if (max) { |
| 1515 | part->max_64 = INT64_C(9223372036854775807); |
| 1516 | } else { |
| 1517 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1518 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1519 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1520 | 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] | 1521 | } |
| 1522 | break; |
| 1523 | case LY_TYPE_UINT8: /* range */ |
| 1524 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1525 | 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] | 1526 | } else if (max) { |
| 1527 | part->max_u64 = UINT64_C(255); |
| 1528 | } else { |
| 1529 | part->min_u64 = UINT64_C(0); |
| 1530 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1531 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1532 | 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] | 1533 | } |
| 1534 | break; |
| 1535 | case LY_TYPE_UINT16: /* range */ |
| 1536 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1537 | 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] | 1538 | } else if (max) { |
| 1539 | part->max_u64 = UINT64_C(65535); |
| 1540 | } else { |
| 1541 | part->min_u64 = UINT64_C(0); |
| 1542 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1543 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1544 | 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] | 1545 | } |
| 1546 | break; |
| 1547 | case LY_TYPE_UINT32: /* range */ |
| 1548 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1549 | 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] | 1550 | } else if (max) { |
| 1551 | part->max_u64 = UINT64_C(4294967295); |
| 1552 | } else { |
| 1553 | part->min_u64 = UINT64_C(0); |
| 1554 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1555 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1556 | 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] | 1557 | } |
| 1558 | break; |
| 1559 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1560 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1561 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1562 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1563 | 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] | 1564 | } else if (max) { |
| 1565 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1566 | } else { |
| 1567 | part->min_u64 = UINT64_C(0); |
| 1568 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1569 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1570 | 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] | 1571 | } |
| 1572 | break; |
| 1573 | default: |
| 1574 | LOGINT(ctx->ctx); |
| 1575 | ret = LY_EINT; |
| 1576 | } |
| 1577 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1578 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1579 | if (ret == LY_EDENIED) { |
| 1580 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1581 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1582 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1583 | } else if (ret == LY_EVALID) { |
| 1584 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1585 | "Invalid %s restriction - invalid value \"%s\".", |
| 1586 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1587 | } else if (ret == LY_EEXIST) { |
| 1588 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1589 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1590 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1591 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1592 | } else if (!ret && value) { |
| 1593 | *value = *value + len; |
| 1594 | } |
| 1595 | free(valcopy); |
| 1596 | return ret; |
| 1597 | } |
| 1598 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1599 | /** |
| 1600 | * @brief Compile the parsed range restriction. |
| 1601 | * @param[in] ctx Compile context. |
| 1602 | * @param[in] range_p Parsed range structure to compile. |
| 1603 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1604 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1605 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1606 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1607 | * range restriction must be more restrictive than the base_range. |
| 1608 | * @param[in,out] range Pointer to the created current range structure. |
| 1609 | * @return LY_ERR value. |
| 1610 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1611 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1612 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1613 | struct lysc_range *base_range, struct lysc_range **range) |
| 1614 | { |
| 1615 | LY_ERR ret = LY_EVALID; |
| 1616 | const char *expr; |
| 1617 | struct lysc_range_part *parts = NULL, *part; |
| 1618 | int range_expected = 0, uns; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1619 | LY_ARRAY_SIZE_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1620 | |
| 1621 | assert(range); |
| 1622 | assert(range_p); |
| 1623 | |
| 1624 | expr = range_p->arg; |
| 1625 | while(1) { |
| 1626 | if (isspace(*expr)) { |
| 1627 | ++expr; |
| 1628 | } else if (*expr == '\0') { |
| 1629 | if (range_expected) { |
| 1630 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1631 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1632 | length_restr ? "length" : "range", range_p->arg); |
| 1633 | goto cleanup; |
| 1634 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1635 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1636 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1637 | length_restr ? "length" : "range", range_p->arg); |
| 1638 | goto cleanup; |
| 1639 | } |
| 1640 | parts_done++; |
| 1641 | break; |
| 1642 | } else if (!strncmp(expr, "min", 3)) { |
| 1643 | if (parts) { |
| 1644 | /* min cannot be used elsewhere than in the first part */ |
| 1645 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1646 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1647 | expr - range_p->arg, range_p->arg); |
| 1648 | goto cleanup; |
| 1649 | } |
| 1650 | expr += 3; |
| 1651 | |
| 1652 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1653 | 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] | 1654 | part->max_64 = part->min_64; |
| 1655 | } else if (*expr == '|') { |
| 1656 | if (!parts || range_expected) { |
| 1657 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1658 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1659 | goto cleanup; |
| 1660 | } |
| 1661 | expr++; |
| 1662 | parts_done++; |
| 1663 | /* process next part of the expression */ |
| 1664 | } else if (!strncmp(expr, "..", 2)) { |
| 1665 | expr += 2; |
| 1666 | while (isspace(*expr)) { |
| 1667 | expr++; |
| 1668 | } |
| 1669 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1670 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1671 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1672 | goto cleanup; |
| 1673 | } |
| 1674 | /* continue expecting the upper boundary */ |
| 1675 | range_expected = 1; |
| 1676 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1677 | /* number */ |
| 1678 | if (range_expected) { |
| 1679 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1680 | 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] | 1681 | range_expected = 0; |
| 1682 | } else { |
| 1683 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1684 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1685 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1686 | part->max_64 = part->min_64; |
| 1687 | } |
| 1688 | |
| 1689 | /* continue with possible another expression part */ |
| 1690 | } else if (!strncmp(expr, "max", 3)) { |
| 1691 | expr += 3; |
| 1692 | while (isspace(*expr)) { |
| 1693 | expr++; |
| 1694 | } |
| 1695 | if (*expr != '\0') { |
| 1696 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1697 | length_restr ? "length" : "range", expr); |
| 1698 | goto cleanup; |
| 1699 | } |
| 1700 | if (range_expected) { |
| 1701 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1702 | 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] | 1703 | range_expected = 0; |
| 1704 | } else { |
| 1705 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1706 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1707 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1708 | part->min_64 = part->max_64; |
| 1709 | } |
| 1710 | } else { |
| 1711 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1712 | length_restr ? "length" : "range", expr); |
| 1713 | goto cleanup; |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | /* check with the previous range/length restriction */ |
| 1718 | if (base_range) { |
| 1719 | switch (basetype) { |
| 1720 | case LY_TYPE_BINARY: |
| 1721 | case LY_TYPE_UINT8: |
| 1722 | case LY_TYPE_UINT16: |
| 1723 | case LY_TYPE_UINT32: |
| 1724 | case LY_TYPE_UINT64: |
| 1725 | case LY_TYPE_STRING: |
| 1726 | uns = 1; |
| 1727 | break; |
| 1728 | case LY_TYPE_DEC64: |
| 1729 | case LY_TYPE_INT8: |
| 1730 | case LY_TYPE_INT16: |
| 1731 | case LY_TYPE_INT32: |
| 1732 | case LY_TYPE_INT64: |
| 1733 | uns = 0; |
| 1734 | break; |
| 1735 | default: |
| 1736 | LOGINT(ctx->ctx); |
| 1737 | ret = LY_EINT; |
| 1738 | goto cleanup; |
| 1739 | } |
| 1740 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1741 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1742 | goto baseerror; |
| 1743 | } |
| 1744 | /* current lower bound is not lower than the base */ |
| 1745 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1746 | /* base has single value */ |
| 1747 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1748 | /* both lower bounds are the same */ |
| 1749 | if (parts[u].min_64 != parts[u].max_64) { |
| 1750 | /* current continues with a range */ |
| 1751 | goto baseerror; |
| 1752 | } else { |
| 1753 | /* equal single values, move both forward */ |
| 1754 | ++v; |
| 1755 | continue; |
| 1756 | } |
| 1757 | } else { |
| 1758 | /* base is single value lower than current range, so the |
| 1759 | * value from base range is removed in the current, |
| 1760 | * move only base and repeat checking */ |
| 1761 | ++v; |
| 1762 | --u; |
| 1763 | continue; |
| 1764 | } |
| 1765 | } else { |
| 1766 | /* base is the range */ |
| 1767 | if (parts[u].min_64 == parts[u].max_64) { |
| 1768 | /* current is a single value */ |
| 1769 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1770 | /* current is behind the base range, so base range is omitted, |
| 1771 | * move the base and keep the current for further check */ |
| 1772 | ++v; |
| 1773 | --u; |
| 1774 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1775 | continue; |
| 1776 | } else { |
| 1777 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1778 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1779 | /* higher bound is higher than the current higher bound */ |
| 1780 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1781 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1782 | * continue with the same current, but move the base */ |
| 1783 | --u; |
| 1784 | ++v; |
| 1785 | continue; |
| 1786 | } |
| 1787 | /* current range starts within the base range but end behind it */ |
| 1788 | goto baseerror; |
| 1789 | } else { |
| 1790 | /* current range is smaller than the base, |
| 1791 | * move current, but stay with the base */ |
| 1792 | continue; |
| 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | } |
| 1797 | if (u != parts_done) { |
| 1798 | baseerror: |
| 1799 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1800 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1801 | length_restr ? "length" : "range", range_p->arg); |
| 1802 | goto cleanup; |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | if (!(*range)) { |
| 1807 | *range = calloc(1, sizeof **range); |
| 1808 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1809 | } |
| 1810 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1811 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1812 | if (range_p->eapptag) { |
| 1813 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1814 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1815 | } |
| 1816 | if (range_p->emsg) { |
| 1817 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1818 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1819 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1820 | if (range_p->dsc) { |
| 1821 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1822 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1823 | } |
| 1824 | if (range_p->ref) { |
| 1825 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1826 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1827 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1828 | /* extensions are taken only from the last range by the caller */ |
| 1829 | |
| 1830 | (*range)->parts = parts; |
| 1831 | parts = NULL; |
| 1832 | ret = LY_SUCCESS; |
| 1833 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1834 | LY_ARRAY_FREE(parts); |
| 1835 | |
| 1836 | return ret; |
| 1837 | } |
| 1838 | |
| 1839 | /** |
| 1840 | * @brief Checks pattern syntax. |
| 1841 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1842 | * @param[in] ctx Context. |
| 1843 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1844 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1845 | * @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] | 1846 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1847 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1848 | LY_ERR |
| 1849 | 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] | 1850 | { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame^] | 1851 | int idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1852 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1853 | int err_code; |
| 1854 | const char *orig_ptr; |
| 1855 | PCRE2_SIZE err_offset; |
| 1856 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1857 | #define URANGE_LEN 19 |
| 1858 | char *ublock2urange[][2] = { |
| 1859 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1860 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1861 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1862 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1863 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1864 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1865 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1866 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1867 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1868 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1869 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1870 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1871 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1872 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1873 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1874 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1875 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1876 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1877 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1878 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1879 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1880 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1881 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1882 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1883 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1884 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1885 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1886 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1887 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1888 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1889 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1890 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1891 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1892 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1893 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1894 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1895 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1896 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1897 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1898 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1899 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1900 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1901 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1902 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1903 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1904 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1905 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1906 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1907 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1908 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1909 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1910 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1911 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1912 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1913 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1914 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1915 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1916 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1917 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1918 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1919 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1920 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1921 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1922 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1923 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1924 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1925 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1926 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1927 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1928 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1929 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1930 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1931 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1932 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1933 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1934 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1935 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1936 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1937 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1938 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1939 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1940 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1941 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1942 | {NULL, NULL} |
| 1943 | }; |
| 1944 | |
| 1945 | /* adjust the expression to a Perl equivalent |
| 1946 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1947 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame^] | 1948 | /* allocate space for the transformed pattern */ |
| 1949 | size = strlen(pattern) + 1; |
| 1950 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1951 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1952 | perl_regex[0] = '\0'; |
| 1953 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame^] | 1954 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 1955 | brack = 0; |
| 1956 | idx = 0; |
| 1957 | orig_ptr = pattern; |
| 1958 | while (orig_ptr[0]) { |
| 1959 | switch (orig_ptr[0]) { |
| 1960 | case '$': |
| 1961 | case '^': |
| 1962 | if (!brack) { |
| 1963 | /* make space for the extra character */ |
| 1964 | ++size; |
| 1965 | perl_regex = ly_realloc(perl_regex, size); |
| 1966 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1967 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame^] | 1968 | /* print escape slash */ |
| 1969 | perl_regex[idx] = '\\'; |
| 1970 | ++idx; |
| 1971 | } |
| 1972 | break; |
| 1973 | case '[': |
| 1974 | /* must not be escaped */ |
| 1975 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 1976 | ++brack; |
| 1977 | } |
| 1978 | break; |
| 1979 | case ']': |
| 1980 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 1981 | /* pattern was checked and compiled already */ |
| 1982 | assert(brack); |
| 1983 | --brack; |
| 1984 | } |
| 1985 | break; |
| 1986 | default: |
| 1987 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1988 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame^] | 1989 | |
| 1990 | /* copy char */ |
| 1991 | perl_regex[idx] = orig_ptr[0]; |
| 1992 | |
| 1993 | ++idx; |
| 1994 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1995 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame^] | 1996 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1997 | |
| 1998 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1999 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2000 | start = ptr - perl_regex; |
| 2001 | |
| 2002 | ptr = strchr(ptr, '}'); |
| 2003 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2004 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2005 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2006 | free(perl_regex); |
| 2007 | return LY_EVALID; |
| 2008 | } |
| 2009 | end = (ptr - perl_regex) + 1; |
| 2010 | |
| 2011 | /* need more space */ |
| 2012 | if (end - start < URANGE_LEN) { |
| 2013 | 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] | 2014 | 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] | 2015 | } |
| 2016 | |
| 2017 | /* find our range */ |
| 2018 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2019 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2020 | break; |
| 2021 | } |
| 2022 | } |
| 2023 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2024 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2025 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2026 | free(perl_regex); |
| 2027 | return LY_EVALID; |
| 2028 | } |
| 2029 | |
| 2030 | /* 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^] | 2031 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2032 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame^] | 2033 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2034 | } |
| 2035 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame^] | 2036 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2037 | } |
| 2038 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame^] | 2039 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2040 | /* skip brackets */ |
| 2041 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2042 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2043 | } else { |
| 2044 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2045 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2050 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2051 | 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] | 2052 | &err_code, &err_offset, NULL); |
| 2053 | if (!code_local) { |
| 2054 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2055 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2056 | 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] | 2057 | free(perl_regex); |
| 2058 | return LY_EVALID; |
| 2059 | } |
| 2060 | free(perl_regex); |
| 2061 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2062 | if (code) { |
| 2063 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2064 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2065 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | return LY_SUCCESS; |
| 2069 | |
| 2070 | #undef URANGE_LEN |
| 2071 | } |
| 2072 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2073 | /** |
| 2074 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2075 | * @param[in] ctx Compile context. |
| 2076 | * @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] | 2077 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2078 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2079 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2080 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2081 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2082 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2083 | 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] | 2084 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2085 | { |
| 2086 | struct lysc_pattern **pattern; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2087 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2088 | LY_ERR ret = LY_SUCCESS; |
| 2089 | |
| 2090 | /* first, copy the patterns from the base type */ |
| 2091 | if (base_patterns) { |
| 2092 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2093 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2094 | } |
| 2095 | |
| 2096 | LY_ARRAY_FOR(patterns_p, u) { |
| 2097 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2098 | *pattern = calloc(1, sizeof **pattern); |
| 2099 | ++(*pattern)->refcount; |
| 2100 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2101 | 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] | 2102 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2103 | |
| 2104 | if (patterns_p[u].arg[0] == 0x15) { |
| 2105 | (*pattern)->inverted = 1; |
| 2106 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2107 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2108 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2109 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2110 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2111 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2112 | 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] | 2113 | } |
| 2114 | done: |
| 2115 | return ret; |
| 2116 | } |
| 2117 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2118 | /** |
| 2119 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2120 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2121 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2122 | 0 /* LY_TYPE_UNKNOWN */, |
| 2123 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2124 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2125 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2126 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2127 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2128 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2129 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2130 | 0 /* LY_TYPE_BOOL */, |
| 2131 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2132 | 0 /* LY_TYPE_EMPTY */, |
| 2133 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2134 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2135 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2136 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2137 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2138 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2139 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2140 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2141 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2142 | }; |
| 2143 | |
| 2144 | /** |
| 2145 | * @brief stringification of the YANG built-in data types |
| 2146 | */ |
| 2147 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2148 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2149 | "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] | 2150 | }; |
| 2151 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2152 | /** |
| 2153 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2154 | * @param[in] ctx Compile context. |
| 2155 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2156 | * @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] | 2157 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2158 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2159 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2160 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2161 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2162 | 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] | 2163 | 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] | 2164 | { |
| 2165 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2166 | LY_ARRAY_SIZE_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2167 | int32_t value = 0; |
| 2168 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2169 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2170 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2171 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2172 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2173 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2174 | return LY_EVALID; |
| 2175 | } |
| 2176 | |
| 2177 | LY_ARRAY_FOR(enums_p, u) { |
| 2178 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2179 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2180 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2181 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2182 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2183 | if (base_enums) { |
| 2184 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2185 | LY_ARRAY_FOR(base_enums, v) { |
| 2186 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2187 | break; |
| 2188 | } |
| 2189 | } |
| 2190 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 2191 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2192 | "Invalid %s - derived type adds new item \"%s\".", |
| 2193 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2194 | return LY_EVALID; |
| 2195 | } |
| 2196 | match = v; |
| 2197 | } |
| 2198 | |
| 2199 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2200 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2201 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2202 | e->value = (int32_t)enums_p[u].value; |
| 2203 | if (!u || e->value >= value) { |
| 2204 | value = e->value + 1; |
| 2205 | } |
| 2206 | /* check collision with other values */ |
| 2207 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2208 | if (e->value == (*enums)[v].value) { |
| 2209 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2210 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2211 | e->value, e->name, (*enums)[v].name); |
| 2212 | return LY_EVALID; |
| 2213 | } |
| 2214 | } |
| 2215 | } else if (base_enums) { |
| 2216 | /* inherit the assigned value */ |
| 2217 | e->value = base_enums[match].value; |
| 2218 | if (!u || e->value >= value) { |
| 2219 | value = e->value + 1; |
| 2220 | } |
| 2221 | } else { |
| 2222 | /* assign value automatically */ |
| 2223 | if (u && value == INT32_MIN) { |
| 2224 | /* counter overflow */ |
| 2225 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2226 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2227 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2228 | return LY_EVALID; |
| 2229 | } |
| 2230 | e->value = value++; |
| 2231 | } |
| 2232 | } else { /* LY_TYPE_BITS */ |
| 2233 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2234 | e->value = (int32_t)enums_p[u].value; |
| 2235 | if (!u || (uint32_t)e->value >= position) { |
| 2236 | position = (uint32_t)e->value + 1; |
| 2237 | } |
| 2238 | /* check collision with other values */ |
| 2239 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2240 | if (e->value == (*enums)[v].value) { |
| 2241 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2242 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2243 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2244 | return LY_EVALID; |
| 2245 | } |
| 2246 | } |
| 2247 | } else if (base_enums) { |
| 2248 | /* inherit the assigned value */ |
| 2249 | e->value = base_enums[match].value; |
| 2250 | if (!u || (uint32_t)e->value >= position) { |
| 2251 | position = (uint32_t)e->value + 1; |
| 2252 | } |
| 2253 | } else { |
| 2254 | /* assign value automatically */ |
| 2255 | if (u && position == 0) { |
| 2256 | /* counter overflow */ |
| 2257 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2258 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2259 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2260 | return LY_EVALID; |
| 2261 | } |
| 2262 | e->value = position++; |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | if (base_enums) { |
| 2267 | /* the assigned values must not change from the derived type */ |
| 2268 | if (e->value != base_enums[match].value) { |
| 2269 | if (basetype == LY_TYPE_ENUM) { |
| 2270 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2271 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2272 | e->name, base_enums[match].value, e->value); |
| 2273 | } else { |
| 2274 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2275 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2276 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2277 | } |
| 2278 | return LY_EVALID; |
| 2279 | } |
| 2280 | } |
| 2281 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2282 | 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] | 2283 | 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] | 2284 | |
| 2285 | if (basetype == LY_TYPE_BITS) { |
| 2286 | /* keep bits ordered by position */ |
| 2287 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2288 | if (v != u) { |
| 2289 | memcpy(&storage, e, sizeof *e); |
| 2290 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2291 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2292 | } |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | done: |
| 2297 | return ret; |
| 2298 | } |
| 2299 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2300 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2301 | for ((NODE) = (NODE)->parent; \ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 2302 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_RPC | LYS_ACTION)); \ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2303 | (NODE) = (NODE)->parent); \ |
| 2304 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2305 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2306 | TERM; \ |
| 2307 | } |
| 2308 | |
| 2309 | /** |
| 2310 | * @brief Validate the predicate(s) from the leafref path. |
| 2311 | * @param[in] ctx Compile context |
| 2312 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2313 | * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated. |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2314 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2315 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2316 | * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2317 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2318 | */ |
| 2319 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2320 | lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2321 | const struct lysc_node_list *context_node, const struct lys_module *path_context) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2322 | { |
| 2323 | LY_ERR ret = LY_EVALID; |
| 2324 | const struct lys_module *mod; |
| 2325 | const struct lysc_node *src_node, *dst_node; |
| 2326 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2327 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2328 | unsigned int dest_parent_times, c; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2329 | const char *start, *end, *pke_end; |
| 2330 | struct ly_set keys = {0}; |
| 2331 | int i; |
| 2332 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2333 | assert(path_context); |
| 2334 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2335 | while (**predicate == '[') { |
| 2336 | start = (*predicate)++; |
| 2337 | |
| 2338 | while (isspace(**predicate)) { |
| 2339 | ++(*predicate); |
| 2340 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2341 | LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2342 | while (isspace(**predicate)) { |
| 2343 | ++(*predicate); |
| 2344 | } |
| 2345 | if (**predicate != '=') { |
| 2346 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2347 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2348 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2349 | goto cleanup; |
| 2350 | } |
| 2351 | ++(*predicate); |
| 2352 | while (isspace(**predicate)) { |
| 2353 | ++(*predicate); |
| 2354 | } |
| 2355 | |
| 2356 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2357 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2358 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2359 | goto cleanup; |
| 2360 | } |
| 2361 | --pke_end; |
| 2362 | while (isspace(*pke_end)) { |
| 2363 | --pke_end; |
| 2364 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2365 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2366 | /* localize path-key-expr */ |
| 2367 | pke_start = path_key_expr = *predicate; |
| 2368 | /* move after the current predicate */ |
| 2369 | *predicate = end + 1; |
| 2370 | |
| 2371 | /* source (must be leaf or leaf-list) */ |
| 2372 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2373 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2374 | if (!mod) { |
| 2375 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2376 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2377 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2378 | goto cleanup; |
| 2379 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2380 | if (!mod->implemented) { |
| 2381 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2382 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2383 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2384 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2385 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2386 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2387 | src_node = NULL; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2388 | if (!(context_node->flags & LYS_KEYLESS)) { |
| 2389 | struct lysc_node *key; |
| 2390 | for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 2391 | if (!ly_strncmp(key->name, src, src_len)) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2392 | src_node = key; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2393 | break; |
| 2394 | } |
| 2395 | } |
| 2396 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2397 | if (!src_node) { |
| 2398 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2399 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2400 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2401 | goto cleanup; |
| 2402 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2403 | |
| 2404 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2405 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2406 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2407 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2408 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2409 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2410 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2411 | *predicate - start, start, src_node->name); |
| 2412 | goto cleanup; |
| 2413 | } |
| 2414 | |
| 2415 | /* destination */ |
| 2416 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2417 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2418 | |
| 2419 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2420 | if (strncmp(path_key_expr, "current", 7)) { |
| 2421 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2422 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2423 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2424 | *predicate - start, start); |
| 2425 | goto cleanup; |
| 2426 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2427 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2428 | if (*path_key_expr != '(') { |
| 2429 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2430 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2431 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2432 | if (*path_key_expr != ')') { |
| 2433 | goto error_current_function_invocation; |
| 2434 | } |
| 2435 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2436 | |
| 2437 | if (*path_key_expr != '/') { |
| 2438 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2439 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2440 | *predicate - start, start); |
| 2441 | goto cleanup; |
| 2442 | } |
| 2443 | ++path_key_expr; |
| 2444 | while (isspace(*path_key_expr)) { |
| 2445 | ++path_key_expr; |
| 2446 | } |
| 2447 | |
| 2448 | /* rel-path-keyexpr: |
| 2449 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2450 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2451 | ++dest_parent_times; |
| 2452 | path_key_expr += 2; |
| 2453 | while (isspace(*path_key_expr)) { |
| 2454 | ++path_key_expr; |
| 2455 | } |
| 2456 | if (*path_key_expr != '/') { |
| 2457 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2458 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2459 | *predicate - start, start); |
| 2460 | goto cleanup; |
| 2461 | } |
| 2462 | ++path_key_expr; |
| 2463 | while (isspace(*path_key_expr)) { |
| 2464 | ++path_key_expr; |
| 2465 | } |
| 2466 | |
| 2467 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2468 | * all schema nodes that cannot be instantiated in data tree */ |
| 2469 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2470 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2471 | *predicate - start, start); |
| 2472 | } |
| 2473 | if (!dest_parent_times) { |
| 2474 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2475 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2476 | *predicate - start, start); |
| 2477 | goto cleanup; |
| 2478 | } |
| 2479 | if (path_key_expr == pke_end) { |
| 2480 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2481 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2482 | *predicate - start, start); |
| 2483 | goto cleanup; |
| 2484 | } |
| 2485 | |
| 2486 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2487 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2488 | if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2489 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2490 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2491 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2492 | goto cleanup; |
| 2493 | } |
| 2494 | |
| 2495 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2496 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2497 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2498 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2499 | } |
| 2500 | if (!mod) { |
| 2501 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2502 | "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2503 | *predicate - start, start, dst_len, dst); |
| 2504 | goto cleanup; |
| 2505 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2506 | if (!mod->implemented) { |
| 2507 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2508 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2509 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2510 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2511 | dst_node = lys_find_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2512 | if (!dst_node) { |
| 2513 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2514 | "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2515 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2516 | goto cleanup; |
| 2517 | } |
| 2518 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2519 | if (!(dst_node->nodetype & (dst_node->module->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2520 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2521 | "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2522 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2523 | goto cleanup; |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | ret = LY_SUCCESS; |
| 2528 | cleanup: |
| 2529 | ly_set_erase(&keys, NULL); |
| 2530 | return ret; |
| 2531 | } |
| 2532 | |
| 2533 | /** |
| 2534 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2535 | * |
| 2536 | * path-arg = absolute-path / relative-path |
| 2537 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2538 | * relative-path = 1*(".." "/") descendant-path |
| 2539 | * |
| 2540 | * @param[in,out] path Path to parse. |
| 2541 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2542 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2543 | * @param[out] name Name of the token. |
| 2544 | * @param[out] nam_len Length of the name. |
| 2545 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2546 | * must not be changed between consecutive calls. -1 if the |
| 2547 | * path is absolute. |
| 2548 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2549 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2550 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2551 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2552 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2553 | int *parent_times, int *has_predicate) |
| 2554 | { |
| 2555 | int par_times = 0; |
| 2556 | |
| 2557 | assert(path && *path); |
| 2558 | assert(parent_times); |
| 2559 | assert(prefix); |
| 2560 | assert(prefix_len); |
| 2561 | assert(name); |
| 2562 | assert(name_len); |
| 2563 | assert(has_predicate); |
| 2564 | |
| 2565 | *prefix = NULL; |
| 2566 | *prefix_len = 0; |
| 2567 | *name = NULL; |
| 2568 | *name_len = 0; |
| 2569 | *has_predicate = 0; |
| 2570 | |
| 2571 | if (!*parent_times) { |
| 2572 | if (!strncmp(*path, "..", 2)) { |
| 2573 | *path += 2; |
| 2574 | ++par_times; |
| 2575 | while (!strncmp(*path, "/..", 3)) { |
| 2576 | *path += 3; |
| 2577 | ++par_times; |
| 2578 | } |
| 2579 | } |
| 2580 | if (par_times) { |
| 2581 | *parent_times = par_times; |
| 2582 | } else { |
| 2583 | *parent_times = -1; |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | if (**path != '/') { |
| 2588 | return LY_EINVAL; |
| 2589 | } |
| 2590 | /* skip '/' */ |
| 2591 | ++(*path); |
| 2592 | |
| 2593 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2594 | 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] | 2595 | |
| 2596 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2597 | /* path continues by another token or this is the last token */ |
| 2598 | return LY_SUCCESS; |
| 2599 | } else if ((*path)[0] != '[') { |
| 2600 | /* unexpected character */ |
| 2601 | return LY_EINVAL; |
| 2602 | } else { |
| 2603 | /* predicate starting with [ */ |
| 2604 | *has_predicate = 1; |
| 2605 | return LY_SUCCESS; |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2610 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2611 | * |
| 2612 | * The set of features used for target must be a subset of features used for the leafref. |
| 2613 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2614 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2615 | * |
| 2616 | * @param[in] refnode The leafref node. |
| 2617 | * @param[in] target Tha target node of the leafref. |
| 2618 | * @return LY_SUCCESS or LY_EVALID; |
| 2619 | */ |
| 2620 | static LY_ERR |
| 2621 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2622 | { |
| 2623 | LY_ERR ret = LY_EVALID; |
| 2624 | const struct lysc_node *iter; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2625 | LY_ARRAY_SIZE_TYPE u, v, count; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2626 | struct ly_set features = {0}; |
| 2627 | |
| 2628 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2629 | if (iter->iffeatures) { |
| 2630 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2631 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2632 | 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] | 2633 | } |
| 2634 | } |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2639 | * So when adding features applicable to the target into the features set, we should not be |
| 2640 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2641 | * to the leafref itself. */ |
| 2642 | count = features.count; |
| 2643 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2644 | if (iter->iffeatures) { |
| 2645 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2646 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2647 | 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] | 2648 | /* new feature was added (or LY_EMEM) */ |
| 2649 | goto cleanup; |
| 2650 | } |
| 2651 | } |
| 2652 | } |
| 2653 | } |
| 2654 | } |
| 2655 | ret = LY_SUCCESS; |
| 2656 | |
| 2657 | cleanup: |
| 2658 | ly_set_erase(&features, NULL); |
| 2659 | return ret; |
| 2660 | } |
| 2661 | |
| 2662 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2663 | * @brief Validate the leafref path. |
| 2664 | * @param[in] ctx Compile context |
| 2665 | * @param[in] startnode Path context node (where the leafref path begins/is placed). |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2666 | * @param[in] leafref Leafref to validate. |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2667 | * @param[out] target Optional resolved leafref target. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2668 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2669 | */ |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2670 | LY_ERR |
| 2671 | lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref, |
| 2672 | const struct lysc_node **target) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2673 | { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 2674 | const struct lysc_node *node = NULL, *parent = NULL; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2675 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2676 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2677 | const char *id, *prefix, *name; |
| 2678 | size_t prefix_len, name_len; |
| 2679 | int parent_times = 0, has_predicate; |
| 2680 | unsigned int iter, u; |
| 2681 | LY_ERR ret = LY_SUCCESS; |
| 2682 | |
| 2683 | assert(ctx); |
| 2684 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2685 | assert(leafref); |
| 2686 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2687 | ctx->path[0] = '\0'; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2688 | lysc_path(startnode, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2689 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2690 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2691 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2692 | id = leafref->path; |
Juraj Vijtiuk | bd0f2a6 | 2019-12-11 15:58:18 +0100 | [diff] [blame] | 2693 | |
| 2694 | if (!*id) { |
| 2695 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Empty leafref path."); |
| 2696 | return LY_EVALID; |
| 2697 | } |
| 2698 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2699 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2700 | if (!iter) { /* first iteration */ |
| 2701 | /* precess ".." in relative paths */ |
| 2702 | if (parent_times > 0) { |
| 2703 | /* move from the context node */ |
| 2704 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2705 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2706 | * all schema nodes that cannot be instantiated in data tree */ |
| 2707 | MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2708 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2709 | } |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2714 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2715 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2716 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2717 | } |
| 2718 | if (!mod) { |
| 2719 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2720 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2721 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2722 | return LY_EVALID; |
| 2723 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2724 | if (!mod->implemented) { |
| 2725 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2726 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2727 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2728 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2729 | node = lys_find_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2730 | if (!node) { |
| 2731 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2732 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2733 | return LY_EVALID; |
| 2734 | } |
| 2735 | parent = node; |
| 2736 | |
| 2737 | if (has_predicate) { |
| 2738 | /* we have predicate, so the current result must be list */ |
| 2739 | if (node->nodetype != LYS_LIST) { |
| 2740 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2741 | "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.", |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2742 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2743 | return LY_EVALID; |
| 2744 | } |
| 2745 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2746 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2747 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | ++iter; |
| 2751 | } |
| 2752 | if (ret) { |
| 2753 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2754 | "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2755 | return LY_EVALID; |
| 2756 | } |
| 2757 | |
| 2758 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2759 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2760 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2761 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2762 | return LY_EVALID; |
| 2763 | } |
| 2764 | |
| 2765 | /* check status */ |
| 2766 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2767 | return LY_EVALID; |
| 2768 | } |
| 2769 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2770 | /* check config */ |
| 2771 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2772 | if (node->flags & LYS_CONFIG_R) { |
| 2773 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2774 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2775 | leafref->path); |
| 2776 | return LY_EVALID; |
| 2777 | } |
| 2778 | } |
| 2779 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2780 | /* store the target's type and check for circular chain of leafrefs */ |
| 2781 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2782 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2783 | if (type == (struct lysc_type*)leafref) { |
| 2784 | /* circular chain detected */ |
| 2785 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2786 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2787 | return LY_EVALID; |
| 2788 | } |
| 2789 | } |
| 2790 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2791 | /* check if leafref and its target are under common if-features */ |
| 2792 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2793 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2794 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2795 | leafref->path); |
| 2796 | return LY_EVALID; |
| 2797 | } |
| 2798 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2799 | ctx->path_len = 1; |
| 2800 | ctx->path[1] = '\0'; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2801 | if (target) { |
| 2802 | *target = node; |
| 2803 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2804 | return LY_SUCCESS; |
| 2805 | } |
| 2806 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2807 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2808 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2809 | /** |
| 2810 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2811 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2812 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2813 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2814 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2815 | * @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] | 2816 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2817 | * @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] | 2818 | * @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] | 2819 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2820 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2821 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2822 | * @return LY_ERR value. |
| 2823 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2824 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2825 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2826 | struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2827 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2828 | { |
| 2829 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2830 | struct lysc_type_bin *bin; |
| 2831 | struct lysc_type_num *num; |
| 2832 | struct lysc_type_str *str; |
| 2833 | struct lysc_type_bits *bits; |
| 2834 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2835 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2836 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2837 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2838 | |
| 2839 | switch (basetype) { |
| 2840 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2841 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2842 | |
| 2843 | /* 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] | 2844 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2845 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2846 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2847 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2848 | 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] | 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | if (tpdfname) { |
| 2853 | type_p->compiled = *type; |
| 2854 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2855 | } |
| 2856 | break; |
| 2857 | case LY_TYPE_BITS: |
| 2858 | /* RFC 7950 9.7 - bits */ |
| 2859 | bits = (struct lysc_type_bits*)(*type); |
| 2860 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2861 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2862 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2863 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2864 | } |
| 2865 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2866 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2867 | /* 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] | 2868 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2869 | 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] | 2870 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2871 | 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] | 2872 | free(*type); |
| 2873 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2874 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2875 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | if (tpdfname) { |
| 2879 | type_p->compiled = *type; |
| 2880 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2881 | } |
| 2882 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2883 | case LY_TYPE_DEC64: |
| 2884 | dec = (struct lysc_type_dec*)(*type); |
| 2885 | |
| 2886 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2887 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2888 | if (!type_p->fraction_digits) { |
| 2889 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2890 | 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] | 2891 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2892 | 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] | 2893 | free(*type); |
| 2894 | *type = NULL; |
| 2895 | } |
| 2896 | return LY_EVALID; |
| 2897 | } |
| 2898 | } else if (type_p->fraction_digits) { |
| 2899 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2900 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2901 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2902 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2903 | tpdfname); |
| 2904 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2905 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2906 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2907 | free(*type); |
| 2908 | *type = NULL; |
| 2909 | } |
| 2910 | return LY_EVALID; |
| 2911 | } |
| 2912 | dec->fraction_digits = type_p->fraction_digits; |
| 2913 | |
| 2914 | /* RFC 7950 9.2.4 - range */ |
| 2915 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2916 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2917 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2918 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2919 | 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] | 2920 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | if (tpdfname) { |
| 2924 | type_p->compiled = *type; |
| 2925 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2926 | } |
| 2927 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2928 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2929 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2930 | |
| 2931 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2932 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2933 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2934 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2935 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2936 | 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] | 2937 | } |
| 2938 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2939 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2940 | } |
| 2941 | |
| 2942 | /* RFC 7950 9.4.5 - pattern */ |
| 2943 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2944 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2945 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2946 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2947 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2948 | } |
| 2949 | |
| 2950 | if (tpdfname) { |
| 2951 | type_p->compiled = *type; |
| 2952 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2953 | } |
| 2954 | break; |
| 2955 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2956 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2957 | |
| 2958 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2959 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2960 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2961 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2962 | } |
| 2963 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2964 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2965 | /* 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] | 2966 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2967 | 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] | 2968 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2969 | 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] | 2970 | free(*type); |
| 2971 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2972 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2973 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | if (tpdfname) { |
| 2977 | type_p->compiled = *type; |
| 2978 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2979 | } |
| 2980 | break; |
| 2981 | case LY_TYPE_INT8: |
| 2982 | case LY_TYPE_UINT8: |
| 2983 | case LY_TYPE_INT16: |
| 2984 | case LY_TYPE_UINT16: |
| 2985 | case LY_TYPE_INT32: |
| 2986 | case LY_TYPE_UINT32: |
| 2987 | case LY_TYPE_INT64: |
| 2988 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2989 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2990 | |
| 2991 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2992 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2993 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2994 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2995 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2996 | 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] | 2997 | } |
| 2998 | } |
| 2999 | |
| 3000 | if (tpdfname) { |
| 3001 | type_p->compiled = *type; |
| 3002 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 3003 | } |
| 3004 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 3005 | case LY_TYPE_IDENT: |
| 3006 | idref = (struct lysc_type_identityref*)(*type); |
| 3007 | |
| 3008 | /* RFC 7950 9.10.2 - base */ |
| 3009 | if (type_p->bases) { |
| 3010 | if (base) { |
| 3011 | /* only the directly derived identityrefs can contain base specification */ |
| 3012 | if (tpdfname) { |
| 3013 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3014 | "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] | 3015 | tpdfname); |
| 3016 | } else { |
| 3017 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3018 | "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] | 3019 | free(*type); |
| 3020 | *type = NULL; |
| 3021 | } |
| 3022 | return LY_EVALID; |
| 3023 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 3024 | 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] | 3025 | } |
| 3026 | |
| 3027 | if (!base && !type_p->flags) { |
| 3028 | /* type derived from identityref built-in type must contain at least one base */ |
| 3029 | if (tpdfname) { |
| 3030 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 3031 | } else { |
| 3032 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 3033 | free(*type); |
| 3034 | *type = NULL; |
| 3035 | } |
| 3036 | return LY_EVALID; |
| 3037 | } |
| 3038 | |
| 3039 | if (tpdfname) { |
| 3040 | type_p->compiled = *type; |
| 3041 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3042 | } |
| 3043 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3044 | case LY_TYPE_LEAFREF: |
| 3045 | /* RFC 7950 9.9.3 - require-instance */ |
| 3046 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3047 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3048 | if (tpdfname) { |
| 3049 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3050 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 3051 | } else { |
| 3052 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3053 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 3054 | free(*type); |
| 3055 | *type = NULL; |
| 3056 | } |
| 3057 | return LY_EVALID; |
| 3058 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3059 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 3060 | } else if (base) { |
| 3061 | /* inherit */ |
| 3062 | ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3063 | } else { |
| 3064 | /* default is true */ |
| 3065 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 3066 | } |
| 3067 | if (type_p->path) { |
| 3068 | DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 3069 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3070 | } else if (base) { |
| 3071 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 3072 | ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3073 | } else if (tpdfname) { |
| 3074 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 3075 | return LY_EVALID; |
| 3076 | } else { |
| 3077 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 3078 | free(*type); |
| 3079 | *type = NULL; |
| 3080 | return LY_EVALID; |
| 3081 | } |
| 3082 | if (tpdfname) { |
| 3083 | type_p->compiled = *type; |
| 3084 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3085 | } |
| 3086 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 3087 | case LY_TYPE_INST: |
| 3088 | /* RFC 7950 9.9.3 - require-instance */ |
| 3089 | if (type_p->flags & LYS_SET_REQINST) { |
| 3090 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 3091 | } else { |
| 3092 | /* default is true */ |
| 3093 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 3094 | } |
| 3095 | |
| 3096 | if (tpdfname) { |
| 3097 | type_p->compiled = *type; |
| 3098 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3099 | } |
| 3100 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3101 | case LY_TYPE_UNION: |
| 3102 | un = (struct lysc_type_union*)(*type); |
| 3103 | |
| 3104 | /* RFC 7950 7.4 - type */ |
| 3105 | if (type_p->types) { |
| 3106 | if (base) { |
| 3107 | /* only the directly derived union can contain types specification */ |
| 3108 | if (tpdfname) { |
| 3109 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3110 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 3111 | tpdfname); |
| 3112 | } else { |
| 3113 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3114 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 3115 | free(*type); |
| 3116 | *type = NULL; |
| 3117 | } |
| 3118 | return LY_EVALID; |
| 3119 | } |
| 3120 | /* compile the type */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3121 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3122 | for (LY_ARRAY_SIZE_TYPE u = 0, additional = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3123 | LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], &un->types[u + additional], NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3124 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 3125 | /* add space for additional types from the union subtype */ |
| 3126 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Radek Krejci | c4fa029 | 2020-05-14 10:54:49 +0200 | [diff] [blame] | 3127 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_SIZE(un_aux->types) - 1, |
| 3128 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3129 | |
| 3130 | /* copy subtypes of the subtype union */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3131 | for (LY_ARRAY_SIZE_TYPE v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3132 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 3133 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 3134 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3135 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3136 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 3137 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 3138 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 3139 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 3140 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 3141 | /* TODO extensions */ |
| 3142 | |
| 3143 | } else { |
| 3144 | un->types[u + additional] = un_aux->types[v]; |
| 3145 | ++un_aux->types[v]->refcount; |
| 3146 | } |
| 3147 | ++additional; |
| 3148 | LY_ARRAY_INCREMENT(un->types); |
| 3149 | } |
| 3150 | /* compensate u increment in main loop */ |
| 3151 | --additional; |
| 3152 | |
| 3153 | /* free the replaced union subtype */ |
| 3154 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 3155 | } else { |
| 3156 | LY_ARRAY_INCREMENT(un->types); |
| 3157 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | if (!base && !type_p->flags) { |
| 3162 | /* type derived from union built-in type must contain at least one type */ |
| 3163 | if (tpdfname) { |
| 3164 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 3165 | } else { |
| 3166 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 3167 | free(*type); |
| 3168 | *type = NULL; |
| 3169 | } |
| 3170 | return LY_EVALID; |
| 3171 | } |
| 3172 | |
| 3173 | if (tpdfname) { |
| 3174 | type_p->compiled = *type; |
| 3175 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3176 | } |
| 3177 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3178 | case LY_TYPE_BOOL: |
| 3179 | case LY_TYPE_EMPTY: |
| 3180 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 3181 | break; |
| 3182 | } |
| 3183 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 3184 | done: |
| 3185 | return ret; |
| 3186 | } |
| 3187 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3188 | /** |
| 3189 | * @brief Compile information about the leaf/leaf-list's type. |
| 3190 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3191 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 3192 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3193 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3194 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 3195 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3196 | * @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] | 3197 | * @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] | 3198 | * @return LY_ERR value. |
| 3199 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3200 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3201 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3202 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3203 | { |
| 3204 | LY_ERR ret = LY_SUCCESS; |
| 3205 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3206 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3207 | struct type_context { |
| 3208 | const struct lysp_tpdf *tpdf; |
| 3209 | struct lysp_node *node; |
| 3210 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3211 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3212 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3213 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3214 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3215 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3216 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3217 | |
| 3218 | (*type) = NULL; |
| 3219 | |
| 3220 | tctx = calloc(1, sizeof *tctx); |
| 3221 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3222 | 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] | 3223 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 3224 | ret == LY_SUCCESS; |
| 3225 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 3226 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 3227 | if (basetype) { |
| 3228 | break; |
| 3229 | } |
| 3230 | |
| 3231 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3232 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3233 | 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] | 3234 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3235 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3236 | if (units && !*units) { |
| 3237 | /* inherit units */ |
| 3238 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3239 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3240 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3241 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3242 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3243 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3244 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3245 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3246 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3247 | break; |
| 3248 | } |
| 3249 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3250 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3251 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3252 | * 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] | 3253 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3254 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3255 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3256 | dummyloops = 1; |
| 3257 | goto preparenext; |
| 3258 | } else { |
| 3259 | tctx = NULL; |
| 3260 | break; |
| 3261 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3262 | } |
| 3263 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3264 | /* circular typedef reference detection */ |
| 3265 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3266 | /* local part */ |
| 3267 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3268 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3269 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3270 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3271 | free(tctx); |
| 3272 | ret = LY_EVALID; |
| 3273 | goto cleanup; |
| 3274 | } |
| 3275 | } |
| 3276 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3277 | /* global part for unions corner case */ |
| 3278 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3279 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3280 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3281 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3282 | free(tctx); |
| 3283 | ret = LY_EVALID; |
| 3284 | goto cleanup; |
| 3285 | } |
| 3286 | } |
| 3287 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3288 | /* store information for the following processing */ |
| 3289 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3290 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3291 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3292 | /* prepare next loop */ |
| 3293 | tctx_prev = tctx; |
| 3294 | tctx = calloc(1, sizeof *tctx); |
| 3295 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3296 | } |
| 3297 | free(tctx); |
| 3298 | |
| 3299 | /* allocate type according to the basetype */ |
| 3300 | switch (basetype) { |
| 3301 | case LY_TYPE_BINARY: |
| 3302 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3303 | break; |
| 3304 | case LY_TYPE_BITS: |
| 3305 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3306 | break; |
| 3307 | case LY_TYPE_BOOL: |
| 3308 | case LY_TYPE_EMPTY: |
| 3309 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3310 | break; |
| 3311 | case LY_TYPE_DEC64: |
| 3312 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3313 | break; |
| 3314 | case LY_TYPE_ENUM: |
| 3315 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3316 | break; |
| 3317 | case LY_TYPE_IDENT: |
| 3318 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3319 | break; |
| 3320 | case LY_TYPE_INST: |
| 3321 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3322 | break; |
| 3323 | case LY_TYPE_LEAFREF: |
| 3324 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3325 | break; |
| 3326 | case LY_TYPE_STRING: |
| 3327 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3328 | break; |
| 3329 | case LY_TYPE_UNION: |
| 3330 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3331 | break; |
| 3332 | case LY_TYPE_INT8: |
| 3333 | case LY_TYPE_UINT8: |
| 3334 | case LY_TYPE_INT16: |
| 3335 | case LY_TYPE_UINT16: |
| 3336 | case LY_TYPE_INT32: |
| 3337 | case LY_TYPE_UINT32: |
| 3338 | case LY_TYPE_INT64: |
| 3339 | case LY_TYPE_UINT64: |
| 3340 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3341 | break; |
| 3342 | case LY_TYPE_UNKNOWN: |
| 3343 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3344 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3345 | ret = LY_EVALID; |
| 3346 | goto cleanup; |
| 3347 | } |
| 3348 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3349 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3350 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3351 | ly_data_type2str[basetype]); |
| 3352 | free(*type); |
| 3353 | (*type) = NULL; |
| 3354 | ret = LY_EVALID; |
| 3355 | goto cleanup; |
| 3356 | } |
| 3357 | |
| 3358 | /* get restrictions from the referred typedefs */ |
| 3359 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3360 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3361 | |
| 3362 | /* remember the typedef context for circular check */ |
| 3363 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3364 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3365 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3366 | base = tctx->tpdf->type.compiled; |
| 3367 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3368 | } 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] | 3369 | /* no change, just use the type information from the base */ |
| 3370 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3371 | ++base->refcount; |
| 3372 | continue; |
| 3373 | } |
| 3374 | |
| 3375 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3376 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3377 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3378 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3379 | ret = LY_EVALID; |
| 3380 | goto cleanup; |
| 3381 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3382 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3383 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3384 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3385 | ret = LY_EVALID; |
| 3386 | goto cleanup; |
| 3387 | } |
| 3388 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3389 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3390 | /* TODO user type plugins */ |
| 3391 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3392 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3393 | 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] | 3394 | 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] | 3395 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3396 | LY_CHECK_GOTO(ret, cleanup); |
| 3397 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3398 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3399 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3400 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3401 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3402 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3403 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3404 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3405 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3406 | /* TODO user type plugins */ |
| 3407 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3408 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3409 | 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] | 3410 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3411 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3412 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3413 | free(*type); |
| 3414 | (*type) = base; |
| 3415 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3416 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3417 | if (dflt && !(*type)->dflt) { |
| 3418 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3419 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3420 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3421 | (*type)->dflt->realtype = (*type); |
| 3422 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3423 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3424 | lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3425 | if (err) { |
| 3426 | ly_err_print(err); |
| 3427 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3428 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3429 | ly_err_free(err); |
| 3430 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3431 | if (ret == LY_EINCOMPLETE) { |
| 3432 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3433 | 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] | 3434 | |
| 3435 | /* but in general result is so far ok */ |
| 3436 | ret = LY_SUCCESS; |
| 3437 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3438 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3439 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3440 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3441 | 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] | 3442 | |
| 3443 | cleanup: |
| 3444 | ly_set_erase(&tpdf_chain, free); |
| 3445 | return ret; |
| 3446 | } |
| 3447 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3448 | /** |
| 3449 | * @brief Compile status information of the given node. |
| 3450 | * |
| 3451 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3452 | * has the status correctly set during the compilation. |
| 3453 | * |
| 3454 | * @param[in] ctx Compile context |
| 3455 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3456 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3457 | * the compatibility with the parent's status value. |
| 3458 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3459 | * @return LY_ERR value. |
| 3460 | */ |
| 3461 | static LY_ERR |
| 3462 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3463 | { |
| 3464 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3465 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3466 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3467 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3468 | if ((parent_flags & 0x3) != 0x3) { |
| 3469 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3470 | * combination of bits (0x3) which marks the uses_status value */ |
| 3471 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3472 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3473 | } |
| 3474 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3475 | } else { |
| 3476 | (*node_flags) |= LYS_STATUS_CURR; |
| 3477 | } |
| 3478 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3479 | /* check status compatibility with the parent */ |
| 3480 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3481 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3482 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3483 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3484 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3485 | } else { /* LYS_STATUS_DEPRC */ |
| 3486 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3487 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3488 | } |
| 3489 | return LY_EVALID; |
| 3490 | } |
| 3491 | } |
| 3492 | return LY_SUCCESS; |
| 3493 | } |
| 3494 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3495 | /** |
| 3496 | * @brief Check uniqness of the node/action/notification name. |
| 3497 | * |
| 3498 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3499 | * structures, but they share the namespace so we need to check their name collisions. |
| 3500 | * |
| 3501 | * @param[in] ctx Compile context. |
| 3502 | * @param[in] children List (linked list) of data nodes to go through. |
| 3503 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3504 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3505 | * @param[in] name Name of the item to find in the given lists. |
| 3506 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3507 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3508 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3509 | */ |
| 3510 | static LY_ERR |
| 3511 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3512 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3513 | const char *name, void *exclude) |
| 3514 | { |
| 3515 | const struct lysc_node *iter; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3516 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3517 | |
| 3518 | LY_LIST_FOR(children, iter) { |
| 3519 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3520 | goto error; |
| 3521 | } |
| 3522 | } |
| 3523 | LY_ARRAY_FOR(actions, u) { |
| 3524 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3525 | goto error; |
| 3526 | } |
| 3527 | } |
| 3528 | LY_ARRAY_FOR(notifs, u) { |
| 3529 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3530 | goto error; |
| 3531 | } |
| 3532 | } |
| 3533 | return LY_SUCCESS; |
| 3534 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3535 | 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] | 3536 | return LY_EEXIST; |
| 3537 | } |
| 3538 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3539 | 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] | 3540 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3541 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3542 | * @brief Compile parsed RPC/action schema node information. |
| 3543 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3544 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3545 | * @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] | 3546 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3547 | * @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). |
| 3548 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3549 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3550 | */ |
| 3551 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3552 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3553 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3554 | { |
| 3555 | LY_ERR ret = LY_SUCCESS; |
| 3556 | struct lysp_node *child_p; |
| 3557 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3558 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3559 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3560 | lysc_update_path(ctx, parent, action_p->name); |
| 3561 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3562 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3563 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3564 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3565 | action_p->name, action)) { |
| 3566 | return LY_EVALID; |
| 3567 | } |
| 3568 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3569 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3570 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3571 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3572 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3573 | return LY_EVALID; |
| 3574 | } |
| 3575 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3576 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3577 | action->module = ctx->mod; |
| 3578 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3579 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3580 | action->sp = action_p; |
| 3581 | } |
| 3582 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3583 | |
| 3584 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3585 | * 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] | 3586 | 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] | 3587 | |
| 3588 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3589 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3590 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3591 | 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] | 3592 | 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] | 3593 | |
| 3594 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3595 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3596 | 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] | 3597 | 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] | 3598 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3599 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3600 | 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] | 3601 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3602 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3603 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3604 | |
| 3605 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3606 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3607 | 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] | 3608 | 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] | 3609 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3610 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3611 | 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] | 3612 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3613 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3614 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3615 | |
| 3616 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3617 | /* do not check "must" semantics in a grouping */ |
| 3618 | ly_set_add(&ctx->unres, action, 0); |
| 3619 | } |
| 3620 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3621 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3622 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3623 | return ret; |
| 3624 | } |
| 3625 | |
| 3626 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3627 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3628 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3629 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3630 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3631 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3632 | * @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] | 3633 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3634 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3635 | */ |
| 3636 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3637 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3638 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3639 | { |
| 3640 | LY_ERR ret = LY_SUCCESS; |
| 3641 | struct lysp_node *child_p; |
| 3642 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3643 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3644 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3645 | lysc_update_path(ctx, parent, notif_p->name); |
| 3646 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3647 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3648 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3649 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3650 | notif_p->name, notif)) { |
| 3651 | return LY_EVALID; |
| 3652 | } |
| 3653 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3654 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3655 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3656 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3657 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3658 | return LY_EVALID; |
| 3659 | } |
| 3660 | |
| 3661 | notif->nodetype = LYS_NOTIF; |
| 3662 | notif->module = ctx->mod; |
| 3663 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3664 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3665 | notif->sp = notif_p; |
| 3666 | } |
| 3667 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3668 | |
| 3669 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3670 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3671 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3672 | |
| 3673 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3674 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3675 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3676 | 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] | 3677 | 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] | 3678 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3679 | /* do not check "must" semantics in a grouping */ |
| 3680 | ly_set_add(&ctx->unres, notif, 0); |
| 3681 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3682 | 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] | 3683 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3684 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3685 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3686 | 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] | 3687 | } |
| 3688 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3689 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3690 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3691 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3692 | return ret; |
| 3693 | } |
| 3694 | |
| 3695 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3696 | * @brief Compile parsed container node information. |
| 3697 | * @param[in] ctx Compile context |
| 3698 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3699 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3700 | * is enriched with the container-specific information. |
| 3701 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3702 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3703 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3704 | 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] | 3705 | { |
| 3706 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3707 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3708 | struct lysp_node *child_p; |
| 3709 | unsigned int u; |
| 3710 | LY_ERR ret = LY_SUCCESS; |
| 3711 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3712 | if (cont_p->presence) { |
| 3713 | cont->flags |= LYS_PRESENCE; |
| 3714 | } |
| 3715 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3716 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3717 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3718 | } |
| 3719 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3720 | 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] | 3721 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3722 | /* do not check "must" semantics in a grouping */ |
| 3723 | ly_set_add(&ctx->unres, cont, 0); |
| 3724 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3725 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3726 | 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] | 3727 | |
| 3728 | done: |
| 3729 | return ret; |
| 3730 | } |
| 3731 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3732 | /* |
| 3733 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3734 | * @param[in] ctx Compile context. |
| 3735 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3736 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3737 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3738 | * @return LY_ERR value. |
| 3739 | */ |
| 3740 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3741 | 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] | 3742 | { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3743 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3744 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3745 | 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] | 3746 | leaf->units ? NULL : &leaf->units)); |
| 3747 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3748 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3749 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3750 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3751 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3752 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3753 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3754 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3755 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3756 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3757 | } |
| 3758 | } else { |
| 3759 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3760 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3761 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3762 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3763 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3764 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3765 | } |
| 3766 | } |
| 3767 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3768 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3769 | ly_set_add(&ctx->unres, leaf, 0); |
| 3770 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3771 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3772 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3773 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3774 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3775 | ly_set_add(&ctx->unres, leaf, 0); |
| 3776 | } |
| 3777 | } |
| 3778 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3779 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3780 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3781 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3782 | return LY_EVALID; |
| 3783 | } |
| 3784 | } |
| 3785 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3786 | return LY_SUCCESS; |
| 3787 | } |
| 3788 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3789 | /** |
| 3790 | * @brief Compile parsed leaf node information. |
| 3791 | * @param[in] ctx Compile context |
| 3792 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3793 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3794 | * is enriched with the leaf-specific information. |
| 3795 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3796 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3797 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3798 | 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] | 3799 | { |
| 3800 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3801 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3802 | unsigned int u; |
| 3803 | LY_ERR ret = LY_SUCCESS; |
| 3804 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3805 | 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] | 3806 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3807 | /* do not check "must" semantics in a grouping */ |
| 3808 | ly_set_add(&ctx->unres, leaf, 0); |
| 3809 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3810 | if (leaf_p->units) { |
| 3811 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3812 | leaf->flags |= LYS_SET_UNITS; |
| 3813 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3814 | |
| 3815 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3816 | leaf->dflt = (void*)leaf_p->dflt; |
| 3817 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3818 | if (ret) { |
| 3819 | leaf->dflt = NULL; |
| 3820 | return ret; |
| 3821 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3822 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3823 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3824 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3825 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3826 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3827 | leaf->dflt->realtype = leaf->type; |
| 3828 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3829 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3830 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3831 | leaf->dflt->realtype->refcount++; |
| 3832 | if (err) { |
| 3833 | ly_err_print(err); |
| 3834 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3835 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3836 | ly_err_free(err); |
| 3837 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3838 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3839 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3840 | 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] | 3841 | |
| 3842 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3843 | ret = LY_SUCCESS; |
| 3844 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3845 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3846 | leaf->flags |= LYS_SET_DFLT; |
| 3847 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3848 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3849 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3850 | done: |
| 3851 | return ret; |
| 3852 | } |
| 3853 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3854 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3855 | * @brief Compile parsed leaf-list node information. |
| 3856 | * @param[in] ctx Compile context |
| 3857 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3858 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3859 | * is enriched with the leaf-list-specific information. |
| 3860 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3861 | */ |
| 3862 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3863 | 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] | 3864 | { |
| 3865 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3866 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3867 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3868 | LY_ERR ret = LY_SUCCESS; |
| 3869 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3870 | 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] | 3871 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3872 | /* do not check "must" semantics in a grouping */ |
| 3873 | ly_set_add(&ctx->unres, llist, 0); |
| 3874 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3875 | if (llist_p->units) { |
| 3876 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3877 | llist->flags |= LYS_SET_UNITS; |
| 3878 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3879 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3880 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3881 | llist->dflts = (void*)llist_p->dflts; |
| 3882 | 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] | 3883 | if (ret != LY_SUCCESS) { |
| 3884 | /* make sure the defaults are freed correctly */ |
| 3885 | if (llist_p->dflts) { |
| 3886 | llist->dflts = NULL; |
| 3887 | } |
| 3888 | return ret; |
| 3889 | } |
| 3890 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3891 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3892 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3893 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3894 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3895 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3896 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3897 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3898 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3899 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3900 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3901 | llist->dflts[u]->realtype = llist->type; |
| 3902 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3903 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3904 | lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3905 | llist->dflts[u]->realtype->refcount++; |
| 3906 | if (err) { |
| 3907 | ly_err_print(err); |
| 3908 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3909 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3910 | ly_err_free(err); |
| 3911 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3912 | if (ret == LY_EINCOMPLETE) { |
| 3913 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3914 | 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] | 3915 | |
| 3916 | /* but in general result is so far ok */ |
| 3917 | ret = LY_SUCCESS; |
| 3918 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3919 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3920 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3921 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3922 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3923 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3924 | /* configuration data values must be unique - so check the default values */ |
| 3925 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3926 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3927 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3928 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3929 | const char *val = llist->type->plugin->print(llist->dflts[v], LYD_XML, lys_get_prefix, llist->dflts_mods[v], &dynamic); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3930 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3931 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3932 | if (dynamic) { |
| 3933 | free((char*)val); |
| 3934 | } |
| 3935 | return LY_EVALID; |
| 3936 | } |
| 3937 | } |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | /* 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] | 3942 | |
| 3943 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3944 | if (llist->min) { |
| 3945 | llist->flags |= LYS_MAND_TRUE; |
| 3946 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3947 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3948 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3949 | done: |
| 3950 | return ret; |
| 3951 | } |
| 3952 | |
| 3953 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3954 | * @brief Compile information about list's uniques. |
| 3955 | * @param[in] ctx Compile context. |
| 3956 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3957 | * @param[in] uniques Sized array list of unique statements. |
| 3958 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3959 | * @return LY_ERR value. |
| 3960 | */ |
| 3961 | static LY_ERR |
| 3962 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3963 | { |
| 3964 | LY_ERR ret = LY_SUCCESS; |
| 3965 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3966 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3967 | const char *keystr, *delim; |
| 3968 | size_t len; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3969 | LY_ARRAY_SIZE_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3970 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3971 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3972 | |
| 3973 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3974 | config = -1; |
| 3975 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3976 | keystr = uniques[v]; |
| 3977 | while (keystr) { |
| 3978 | delim = strpbrk(keystr, " \t\n"); |
| 3979 | if (delim) { |
| 3980 | len = delim - keystr; |
| 3981 | while (isspace(*delim)) { |
| 3982 | ++delim; |
| 3983 | } |
| 3984 | } else { |
| 3985 | len = strlen(keystr); |
| 3986 | } |
| 3987 | |
| 3988 | /* unique node must be present */ |
| 3989 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3990 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3991 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3992 | if (ret != LY_SUCCESS) { |
| 3993 | if (ret == LY_EDENIED) { |
| 3994 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3995 | "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] | 3996 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3997 | } |
| 3998 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3999 | } else if (flags) { |
| 4000 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4001 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4002 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4003 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4004 | } |
| 4005 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4006 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4007 | /* all referenced leafs must be of the same config type */ |
| 4008 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 4009 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4010 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4011 | return LY_EVALID; |
| 4012 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 4013 | config = 1; |
| 4014 | } else { /* LYS_CONFIG_R */ |
| 4015 | config = 0; |
| 4016 | } |
| 4017 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4018 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 4019 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 4020 | if (parent->nodetype == LYS_LIST) { |
| 4021 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4022 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 4023 | return LY_EVALID; |
| 4024 | } |
| 4025 | } |
| 4026 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4027 | /* check status */ |
| 4028 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 4029 | (*key)->flags, (*key)->module, (*key)->name)); |
| 4030 | |
| 4031 | /* mark leaf as unique */ |
| 4032 | (*key)->flags |= LYS_UNIQUE; |
| 4033 | |
| 4034 | /* next unique value in line */ |
| 4035 | keystr = delim; |
| 4036 | } |
| 4037 | /* next unique definition */ |
| 4038 | } |
| 4039 | |
| 4040 | return LY_SUCCESS; |
| 4041 | } |
| 4042 | |
| 4043 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4044 | * @brief Compile parsed list node information. |
| 4045 | * @param[in] ctx Compile context |
| 4046 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4047 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4048 | * is enriched with the list-specific information. |
| 4049 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4050 | */ |
| 4051 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4052 | 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] | 4053 | { |
| 4054 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 4055 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 4056 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4057 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4058 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4059 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4060 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4061 | LY_ERR ret = LY_SUCCESS; |
| 4062 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4063 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4064 | if (list->min) { |
| 4065 | list->flags |= LYS_MAND_TRUE; |
| 4066 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4067 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 4068 | |
| 4069 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4070 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4071 | } |
| 4072 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4073 | 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] | 4074 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4075 | /* do not check "must" semantics in a grouping */ |
| 4076 | ly_set_add(&ctx->unres, list, 0); |
| 4077 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4078 | |
| 4079 | /* keys */ |
| 4080 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 4081 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 4082 | return LY_EVALID; |
| 4083 | } |
| 4084 | |
| 4085 | /* find all the keys (must be direct children) */ |
| 4086 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4087 | if (!keystr) { |
| 4088 | /* keyless list */ |
| 4089 | list->flags |= LYS_KEYLESS; |
| 4090 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4091 | while (keystr) { |
| 4092 | delim = strpbrk(keystr, " \t\n"); |
| 4093 | if (delim) { |
| 4094 | len = delim - keystr; |
| 4095 | while (isspace(*delim)) { |
| 4096 | ++delim; |
| 4097 | } |
| 4098 | } else { |
| 4099 | len = strlen(keystr); |
| 4100 | } |
| 4101 | |
| 4102 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4103 | 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] | 4104 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4105 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4106 | "The list's key \"%.*s\" not found.", len, keystr); |
| 4107 | return LY_EVALID; |
| 4108 | } |
| 4109 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4110 | if (key->flags & LYS_KEY) { |
| 4111 | /* the node was already marked as a key */ |
| 4112 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4113 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 4114 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4115 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4116 | |
| 4117 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4118 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4119 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4120 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 4121 | return LY_EVALID; |
| 4122 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4123 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4124 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4125 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4127 | "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] | 4128 | return LY_EVALID; |
| 4129 | } |
| 4130 | } else { |
| 4131 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4132 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4133 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4134 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4135 | return LY_EVALID; |
| 4136 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4137 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4138 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4139 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4140 | return LY_EVALID; |
| 4141 | } |
| 4142 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4143 | |
| 4144 | /* check status */ |
| 4145 | 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] | 4146 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4147 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4148 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4149 | if (key->dflt) { |
| 4150 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 4151 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 4152 | free(key->dflt); |
| 4153 | key->dflt = NULL; |
| 4154 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4155 | } |
| 4156 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4157 | key->flags |= LYS_KEY; |
| 4158 | |
| 4159 | /* move it to the correct position */ |
| 4160 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 4161 | /* fix links in closest previous siblings of the key */ |
| 4162 | if (key->next) { |
| 4163 | key->next->prev = key->prev; |
| 4164 | } else { |
| 4165 | /* last child */ |
| 4166 | list->child->prev = key->prev; |
| 4167 | } |
| 4168 | if (key->prev->next) { |
| 4169 | key->prev->next = key->next; |
| 4170 | } |
| 4171 | /* fix links in the key */ |
| 4172 | if (prev_key) { |
| 4173 | key->prev = (struct lysc_node*)prev_key; |
| 4174 | key->next = prev_key->next; |
| 4175 | } else { |
| 4176 | key->prev = list->child->prev; |
| 4177 | key->next = list->child; |
| 4178 | } |
| 4179 | /* fix links in closes future siblings of the key */ |
| 4180 | if (prev_key) { |
| 4181 | if (prev_key->next) { |
| 4182 | prev_key->next->prev = (struct lysc_node*)key; |
| 4183 | } else { |
| 4184 | list->child->prev = (struct lysc_node*)key; |
| 4185 | } |
| 4186 | prev_key->next = (struct lysc_node*)key; |
| 4187 | } else { |
| 4188 | list->child->prev = (struct lysc_node*)key; |
| 4189 | } |
| 4190 | /* fix links in parent */ |
| 4191 | if (!key->prev->next) { |
| 4192 | list->child = (struct lysc_node*)key; |
| 4193 | } |
| 4194 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4195 | |
| 4196 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4197 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4198 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4199 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4200 | } |
| 4201 | |
| 4202 | /* uniques */ |
| 4203 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4204 | 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] | 4205 | } |
| 4206 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4207 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 4208 | 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] | 4209 | |
| 4210 | done: |
| 4211 | return ret; |
| 4212 | } |
| 4213 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4214 | /** |
| 4215 | * @brief Do some checks and set the default choice's case. |
| 4216 | * |
| 4217 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 4218 | * |
| 4219 | * @param[in] ctx Compile context. |
| 4220 | * @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, |
| 4221 | * not the reference to the imported module. |
| 4222 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 4223 | * @return LY_ERR value. |
| 4224 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4225 | static LY_ERR |
| 4226 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 4227 | { |
| 4228 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 4229 | const char *prefix = NULL, *name; |
| 4230 | size_t prefix_len = 0; |
| 4231 | |
| 4232 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4233 | name = strchr(dflt, ':'); |
| 4234 | if (name) { |
| 4235 | prefix = dflt; |
| 4236 | prefix_len = name - prefix; |
| 4237 | ++name; |
| 4238 | } else { |
| 4239 | name = dflt; |
| 4240 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 4241 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4242 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 4243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4244 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 4245 | prefix_len, prefix); |
| 4246 | return LY_EVALID; |
| 4247 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4248 | 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] | 4249 | if (!ch->dflt) { |
| 4250 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4251 | "Default case \"%s\" not found.", dflt); |
| 4252 | return LY_EVALID; |
| 4253 | } |
| 4254 | /* no mandatory nodes directly under the default case */ |
| 4255 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4256 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 4257 | break; |
| 4258 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4259 | if (iter->flags & LYS_MAND_TRUE) { |
| 4260 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4261 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 4262 | return LY_EVALID; |
| 4263 | } |
| 4264 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4265 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4266 | return LY_SUCCESS; |
| 4267 | } |
| 4268 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4269 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4270 | 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] | 4271 | { |
| 4272 | struct lys_module *mod; |
| 4273 | const char *prefix = NULL, *name; |
| 4274 | size_t prefix_len = 0; |
| 4275 | struct lysc_node_case *cs; |
| 4276 | struct lysc_node *node; |
| 4277 | |
| 4278 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4279 | name = strchr(dflt, ':'); |
| 4280 | if (name) { |
| 4281 | prefix = dflt; |
| 4282 | prefix_len = name - prefix; |
| 4283 | ++name; |
| 4284 | } else { |
| 4285 | name = dflt; |
| 4286 | } |
| 4287 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 4288 | if (prefix) { |
| 4289 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 4290 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4291 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 4292 | "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] | 4293 | return LY_EVALID; |
| 4294 | } |
| 4295 | } else { |
| 4296 | mod = ctx->mod; |
| 4297 | } |
| 4298 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4299 | 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] | 4300 | if (!cs) { |
| 4301 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4302 | "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] | 4303 | return LY_EVALID; |
| 4304 | } |
| 4305 | |
| 4306 | /* check that there is no mandatory node */ |
| 4307 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4308 | if (node->parent != (struct lysc_node*)cs) { |
| 4309 | break; |
| 4310 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4311 | if (node->flags & LYS_MAND_TRUE) { |
| 4312 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4313 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4314 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4315 | return LY_EVALID; |
| 4316 | } |
| 4317 | } |
| 4318 | |
| 4319 | /* set the default case in choice */ |
| 4320 | ch->dflt = cs; |
| 4321 | cs->flags |= LYS_SET_DFLT; |
| 4322 | |
| 4323 | return LY_SUCCESS; |
| 4324 | } |
| 4325 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4326 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4327 | * @brief Compile parsed choice node information. |
| 4328 | * @param[in] ctx Compile context |
| 4329 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4330 | * @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] | 4331 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4332 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4333 | */ |
| 4334 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4335 | 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] | 4336 | { |
| 4337 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4338 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4339 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4340 | LY_ERR ret = LY_SUCCESS; |
| 4341 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4342 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4343 | if (child_p->nodetype == LYS_CASE) { |
| 4344 | 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] | 4345 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4346 | } |
| 4347 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4348 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4349 | } |
| 4350 | } |
| 4351 | |
| 4352 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4353 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4354 | 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] | 4355 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4356 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4357 | return ret; |
| 4358 | } |
| 4359 | |
| 4360 | /** |
| 4361 | * @brief Compile parsed anydata or anyxml node information. |
| 4362 | * @param[in] ctx Compile context |
| 4363 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4364 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4365 | * is enriched with the any-specific information. |
| 4366 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4367 | */ |
| 4368 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4369 | 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] | 4370 | { |
| 4371 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4372 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4373 | unsigned int u; |
| 4374 | LY_ERR ret = LY_SUCCESS; |
| 4375 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4376 | 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] | 4377 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4378 | /* do not check "must" semantics in a grouping */ |
| 4379 | ly_set_add(&ctx->unres, any, 0); |
| 4380 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4381 | |
| 4382 | if (any->flags & LYS_CONFIG_W) { |
| 4383 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4384 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4385 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4386 | done: |
| 4387 | return ret; |
| 4388 | } |
| 4389 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4390 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4391 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4392 | * |
| 4393 | * @param[in] ctx Compile context |
| 4394 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4395 | * the choice itself is expected instead of a specific case node. |
| 4396 | * @param[in] node Schema node to connect into the list. |
| 4397 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4398 | * 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] | 4399 | */ |
| 4400 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4401 | 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] | 4402 | { |
| 4403 | struct lysc_node **children; |
| 4404 | |
| 4405 | if (node->nodetype == LYS_CASE) { |
| 4406 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4407 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4408 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4409 | } |
| 4410 | if (children) { |
| 4411 | if (!(*children)) { |
| 4412 | /* first child */ |
| 4413 | *children = node; |
| 4414 | } else if (*children != node) { |
| 4415 | /* by the condition in previous branch we cover the choice/case children |
| 4416 | * - the children list is shared by the choice and the the first case, in addition |
| 4417 | * the first child of each case must be referenced from the case node. So the node is |
| 4418 | * actually always already inserted in case it is the first children - so here such |
| 4419 | * a situation actually corresponds to the first branch */ |
| 4420 | /* insert at the end of the parent's children list */ |
| 4421 | (*children)->prev->next = node; |
| 4422 | node->prev = (*children)->prev; |
| 4423 | (*children)->prev = node; |
| 4424 | |
| 4425 | /* check the name uniqueness */ |
| 4426 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4427 | lysc_node_notifs(parent), node->name, node)) { |
| 4428 | return LY_EEXIST; |
| 4429 | } |
| 4430 | } |
| 4431 | } |
| 4432 | return LY_SUCCESS; |
| 4433 | } |
| 4434 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4435 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4436 | * @brief Prepare the case structure in choice node for the new data node. |
| 4437 | * |
| 4438 | * 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 |
| 4439 | * created in the choice when the first child was processed. |
| 4440 | * |
| 4441 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4442 | * @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, |
| 4443 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4444 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4445 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4446 | * @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, |
| 4447 | * 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] | 4448 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4449 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4450 | 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] | 4451 | { |
| 4452 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4453 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4454 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4455 | unsigned int u; |
| 4456 | LY_ERR ret; |
| 4457 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4458 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4459 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4460 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4461 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4462 | return NULL; \ |
| 4463 | } \ |
| 4464 | } |
| 4465 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4466 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4467 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4468 | |
| 4469 | /* we have to add an implicit case node into the parent choice */ |
| 4470 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4471 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4472 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4473 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4474 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4475 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4476 | /* the case is already present since the child is not its first children */ |
| 4477 | return (struct lysc_node_case*)ch->cases->prev; |
| 4478 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4479 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4480 | |
| 4481 | /* explicit parent case is not present (this is its first child) */ |
| 4482 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4483 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4484 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4485 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4486 | cs->sp = node_p; |
| 4487 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4488 | /* 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] | 4489 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4490 | |
| 4491 | if (node_p->when) { |
| 4492 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4493 | 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] | 4494 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4495 | |
| 4496 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4497 | /* do not check "when" semantics in a grouping */ |
| 4498 | ly_set_add(&ctx->unres, cs, 0); |
| 4499 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4500 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4501 | 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] | 4502 | } else { |
| 4503 | LOGINT(ctx->ctx); |
| 4504 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4505 | } |
| 4506 | cs->module = ctx->mod; |
| 4507 | cs->prev = (struct lysc_node*)cs; |
| 4508 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4509 | 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] | 4510 | cs->child = child; |
| 4511 | |
| 4512 | return cs; |
| 4513 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4514 | if (cs) { |
| 4515 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4516 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4517 | return NULL; |
| 4518 | |
| 4519 | #undef UNIQUE_CHECK |
| 4520 | } |
| 4521 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4522 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4523 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4524 | * |
| 4525 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4526 | * @param[in] node Target node where the config is supposed to be changed. |
| 4527 | * @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] | 4528 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4529 | * 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] | 4530 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4531 | * @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] | 4532 | * @return LY_ERR value. |
| 4533 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4534 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4535 | 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] | 4536 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4537 | { |
| 4538 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4539 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4540 | |
| 4541 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4542 | /* nothing to do */ |
| 4543 | return LY_SUCCESS; |
| 4544 | } |
| 4545 | |
| 4546 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4547 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4548 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4549 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4550 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4551 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4552 | return LY_EVALID; |
| 4553 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4554 | node->flags |= LYS_SET_CONFIG; |
| 4555 | } else { |
| 4556 | if (node->flags & LYS_SET_CONFIG) { |
| 4557 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4558 | /* setting config flags, but have node with explicit config true */ |
| 4559 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4560 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4561 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4562 | return LY_EVALID; |
| 4563 | } |
| 4564 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4565 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4566 | return LY_SUCCESS; |
| 4567 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4568 | } |
| 4569 | node->flags &= ~LYS_CONFIG_MASK; |
| 4570 | node->flags |= config; |
| 4571 | |
| 4572 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4573 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4574 | 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] | 4575 | } |
| 4576 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4577 | return LY_SUCCESS; |
| 4578 | } |
| 4579 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4580 | /** |
| 4581 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4582 | * |
| 4583 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4584 | * the flag to such parents from a mandatory children. |
| 4585 | * |
| 4586 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4587 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4588 | * (mandatory children was removed). |
| 4589 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4590 | void |
| 4591 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4592 | { |
| 4593 | struct lysc_node *iter; |
| 4594 | |
| 4595 | if (add) { /* set flag */ |
| 4596 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4597 | parent = parent->parent) { |
| 4598 | parent->flags |= LYS_MAND_TRUE; |
| 4599 | } |
| 4600 | } else { /* unset flag */ |
| 4601 | 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] | 4602 | 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] | 4603 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4604 | /* there is another mandatory node */ |
| 4605 | return; |
| 4606 | } |
| 4607 | } |
| 4608 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4609 | parent->flags &= ~LYS_MAND_TRUE; |
| 4610 | } |
| 4611 | } |
| 4612 | } |
| 4613 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4614 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4615 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4616 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4617 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4618 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4619 | */ |
| 4620 | static void |
| 4621 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4622 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4623 | LY_ARRAY_SIZE_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4624 | size_t len; |
| 4625 | |
| 4626 | len = strlen(aug_p->nodeid); |
| 4627 | LY_ARRAY_FOR(result, v) { |
| 4628 | if (strlen(result[v]->nodeid) <= len) { |
| 4629 | continue; |
| 4630 | } |
| 4631 | if (v < LY_ARRAY_SIZE(result)) { |
| 4632 | /* move the rest of array */ |
| 4633 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4634 | break; |
| 4635 | } |
| 4636 | } |
| 4637 | result[v] = aug_p; |
| 4638 | LY_ARRAY_INCREMENT(result); |
| 4639 | } |
| 4640 | |
| 4641 | /** |
| 4642 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4643 | * |
| 4644 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4645 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4646 | * |
| 4647 | * @param[in] ctx Compile context. |
| 4648 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4649 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4650 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4651 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4652 | * @return LY_ERR value. |
| 4653 | */ |
| 4654 | LY_ERR |
| 4655 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4656 | { |
| 4657 | struct lysp_augment **result = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4658 | LY_ARRAY_SIZE_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4659 | |
| 4660 | assert(augments); |
| 4661 | |
| 4662 | /* get count of the augments in module and all its submodules */ |
| 4663 | if (aug_p) { |
| 4664 | count += LY_ARRAY_SIZE(aug_p); |
| 4665 | } |
| 4666 | LY_ARRAY_FOR(inc_p, u) { |
| 4667 | if (inc_p[u].submodule->augments) { |
| 4668 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4669 | } |
| 4670 | } |
| 4671 | |
| 4672 | if (!count) { |
| 4673 | *augments = NULL; |
| 4674 | return LY_SUCCESS; |
| 4675 | } |
| 4676 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4677 | |
| 4678 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4679 | * together, so there can be even /z/y betwwen them. */ |
| 4680 | LY_ARRAY_FOR(aug_p, u) { |
| 4681 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4682 | } |
| 4683 | LY_ARRAY_FOR(inc_p, u) { |
| 4684 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4685 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | *augments = result; |
| 4690 | return LY_SUCCESS; |
| 4691 | } |
| 4692 | |
| 4693 | /** |
| 4694 | * @brief Compile the parsed augment connecting it into its target. |
| 4695 | * |
| 4696 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4697 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4698 | * are already implemented and compiled. |
| 4699 | * |
| 4700 | * @param[in] ctx Compile context. |
| 4701 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4702 | * @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 |
| 4703 | * children in case of the augmenting uses data. |
| 4704 | * @return LY_SUCCESS on success. |
| 4705 | * @return LY_EVALID on failure. |
| 4706 | */ |
| 4707 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4708 | 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] | 4709 | { |
| 4710 | LY_ERR ret = LY_SUCCESS; |
| 4711 | struct lysp_node *node_p, *case_node_p; |
| 4712 | struct lysc_node *target; /* target target of the augment */ |
| 4713 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4714 | struct lysc_when **when, *when_shared; |
| 4715 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4716 | uint16_t flags = 0; |
| 4717 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4718 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4719 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4720 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4721 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4722 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4723 | 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] | 4724 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4725 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4726 | if (ret != LY_SUCCESS) { |
| 4727 | if (ret == LY_EDENIED) { |
| 4728 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4729 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4730 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4731 | } |
| 4732 | return LY_EVALID; |
| 4733 | } |
| 4734 | |
| 4735 | /* check for mandatory nodes |
| 4736 | * - new cases augmenting some choice can have mandatory nodes |
| 4737 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4738 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4739 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4740 | allow_mandatory = 1; |
| 4741 | } |
| 4742 | |
| 4743 | when_shared = NULL; |
| 4744 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4745 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4746 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4747 | && !((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] | 4748 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4749 | && !(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] | 4750 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4751 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4752 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4753 | return LY_EVALID; |
| 4754 | } |
| 4755 | |
| 4756 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4757 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4758 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4759 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4760 | } else { |
| 4761 | 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] | 4762 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4763 | } |
| 4764 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4765 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4766 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4767 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4768 | * 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] | 4769 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4770 | /* 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] | 4771 | 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] | 4772 | } else if (target->nodetype == LYS_CHOICE) { |
| 4773 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4774 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4775 | } else { |
| 4776 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4777 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4778 | if (!node) { |
| 4779 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4780 | break; |
| 4781 | } |
| 4782 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4783 | } |
| 4784 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4785 | 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] | 4786 | node->flags &= ~LYS_MAND_TRUE; |
| 4787 | lys_compile_mandatory_parents(target, 0); |
| 4788 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4789 | "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] | 4790 | return LY_EVALID; |
| 4791 | } |
| 4792 | |
| 4793 | /* pass augment's when to all the children */ |
| 4794 | if (aug_p->when) { |
| 4795 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4796 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4797 | 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] | 4798 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4799 | |
| 4800 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4801 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4802 | ly_set_add(&ctx->unres, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4803 | } |
| 4804 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4805 | when_shared = *when; |
| 4806 | } else { |
| 4807 | ++when_shared->refcount; |
| 4808 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4809 | |
| 4810 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4811 | /* in this case check "when" again for all children because of dummy node check */ |
| 4812 | ly_set_add(&ctx->unres, node, 0); |
| 4813 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4814 | } |
| 4815 | } |
| 4816 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4817 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4818 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4819 | switch (target->nodetype) { |
| 4820 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4821 | 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] | 4822 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4823 | 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] | 4824 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4825 | break; |
| 4826 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4827 | 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] | 4828 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4829 | 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] | 4830 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4831 | break; |
| 4832 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4833 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4834 | if (aug_p->actions) { |
| 4835 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4836 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4837 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4838 | return LY_EVALID; |
| 4839 | } |
| 4840 | if (aug_p->notifs) { |
| 4841 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4842 | "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] | 4843 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4844 | return LY_EVALID; |
| 4845 | } |
| 4846 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4847 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4848 | lysc_update_path(ctx, NULL, NULL); |
| 4849 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4850 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4851 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4852 | return ret; |
| 4853 | } |
| 4854 | |
| 4855 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4856 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4857 | * |
| 4858 | * @param[in] ctx Compile context. |
| 4859 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4860 | * @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] | 4861 | * @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] | 4862 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4863 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4864 | * 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] | 4865 | * @return LY_ERR value. |
| 4866 | */ |
| 4867 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4868 | 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] | 4869 | { |
| 4870 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4871 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4872 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4873 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4874 | return LY_EVALID; |
| 4875 | } |
| 4876 | |
| 4877 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4878 | /* check if node has default value */ |
| 4879 | if (node->nodetype & LYS_LEAF) { |
| 4880 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4881 | if (refine_flag) { |
| 4882 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4883 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4884 | return LY_EVALID; |
| 4885 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4886 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4887 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4888 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4889 | |
| 4890 | /* update the list of incomplete default values if needed */ |
| 4891 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4892 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4893 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4894 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4895 | free(leaf->dflt); |
| 4896 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4897 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4898 | } |
| 4899 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4900 | if (refine_flag) { |
| 4901 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4902 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4903 | return LY_EVALID; |
| 4904 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4905 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4906 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4907 | 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] | 4908 | return LY_EVALID; |
| 4909 | } |
| 4910 | |
| 4911 | node->flags &= ~LYS_MAND_FALSE; |
| 4912 | node->flags |= LYS_MAND_TRUE; |
| 4913 | lys_compile_mandatory_parents(node->parent, 1); |
| 4914 | } else { |
| 4915 | /* make mandatory false */ |
| 4916 | node->flags &= ~LYS_MAND_TRUE; |
| 4917 | node->flags |= LYS_MAND_FALSE; |
| 4918 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4919 | 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] | 4920 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4921 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4922 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4923 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4924 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4925 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4926 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4927 | } |
| 4928 | } |
| 4929 | return LY_SUCCESS; |
| 4930 | } |
| 4931 | |
| 4932 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4933 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4934 | * If present, also apply uses's modificators. |
| 4935 | * |
| 4936 | * @param[in] ctx Compile context |
| 4937 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4938 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4939 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4940 | * the compile context. |
| 4941 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4942 | */ |
| 4943 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4944 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent) |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4945 | { |
| 4946 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4947 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4948 | /* 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] | 4949 | struct lysc_node_container context_node_fake = |
| 4950 | {.nodetype = LYS_CONTAINER, |
| 4951 | .module = ctx->mod, |
| 4952 | .flags = parent ? parent->flags : 0, |
| 4953 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4954 | .prev = (struct lysc_node*)&context_node_fake, |
| 4955 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4956 | struct lysp_grp *grp = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4957 | LY_ARRAY_SIZE_TYPE u, v; |
| 4958 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4959 | int found; |
| 4960 | const char *id, *name, *prefix; |
| 4961 | size_t prefix_len, name_len; |
| 4962 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4963 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4964 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4965 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4966 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4967 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4968 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4969 | struct lysp_augment **augments = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4970 | LY_ARRAY_SIZE_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4971 | struct lysc_notif **notifs = NULL; |
| 4972 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4973 | |
| 4974 | /* search for the grouping definition */ |
| 4975 | found = 0; |
| 4976 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4977 | 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] | 4978 | if (prefix) { |
| 4979 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4980 | if (!mod) { |
| 4981 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4982 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4983 | return LY_EVALID; |
| 4984 | } |
| 4985 | } else { |
| 4986 | mod = ctx->mod_def; |
| 4987 | } |
| 4988 | if (mod == ctx->mod_def) { |
| 4989 | 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] | 4990 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4991 | LY_ARRAY_FOR(grp, u) { |
| 4992 | if (!strcmp(grp[u].name, name)) { |
| 4993 | grp = &grp[u]; |
| 4994 | found = 1; |
| 4995 | break; |
| 4996 | } |
| 4997 | } |
| 4998 | } |
| 4999 | } |
| 5000 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5001 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5002 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5003 | if (grp) { |
| 5004 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 5005 | if (!strcmp(grp[u].name, name)) { |
| 5006 | grp = &grp[u]; |
| 5007 | found = 1; |
| 5008 | } |
| 5009 | } |
| 5010 | } |
| 5011 | if (!found && mod->parsed->includes) { |
| 5012 | /* ... and all the submodules */ |
| 5013 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 5014 | grp = mod->parsed->includes[u].submodule->groupings; |
| 5015 | if (grp) { |
| 5016 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 5017 | if (!strcmp(grp[v].name, name)) { |
| 5018 | grp = &grp[v]; |
| 5019 | found = 1; |
| 5020 | } |
| 5021 | } |
| 5022 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5023 | } |
| 5024 | } |
| 5025 | } |
| 5026 | if (!found) { |
| 5027 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5028 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 5029 | return LY_EVALID; |
| 5030 | } |
| 5031 | |
| 5032 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 5033 | grp_stack_count = ctx->groupings.count; |
| 5034 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 5035 | if (grp_stack_count == ctx->groupings.count) { |
| 5036 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 5037 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5038 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 5039 | return LY_EVALID; |
| 5040 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5041 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5042 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 5043 | grp->flags |= LYS_USED_GRP; |
| 5044 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5045 | |
| 5046 | /* switch context's mod_def */ |
| 5047 | mod_old = ctx->mod_def; |
| 5048 | ctx->mod_def = mod; |
| 5049 | |
| 5050 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5051 | 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] | 5052 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5053 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5054 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5055 | /* 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] | 5056 | LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5057 | |
| 5058 | /* some preparation for applying refines */ |
| 5059 | if (grp->data == node_p) { |
| 5060 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 5061 | if (parent) { |
| 5062 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 5063 | } else if (ctx->mod->compiled->data) { |
| 5064 | child = ctx->mod->compiled->data; |
| 5065 | } else { |
| 5066 | child = NULL; |
| 5067 | } |
| 5068 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5069 | } |
| 5070 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5071 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5072 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5073 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5074 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5075 | /* 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] | 5076 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5077 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5078 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5079 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 5080 | |
| 5081 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5082 | /* do not check "when" semantics in a grouping */ |
| 5083 | ly_set_add(&ctx->unres, child, 0); |
| 5084 | } |
| 5085 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5086 | when_shared = *when; |
| 5087 | } else { |
| 5088 | ++when_shared->refcount; |
| 5089 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5090 | |
| 5091 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5092 | /* in this case check "when" again for all children because of dummy node check */ |
| 5093 | ly_set_add(&ctx->unres, child, 0); |
| 5094 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5095 | } |
| 5096 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5097 | } |
| 5098 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5099 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5100 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5101 | /* fix child link of our fake container to point to the first child of the original list */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5102 | context_node_fake.child->prev = parent ? lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5103 | } |
| 5104 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5105 | /* compile actions */ |
| 5106 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 5107 | if (actions) { |
| 5108 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5109 | 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] | 5110 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 5111 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 5112 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 5113 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 5114 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5115 | } |
| 5116 | } |
| 5117 | |
| 5118 | /* compile notifications */ |
| 5119 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 5120 | if (notifs) { |
| 5121 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5122 | 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] | 5123 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 5124 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 5125 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 5126 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 5127 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5128 | } |
| 5129 | } |
| 5130 | |
| 5131 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5132 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5133 | 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] | 5134 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5135 | 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] | 5136 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5137 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5138 | /* reload previous context's mod_def */ |
| 5139 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5140 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5141 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5142 | /* apply refine */ |
| 5143 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5144 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 5145 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5146 | 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] | 5147 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5148 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5149 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5150 | |
| 5151 | /* default value */ |
| 5152 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5153 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5154 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5155 | "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_SIZE_TYPE" default values.", |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5156 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5157 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5158 | } |
| 5159 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 5160 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5161 | "Invalid refine of default - %s cannot hold default value(s).", |
| 5162 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5163 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5164 | } |
| 5165 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5166 | struct ly_err_item *err = NULL; |
| 5167 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 5168 | if (leaf->dflt) { |
| 5169 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5170 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5171 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5172 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5173 | } else { |
| 5174 | /* prepare a new one */ |
| 5175 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 5176 | leaf->dflt->realtype = leaf->type; |
| 5177 | } |
| 5178 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5179 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5180 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 5181 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5182 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5183 | leaf->dflt->realtype->refcount++; |
| 5184 | if (err) { |
| 5185 | ly_err_print(err); |
| 5186 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5187 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 5188 | ly_err_free(err); |
| 5189 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5190 | if (rc == LY_EINCOMPLETE) { |
| 5191 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5192 | 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] | 5193 | |
| 5194 | /* but in general result is so far ok */ |
| 5195 | rc = LY_SUCCESS; |
| 5196 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5197 | LY_CHECK_GOTO(rc, cleanup); |
| 5198 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5199 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5200 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 5201 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5202 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5203 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5204 | "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] | 5205 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5206 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5207 | |
| 5208 | /* remove previous set of default values */ |
| 5209 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5210 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5211 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 5212 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 5213 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5214 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5215 | LY_ARRAY_FREE(llist->dflts); |
| 5216 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5217 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5218 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5219 | |
| 5220 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5221 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5222 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5223 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5224 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5225 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5226 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5227 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5228 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 5229 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5230 | rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]), |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5231 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5232 | lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5233 | llist->dflts[u]->realtype->refcount++; |
| 5234 | if (err) { |
| 5235 | ly_err_print(err); |
| 5236 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5237 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 5238 | ly_err_free(err); |
| 5239 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5240 | if (rc == LY_EINCOMPLETE) { |
| 5241 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5242 | 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] | 5243 | |
| 5244 | /* but in general result is so far ok */ |
| 5245 | rc = LY_SUCCESS; |
| 5246 | } |
| 5247 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5248 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5249 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5250 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5251 | if (((struct lysc_node_choice*)node)->dflt) { |
| 5252 | /* unset LYS_SET_DFLT from the current default case */ |
| 5253 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 5254 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5255 | 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] | 5256 | } |
| 5257 | } |
| 5258 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5259 | /* description */ |
| 5260 | if (rfn->dsc) { |
| 5261 | FREE_STRING(ctx->ctx, node->dsc); |
| 5262 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 5263 | } |
| 5264 | |
| 5265 | /* reference */ |
| 5266 | if (rfn->ref) { |
| 5267 | FREE_STRING(ctx->ctx, node->ref); |
| 5268 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 5269 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5270 | |
| 5271 | /* config */ |
| 5272 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5273 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5274 | 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] | 5275 | } else { |
| 5276 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5277 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5278 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5279 | } |
| 5280 | |
| 5281 | /* mandatory */ |
| 5282 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5283 | 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] | 5284 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5285 | |
| 5286 | /* presence */ |
| 5287 | if (rfn->presence) { |
| 5288 | if (node->nodetype != LYS_CONTAINER) { |
| 5289 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5290 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5291 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5292 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5293 | } |
| 5294 | node->flags |= LYS_PRESENCE; |
| 5295 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5296 | |
| 5297 | /* must */ |
| 5298 | if (rfn->musts) { |
| 5299 | switch (node->nodetype) { |
| 5300 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5301 | 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] | 5302 | break; |
| 5303 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5304 | 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] | 5305 | break; |
| 5306 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5307 | 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] | 5308 | break; |
| 5309 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5310 | 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] | 5311 | break; |
| 5312 | case LYS_ANYXML: |
| 5313 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5314 | 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] | 5315 | break; |
| 5316 | default: |
| 5317 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5318 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5319 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5320 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5321 | } |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 5322 | ly_set_add(&ctx->unres, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5323 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5324 | |
| 5325 | /* min/max-elements */ |
| 5326 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5327 | switch (node->nodetype) { |
| 5328 | case LYS_LEAFLIST: |
| 5329 | if (rfn->flags & LYS_SET_MAX) { |
| 5330 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5331 | } |
| 5332 | if (rfn->flags & LYS_SET_MIN) { |
| 5333 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5334 | if (rfn->min) { |
| 5335 | node->flags |= LYS_MAND_TRUE; |
| 5336 | lys_compile_mandatory_parents(node->parent, 1); |
| 5337 | } else { |
| 5338 | node->flags &= ~LYS_MAND_TRUE; |
| 5339 | lys_compile_mandatory_parents(node->parent, 0); |
| 5340 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5341 | } |
| 5342 | break; |
| 5343 | case LYS_LIST: |
| 5344 | if (rfn->flags & LYS_SET_MAX) { |
| 5345 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5346 | } |
| 5347 | if (rfn->flags & LYS_SET_MIN) { |
| 5348 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5349 | if (rfn->min) { |
| 5350 | node->flags |= LYS_MAND_TRUE; |
| 5351 | lys_compile_mandatory_parents(node->parent, 1); |
| 5352 | } else { |
| 5353 | node->flags &= ~LYS_MAND_TRUE; |
| 5354 | lys_compile_mandatory_parents(node->parent, 0); |
| 5355 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5356 | } |
| 5357 | break; |
| 5358 | default: |
| 5359 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5360 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5361 | (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] | 5362 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5363 | } |
| 5364 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5365 | |
| 5366 | /* if-feature */ |
| 5367 | if (rfn->iffeatures) { |
| 5368 | /* 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] | 5369 | 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] | 5370 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5371 | |
| 5372 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5373 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5374 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5375 | /* 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] | 5376 | for (uint32_t i = 0; i < refined.count; ++i) { |
| 5377 | node = (struct lysc_node*)refined.objs[i]; |
| 5378 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5379 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5380 | |
| 5381 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5382 | if ((node->flags & LYS_MAND_TRUE) && |
| 5383 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5384 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5385 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5386 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5387 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5391 | if (node->nodetype == LYS_LIST) { |
| 5392 | min = ((struct lysc_node_list*)node)->min; |
| 5393 | max = ((struct lysc_node_list*)node)->max; |
| 5394 | } else { |
| 5395 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5396 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5397 | } |
| 5398 | if (min > max) { |
| 5399 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5400 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5401 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5402 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5403 | } |
| 5404 | } |
| 5405 | } |
| 5406 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5407 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5408 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5409 | |
| 5410 | cleanup: |
| 5411 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5412 | if (context_node_fake.child) { |
| 5413 | context_node_fake.child->prev = child; |
| 5414 | } |
| 5415 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5416 | child->parent = parent; |
| 5417 | } |
| 5418 | |
| 5419 | if (uses_p->augments || uses_p->refines) { |
| 5420 | /* 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] | 5421 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5422 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5423 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5424 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5425 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5426 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5427 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5428 | } |
| 5429 | } |
| 5430 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5431 | /* reload previous context's mod_def */ |
| 5432 | ctx->mod_def = mod_old; |
| 5433 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5434 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5435 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5436 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5437 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5438 | |
| 5439 | return ret; |
| 5440 | } |
| 5441 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5442 | static int |
| 5443 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5444 | { |
| 5445 | struct lysp_node *iter; |
| 5446 | int len = 0; |
| 5447 | |
| 5448 | *path = NULL; |
| 5449 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5450 | char *s = *path; |
| 5451 | char *id; |
| 5452 | |
| 5453 | switch (iter->nodetype) { |
| 5454 | case LYS_USES: |
| 5455 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5456 | break; |
| 5457 | case LYS_GROUPING: |
| 5458 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5459 | break; |
| 5460 | case LYS_AUGMENT: |
| 5461 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5462 | break; |
| 5463 | default: |
| 5464 | id = strdup(iter->name); |
| 5465 | break; |
| 5466 | } |
| 5467 | |
| 5468 | if (!iter->parent) { |
| 5469 | /* print prefix */ |
| 5470 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5471 | } else { |
| 5472 | /* prefix is the same as in parent */ |
| 5473 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5474 | } |
| 5475 | free(s); |
| 5476 | free(id); |
| 5477 | } |
| 5478 | |
| 5479 | if (len < 0) { |
| 5480 | free(*path); |
| 5481 | *path = NULL; |
| 5482 | } else if (len == 0) { |
| 5483 | *path = strdup("/"); |
| 5484 | len = 1; |
| 5485 | } |
| 5486 | return len; |
| 5487 | } |
| 5488 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5489 | /** |
| 5490 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5491 | * |
| 5492 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5493 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5494 | */ |
| 5495 | static LY_ERR |
| 5496 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5497 | { |
| 5498 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5499 | char *path; |
| 5500 | int len; |
| 5501 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5502 | struct lysp_node_uses fake_uses = { |
| 5503 | .parent = node_p, |
| 5504 | .nodetype = LYS_USES, |
| 5505 | .flags = 0, .next = NULL, |
| 5506 | .name = grp->name, |
| 5507 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5508 | .refines = NULL, .augments = NULL |
| 5509 | }; |
| 5510 | struct lysc_node_container fake_container = { |
| 5511 | .nodetype = LYS_CONTAINER, |
| 5512 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5513 | .module = ctx->mod, |
| 5514 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5515 | .prev = (struct lysc_node*)&fake_container, |
| 5516 | .name = "fake", |
| 5517 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5518 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5519 | }; |
| 5520 | |
| 5521 | if (grp->parent) { |
| 5522 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5523 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5524 | |
| 5525 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5526 | if (len < 0) { |
| 5527 | LOGMEM(ctx->ctx); |
| 5528 | return LY_EMEM; |
| 5529 | } |
| 5530 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5531 | ctx->path_len = (uint16_t)len; |
| 5532 | free(path); |
| 5533 | |
| 5534 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5535 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5536 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5537 | lysc_update_path(ctx, NULL, NULL); |
| 5538 | lysc_update_path(ctx, NULL, NULL); |
| 5539 | |
| 5540 | ctx->path_len = 1; |
| 5541 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5542 | |
| 5543 | /* cleanup */ |
| 5544 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5545 | |
| 5546 | return ret; |
| 5547 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5548 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5549 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5550 | * @brief Compile parsed schema node information. |
| 5551 | * @param[in] ctx Compile context |
| 5552 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5553 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5554 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5555 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5556 | * @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). |
| 5557 | * 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] | 5558 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5559 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5560 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5561 | 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] | 5562 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5563 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5564 | struct lysc_node *node; |
| 5565 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5566 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5567 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5568 | 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] | 5569 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5570 | if (node_p->nodetype != LYS_USES) { |
| 5571 | lysc_update_path(ctx, parent, node_p->name); |
| 5572 | } else { |
| 5573 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5574 | lysc_update_path(ctx, NULL, node_p->name); |
| 5575 | } |
| 5576 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5577 | switch (node_p->nodetype) { |
| 5578 | case LYS_CONTAINER: |
| 5579 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5580 | node_compile_spec = lys_compile_node_container; |
| 5581 | break; |
| 5582 | case LYS_LEAF: |
| 5583 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5584 | node_compile_spec = lys_compile_node_leaf; |
| 5585 | break; |
| 5586 | case LYS_LIST: |
| 5587 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5588 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5589 | break; |
| 5590 | case LYS_LEAFLIST: |
| 5591 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5592 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5593 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5594 | case LYS_CHOICE: |
| 5595 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5596 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5597 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5598 | case LYS_ANYXML: |
| 5599 | case LYS_ANYDATA: |
| 5600 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5601 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5602 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5603 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5604 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5605 | lysc_update_path(ctx, NULL, NULL); |
| 5606 | lysc_update_path(ctx, NULL, NULL); |
| 5607 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5608 | default: |
| 5609 | LOGINT(ctx->ctx); |
| 5610 | return LY_EINT; |
| 5611 | } |
| 5612 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5613 | node->nodetype = node_p->nodetype; |
| 5614 | node->module = ctx->mod; |
| 5615 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5616 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5617 | |
| 5618 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5619 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5620 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5621 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5622 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5623 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5624 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5625 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5626 | node->flags |= LYS_CONFIG_R; |
| 5627 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5628 | /* config not explicitely set, inherit it from parent */ |
| 5629 | if (parent) { |
| 5630 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5631 | } else { |
| 5632 | /* default is config true */ |
| 5633 | node->flags |= LYS_CONFIG_W; |
| 5634 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5635 | } else { |
| 5636 | /* config set explicitely */ |
| 5637 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5638 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5639 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5640 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5641 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5642 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5643 | goto error; |
| 5644 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5645 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5646 | /* *list ordering */ |
| 5647 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5648 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5649 | 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] | 5650 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5651 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5652 | node->flags &= ~LYS_ORDBY_MASK; |
| 5653 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5654 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5655 | /* default ordering is system */ |
| 5656 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5657 | } |
| 5658 | } |
| 5659 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5660 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5661 | * 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] | 5662 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5663 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5664 | * 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] | 5665 | 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] | 5666 | } |
| 5667 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5668 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5669 | node->sp = node_p; |
| 5670 | } |
| 5671 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5672 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5673 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5674 | if (node_p->when) { |
| 5675 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5676 | 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] | 5677 | |
| 5678 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5679 | /* do not check "when" semantics in a grouping */ |
| 5680 | ly_set_add(&ctx->unres, node, 0); |
| 5681 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5682 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5683 | 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] | 5684 | |
| 5685 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5686 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5687 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5688 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5689 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5690 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5691 | if (node->flags & LYS_MAND_TRUE) { |
| 5692 | lys_compile_mandatory_parents(parent, 1); |
| 5693 | } |
| 5694 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5695 | lysc_update_path(ctx, NULL, NULL); |
| 5696 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5697 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5698 | if (parent) { |
| 5699 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5700 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5701 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5702 | } else { |
| 5703 | lysc_update_path(ctx, parent, node->name); |
| 5704 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5705 | 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] | 5706 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5707 | if (uses_status) { |
| 5708 | |
| 5709 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5710 | /* 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] | 5711 | * it directly gets the same status flags as the choice; |
| 5712 | * 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] | 5713 | 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] | 5714 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5715 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5716 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5717 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5718 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5719 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5720 | 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] | 5721 | |
| 5722 | if (parent->nodetype == LYS_CHOICE) { |
| 5723 | lysc_update_path(ctx, NULL, NULL); |
| 5724 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5725 | } else { |
| 5726 | /* top-level element */ |
| 5727 | if (!ctx->mod->compiled->data) { |
| 5728 | ctx->mod->compiled->data = node; |
| 5729 | } else { |
| 5730 | /* insert at the end of the module's top-level nodes list */ |
| 5731 | ctx->mod->compiled->data->prev->next = node; |
| 5732 | node->prev = ctx->mod->compiled->data->prev; |
| 5733 | ctx->mod->compiled->data->prev = node; |
| 5734 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5735 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5736 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5737 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5738 | return LY_EVALID; |
| 5739 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5740 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5741 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5742 | |
| 5743 | return LY_SUCCESS; |
| 5744 | |
| 5745 | error: |
| 5746 | lysc_node_free(ctx->ctx, node); |
| 5747 | return ret; |
| 5748 | } |
| 5749 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5750 | static void |
| 5751 | lysc_disconnect(struct lysc_node *node) |
| 5752 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5753 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5754 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5755 | int remove_cs = 0; |
| 5756 | |
| 5757 | parent = node->parent; |
| 5758 | |
| 5759 | /* parent's first child */ |
| 5760 | if (!parent) { |
| 5761 | return; |
| 5762 | } |
| 5763 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5764 | cs = (struct lysc_node_case*)node; |
| 5765 | } else if (parent->nodetype == LYS_CASE) { |
| 5766 | /* disconnecting some node in a case */ |
| 5767 | cs = (struct lysc_node_case*)parent; |
| 5768 | parent = cs->parent; |
| 5769 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5770 | if (child == node) { |
| 5771 | if (cs->child == child) { |
| 5772 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5773 | /* case with a single child -> remove also the case */ |
| 5774 | child->parent = NULL; |
| 5775 | remove_cs = 1; |
| 5776 | } else { |
| 5777 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5778 | } |
| 5779 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5780 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5781 | } |
| 5782 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5783 | if (!remove_cs) { |
| 5784 | cs = NULL; |
| 5785 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5786 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5787 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | if (cs) { |
| 5791 | if (remove_cs) { |
| 5792 | /* cs has only one child which is being also removed */ |
| 5793 | lysc_disconnect((struct lysc_node*)cs); |
| 5794 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5795 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5796 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5797 | /* default case removed */ |
| 5798 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5799 | } |
| 5800 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5801 | /* first case removed */ |
| 5802 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5803 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5804 | if (cs->child) { |
| 5805 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5806 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5807 | prev = cs->child->prev; |
| 5808 | } /* else all the children are under a single case */ |
| 5809 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5810 | if (child->parent != (struct lysc_node*)cs) { |
| 5811 | break; |
| 5812 | } |
| 5813 | lysc_node_free(node->module->ctx, child); |
| 5814 | } |
| 5815 | if (prev) { |
| 5816 | if (prev->next) { |
| 5817 | prev->next = child; |
| 5818 | } |
| 5819 | if (child) { |
| 5820 | child->prev = prev; |
| 5821 | } else { |
| 5822 | /* link from the first child under the cases */ |
| 5823 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5824 | } |
| 5825 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5826 | } |
| 5827 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5828 | } |
| 5829 | |
| 5830 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5831 | if (node->prev->next) { |
| 5832 | node->prev->next = node->next; |
| 5833 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5834 | if (node->next) { |
| 5835 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5836 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5837 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5838 | if (child) { |
| 5839 | child->prev = node->prev; |
| 5840 | } |
| 5841 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5842 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5843 | } |
| 5844 | } |
| 5845 | |
| 5846 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5847 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5848 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5849 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5850 | struct ly_set devs_p = {0}; |
| 5851 | struct ly_set targets = {0}; |
| 5852 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5853 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5854 | struct lysc_action *rpcs; |
| 5855 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5856 | struct lysp_deviation *dev; |
| 5857 | struct lysp_deviate *d, **dp_new; |
| 5858 | struct lysp_deviate_add *d_add; |
| 5859 | struct lysp_deviate_del *d_del; |
| 5860 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5861 | LY_ARRAY_SIZE_TYPE u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5862 | struct lysc_deviation { |
| 5863 | const char *nodeid; |
| 5864 | struct lysc_node *target; /* target node of the deviation */ |
| 5865 | struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5866 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5867 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5868 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5869 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5870 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5871 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5872 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5873 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5874 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5875 | |
| 5876 | /* get all deviations from the module and all its submodules ... */ |
| 5877 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5878 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5879 | } |
| 5880 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5881 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5882 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5883 | } |
| 5884 | } |
| 5885 | if (!devs_p.count) { |
| 5886 | /* nothing to do */ |
| 5887 | return LY_SUCCESS; |
| 5888 | } |
| 5889 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5890 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5891 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5892 | /* ... and group them by the target node */ |
| 5893 | devs = calloc(devs_p.count, sizeof *devs); |
| 5894 | for (u = 0; u < devs_p.count; ++u) { |
| 5895 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5896 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5897 | |
| 5898 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5899 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5900 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5901 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5902 | /* move the target pointer to input/output to make them different from the action and |
| 5903 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5904 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5905 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5906 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5907 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5908 | flags |= LYSC_OPT_INTERNAL; |
| 5909 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5910 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5911 | flags |= LYSC_OPT_INTERNAL; |
| 5912 | } |
| 5913 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5914 | /* insert into the set of targets with duplicity detection */ |
| 5915 | i = ly_set_add(&targets, target, 0); |
| 5916 | if (!devs[i]) { |
| 5917 | /* new record */ |
| 5918 | devs[i] = calloc(1, sizeof **devs); |
| 5919 | devs[i]->target = target; |
| 5920 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5921 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5922 | } |
| 5923 | /* add deviates into the deviation's list of deviates */ |
| 5924 | for (d = dev->deviates; d; d = d->next) { |
| 5925 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5926 | *dp_new = d; |
| 5927 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5928 | devs[i]->not_supported = 1; |
| 5929 | } |
| 5930 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5931 | |
| 5932 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5933 | } |
| 5934 | |
| 5935 | /* MACROS for deviates checking */ |
| 5936 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5937 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5938 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5939 | goto cleanup; \ |
| 5940 | } |
| 5941 | |
| 5942 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5943 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5944 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_SIZE_TYPE") %s properties.", \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5945 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5946 | goto cleanup; \ |
| 5947 | } |
| 5948 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5949 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5950 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5951 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5952 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5953 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5954 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5955 | goto cleanup; \ |
| 5956 | } |
| 5957 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5958 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5959 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5960 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5961 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5962 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5963 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5964 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5965 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5966 | goto cleanup; \ |
| 5967 | } |
| 5968 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5969 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5970 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5971 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5972 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5973 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5974 | goto cleanup; \ |
| 5975 | } |
| 5976 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5977 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5978 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5979 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5980 | goto cleanup; \ |
| 5981 | } |
| 5982 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5983 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5984 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5985 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5986 | "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5987 | goto cleanup; \ |
| 5988 | } |
| 5989 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5990 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5991 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5992 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5993 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5994 | if (!strcmp(((TYPE)devs[u]->target)->ARRAY_TRG[y]VALMEMBER_CMP, d_del->ARRAY_DEV[x]VALMEMBER)) { break; } \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5995 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5996 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5997 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5998 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5999 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6000 | goto cleanup; \ |
| 6001 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6002 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 6003 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 6004 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 6005 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 6006 | (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6007 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6008 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 6009 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 6010 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6011 | } |
| 6012 | |
| 6013 | /* apply deviations */ |
| 6014 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6015 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 6016 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 6017 | struct ly_err_item *err = NULL; |
| 6018 | |
| 6019 | dflt = NULL; |
| 6020 | changed_type = 0; |
| 6021 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6022 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 6023 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6024 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 6025 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 6026 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6027 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 6028 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6029 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 6030 | } |
| 6031 | } |
| 6032 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6033 | /* not-supported */ |
| 6034 | if (devs[u]->not_supported) { |
| 6035 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6036 | LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_SIZE_TYPE") deviates on node \"%s\" since the node is not-supported.", |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6037 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 6038 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6039 | |
| 6040 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6041 | if (devs[u]->target->parent) { \ |
| 6042 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 6043 | } else { \ |
| 6044 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 6045 | } \ |
| 6046 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6047 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 6048 | } \ |
| 6049 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 6050 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6051 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6052 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6053 | } |
| 6054 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6055 | if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6056 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6057 | /* remove RPC's/action's input */ |
| 6058 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 6059 | memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6060 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 6061 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6062 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6063 | /* remove RPC's/action's output */ |
| 6064 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 6065 | memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6066 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 6067 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6068 | } else { |
| 6069 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6070 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6071 | } |
| 6072 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6073 | /* remove Notification */ |
| 6074 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6075 | } else { |
| 6076 | /* remove the target node */ |
| 6077 | lysc_disconnect(devs[u]->target); |
| 6078 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 6079 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6080 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6081 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6082 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6083 | continue; |
| 6084 | } |
| 6085 | |
| 6086 | /* list of deviates (not-supported is not present in the list) */ |
| 6087 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 6088 | d = devs[u]->deviates[v]; |
| 6089 | |
| 6090 | switch (d->mod) { |
| 6091 | case LYS_DEV_ADD: |
| 6092 | d_add = (struct lysp_deviate_add*)d; |
| 6093 | /* [units-stmt] */ |
| 6094 | if (d_add->units) { |
| 6095 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 6096 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 6097 | |
| 6098 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6099 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6100 | } |
| 6101 | |
| 6102 | /* *must-stmt */ |
| 6103 | if (d_add->musts) { |
| 6104 | switch (devs[u]->target->nodetype) { |
| 6105 | case LYS_CONTAINER: |
| 6106 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6107 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 6108 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6109 | break; |
| 6110 | case LYS_LEAF: |
| 6111 | case LYS_LEAFLIST: |
| 6112 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6113 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 6114 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6115 | break; |
| 6116 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6117 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 6118 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6119 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6120 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6121 | case LYS_ACTION: |
| 6122 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6123 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 6124 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6125 | break; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 6126 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6127 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 6128 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6129 | break; |
| 6130 | } |
| 6131 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6132 | default: |
| 6133 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6134 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6135 | goto cleanup; |
| 6136 | } |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6137 | ly_set_add(&ctx->unres, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6138 | } |
| 6139 | |
| 6140 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6141 | if (d_add->uniques) { |
| 6142 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 6143 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 6144 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6145 | |
| 6146 | /* *default-stmt */ |
| 6147 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6148 | switch (devs[u]->target->nodetype) { |
| 6149 | case LYS_LEAF: |
| 6150 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6151 | DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt, dflt_mod); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6152 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6153 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6154 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6155 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6156 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6157 | } else { |
| 6158 | /* prepare new default value storage */ |
| 6159 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6160 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6161 | dflt = d_add->dflts[0]; |
| 6162 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 6163 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6164 | /* mark the new default values as leaf's own */ |
| 6165 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6166 | break; |
| 6167 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6168 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6169 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6170 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6171 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6172 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6173 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6174 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6175 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6176 | LY_ARRAY_FREE(llist->dflts); |
| 6177 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6178 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6179 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6180 | } |
| 6181 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6182 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6183 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 6184 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6185 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6186 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 6187 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6188 | LY_ARRAY_INCREMENT(llist->dflts); |
| 6189 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 6190 | llist->dflts[x]->realtype = llist->type; |
| 6191 | rc = llist->type->plugin->store(ctx->ctx, llist->type, d_add->dflts[x - y], strlen(d_add->dflts[x - y]), |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6192 | LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6193 | (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6194 | llist->dflts[x]->realtype->refcount++; |
| 6195 | if (err) { |
| 6196 | ly_err_print(err); |
| 6197 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6198 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 6199 | d_add->dflts[x - y], err->msg); |
| 6200 | ly_err_free(err); |
| 6201 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6202 | if (rc == LY_EINCOMPLETE) { |
| 6203 | /* postpone default compilation when the tree is complete */ |
| 6204 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6205 | |
| 6206 | /* but in general result is so far ok */ |
| 6207 | rc = LY_SUCCESS; |
| 6208 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6209 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6210 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6211 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6212 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 6213 | break; |
| 6214 | case LYS_CHOICE: |
| 6215 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 6216 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 6217 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 6218 | * to allow making the default case even the augmented case from the deviating module */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6219 | if (lys_compile_deviation_set_choice_dflt(ctx, d_add->dflts[0], (struct lysc_node_choice*)devs[u]->target)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6220 | goto cleanup; |
| 6221 | } |
| 6222 | break; |
| 6223 | default: |
| 6224 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6225 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6226 | goto cleanup; |
| 6227 | } |
| 6228 | } |
| 6229 | |
| 6230 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6231 | if (d_add->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6232 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6233 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6234 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 6235 | goto cleanup; |
| 6236 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6237 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6238 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 6239 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6240 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6241 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 6242 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6243 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 6244 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6245 | goto cleanup; |
| 6246 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6247 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6248 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6249 | |
| 6250 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6251 | if (d_add->flags & LYS_MAND_MASK) { |
| 6252 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 6253 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6254 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 6255 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6256 | goto cleanup; |
| 6257 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6258 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6259 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6260 | |
| 6261 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6262 | if (d_add->flags & LYS_SET_MIN) { |
| 6263 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6264 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6265 | /* change value */ |
| 6266 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 6267 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6268 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6269 | /* change value */ |
| 6270 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 6271 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6272 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6273 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 6274 | goto cleanup; |
| 6275 | } |
| 6276 | if (d_add->min) { |
| 6277 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6278 | } |
| 6279 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6280 | |
| 6281 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6282 | if (d_add->flags & LYS_SET_MAX) { |
| 6283 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6284 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6285 | /* change value */ |
| 6286 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6287 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6288 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6289 | /* change value */ |
| 6290 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6291 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6292 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6293 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6294 | goto cleanup; |
| 6295 | } |
| 6296 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6297 | |
| 6298 | break; |
| 6299 | case LYS_DEV_DELETE: |
| 6300 | d_del = (struct lysp_deviate_del*)d; |
| 6301 | |
| 6302 | /* [units-stmt] */ |
| 6303 | if (d_del->units) { |
| 6304 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6305 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6306 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6307 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6308 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6309 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6310 | goto cleanup; |
| 6311 | } |
| 6312 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6313 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6314 | } |
| 6315 | |
| 6316 | /* *must-stmt */ |
| 6317 | if (d_del->musts) { |
| 6318 | switch (devs[u]->target->nodetype) { |
| 6319 | case LYS_CONTAINER: |
| 6320 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6321 | DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6322 | break; |
| 6323 | case LYS_LEAF: |
| 6324 | case LYS_LEAFLIST: |
| 6325 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6326 | DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6327 | break; |
| 6328 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6329 | DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6330 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6331 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6332 | case LYS_ACTION: |
| 6333 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6334 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6335 | break; |
| 6336 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6337 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6338 | break; |
| 6339 | } |
| 6340 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6341 | default: |
| 6342 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6343 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6344 | goto cleanup; |
| 6345 | } |
| 6346 | } |
| 6347 | |
| 6348 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6349 | if (d_del->uniques) { |
| 6350 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6351 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6352 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6353 | LY_ARRAY_FOR(list->uniques, z) { |
| 6354 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6355 | nodeid = strpbrk(name, " \t\n"); |
| 6356 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 6357 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6358 | break; |
| 6359 | } |
| 6360 | while (isspace(*nodeid)) { |
| 6361 | ++nodeid; |
| 6362 | } |
| 6363 | } else { |
| 6364 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6365 | break; |
| 6366 | } |
| 6367 | } |
| 6368 | } |
| 6369 | if (!name) { |
| 6370 | /* complete match - remove the unique */ |
| 6371 | LY_ARRAY_DECREMENT(list->uniques); |
| 6372 | LY_ARRAY_FREE(list->uniques[z]); |
| 6373 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6374 | --z; |
| 6375 | break; |
| 6376 | } |
| 6377 | } |
| 6378 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6379 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6380 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6381 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6382 | goto cleanup; |
| 6383 | } |
| 6384 | } |
| 6385 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6386 | LY_ARRAY_FREE(list->uniques); |
| 6387 | list->uniques = NULL; |
| 6388 | } |
| 6389 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6390 | |
| 6391 | /* *default-stmt */ |
| 6392 | if (d_del->dflts) { |
| 6393 | switch (devs[u]->target->nodetype) { |
| 6394 | case LYS_LEAF: |
| 6395 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6396 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6397 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6398 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6399 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6400 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6401 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6402 | if (i) { |
| 6403 | free((char*)dflt); |
| 6404 | } |
| 6405 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6406 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6407 | d_del->dflts[0], dflt); |
| 6408 | goto cleanup; |
| 6409 | } |
| 6410 | if (i) { |
| 6411 | free((char*)dflt); |
| 6412 | } |
| 6413 | dflt = NULL; |
| 6414 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6415 | /* update the list of incomplete default values if needed */ |
| 6416 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6417 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6418 | /* remove the default specification */ |
| 6419 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6420 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6421 | free(leaf->dflt); |
| 6422 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6423 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6424 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6425 | break; |
| 6426 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6427 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6428 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6429 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6430 | dflt = llist->type->plugin->print(llist->dflts[y], LYD_XML, lys_get_prefix, llist->dflts_mods[y], &i); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6431 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6432 | if (i) { |
| 6433 | free((char*)dflt); |
| 6434 | } |
| 6435 | dflt = NULL; |
| 6436 | break; |
| 6437 | } |
| 6438 | if (i) { |
| 6439 | free((char*)dflt); |
| 6440 | } |
| 6441 | dflt = NULL; |
| 6442 | } |
| 6443 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6444 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6445 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6446 | goto cleanup; |
| 6447 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6448 | |
| 6449 | /* update the list of incomplete default values if needed */ |
| 6450 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6451 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6452 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6453 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6454 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6455 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6456 | free(llist->dflts[y]); |
| 6457 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts)); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6458 | memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_SIZE(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods)); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6459 | } |
| 6460 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6461 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6462 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6463 | LY_ARRAY_FREE(llist->dflts); |
| 6464 | llist->dflts = NULL; |
| 6465 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6466 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6467 | break; |
| 6468 | case LYS_CHOICE: |
| 6469 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6470 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6471 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6472 | LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6473 | if (prefix) { |
| 6474 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6475 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6476 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6477 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6478 | "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6479 | goto cleanup; |
| 6480 | } |
| 6481 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6482 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6483 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6484 | "The prefix does not match the default case's module.", d_del->dflts[0]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6485 | goto cleanup; |
| 6486 | } |
| 6487 | } |
| 6488 | /* else { |
| 6489 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6490 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6491 | * the module of the default case, just matching its name */ |
| 6492 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6493 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6494 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6495 | d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6496 | goto cleanup; |
| 6497 | } |
| 6498 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6499 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6500 | break; |
| 6501 | default: |
| 6502 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6503 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6504 | goto cleanup; |
| 6505 | } |
| 6506 | } |
| 6507 | |
| 6508 | break; |
| 6509 | case LYS_DEV_REPLACE: |
| 6510 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6511 | |
| 6512 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6513 | if (d_rpl->type) { |
| 6514 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6515 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6516 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6517 | |
| 6518 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6519 | /* the target has default from the previous type - remove it */ |
| 6520 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6521 | /* update the list of incomplete default values if needed */ |
| 6522 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6523 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6524 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6525 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6526 | free(leaf->dflt); |
| 6527 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6528 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6529 | } else { /* LYS_LEAFLIST */ |
| 6530 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6531 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6532 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6533 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6534 | free(llist->dflts[x]); |
| 6535 | } |
| 6536 | LY_ARRAY_FREE(llist->dflts); |
| 6537 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6538 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6539 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6540 | } |
| 6541 | } |
| 6542 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6543 | /* there is no default value, do not set changed_type after type compilation |
| 6544 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6545 | changed_type = -1; |
| 6546 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6547 | LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, (struct lysc_node_leaf*)devs[u]->target), cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6548 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6549 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6550 | |
| 6551 | /* [units-stmt] */ |
| 6552 | if (d_rpl->units) { |
| 6553 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6554 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6555 | units, "replacing", "units", d_rpl->units); |
| 6556 | |
| 6557 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6558 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6559 | } |
| 6560 | |
| 6561 | /* [default-stmt] */ |
| 6562 | if (d_rpl->dflt) { |
| 6563 | switch (devs[u]->target->nodetype) { |
| 6564 | case LYS_LEAF: |
| 6565 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6566 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6567 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6568 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6569 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6570 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6571 | dflt = d_rpl->dflt; |
| 6572 | /* parsing is done at the end after possible replace of the leaf's type */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6573 | break; |
| 6574 | case LYS_CHOICE: |
| 6575 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6576 | if (lys_compile_deviation_set_choice_dflt(ctx, d_rpl->dflt, (struct lysc_node_choice*)devs[u]->target)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6577 | goto cleanup; |
| 6578 | } |
| 6579 | break; |
| 6580 | default: |
| 6581 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6582 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6583 | goto cleanup; |
| 6584 | } |
| 6585 | } |
| 6586 | |
| 6587 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6588 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6589 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6590 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6591 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6592 | goto cleanup; |
| 6593 | } |
| 6594 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6595 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6596 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6597 | goto cleanup; |
| 6598 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6599 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6600 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6601 | |
| 6602 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6603 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6604 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6605 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6606 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6607 | goto cleanup; |
| 6608 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6609 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6610 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6611 | |
| 6612 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6613 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6614 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6615 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6616 | /* change value */ |
| 6617 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6618 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6619 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6620 | /* change value */ |
| 6621 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6622 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6623 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6624 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6625 | goto cleanup; |
| 6626 | } |
| 6627 | if (d_rpl->min) { |
| 6628 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6629 | } |
| 6630 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6631 | |
| 6632 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6633 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6634 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6635 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6636 | /* change value */ |
| 6637 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6638 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6639 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6640 | /* change value */ |
| 6641 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6642 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6643 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6644 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6645 | goto cleanup; |
| 6646 | } |
| 6647 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6648 | |
| 6649 | break; |
| 6650 | default: |
| 6651 | LOGINT(ctx->ctx); |
| 6652 | goto cleanup; |
| 6653 | } |
| 6654 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6655 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6656 | /* final check when all deviations of a single target node are applied */ |
| 6657 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6658 | /* check min-max compatibility */ |
| 6659 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6660 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6661 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6662 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6663 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6664 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6665 | } else { |
| 6666 | min = max = 0; |
| 6667 | } |
| 6668 | if (min > max) { |
| 6669 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6670 | "after deviation: min value %u is bigger than max value %u.", min, max); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6671 | goto cleanup; |
| 6672 | } |
| 6673 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6674 | if (dflt) { |
| 6675 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6676 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6677 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6678 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6679 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6680 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6681 | leaf->dflt->realtype->refcount++; |
| 6682 | if (err) { |
| 6683 | ly_err_print(err); |
| 6684 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6685 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6686 | ly_err_free(err); |
| 6687 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6688 | if (rc == LY_EINCOMPLETE) { |
| 6689 | /* postpone default compilation when the tree is complete */ |
| 6690 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6691 | |
| 6692 | /* but in general result is so far ok */ |
| 6693 | rc = LY_SUCCESS; |
| 6694 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6695 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6696 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6697 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6698 | int dynamic; |
| 6699 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6700 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6701 | |
| 6702 | /* update the list of incomplete default values if needed */ |
| 6703 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6704 | |
| 6705 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6706 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6707 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6708 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6709 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6710 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6711 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6712 | leaf->dflt->realtype->refcount++; |
| 6713 | if (err) { |
| 6714 | ly_err_print(err); |
| 6715 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6716 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6717 | ly_err_free(err); |
| 6718 | } |
| 6719 | if (dynamic) { |
| 6720 | free((void*)dflt); |
| 6721 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6722 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6723 | if (rc == LY_EINCOMPLETE) { |
| 6724 | /* postpone default compilation when the tree is complete */ |
| 6725 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6726 | |
| 6727 | /* but in general result is so far ok */ |
| 6728 | rc = LY_SUCCESS; |
| 6729 | } |
| 6730 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6731 | } else { /* LYS_LEAFLIST */ |
| 6732 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6733 | dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LYD_XML, lys_get_prefix, llist->dflts_mods[x], &dynamic); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6734 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6735 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6736 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6737 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6738 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6739 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6740 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6741 | llist->dflts[x]->realtype->refcount++; |
| 6742 | if (err) { |
| 6743 | ly_err_print(err); |
| 6744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6745 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6746 | dflt, err->msg); |
| 6747 | ly_err_free(err); |
| 6748 | } |
| 6749 | if (dynamic) { |
| 6750 | free((void*)dflt); |
| 6751 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6752 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6753 | if (rc == LY_EINCOMPLETE) { |
| 6754 | /* postpone default compilation when the tree is complete */ |
| 6755 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6756 | |
| 6757 | /* but in general result is so far ok */ |
| 6758 | rc = LY_SUCCESS; |
| 6759 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6760 | LY_CHECK_GOTO(rc, cleanup); |
| 6761 | } |
| 6762 | } |
| 6763 | } |
| 6764 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6765 | /* check mandatory - default compatibility */ |
| 6766 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6767 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6768 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6769 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6770 | "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype)); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6771 | goto cleanup; |
| 6772 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6773 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6774 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6775 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6776 | goto cleanup; |
| 6777 | } |
| 6778 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6779 | /* mandatory node under a default case */ |
| 6780 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6781 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6782 | lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6783 | goto cleanup; |
| 6784 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6785 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6786 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6787 | } |
| 6788 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6789 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6790 | ret = LY_SUCCESS; |
| 6791 | |
| 6792 | cleanup: |
| 6793 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6794 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6795 | free(devs[u]); |
| 6796 | } |
| 6797 | free(devs); |
| 6798 | ly_set_erase(&targets, NULL); |
| 6799 | ly_set_erase(&devs_p, NULL); |
| 6800 | |
| 6801 | return ret; |
| 6802 | } |
| 6803 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6804 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6805 | * @brief Compile the given YANG submodule into the main module. |
| 6806 | * @param[in] ctx Compile context |
| 6807 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6808 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6809 | */ |
| 6810 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6811 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6812 | { |
| 6813 | unsigned int u; |
| 6814 | LY_ERR ret = LY_SUCCESS; |
| 6815 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6816 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6817 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6818 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6819 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6820 | if (!mainmod->mod->off_features) { |
| 6821 | /* features are compiled directly into the compiled module structure, |
| 6822 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6823 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6824 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6825 | LY_CHECK_GOTO(ret, error); |
| 6826 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6827 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6828 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6829 | COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6830 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6831 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6832 | /* data nodes */ |
| 6833 | LY_LIST_FOR(submod->data, node_p) { |
| 6834 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6835 | LY_CHECK_GOTO(ret, error); |
| 6836 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6837 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6838 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6839 | 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] | 6840 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6841 | error: |
| 6842 | return ret; |
| 6843 | } |
| 6844 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6845 | static void * |
| 6846 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6847 | { |
| 6848 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6849 | if (substmts[u].stmt == stmt) { |
| 6850 | return substmts[u].storage; |
| 6851 | } |
| 6852 | } |
| 6853 | return NULL; |
| 6854 | } |
| 6855 | |
| 6856 | LY_ERR |
| 6857 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6858 | { |
| 6859 | LY_ERR ret = LY_EVALID, r; |
| 6860 | unsigned int u; |
| 6861 | struct lysp_stmt *stmt; |
| 6862 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6863 | |
| 6864 | /* check for invalid substatements */ |
| 6865 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6866 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6867 | continue; |
| 6868 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6869 | for (u = 0; substmts[u].stmt; ++u) { |
| 6870 | if (substmts[u].stmt == stmt->kw) { |
| 6871 | break; |
| 6872 | } |
| 6873 | } |
| 6874 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6875 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6876 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6877 | goto cleanup; |
| 6878 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6879 | } |
| 6880 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6881 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6882 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6883 | /* keep order of the processing the same as the order in the defined substmts, |
| 6884 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6885 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6886 | int stmt_present = 0; |
| 6887 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6888 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6889 | if (substmts[u].stmt != stmt->kw) { |
| 6890 | continue; |
| 6891 | } |
| 6892 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6893 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6894 | if (substmts[u].storage) { |
| 6895 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6896 | case LY_STMT_STATUS: |
| 6897 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6898 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6899 | break; |
| 6900 | case LY_STMT_UNITS: { |
| 6901 | const char **units; |
| 6902 | |
| 6903 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6904 | /* single item */ |
| 6905 | if (*((const char **)substmts[u].storage)) { |
| 6906 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6907 | goto cleanup; |
| 6908 | } |
| 6909 | units = (const char **)substmts[u].storage; |
| 6910 | } else { |
| 6911 | /* sized array */ |
| 6912 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6913 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6914 | } |
| 6915 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6916 | break; |
| 6917 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6918 | case LY_STMT_TYPE: { |
| 6919 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6920 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6921 | |
| 6922 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6923 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6924 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6925 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6926 | goto cleanup; |
| 6927 | } |
| 6928 | compiled = substmts[u].storage; |
| 6929 | } else { |
| 6930 | /* sized array */ |
| 6931 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6932 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6933 | compiled = (void*)type; |
| 6934 | } |
| 6935 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6936 | 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] | 6937 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6938 | 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] | 6939 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6940 | lysp_type_free(ctx->ctx, parsed); |
| 6941 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6942 | break; |
| 6943 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6944 | case LY_STMT_IF_FEATURE: { |
| 6945 | struct lysc_iffeature *iff = NULL; |
| 6946 | |
| 6947 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6948 | /* single item */ |
| 6949 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6950 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6951 | goto cleanup; |
| 6952 | } |
| 6953 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6954 | } else { |
| 6955 | /* sized array */ |
| 6956 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6957 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6958 | } |
| 6959 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6960 | break; |
| 6961 | } |
| 6962 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6963 | * 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] | 6964 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6965 | 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.", |
| 6966 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6967 | goto cleanup; |
| 6968 | } |
| 6969 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6970 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6971 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6972 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6973 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6974 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6975 | goto cleanup; |
| 6976 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6977 | } |
| 6978 | |
| 6979 | ret = LY_SUCCESS; |
| 6980 | |
| 6981 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6982 | return ret; |
| 6983 | } |
| 6984 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6985 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6986 | * @brief Check when for cyclic dependencies. |
| 6987 | * @param[in] set Set with all the referenced nodes. |
| 6988 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6989 | * @return LY_ERR value |
| 6990 | */ |
| 6991 | static LY_ERR |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6992 | lys_compile_check_when_cyclic(struct lyxp_set *set, const struct lysc_node *node) |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6993 | { |
| 6994 | struct lyxp_set tmp_set; |
| 6995 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6996 | uint32_t i, j; |
| 6997 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6998 | int idx; |
| 6999 | struct lysc_when *when; |
| 7000 | LY_ERR ret = LY_SUCCESS; |
| 7001 | |
| 7002 | memset(&tmp_set, 0, sizeof tmp_set); |
| 7003 | |
| 7004 | /* prepare in_ctx of the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7005 | for ( i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7006 | xp_scnode = &set->val.scnodes[i]; |
| 7007 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7008 | if (xp_scnode->in_ctx != -1) { |
| 7009 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7010 | xp_scnode->in_ctx = 1; |
| 7011 | } |
| 7012 | } |
| 7013 | |
| 7014 | for (i = 0; i < set->used; ++i) { |
| 7015 | xp_scnode = &set->val.scnodes[i]; |
| 7016 | if (xp_scnode->in_ctx != 1) { |
| 7017 | /* already checked */ |
| 7018 | continue; |
| 7019 | } |
| 7020 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7021 | 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] | 7022 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7023 | /* no when to check */ |
| 7024 | xp_scnode->in_ctx = 0; |
| 7025 | continue; |
| 7026 | } |
| 7027 | |
| 7028 | node = xp_scnode->scnode; |
| 7029 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7030 | LY_ARRAY_FOR(node->when, u) { |
| 7031 | when = node->when[u]; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 7032 | ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7033 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 7034 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7035 | 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] | 7036 | goto cleanup; |
| 7037 | } |
| 7038 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7039 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7040 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7041 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7042 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7043 | 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] | 7044 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7045 | 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] | 7046 | ret = LY_EVALID; |
| 7047 | goto cleanup; |
| 7048 | } |
| 7049 | |
| 7050 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7051 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7052 | } else { |
| 7053 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7054 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7055 | } |
| 7056 | } |
| 7057 | |
| 7058 | /* merge this set into the global when set */ |
| 7059 | lyxp_set_scnode_merge(set, &tmp_set); |
| 7060 | } |
| 7061 | |
| 7062 | /* check when of non-data parents as well */ |
| 7063 | node = node->parent; |
| 7064 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 7065 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7066 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 7067 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7068 | } |
| 7069 | |
| 7070 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7071 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7072 | return ret; |
| 7073 | } |
| 7074 | |
| 7075 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7076 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 7077 | * @param[in] ctx Compile context. |
| 7078 | * @param[in] node Node to check. |
| 7079 | * @return LY_ERR value |
| 7080 | */ |
| 7081 | static LY_ERR |
| 7082 | lys_compile_check_xpath(struct lysc_ctx *ctx, const struct lysc_node *node) |
| 7083 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7084 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7085 | uint32_t i; |
| 7086 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7087 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7088 | struct lysc_when **when = NULL; |
| 7089 | struct lysc_must *musts = NULL; |
| 7090 | LY_ERR ret = LY_SUCCESS; |
| 7091 | |
| 7092 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7093 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7094 | |
| 7095 | switch (node->nodetype) { |
| 7096 | case LYS_CONTAINER: |
| 7097 | when = ((struct lysc_node_container *)node)->when; |
| 7098 | musts = ((struct lysc_node_container *)node)->musts; |
| 7099 | break; |
| 7100 | case LYS_CHOICE: |
| 7101 | when = ((struct lysc_node_choice *)node)->when; |
| 7102 | break; |
| 7103 | case LYS_LEAF: |
| 7104 | when = ((struct lysc_node_leaf *)node)->when; |
| 7105 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 7106 | break; |
| 7107 | case LYS_LEAFLIST: |
| 7108 | when = ((struct lysc_node_leaflist *)node)->when; |
| 7109 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 7110 | break; |
| 7111 | case LYS_LIST: |
| 7112 | when = ((struct lysc_node_list *)node)->when; |
| 7113 | musts = ((struct lysc_node_list *)node)->musts; |
| 7114 | break; |
| 7115 | case LYS_ANYXML: |
| 7116 | case LYS_ANYDATA: |
| 7117 | when = ((struct lysc_node_anydata *)node)->when; |
| 7118 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 7119 | break; |
| 7120 | case LYS_CASE: |
| 7121 | when = ((struct lysc_node_case *)node)->when; |
| 7122 | break; |
| 7123 | case LYS_NOTIF: |
| 7124 | musts = ((struct lysc_notif *)node)->musts; |
| 7125 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7126 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7127 | case LYS_ACTION: |
| 7128 | /* first process input musts */ |
| 7129 | musts = ((struct lysc_action *)node)->input.musts; |
| 7130 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7131 | default: |
| 7132 | /* nothing to check */ |
| 7133 | break; |
| 7134 | } |
| 7135 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7136 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7137 | LY_ARRAY_FOR(when, u) { |
| 7138 | ret = lyxp_atomize(when[u]->cond, LYD_SCHEMA, when[u]->module, when[u]->context, |
| 7139 | 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] | 7140 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7141 | 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] | 7142 | goto cleanup; |
| 7143 | } |
| 7144 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7145 | ctx->path[0] = '\0'; |
| 7146 | 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] | 7147 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7148 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7149 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 7150 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7151 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7152 | /* 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] | 7153 | 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] | 7154 | schema->name); |
| 7155 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7156 | |
| 7157 | /* check dummy node accessing */ |
| 7158 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7159 | 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] | 7160 | ret = LY_EVALID; |
| 7161 | goto cleanup; |
| 7162 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7163 | } |
| 7164 | } |
| 7165 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7166 | /* check cyclic dependencies */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7167 | ret = lys_compile_check_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7168 | LY_CHECK_GOTO(ret, cleanup); |
| 7169 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7170 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7171 | } |
| 7172 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7173 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7174 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7175 | LY_ARRAY_FOR(musts, u) { |
| 7176 | ret = lyxp_atomize(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, &tmp_set, opts); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7177 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7178 | 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] | 7179 | goto cleanup; |
| 7180 | } |
| 7181 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7182 | ctx->path[0] = '\0'; |
| 7183 | 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] | 7184 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7185 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7186 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7187 | /* 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] | 7188 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 7189 | 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] | 7190 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7191 | } |
| 7192 | } |
| 7193 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7194 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7195 | } |
| 7196 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7197 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7198 | /* now check output musts */ |
| 7199 | input_done = 1; |
| 7200 | musts = ((struct lysc_action *)node)->output.musts; |
| 7201 | opts = LYXP_SCNODE_OUTPUT; |
| 7202 | goto check_musts; |
| 7203 | } |
| 7204 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7205 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7206 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7207 | return ret; |
| 7208 | } |
| 7209 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7210 | static LY_ERR |
| 7211 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7212 | { |
| 7213 | struct lysc_ext_instance *ext; |
| 7214 | struct lysp_ext_instance *ext_p = NULL; |
| 7215 | struct lysp_stmt *stmt; |
| 7216 | const struct lys_module *ext_mod; |
| 7217 | LY_ERR ret = LY_SUCCESS; |
| 7218 | |
| 7219 | /* create the parsed extension instance manually */ |
| 7220 | ext_p = calloc(1, sizeof *ext_p); |
| 7221 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 7222 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 7223 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 7224 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7225 | ext_p->insubstmt_index = 0; |
| 7226 | |
| 7227 | stmt = calloc(1, sizeof *ext_p->child); |
| 7228 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 7229 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 7230 | stmt->kw = LY_STMT_TYPE; |
| 7231 | ext_p->child = stmt; |
| 7232 | |
| 7233 | /* allocate new extension instance */ |
| 7234 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7235 | |
| 7236 | /* manually get extension definition module */ |
| 7237 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7238 | |
| 7239 | /* compile the extension instance */ |
| 7240 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7241 | |
| 7242 | cleanup: |
| 7243 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7244 | free(ext_p); |
| 7245 | return ret; |
| 7246 | } |
| 7247 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7248 | LY_ERR |
| 7249 | lys_compile(struct lys_module *mod, int options) |
| 7250 | { |
| 7251 | struct lysc_ctx ctx = {0}; |
| 7252 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7253 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7254 | struct lysp_module *sp; |
| 7255 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7256 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7257 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7258 | struct lys_module *m; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7259 | LY_ARRAY_SIZE_TYPE u, v; |
| 7260 | uint32_t i; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7261 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7262 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7263 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7264 | |
| 7265 | if (!mod->implemented) { |
| 7266 | /* just imported modules are not compiled */ |
| 7267 | return LY_SUCCESS; |
| 7268 | } |
| 7269 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7270 | sp = mod->parsed; |
| 7271 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7272 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7273 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7274 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7275 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7276 | ctx.path_len = 1; |
| 7277 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7278 | |
| 7279 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7280 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 7281 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7282 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7283 | COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7284 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7285 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7286 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7287 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7288 | |
| 7289 | /* features */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7290 | if (mod->off_features) { |
| 7291 | /* there is already precompiled array of features */ |
| 7292 | mod_c->features = mod->off_features; |
| 7293 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7294 | } else { |
| 7295 | /* features are compiled directly into the compiled module structure, |
| 7296 | * 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] | 7297 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7298 | LY_CHECK_GOTO(ret, error); |
| 7299 | } |
| 7300 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7301 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7302 | * are present. */ |
| 7303 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7304 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7305 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7306 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7307 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7308 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7309 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7310 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7311 | 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] | 7312 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7313 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7314 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7315 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7316 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7317 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7318 | /* identities */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7319 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7320 | COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7321 | if (sp->identities) { |
| 7322 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 7323 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7324 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7325 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7326 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7327 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7328 | 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] | 7329 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7330 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7331 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7332 | 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] | 7333 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7334 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7335 | 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] | 7336 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7337 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7338 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7339 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7340 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7341 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7342 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7343 | /* extension instances TODO cover extension instances from submodules */ |
| 7344 | 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] | 7345 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7346 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7347 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7348 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7349 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7350 | for (i = 0; i < ctx.unres.count; ++i) { |
| 7351 | if (((struct lysc_node*)ctx.unres.objs[i])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 7352 | type = ((struct lysc_node_leaf*)ctx.unres.objs[i])->type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7353 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7354 | /* validate the path */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7355 | LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[i]), (struct lysc_type_leafref*)type, NULL), error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7356 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7357 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7358 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7359 | /* validate the path */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7360 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[i]), |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7361 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v], NULL); |
| 7362 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7363 | } |
| 7364 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7365 | } |
| 7366 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7367 | |
| 7368 | /* check xpath */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7369 | LY_CHECK_GOTO(ret = lys_compile_check_xpath(&ctx, ctx.unres.objs[i]), error); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7370 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7371 | for (i = 0; i < ctx.unres.count; ++i) { |
| 7372 | if (((struct lysc_node*)ctx.unres.objs[i])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 7373 | type = ((struct lysc_node_leaf*)ctx.unres.objs[i])->type; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7374 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7375 | /* store pointer to the real type */ |
| 7376 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7377 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7378 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7379 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7380 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7381 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7382 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7383 | /* store pointer to the real type */ |
| 7384 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7385 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7386 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7387 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7388 | } |
| 7389 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7390 | } |
| 7391 | } |
| 7392 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7393 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7394 | /* finish incomplete default values compilation */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7395 | for (i = 0; i < ctx.dflts.count; ++i) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7396 | struct ly_err_item *err = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7397 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[i]; |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 7398 | ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original), |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7399 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7400 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7401 | if (err) { |
| 7402 | ly_err_print(err); |
| 7403 | ctx.path[0] = '\0'; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7404 | lysc_path(r->context_node, LYSC_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7405 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 7406 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7407 | ly_err_free(err); |
| 7408 | } |
| 7409 | LY_CHECK_GOTO(ret, error); |
| 7410 | } |
| 7411 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7412 | /* validate non-instantiated groupings from the parsed schema, |
| 7413 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7414 | ctx.options |= LYSC_OPT_GROUPING; |
| 7415 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7416 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7417 | 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] | 7418 | } |
| 7419 | } |
| 7420 | LY_LIST_FOR(sp->data, node_p) { |
| 7421 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7422 | LY_ARRAY_FOR(grps, u) { |
| 7423 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7424 | 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] | 7425 | } |
| 7426 | } |
| 7427 | } |
| 7428 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7429 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7430 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7431 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7432 | } |
| 7433 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7434 | #if 0 |
| 7435 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7436 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7437 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7438 | * the anotation definitions available in the internal schema structure. */ |
| 7439 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7440 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7441 | lys_free(mod, NULL, 1, 1); |
| 7442 | return NULL; |
| 7443 | } |
| 7444 | } |
| 7445 | #endif |
| 7446 | |
| 7447 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
| 7448 | if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
| 7449 | LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error); |
| 7450 | } |
| 7451 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7452 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7453 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7454 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7455 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7456 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7457 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7458 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7459 | lysp_module_free(mod->parsed); |
| 7460 | ((struct lys_module*)mod)->parsed = NULL; |
| 7461 | } |
| 7462 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7463 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7464 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7465 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7466 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7467 | if (m->implemented == 2) { |
| 7468 | m->implemented = 1; |
| 7469 | } |
| 7470 | } |
| 7471 | } |
| 7472 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7473 | ((struct lys_module*)mod)->compiled = mod_c; |
| 7474 | return LY_SUCCESS; |
| 7475 | |
| 7476 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7477 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7478 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7479 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7480 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7481 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7482 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7483 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7484 | mod->compiled = NULL; |
| 7485 | |
| 7486 | /* revert compilation of modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7487 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7488 | m = ctx.ctx->list.objs[i]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7489 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7490 | /* revert features list to the precompiled state */ |
| 7491 | lys_feature_precompile_revert(&ctx, m); |
| 7492 | /* mark module as imported-only / not-implemented */ |
| 7493 | m->implemented = 0; |
| 7494 | /* free the compiled version of the module */ |
| 7495 | lysc_module_free(m->compiled, NULL); |
| 7496 | m->compiled = NULL; |
| 7497 | } |
| 7498 | } |
| 7499 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7500 | return ret; |
| 7501 | } |