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 | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1041 | lys_compile_identity_bases(struct lysc_ctx *ctx, 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 | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1061 | mod = lys_module_find_prefix(ctx->mod_def, 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 | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1064 | mod = ctx->mod_def; |
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); |
| 1130 | LY_CHECK_RET(lys_compile_identity_bases(ctx, 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 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1851 | int idx, idx2, start, end, count; |
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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1948 | /* we need to replace all "$" and "^" with "\$" and "\^", count them now */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1949 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1950 | |
| 1951 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1952 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1953 | perl_regex[0] = '\0'; |
| 1954 | |
| 1955 | ptr = perl_regex; |
| 1956 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1957 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1958 | if (orig_ptr[0] == '$') { |
| 1959 | ptr += sprintf(ptr, "\\$"); |
| 1960 | } else if (orig_ptr[0] == '^') { |
| 1961 | ptr += sprintf(ptr, "\\^"); |
| 1962 | } else { |
| 1963 | ptr[0] = orig_ptr[0]; |
| 1964 | ++ptr; |
| 1965 | } |
| 1966 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1967 | ptr[0] = '\0'; |
| 1968 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1969 | |
| 1970 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1971 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1972 | start = ptr - perl_regex; |
| 1973 | |
| 1974 | ptr = strchr(ptr, '}'); |
| 1975 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1976 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1977 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1978 | free(perl_regex); |
| 1979 | return LY_EVALID; |
| 1980 | } |
| 1981 | end = (ptr - perl_regex) + 1; |
| 1982 | |
| 1983 | /* need more space */ |
| 1984 | if (end - start < URANGE_LEN) { |
| 1985 | 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] | 1986 | 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] | 1987 | } |
| 1988 | |
| 1989 | /* find our range */ |
| 1990 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1991 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1992 | break; |
| 1993 | } |
| 1994 | } |
| 1995 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1996 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1997 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1998 | free(perl_regex); |
| 1999 | return LY_EVALID; |
| 2000 | } |
| 2001 | |
| 2002 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 2003 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 2004 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 2005 | ++count; |
| 2006 | } |
| 2007 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 2008 | --count; |
| 2009 | } |
| 2010 | } |
| 2011 | if (count) { |
| 2012 | /* skip brackets */ |
| 2013 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2014 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2015 | } else { |
| 2016 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2017 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2022 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2023 | 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] | 2024 | &err_code, &err_offset, NULL); |
| 2025 | if (!code_local) { |
| 2026 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2027 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2028 | 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] | 2029 | free(perl_regex); |
| 2030 | return LY_EVALID; |
| 2031 | } |
| 2032 | free(perl_regex); |
| 2033 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2034 | if (code) { |
| 2035 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2036 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2037 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | return LY_SUCCESS; |
| 2041 | |
| 2042 | #undef URANGE_LEN |
| 2043 | } |
| 2044 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2045 | /** |
| 2046 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2047 | * @param[in] ctx Compile context. |
| 2048 | * @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] | 2049 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2050 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2051 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2052 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2053 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2054 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2055 | 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] | 2056 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2057 | { |
| 2058 | struct lysc_pattern **pattern; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2059 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2060 | LY_ERR ret = LY_SUCCESS; |
| 2061 | |
| 2062 | /* first, copy the patterns from the base type */ |
| 2063 | if (base_patterns) { |
| 2064 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2065 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2066 | } |
| 2067 | |
| 2068 | LY_ARRAY_FOR(patterns_p, u) { |
| 2069 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2070 | *pattern = calloc(1, sizeof **pattern); |
| 2071 | ++(*pattern)->refcount; |
| 2072 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2073 | 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] | 2074 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2075 | |
| 2076 | if (patterns_p[u].arg[0] == 0x15) { |
| 2077 | (*pattern)->inverted = 1; |
| 2078 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2079 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2080 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2081 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2082 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2083 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2084 | 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] | 2085 | } |
| 2086 | done: |
| 2087 | return ret; |
| 2088 | } |
| 2089 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2090 | /** |
| 2091 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2092 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2093 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2094 | 0 /* LY_TYPE_UNKNOWN */, |
| 2095 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2096 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2097 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2098 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2099 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2100 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2101 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2102 | 0 /* LY_TYPE_BOOL */, |
| 2103 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2104 | 0 /* LY_TYPE_EMPTY */, |
| 2105 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2106 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2107 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2108 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2109 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2110 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2111 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2112 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2113 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2114 | }; |
| 2115 | |
| 2116 | /** |
| 2117 | * @brief stringification of the YANG built-in data types |
| 2118 | */ |
| 2119 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2120 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2121 | "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] | 2122 | }; |
| 2123 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2124 | /** |
| 2125 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2126 | * @param[in] ctx Compile context. |
| 2127 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2128 | * @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] | 2129 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2130 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2131 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2132 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2133 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2134 | 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] | 2135 | 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] | 2136 | { |
| 2137 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2138 | LY_ARRAY_SIZE_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2139 | int32_t value = 0; |
| 2140 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2141 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2142 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2143 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2144 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2145 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2146 | return LY_EVALID; |
| 2147 | } |
| 2148 | |
| 2149 | LY_ARRAY_FOR(enums_p, u) { |
| 2150 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2151 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2152 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2153 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2154 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2155 | if (base_enums) { |
| 2156 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2157 | LY_ARRAY_FOR(base_enums, v) { |
| 2158 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2159 | break; |
| 2160 | } |
| 2161 | } |
| 2162 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 2163 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2164 | "Invalid %s - derived type adds new item \"%s\".", |
| 2165 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2166 | return LY_EVALID; |
| 2167 | } |
| 2168 | match = v; |
| 2169 | } |
| 2170 | |
| 2171 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2172 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2173 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2174 | e->value = (int32_t)enums_p[u].value; |
| 2175 | if (!u || e->value >= value) { |
| 2176 | value = e->value + 1; |
| 2177 | } |
| 2178 | /* check collision with other values */ |
| 2179 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2180 | if (e->value == (*enums)[v].value) { |
| 2181 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2182 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2183 | e->value, e->name, (*enums)[v].name); |
| 2184 | return LY_EVALID; |
| 2185 | } |
| 2186 | } |
| 2187 | } else if (base_enums) { |
| 2188 | /* inherit the assigned value */ |
| 2189 | e->value = base_enums[match].value; |
| 2190 | if (!u || e->value >= value) { |
| 2191 | value = e->value + 1; |
| 2192 | } |
| 2193 | } else { |
| 2194 | /* assign value automatically */ |
| 2195 | if (u && value == INT32_MIN) { |
| 2196 | /* counter overflow */ |
| 2197 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2198 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2199 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2200 | return LY_EVALID; |
| 2201 | } |
| 2202 | e->value = value++; |
| 2203 | } |
| 2204 | } else { /* LY_TYPE_BITS */ |
| 2205 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2206 | e->value = (int32_t)enums_p[u].value; |
| 2207 | if (!u || (uint32_t)e->value >= position) { |
| 2208 | position = (uint32_t)e->value + 1; |
| 2209 | } |
| 2210 | /* check collision with other values */ |
| 2211 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2212 | if (e->value == (*enums)[v].value) { |
| 2213 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2214 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2215 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2216 | return LY_EVALID; |
| 2217 | } |
| 2218 | } |
| 2219 | } else if (base_enums) { |
| 2220 | /* inherit the assigned value */ |
| 2221 | e->value = base_enums[match].value; |
| 2222 | if (!u || (uint32_t)e->value >= position) { |
| 2223 | position = (uint32_t)e->value + 1; |
| 2224 | } |
| 2225 | } else { |
| 2226 | /* assign value automatically */ |
| 2227 | if (u && position == 0) { |
| 2228 | /* counter overflow */ |
| 2229 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2230 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2231 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2232 | return LY_EVALID; |
| 2233 | } |
| 2234 | e->value = position++; |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | if (base_enums) { |
| 2239 | /* the assigned values must not change from the derived type */ |
| 2240 | if (e->value != base_enums[match].value) { |
| 2241 | if (basetype == LY_TYPE_ENUM) { |
| 2242 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2243 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2244 | e->name, base_enums[match].value, e->value); |
| 2245 | } else { |
| 2246 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2247 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2248 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2249 | } |
| 2250 | return LY_EVALID; |
| 2251 | } |
| 2252 | } |
| 2253 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2254 | 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] | 2255 | 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] | 2256 | |
| 2257 | if (basetype == LY_TYPE_BITS) { |
| 2258 | /* keep bits ordered by position */ |
| 2259 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2260 | if (v != u) { |
| 2261 | memcpy(&storage, e, sizeof *e); |
| 2262 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2263 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | done: |
| 2269 | return ret; |
| 2270 | } |
| 2271 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2272 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2273 | for ((NODE) = (NODE)->parent; \ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 2274 | (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] | 2275 | (NODE) = (NODE)->parent); \ |
| 2276 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2277 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2278 | TERM; \ |
| 2279 | } |
| 2280 | |
| 2281 | /** |
| 2282 | * @brief Validate the predicate(s) from the leafref path. |
| 2283 | * @param[in] ctx Compile context |
| 2284 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2285 | * 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] | 2286 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2287 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2288 | * @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] | 2289 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2290 | */ |
| 2291 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2292 | 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] | 2293 | 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] | 2294 | { |
| 2295 | LY_ERR ret = LY_EVALID; |
| 2296 | const struct lys_module *mod; |
| 2297 | const struct lysc_node *src_node, *dst_node; |
| 2298 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2299 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2300 | unsigned int dest_parent_times, c; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2301 | const char *start, *end, *pke_end; |
| 2302 | struct ly_set keys = {0}; |
| 2303 | int i; |
| 2304 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2305 | assert(path_context); |
| 2306 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2307 | while (**predicate == '[') { |
| 2308 | start = (*predicate)++; |
| 2309 | |
| 2310 | while (isspace(**predicate)) { |
| 2311 | ++(*predicate); |
| 2312 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2313 | 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] | 2314 | while (isspace(**predicate)) { |
| 2315 | ++(*predicate); |
| 2316 | } |
| 2317 | if (**predicate != '=') { |
| 2318 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2319 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2320 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2321 | goto cleanup; |
| 2322 | } |
| 2323 | ++(*predicate); |
| 2324 | while (isspace(**predicate)) { |
| 2325 | ++(*predicate); |
| 2326 | } |
| 2327 | |
| 2328 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2329 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2330 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2331 | goto cleanup; |
| 2332 | } |
| 2333 | --pke_end; |
| 2334 | while (isspace(*pke_end)) { |
| 2335 | --pke_end; |
| 2336 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2337 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2338 | /* localize path-key-expr */ |
| 2339 | pke_start = path_key_expr = *predicate; |
| 2340 | /* move after the current predicate */ |
| 2341 | *predicate = end + 1; |
| 2342 | |
| 2343 | /* source (must be leaf or leaf-list) */ |
| 2344 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2345 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2346 | if (!mod) { |
| 2347 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2348 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2349 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2350 | goto cleanup; |
| 2351 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2352 | if (!mod->implemented) { |
| 2353 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2354 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2355 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2356 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2357 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2358 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2359 | src_node = NULL; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2360 | if (!(context_node->flags & LYS_KEYLESS)) { |
| 2361 | struct lysc_node *key; |
| 2362 | 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] | 2363 | if (!ly_strncmp(key->name, src, src_len)) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2364 | src_node = key; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2365 | break; |
| 2366 | } |
| 2367 | } |
| 2368 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2369 | if (!src_node) { |
| 2370 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2371 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2372 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2373 | goto cleanup; |
| 2374 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2375 | |
| 2376 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2377 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2378 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2379 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2380 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2381 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2382 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2383 | *predicate - start, start, src_node->name); |
| 2384 | goto cleanup; |
| 2385 | } |
| 2386 | |
| 2387 | /* destination */ |
| 2388 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2389 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2390 | |
| 2391 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2392 | if (strncmp(path_key_expr, "current", 7)) { |
| 2393 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2394 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2395 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2396 | *predicate - start, start); |
| 2397 | goto cleanup; |
| 2398 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2399 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2400 | if (*path_key_expr != '(') { |
| 2401 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2402 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2403 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2404 | if (*path_key_expr != ')') { |
| 2405 | goto error_current_function_invocation; |
| 2406 | } |
| 2407 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2408 | |
| 2409 | if (*path_key_expr != '/') { |
| 2410 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2411 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2412 | *predicate - start, start); |
| 2413 | goto cleanup; |
| 2414 | } |
| 2415 | ++path_key_expr; |
| 2416 | while (isspace(*path_key_expr)) { |
| 2417 | ++path_key_expr; |
| 2418 | } |
| 2419 | |
| 2420 | /* rel-path-keyexpr: |
| 2421 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2422 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2423 | ++dest_parent_times; |
| 2424 | path_key_expr += 2; |
| 2425 | while (isspace(*path_key_expr)) { |
| 2426 | ++path_key_expr; |
| 2427 | } |
| 2428 | if (*path_key_expr != '/') { |
| 2429 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2430 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2431 | *predicate - start, start); |
| 2432 | goto cleanup; |
| 2433 | } |
| 2434 | ++path_key_expr; |
| 2435 | while (isspace(*path_key_expr)) { |
| 2436 | ++path_key_expr; |
| 2437 | } |
| 2438 | |
| 2439 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2440 | * all schema nodes that cannot be instantiated in data tree */ |
| 2441 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2442 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2443 | *predicate - start, start); |
| 2444 | } |
| 2445 | if (!dest_parent_times) { |
| 2446 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2447 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2448 | *predicate - start, start); |
| 2449 | goto cleanup; |
| 2450 | } |
| 2451 | if (path_key_expr == pke_end) { |
| 2452 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2453 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2454 | *predicate - start, start); |
| 2455 | goto cleanup; |
| 2456 | } |
| 2457 | |
| 2458 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2459 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2460 | 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] | 2461 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2462 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2463 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2464 | goto cleanup; |
| 2465 | } |
| 2466 | |
| 2467 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2468 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2469 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2470 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2471 | } |
| 2472 | if (!mod) { |
| 2473 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2474 | "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] | 2475 | *predicate - start, start, dst_len, dst); |
| 2476 | goto cleanup; |
| 2477 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2478 | if (!mod->implemented) { |
| 2479 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2480 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2481 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2482 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2483 | 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] | 2484 | if (!dst_node) { |
| 2485 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2486 | "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] | 2487 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2488 | goto cleanup; |
| 2489 | } |
| 2490 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2491 | 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] | 2492 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2493 | "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] | 2494 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2495 | goto cleanup; |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | ret = LY_SUCCESS; |
| 2500 | cleanup: |
| 2501 | ly_set_erase(&keys, NULL); |
| 2502 | return ret; |
| 2503 | } |
| 2504 | |
| 2505 | /** |
| 2506 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2507 | * |
| 2508 | * path-arg = absolute-path / relative-path |
| 2509 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2510 | * relative-path = 1*(".." "/") descendant-path |
| 2511 | * |
| 2512 | * @param[in,out] path Path to parse. |
| 2513 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2514 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2515 | * @param[out] name Name of the token. |
| 2516 | * @param[out] nam_len Length of the name. |
| 2517 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2518 | * must not be changed between consecutive calls. -1 if the |
| 2519 | * path is absolute. |
| 2520 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2521 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2522 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2523 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2524 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2525 | int *parent_times, int *has_predicate) |
| 2526 | { |
| 2527 | int par_times = 0; |
| 2528 | |
| 2529 | assert(path && *path); |
| 2530 | assert(parent_times); |
| 2531 | assert(prefix); |
| 2532 | assert(prefix_len); |
| 2533 | assert(name); |
| 2534 | assert(name_len); |
| 2535 | assert(has_predicate); |
| 2536 | |
| 2537 | *prefix = NULL; |
| 2538 | *prefix_len = 0; |
| 2539 | *name = NULL; |
| 2540 | *name_len = 0; |
| 2541 | *has_predicate = 0; |
| 2542 | |
| 2543 | if (!*parent_times) { |
| 2544 | if (!strncmp(*path, "..", 2)) { |
| 2545 | *path += 2; |
| 2546 | ++par_times; |
| 2547 | while (!strncmp(*path, "/..", 3)) { |
| 2548 | *path += 3; |
| 2549 | ++par_times; |
| 2550 | } |
| 2551 | } |
| 2552 | if (par_times) { |
| 2553 | *parent_times = par_times; |
| 2554 | } else { |
| 2555 | *parent_times = -1; |
| 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | if (**path != '/') { |
| 2560 | return LY_EINVAL; |
| 2561 | } |
| 2562 | /* skip '/' */ |
| 2563 | ++(*path); |
| 2564 | |
| 2565 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2566 | 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] | 2567 | |
| 2568 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2569 | /* path continues by another token or this is the last token */ |
| 2570 | return LY_SUCCESS; |
| 2571 | } else if ((*path)[0] != '[') { |
| 2572 | /* unexpected character */ |
| 2573 | return LY_EINVAL; |
| 2574 | } else { |
| 2575 | /* predicate starting with [ */ |
| 2576 | *has_predicate = 1; |
| 2577 | return LY_SUCCESS; |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2582 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2583 | * |
| 2584 | * The set of features used for target must be a subset of features used for the leafref. |
| 2585 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2586 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2587 | * |
| 2588 | * @param[in] refnode The leafref node. |
| 2589 | * @param[in] target Tha target node of the leafref. |
| 2590 | * @return LY_SUCCESS or LY_EVALID; |
| 2591 | */ |
| 2592 | static LY_ERR |
| 2593 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2594 | { |
| 2595 | LY_ERR ret = LY_EVALID; |
| 2596 | const struct lysc_node *iter; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2597 | LY_ARRAY_SIZE_TYPE u, v, count; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2598 | struct ly_set features = {0}; |
| 2599 | |
| 2600 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2601 | if (iter->iffeatures) { |
| 2602 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2603 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2604 | 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] | 2605 | } |
| 2606 | } |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2611 | * So when adding features applicable to the target into the features set, we should not be |
| 2612 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2613 | * to the leafref itself. */ |
| 2614 | count = features.count; |
| 2615 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2616 | if (iter->iffeatures) { |
| 2617 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2618 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2619 | 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] | 2620 | /* new feature was added (or LY_EMEM) */ |
| 2621 | goto cleanup; |
| 2622 | } |
| 2623 | } |
| 2624 | } |
| 2625 | } |
| 2626 | } |
| 2627 | ret = LY_SUCCESS; |
| 2628 | |
| 2629 | cleanup: |
| 2630 | ly_set_erase(&features, NULL); |
| 2631 | return ret; |
| 2632 | } |
| 2633 | |
| 2634 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2635 | * @brief Validate the leafref path. |
| 2636 | * @param[in] ctx Compile context |
| 2637 | * @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] | 2638 | * @param[in] leafref Leafref to validate. |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2639 | * @param[out] target Optional resolved leafref target. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2640 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2641 | */ |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2642 | LY_ERR |
| 2643 | lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref, |
| 2644 | const struct lysc_node **target) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2645 | { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 2646 | const struct lysc_node *node = NULL, *parent = NULL; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2647 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2648 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2649 | const char *id, *prefix, *name; |
| 2650 | size_t prefix_len, name_len; |
| 2651 | int parent_times = 0, has_predicate; |
| 2652 | unsigned int iter, u; |
| 2653 | LY_ERR ret = LY_SUCCESS; |
| 2654 | |
| 2655 | assert(ctx); |
| 2656 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2657 | assert(leafref); |
| 2658 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2659 | ctx->path[0] = '\0'; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2660 | lysc_path(startnode, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2661 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2662 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2663 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2664 | id = leafref->path; |
Juraj Vijtiuk | bd0f2a6 | 2019-12-11 15:58:18 +0100 | [diff] [blame] | 2665 | |
| 2666 | if (!*id) { |
| 2667 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Empty leafref path."); |
| 2668 | return LY_EVALID; |
| 2669 | } |
| 2670 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2671 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2672 | if (!iter) { /* first iteration */ |
| 2673 | /* precess ".." in relative paths */ |
| 2674 | if (parent_times > 0) { |
| 2675 | /* move from the context node */ |
| 2676 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2677 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2678 | * all schema nodes that cannot be instantiated in data tree */ |
| 2679 | 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] | 2680 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2681 | } |
| 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2686 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2687 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2688 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2689 | } |
| 2690 | if (!mod) { |
| 2691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2692 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2693 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2694 | return LY_EVALID; |
| 2695 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2696 | if (!mod->implemented) { |
| 2697 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2698 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2699 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2700 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2701 | 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] | 2702 | if (!node) { |
| 2703 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2704 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2705 | return LY_EVALID; |
| 2706 | } |
| 2707 | parent = node; |
| 2708 | |
| 2709 | if (has_predicate) { |
| 2710 | /* we have predicate, so the current result must be list */ |
| 2711 | if (node->nodetype != LYS_LIST) { |
| 2712 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2713 | "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] | 2714 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2715 | return LY_EVALID; |
| 2716 | } |
| 2717 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2718 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2719 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | ++iter; |
| 2723 | } |
| 2724 | if (ret) { |
| 2725 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2726 | "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] | 2727 | return LY_EVALID; |
| 2728 | } |
| 2729 | |
| 2730 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2731 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2732 | "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] | 2733 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2734 | return LY_EVALID; |
| 2735 | } |
| 2736 | |
| 2737 | /* check status */ |
| 2738 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2739 | return LY_EVALID; |
| 2740 | } |
| 2741 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2742 | /* check config */ |
| 2743 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2744 | if (node->flags & LYS_CONFIG_R) { |
| 2745 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2746 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2747 | leafref->path); |
| 2748 | return LY_EVALID; |
| 2749 | } |
| 2750 | } |
| 2751 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2752 | /* store the target's type and check for circular chain of leafrefs */ |
| 2753 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2754 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2755 | if (type == (struct lysc_type*)leafref) { |
| 2756 | /* circular chain detected */ |
| 2757 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2758 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2759 | return LY_EVALID; |
| 2760 | } |
| 2761 | } |
| 2762 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2763 | /* check if leafref and its target are under common if-features */ |
| 2764 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2765 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2766 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2767 | leafref->path); |
| 2768 | return LY_EVALID; |
| 2769 | } |
| 2770 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2771 | ctx->path_len = 1; |
| 2772 | ctx->path[1] = '\0'; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2773 | if (target) { |
| 2774 | *target = node; |
| 2775 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2776 | return LY_SUCCESS; |
| 2777 | } |
| 2778 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2779 | 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] | 2780 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2781 | /** |
| 2782 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2783 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2784 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2785 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2786 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2787 | * @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] | 2788 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2789 | * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2790 | * @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] | 2791 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2792 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2793 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2794 | * @return LY_ERR value. |
| 2795 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2796 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2797 | 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] | 2798 | 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] | 2799 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2800 | { |
| 2801 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2802 | struct lysc_type_bin *bin; |
| 2803 | struct lysc_type_num *num; |
| 2804 | struct lysc_type_str *str; |
| 2805 | struct lysc_type_bits *bits; |
| 2806 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2807 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2808 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2809 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2810 | |
| 2811 | switch (basetype) { |
| 2812 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2813 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2814 | |
| 2815 | /* 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] | 2816 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2817 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2818 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2819 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2820 | 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] | 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | if (tpdfname) { |
| 2825 | type_p->compiled = *type; |
| 2826 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2827 | } |
| 2828 | break; |
| 2829 | case LY_TYPE_BITS: |
| 2830 | /* RFC 7950 9.7 - bits */ |
| 2831 | bits = (struct lysc_type_bits*)(*type); |
| 2832 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2833 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2834 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2835 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2836 | } |
| 2837 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2838 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2839 | /* 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] | 2840 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2841 | 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] | 2842 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2843 | 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] | 2844 | free(*type); |
| 2845 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2846 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2847 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | if (tpdfname) { |
| 2851 | type_p->compiled = *type; |
| 2852 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2853 | } |
| 2854 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2855 | case LY_TYPE_DEC64: |
| 2856 | dec = (struct lysc_type_dec*)(*type); |
| 2857 | |
| 2858 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2859 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2860 | if (!type_p->fraction_digits) { |
| 2861 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2862 | 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] | 2863 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2864 | 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] | 2865 | free(*type); |
| 2866 | *type = NULL; |
| 2867 | } |
| 2868 | return LY_EVALID; |
| 2869 | } |
| 2870 | } else if (type_p->fraction_digits) { |
| 2871 | /* 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] | 2872 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2873 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2874 | "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] | 2875 | tpdfname); |
| 2876 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2877 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2878 | "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] | 2879 | free(*type); |
| 2880 | *type = NULL; |
| 2881 | } |
| 2882 | return LY_EVALID; |
| 2883 | } |
| 2884 | dec->fraction_digits = type_p->fraction_digits; |
| 2885 | |
| 2886 | /* RFC 7950 9.2.4 - range */ |
| 2887 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2888 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2889 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2890 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2891 | 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] | 2892 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | if (tpdfname) { |
| 2896 | type_p->compiled = *type; |
| 2897 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2898 | } |
| 2899 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2900 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2901 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2902 | |
| 2903 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2904 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2905 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2906 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2907 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2908 | 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] | 2909 | } |
| 2910 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2911 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2912 | } |
| 2913 | |
| 2914 | /* RFC 7950 9.4.5 - pattern */ |
| 2915 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2916 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2917 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2918 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2919 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2920 | } |
| 2921 | |
| 2922 | if (tpdfname) { |
| 2923 | type_p->compiled = *type; |
| 2924 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2925 | } |
| 2926 | break; |
| 2927 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2928 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2929 | |
| 2930 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2931 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2932 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2933 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2934 | } |
| 2935 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2936 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2937 | /* 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] | 2938 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2939 | 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] | 2940 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2941 | 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] | 2942 | free(*type); |
| 2943 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2944 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2945 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | if (tpdfname) { |
| 2949 | type_p->compiled = *type; |
| 2950 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2951 | } |
| 2952 | break; |
| 2953 | case LY_TYPE_INT8: |
| 2954 | case LY_TYPE_UINT8: |
| 2955 | case LY_TYPE_INT16: |
| 2956 | case LY_TYPE_UINT16: |
| 2957 | case LY_TYPE_INT32: |
| 2958 | case LY_TYPE_UINT32: |
| 2959 | case LY_TYPE_INT64: |
| 2960 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2961 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2962 | |
| 2963 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2964 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2965 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2966 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2967 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2968 | 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] | 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | if (tpdfname) { |
| 2973 | type_p->compiled = *type; |
| 2974 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2975 | } |
| 2976 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2977 | case LY_TYPE_IDENT: |
| 2978 | idref = (struct lysc_type_identityref*)(*type); |
| 2979 | |
| 2980 | /* RFC 7950 9.10.2 - base */ |
| 2981 | if (type_p->bases) { |
| 2982 | if (base) { |
| 2983 | /* only the directly derived identityrefs can contain base specification */ |
| 2984 | if (tpdfname) { |
| 2985 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2986 | "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] | 2987 | tpdfname); |
| 2988 | } else { |
| 2989 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2990 | "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] | 2991 | free(*type); |
| 2992 | *type = NULL; |
| 2993 | } |
| 2994 | return LY_EVALID; |
| 2995 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2996 | LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | if (!base && !type_p->flags) { |
| 3000 | /* type derived from identityref built-in type must contain at least one base */ |
| 3001 | if (tpdfname) { |
| 3002 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 3003 | } else { |
| 3004 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 3005 | free(*type); |
| 3006 | *type = NULL; |
| 3007 | } |
| 3008 | return LY_EVALID; |
| 3009 | } |
| 3010 | |
| 3011 | if (tpdfname) { |
| 3012 | type_p->compiled = *type; |
| 3013 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3014 | } |
| 3015 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3016 | case LY_TYPE_LEAFREF: |
| 3017 | /* RFC 7950 9.9.3 - require-instance */ |
| 3018 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3019 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3020 | if (tpdfname) { |
| 3021 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3022 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 3023 | } else { |
| 3024 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3025 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 3026 | free(*type); |
| 3027 | *type = NULL; |
| 3028 | } |
| 3029 | return LY_EVALID; |
| 3030 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3031 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 3032 | } else if (base) { |
| 3033 | /* inherit */ |
| 3034 | ((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] | 3035 | } else { |
| 3036 | /* default is true */ |
| 3037 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 3038 | } |
| 3039 | if (type_p->path) { |
| 3040 | 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] | 3041 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3042 | } else if (base) { |
| 3043 | 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] | 3044 | ((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] | 3045 | } else if (tpdfname) { |
| 3046 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 3047 | return LY_EVALID; |
| 3048 | } else { |
| 3049 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 3050 | free(*type); |
| 3051 | *type = NULL; |
| 3052 | return LY_EVALID; |
| 3053 | } |
| 3054 | if (tpdfname) { |
| 3055 | type_p->compiled = *type; |
| 3056 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3057 | } |
| 3058 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 3059 | case LY_TYPE_INST: |
| 3060 | /* RFC 7950 9.9.3 - require-instance */ |
| 3061 | if (type_p->flags & LYS_SET_REQINST) { |
| 3062 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 3063 | } else { |
| 3064 | /* default is true */ |
| 3065 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 3066 | } |
| 3067 | |
| 3068 | if (tpdfname) { |
| 3069 | type_p->compiled = *type; |
| 3070 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3071 | } |
| 3072 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3073 | case LY_TYPE_UNION: |
| 3074 | un = (struct lysc_type_union*)(*type); |
| 3075 | |
| 3076 | /* RFC 7950 7.4 - type */ |
| 3077 | if (type_p->types) { |
| 3078 | if (base) { |
| 3079 | /* only the directly derived union can contain types specification */ |
| 3080 | if (tpdfname) { |
| 3081 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3082 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 3083 | tpdfname); |
| 3084 | } else { |
| 3085 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3086 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 3087 | free(*type); |
| 3088 | *type = NULL; |
| 3089 | } |
| 3090 | return LY_EVALID; |
| 3091 | } |
| 3092 | /* compile the type */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3093 | 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] | 3094 | 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] | 3095 | 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] | 3096 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 3097 | /* add space for additional types from the union subtype */ |
| 3098 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Radek Krejci | c4fa029 | 2020-05-14 10:54:49 +0200 | [diff] [blame] | 3099 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_SIZE(un_aux->types) - 1, |
| 3100 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3101 | |
| 3102 | /* copy subtypes of the subtype union */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3103 | 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] | 3104 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 3105 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 3106 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3107 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3108 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 3109 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 3110 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 3111 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 3112 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 3113 | /* TODO extensions */ |
| 3114 | |
| 3115 | } else { |
| 3116 | un->types[u + additional] = un_aux->types[v]; |
| 3117 | ++un_aux->types[v]->refcount; |
| 3118 | } |
| 3119 | ++additional; |
| 3120 | LY_ARRAY_INCREMENT(un->types); |
| 3121 | } |
| 3122 | /* compensate u increment in main loop */ |
| 3123 | --additional; |
| 3124 | |
| 3125 | /* free the replaced union subtype */ |
| 3126 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 3127 | } else { |
| 3128 | LY_ARRAY_INCREMENT(un->types); |
| 3129 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | if (!base && !type_p->flags) { |
| 3134 | /* type derived from union built-in type must contain at least one type */ |
| 3135 | if (tpdfname) { |
| 3136 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 3137 | } else { |
| 3138 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 3139 | free(*type); |
| 3140 | *type = NULL; |
| 3141 | } |
| 3142 | return LY_EVALID; |
| 3143 | } |
| 3144 | |
| 3145 | if (tpdfname) { |
| 3146 | type_p->compiled = *type; |
| 3147 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3148 | } |
| 3149 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3150 | case LY_TYPE_BOOL: |
| 3151 | case LY_TYPE_EMPTY: |
| 3152 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 3153 | break; |
| 3154 | } |
| 3155 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 3156 | done: |
| 3157 | return ret; |
| 3158 | } |
| 3159 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3160 | /** |
| 3161 | * @brief Compile information about the leaf/leaf-list's type. |
| 3162 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3163 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 3164 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3165 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3166 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 3167 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3168 | * @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] | 3169 | * @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] | 3170 | * @return LY_ERR value. |
| 3171 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3172 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3173 | 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] | 3174 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3175 | { |
| 3176 | LY_ERR ret = LY_SUCCESS; |
| 3177 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3178 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3179 | struct type_context { |
| 3180 | const struct lysp_tpdf *tpdf; |
| 3181 | struct lysp_node *node; |
| 3182 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3183 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3184 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3185 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3186 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3187 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3188 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3189 | |
| 3190 | (*type) = NULL; |
| 3191 | |
| 3192 | tctx = calloc(1, sizeof *tctx); |
| 3193 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3194 | 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] | 3195 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 3196 | ret == LY_SUCCESS; |
| 3197 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 3198 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 3199 | if (basetype) { |
| 3200 | break; |
| 3201 | } |
| 3202 | |
| 3203 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3204 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3205 | 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] | 3206 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3207 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3208 | if (units && !*units) { |
| 3209 | /* inherit units */ |
| 3210 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3211 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3212 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3213 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3214 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3215 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3216 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3217 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3218 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3219 | break; |
| 3220 | } |
| 3221 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3222 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3223 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3224 | * 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] | 3225 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3226 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3227 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3228 | dummyloops = 1; |
| 3229 | goto preparenext; |
| 3230 | } else { |
| 3231 | tctx = NULL; |
| 3232 | break; |
| 3233 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3234 | } |
| 3235 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3236 | /* circular typedef reference detection */ |
| 3237 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3238 | /* local part */ |
| 3239 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3240 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3241 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3242 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3243 | free(tctx); |
| 3244 | ret = LY_EVALID; |
| 3245 | goto cleanup; |
| 3246 | } |
| 3247 | } |
| 3248 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3249 | /* global part for unions corner case */ |
| 3250 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3251 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3252 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3253 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3254 | free(tctx); |
| 3255 | ret = LY_EVALID; |
| 3256 | goto cleanup; |
| 3257 | } |
| 3258 | } |
| 3259 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3260 | /* store information for the following processing */ |
| 3261 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3262 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3263 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3264 | /* prepare next loop */ |
| 3265 | tctx_prev = tctx; |
| 3266 | tctx = calloc(1, sizeof *tctx); |
| 3267 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3268 | } |
| 3269 | free(tctx); |
| 3270 | |
| 3271 | /* allocate type according to the basetype */ |
| 3272 | switch (basetype) { |
| 3273 | case LY_TYPE_BINARY: |
| 3274 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3275 | break; |
| 3276 | case LY_TYPE_BITS: |
| 3277 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3278 | break; |
| 3279 | case LY_TYPE_BOOL: |
| 3280 | case LY_TYPE_EMPTY: |
| 3281 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3282 | break; |
| 3283 | case LY_TYPE_DEC64: |
| 3284 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3285 | break; |
| 3286 | case LY_TYPE_ENUM: |
| 3287 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3288 | break; |
| 3289 | case LY_TYPE_IDENT: |
| 3290 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3291 | break; |
| 3292 | case LY_TYPE_INST: |
| 3293 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3294 | break; |
| 3295 | case LY_TYPE_LEAFREF: |
| 3296 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3297 | break; |
| 3298 | case LY_TYPE_STRING: |
| 3299 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3300 | break; |
| 3301 | case LY_TYPE_UNION: |
| 3302 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3303 | break; |
| 3304 | case LY_TYPE_INT8: |
| 3305 | case LY_TYPE_UINT8: |
| 3306 | case LY_TYPE_INT16: |
| 3307 | case LY_TYPE_UINT16: |
| 3308 | case LY_TYPE_INT32: |
| 3309 | case LY_TYPE_UINT32: |
| 3310 | case LY_TYPE_INT64: |
| 3311 | case LY_TYPE_UINT64: |
| 3312 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3313 | break; |
| 3314 | case LY_TYPE_UNKNOWN: |
| 3315 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3316 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3317 | ret = LY_EVALID; |
| 3318 | goto cleanup; |
| 3319 | } |
| 3320 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3321 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3322 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3323 | ly_data_type2str[basetype]); |
| 3324 | free(*type); |
| 3325 | (*type) = NULL; |
| 3326 | ret = LY_EVALID; |
| 3327 | goto cleanup; |
| 3328 | } |
| 3329 | |
| 3330 | /* get restrictions from the referred typedefs */ |
| 3331 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3332 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3333 | |
| 3334 | /* remember the typedef context for circular check */ |
| 3335 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3336 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3337 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3338 | base = tctx->tpdf->type.compiled; |
| 3339 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3340 | } 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] | 3341 | /* no change, just use the type information from the base */ |
| 3342 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3343 | ++base->refcount; |
| 3344 | continue; |
| 3345 | } |
| 3346 | |
| 3347 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3348 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3349 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3350 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3351 | ret = LY_EVALID; |
| 3352 | goto cleanup; |
| 3353 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3354 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3355 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3356 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3357 | ret = LY_EVALID; |
| 3358 | goto cleanup; |
| 3359 | } |
| 3360 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3361 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3362 | /* TODO user type plugins */ |
| 3363 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3364 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3365 | 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] | 3366 | 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] | 3367 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3368 | LY_CHECK_GOTO(ret, cleanup); |
| 3369 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3370 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3371 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3372 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3373 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3374 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3375 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3376 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3377 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3378 | /* TODO user type plugins */ |
| 3379 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3380 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3381 | 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] | 3382 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3383 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3384 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3385 | free(*type); |
| 3386 | (*type) = base; |
| 3387 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3388 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3389 | if (dflt && !(*type)->dflt) { |
| 3390 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3391 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3392 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3393 | (*type)->dflt->realtype = (*type); |
| 3394 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3395 | 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] | 3396 | 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] | 3397 | if (err) { |
| 3398 | ly_err_print(err); |
| 3399 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3400 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3401 | ly_err_free(err); |
| 3402 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3403 | if (ret == LY_EINCOMPLETE) { |
| 3404 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3405 | 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] | 3406 | |
| 3407 | /* but in general result is so far ok */ |
| 3408 | ret = LY_SUCCESS; |
| 3409 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3410 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3411 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3412 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3413 | 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] | 3414 | |
| 3415 | cleanup: |
| 3416 | ly_set_erase(&tpdf_chain, free); |
| 3417 | return ret; |
| 3418 | } |
| 3419 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3420 | /** |
| 3421 | * @brief Compile status information of the given node. |
| 3422 | * |
| 3423 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3424 | * has the status correctly set during the compilation. |
| 3425 | * |
| 3426 | * @param[in] ctx Compile context |
| 3427 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3428 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3429 | * the compatibility with the parent's status value. |
| 3430 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3431 | * @return LY_ERR value. |
| 3432 | */ |
| 3433 | static LY_ERR |
| 3434 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3435 | { |
| 3436 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3437 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3438 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3439 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3440 | if ((parent_flags & 0x3) != 0x3) { |
| 3441 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3442 | * combination of bits (0x3) which marks the uses_status value */ |
| 3443 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3444 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3445 | } |
| 3446 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3447 | } else { |
| 3448 | (*node_flags) |= LYS_STATUS_CURR; |
| 3449 | } |
| 3450 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3451 | /* check status compatibility with the parent */ |
| 3452 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3453 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3454 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3455 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3456 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3457 | } else { /* LYS_STATUS_DEPRC */ |
| 3458 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3459 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3460 | } |
| 3461 | return LY_EVALID; |
| 3462 | } |
| 3463 | } |
| 3464 | return LY_SUCCESS; |
| 3465 | } |
| 3466 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3467 | /** |
| 3468 | * @brief Check uniqness of the node/action/notification name. |
| 3469 | * |
| 3470 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3471 | * structures, but they share the namespace so we need to check their name collisions. |
| 3472 | * |
| 3473 | * @param[in] ctx Compile context. |
| 3474 | * @param[in] children List (linked list) of data nodes to go through. |
| 3475 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3476 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3477 | * @param[in] name Name of the item to find in the given lists. |
| 3478 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3479 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3480 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3481 | */ |
| 3482 | static LY_ERR |
| 3483 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3484 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3485 | const char *name, void *exclude) |
| 3486 | { |
| 3487 | const struct lysc_node *iter; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3488 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3489 | |
| 3490 | LY_LIST_FOR(children, iter) { |
| 3491 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3492 | goto error; |
| 3493 | } |
| 3494 | } |
| 3495 | LY_ARRAY_FOR(actions, u) { |
| 3496 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3497 | goto error; |
| 3498 | } |
| 3499 | } |
| 3500 | LY_ARRAY_FOR(notifs, u) { |
| 3501 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3502 | goto error; |
| 3503 | } |
| 3504 | } |
| 3505 | return LY_SUCCESS; |
| 3506 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3507 | 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] | 3508 | return LY_EEXIST; |
| 3509 | } |
| 3510 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3511 | 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] | 3512 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3513 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3514 | * @brief Compile parsed RPC/action schema node information. |
| 3515 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3516 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3517 | * @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] | 3518 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3519 | * @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). |
| 3520 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3521 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3522 | */ |
| 3523 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3524 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3525 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3526 | { |
| 3527 | LY_ERR ret = LY_SUCCESS; |
| 3528 | struct lysp_node *child_p; |
| 3529 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3530 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3531 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3532 | lysc_update_path(ctx, parent, action_p->name); |
| 3533 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3534 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3535 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3536 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3537 | action_p->name, action)) { |
| 3538 | return LY_EVALID; |
| 3539 | } |
| 3540 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3541 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3542 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3543 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3544 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3545 | return LY_EVALID; |
| 3546 | } |
| 3547 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3548 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3549 | action->module = ctx->mod; |
| 3550 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3551 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3552 | action->sp = action_p; |
| 3553 | } |
| 3554 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3555 | |
| 3556 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3557 | * 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] | 3558 | 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] | 3559 | |
| 3560 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3561 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3562 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3563 | 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] | 3564 | 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] | 3565 | |
| 3566 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3567 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3568 | 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] | 3569 | 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] | 3570 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3571 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3572 | 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] | 3573 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3574 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3575 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3576 | |
| 3577 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3578 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3579 | 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] | 3580 | 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] | 3581 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3582 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3583 | 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] | 3584 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3585 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3586 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3587 | |
| 3588 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3589 | /* do not check "must" semantics in a grouping */ |
| 3590 | ly_set_add(&ctx->unres, action, 0); |
| 3591 | } |
| 3592 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3593 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3594 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3595 | return ret; |
| 3596 | } |
| 3597 | |
| 3598 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3599 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3600 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3601 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3602 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3603 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3604 | * @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] | 3605 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3606 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3607 | */ |
| 3608 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3609 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3610 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3611 | { |
| 3612 | LY_ERR ret = LY_SUCCESS; |
| 3613 | struct lysp_node *child_p; |
| 3614 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3615 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3616 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3617 | lysc_update_path(ctx, parent, notif_p->name); |
| 3618 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3619 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3620 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3621 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3622 | notif_p->name, notif)) { |
| 3623 | return LY_EVALID; |
| 3624 | } |
| 3625 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3626 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3627 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3628 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3629 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3630 | return LY_EVALID; |
| 3631 | } |
| 3632 | |
| 3633 | notif->nodetype = LYS_NOTIF; |
| 3634 | notif->module = ctx->mod; |
| 3635 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3636 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3637 | notif->sp = notif_p; |
| 3638 | } |
| 3639 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3640 | |
| 3641 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3642 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3643 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3644 | |
| 3645 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3646 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3647 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3648 | 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] | 3649 | 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] | 3650 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3651 | /* do not check "must" semantics in a grouping */ |
| 3652 | ly_set_add(&ctx->unres, notif, 0); |
| 3653 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3654 | 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] | 3655 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3656 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3657 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3658 | 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] | 3659 | } |
| 3660 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3661 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3662 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3663 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3664 | return ret; |
| 3665 | } |
| 3666 | |
| 3667 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3668 | * @brief Compile parsed container node information. |
| 3669 | * @param[in] ctx Compile context |
| 3670 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3671 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3672 | * is enriched with the container-specific information. |
| 3673 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3674 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3675 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3676 | 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] | 3677 | { |
| 3678 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3679 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3680 | struct lysp_node *child_p; |
| 3681 | unsigned int u; |
| 3682 | LY_ERR ret = LY_SUCCESS; |
| 3683 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3684 | if (cont_p->presence) { |
| 3685 | cont->flags |= LYS_PRESENCE; |
| 3686 | } |
| 3687 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3688 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3689 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3690 | } |
| 3691 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3692 | 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] | 3693 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3694 | /* do not check "must" semantics in a grouping */ |
| 3695 | ly_set_add(&ctx->unres, cont, 0); |
| 3696 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3697 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3698 | 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] | 3699 | |
| 3700 | done: |
| 3701 | return ret; |
| 3702 | } |
| 3703 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3704 | /* |
| 3705 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3706 | * @param[in] ctx Compile context. |
| 3707 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3708 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3709 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3710 | * @return LY_ERR value. |
| 3711 | */ |
| 3712 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3713 | 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] | 3714 | { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3715 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3716 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3717 | 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] | 3718 | leaf->units ? NULL : &leaf->units)); |
| 3719 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3720 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3721 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3722 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3723 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3724 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3725 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3726 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3727 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3728 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3729 | } |
| 3730 | } else { |
| 3731 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3732 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3733 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3734 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3735 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3736 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3737 | } |
| 3738 | } |
| 3739 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3740 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3741 | ly_set_add(&ctx->unres, leaf, 0); |
| 3742 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3743 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3744 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3745 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3746 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3747 | ly_set_add(&ctx->unres, leaf, 0); |
| 3748 | } |
| 3749 | } |
| 3750 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3751 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3752 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3753 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3754 | return LY_EVALID; |
| 3755 | } |
| 3756 | } |
| 3757 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3758 | return LY_SUCCESS; |
| 3759 | } |
| 3760 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3761 | /** |
| 3762 | * @brief Compile parsed leaf node information. |
| 3763 | * @param[in] ctx Compile context |
| 3764 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3765 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3766 | * is enriched with the leaf-specific information. |
| 3767 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3768 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3769 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3770 | 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] | 3771 | { |
| 3772 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3773 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3774 | unsigned int u; |
| 3775 | LY_ERR ret = LY_SUCCESS; |
| 3776 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3777 | 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] | 3778 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3779 | /* do not check "must" semantics in a grouping */ |
| 3780 | ly_set_add(&ctx->unres, leaf, 0); |
| 3781 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3782 | if (leaf_p->units) { |
| 3783 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3784 | leaf->flags |= LYS_SET_UNITS; |
| 3785 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3786 | |
| 3787 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3788 | leaf->dflt = (void*)leaf_p->dflt; |
| 3789 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3790 | if (ret) { |
| 3791 | leaf->dflt = NULL; |
| 3792 | return ret; |
| 3793 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3794 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3795 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3796 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3797 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3798 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3799 | leaf->dflt->realtype = leaf->type; |
| 3800 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3801 | 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] | 3802 | 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] | 3803 | leaf->dflt->realtype->refcount++; |
| 3804 | if (err) { |
| 3805 | ly_err_print(err); |
| 3806 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3807 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3808 | ly_err_free(err); |
| 3809 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3810 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3811 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3812 | 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] | 3813 | |
| 3814 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3815 | ret = LY_SUCCESS; |
| 3816 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3817 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3818 | leaf->flags |= LYS_SET_DFLT; |
| 3819 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3820 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3821 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3822 | done: |
| 3823 | return ret; |
| 3824 | } |
| 3825 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3826 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3827 | * @brief Compile parsed leaf-list node information. |
| 3828 | * @param[in] ctx Compile context |
| 3829 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3830 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3831 | * is enriched with the leaf-list-specific information. |
| 3832 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3833 | */ |
| 3834 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3835 | 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] | 3836 | { |
| 3837 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3838 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3839 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3840 | LY_ERR ret = LY_SUCCESS; |
| 3841 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3842 | 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] | 3843 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3844 | /* do not check "must" semantics in a grouping */ |
| 3845 | ly_set_add(&ctx->unres, llist, 0); |
| 3846 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3847 | if (llist_p->units) { |
| 3848 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3849 | llist->flags |= LYS_SET_UNITS; |
| 3850 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3851 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3852 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3853 | llist->dflts = (void*)llist_p->dflts; |
| 3854 | 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] | 3855 | if (ret != LY_SUCCESS) { |
| 3856 | /* make sure the defaults are freed correctly */ |
| 3857 | if (llist_p->dflts) { |
| 3858 | llist->dflts = NULL; |
| 3859 | } |
| 3860 | return ret; |
| 3861 | } |
| 3862 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3863 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3864 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3865 | 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] | 3866 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3867 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3868 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3869 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3870 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3871 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3872 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3873 | llist->dflts[u]->realtype = llist->type; |
| 3874 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3875 | 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] | 3876 | 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] | 3877 | llist->dflts[u]->realtype->refcount++; |
| 3878 | if (err) { |
| 3879 | ly_err_print(err); |
| 3880 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3881 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3882 | ly_err_free(err); |
| 3883 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3884 | if (ret == LY_EINCOMPLETE) { |
| 3885 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3886 | 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] | 3887 | |
| 3888 | /* but in general result is so far ok */ |
| 3889 | ret = LY_SUCCESS; |
| 3890 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3891 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3892 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3893 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3894 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3895 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3896 | /* configuration data values must be unique - so check the default values */ |
| 3897 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3898 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3899 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3900 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3901 | 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] | 3902 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3903 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3904 | if (dynamic) { |
| 3905 | free((char*)val); |
| 3906 | } |
| 3907 | return LY_EVALID; |
| 3908 | } |
| 3909 | } |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | /* 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] | 3914 | |
| 3915 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3916 | if (llist->min) { |
| 3917 | llist->flags |= LYS_MAND_TRUE; |
| 3918 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3919 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3920 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3921 | done: |
| 3922 | return ret; |
| 3923 | } |
| 3924 | |
| 3925 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3926 | * @brief Compile information about list's uniques. |
| 3927 | * @param[in] ctx Compile context. |
| 3928 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3929 | * @param[in] uniques Sized array list of unique statements. |
| 3930 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3931 | * @return LY_ERR value. |
| 3932 | */ |
| 3933 | static LY_ERR |
| 3934 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3935 | { |
| 3936 | LY_ERR ret = LY_SUCCESS; |
| 3937 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3938 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3939 | const char *keystr, *delim; |
| 3940 | size_t len; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3941 | LY_ARRAY_SIZE_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3942 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3943 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3944 | |
| 3945 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3946 | config = -1; |
| 3947 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3948 | keystr = uniques[v]; |
| 3949 | while (keystr) { |
| 3950 | delim = strpbrk(keystr, " \t\n"); |
| 3951 | if (delim) { |
| 3952 | len = delim - keystr; |
| 3953 | while (isspace(*delim)) { |
| 3954 | ++delim; |
| 3955 | } |
| 3956 | } else { |
| 3957 | len = strlen(keystr); |
| 3958 | } |
| 3959 | |
| 3960 | /* unique node must be present */ |
| 3961 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3962 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3963 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3964 | if (ret != LY_SUCCESS) { |
| 3965 | if (ret == LY_EDENIED) { |
| 3966 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3967 | "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] | 3968 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3969 | } |
| 3970 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3971 | } else if (flags) { |
| 3972 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3973 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3974 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3975 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3976 | } |
| 3977 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3978 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3979 | /* all referenced leafs must be of the same config type */ |
| 3980 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3981 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3982 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3983 | return LY_EVALID; |
| 3984 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3985 | config = 1; |
| 3986 | } else { /* LYS_CONFIG_R */ |
| 3987 | config = 0; |
| 3988 | } |
| 3989 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3990 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3991 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3992 | if (parent->nodetype == LYS_LIST) { |
| 3993 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3994 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3995 | return LY_EVALID; |
| 3996 | } |
| 3997 | } |
| 3998 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3999 | /* check status */ |
| 4000 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 4001 | (*key)->flags, (*key)->module, (*key)->name)); |
| 4002 | |
| 4003 | /* mark leaf as unique */ |
| 4004 | (*key)->flags |= LYS_UNIQUE; |
| 4005 | |
| 4006 | /* next unique value in line */ |
| 4007 | keystr = delim; |
| 4008 | } |
| 4009 | /* next unique definition */ |
| 4010 | } |
| 4011 | |
| 4012 | return LY_SUCCESS; |
| 4013 | } |
| 4014 | |
| 4015 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4016 | * @brief Compile parsed list node information. |
| 4017 | * @param[in] ctx Compile context |
| 4018 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4019 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4020 | * is enriched with the list-specific information. |
| 4021 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4022 | */ |
| 4023 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4024 | 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] | 4025 | { |
| 4026 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 4027 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 4028 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4029 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4030 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4031 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4032 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4033 | LY_ERR ret = LY_SUCCESS; |
| 4034 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4035 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4036 | if (list->min) { |
| 4037 | list->flags |= LYS_MAND_TRUE; |
| 4038 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4039 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 4040 | |
| 4041 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4042 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4043 | } |
| 4044 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4045 | 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] | 4046 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4047 | /* do not check "must" semantics in a grouping */ |
| 4048 | ly_set_add(&ctx->unres, list, 0); |
| 4049 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4050 | |
| 4051 | /* keys */ |
| 4052 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 4053 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 4054 | return LY_EVALID; |
| 4055 | } |
| 4056 | |
| 4057 | /* find all the keys (must be direct children) */ |
| 4058 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4059 | if (!keystr) { |
| 4060 | /* keyless list */ |
| 4061 | list->flags |= LYS_KEYLESS; |
| 4062 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4063 | while (keystr) { |
| 4064 | delim = strpbrk(keystr, " \t\n"); |
| 4065 | if (delim) { |
| 4066 | len = delim - keystr; |
| 4067 | while (isspace(*delim)) { |
| 4068 | ++delim; |
| 4069 | } |
| 4070 | } else { |
| 4071 | len = strlen(keystr); |
| 4072 | } |
| 4073 | |
| 4074 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4075 | 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] | 4076 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4077 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4078 | "The list's key \"%.*s\" not found.", len, keystr); |
| 4079 | return LY_EVALID; |
| 4080 | } |
| 4081 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4082 | if (key->flags & LYS_KEY) { |
| 4083 | /* the node was already marked as a key */ |
| 4084 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4085 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 4086 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4087 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4088 | |
| 4089 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4090 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4091 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4092 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 4093 | return LY_EVALID; |
| 4094 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4095 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4096 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4097 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4098 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4099 | "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] | 4100 | return LY_EVALID; |
| 4101 | } |
| 4102 | } else { |
| 4103 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4104 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4105 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4106 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4107 | return LY_EVALID; |
| 4108 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4109 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4110 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4111 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4112 | return LY_EVALID; |
| 4113 | } |
| 4114 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4115 | |
| 4116 | /* check status */ |
| 4117 | 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] | 4118 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4119 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4120 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4121 | if (key->dflt) { |
| 4122 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 4123 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 4124 | free(key->dflt); |
| 4125 | key->dflt = NULL; |
| 4126 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4127 | } |
| 4128 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4129 | key->flags |= LYS_KEY; |
| 4130 | |
| 4131 | /* move it to the correct position */ |
| 4132 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 4133 | /* fix links in closest previous siblings of the key */ |
| 4134 | if (key->next) { |
| 4135 | key->next->prev = key->prev; |
| 4136 | } else { |
| 4137 | /* last child */ |
| 4138 | list->child->prev = key->prev; |
| 4139 | } |
| 4140 | if (key->prev->next) { |
| 4141 | key->prev->next = key->next; |
| 4142 | } |
| 4143 | /* fix links in the key */ |
| 4144 | if (prev_key) { |
| 4145 | key->prev = (struct lysc_node*)prev_key; |
| 4146 | key->next = prev_key->next; |
| 4147 | } else { |
| 4148 | key->prev = list->child->prev; |
| 4149 | key->next = list->child; |
| 4150 | } |
| 4151 | /* fix links in closes future siblings of the key */ |
| 4152 | if (prev_key) { |
| 4153 | if (prev_key->next) { |
| 4154 | prev_key->next->prev = (struct lysc_node*)key; |
| 4155 | } else { |
| 4156 | list->child->prev = (struct lysc_node*)key; |
| 4157 | } |
| 4158 | prev_key->next = (struct lysc_node*)key; |
| 4159 | } else { |
| 4160 | list->child->prev = (struct lysc_node*)key; |
| 4161 | } |
| 4162 | /* fix links in parent */ |
| 4163 | if (!key->prev->next) { |
| 4164 | list->child = (struct lysc_node*)key; |
| 4165 | } |
| 4166 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4167 | |
| 4168 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4169 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4170 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4171 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | /* uniques */ |
| 4175 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4176 | 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] | 4177 | } |
| 4178 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4179 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 4180 | 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] | 4181 | |
| 4182 | done: |
| 4183 | return ret; |
| 4184 | } |
| 4185 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4186 | /** |
| 4187 | * @brief Do some checks and set the default choice's case. |
| 4188 | * |
| 4189 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 4190 | * |
| 4191 | * @param[in] ctx Compile context. |
| 4192 | * @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, |
| 4193 | * not the reference to the imported module. |
| 4194 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 4195 | * @return LY_ERR value. |
| 4196 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4197 | static LY_ERR |
| 4198 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 4199 | { |
| 4200 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 4201 | const char *prefix = NULL, *name; |
| 4202 | size_t prefix_len = 0; |
| 4203 | |
| 4204 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4205 | name = strchr(dflt, ':'); |
| 4206 | if (name) { |
| 4207 | prefix = dflt; |
| 4208 | prefix_len = name - prefix; |
| 4209 | ++name; |
| 4210 | } else { |
| 4211 | name = dflt; |
| 4212 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 4213 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4214 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 4215 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4216 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 4217 | prefix_len, prefix); |
| 4218 | return LY_EVALID; |
| 4219 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4220 | 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] | 4221 | if (!ch->dflt) { |
| 4222 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4223 | "Default case \"%s\" not found.", dflt); |
| 4224 | return LY_EVALID; |
| 4225 | } |
| 4226 | /* no mandatory nodes directly under the default case */ |
| 4227 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4228 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 4229 | break; |
| 4230 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4231 | if (iter->flags & LYS_MAND_TRUE) { |
| 4232 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4233 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 4234 | return LY_EVALID; |
| 4235 | } |
| 4236 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4237 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4238 | return LY_SUCCESS; |
| 4239 | } |
| 4240 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4241 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4242 | 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] | 4243 | { |
| 4244 | struct lys_module *mod; |
| 4245 | const char *prefix = NULL, *name; |
| 4246 | size_t prefix_len = 0; |
| 4247 | struct lysc_node_case *cs; |
| 4248 | struct lysc_node *node; |
| 4249 | |
| 4250 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4251 | name = strchr(dflt, ':'); |
| 4252 | if (name) { |
| 4253 | prefix = dflt; |
| 4254 | prefix_len = name - prefix; |
| 4255 | ++name; |
| 4256 | } else { |
| 4257 | name = dflt; |
| 4258 | } |
| 4259 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 4260 | if (prefix) { |
| 4261 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 4262 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4263 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 4264 | "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] | 4265 | return LY_EVALID; |
| 4266 | } |
| 4267 | } else { |
| 4268 | mod = ctx->mod; |
| 4269 | } |
| 4270 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4271 | 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] | 4272 | if (!cs) { |
| 4273 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4274 | "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] | 4275 | return LY_EVALID; |
| 4276 | } |
| 4277 | |
| 4278 | /* check that there is no mandatory node */ |
| 4279 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4280 | if (node->parent != (struct lysc_node*)cs) { |
| 4281 | break; |
| 4282 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4283 | if (node->flags & LYS_MAND_TRUE) { |
| 4284 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4285 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4286 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4287 | return LY_EVALID; |
| 4288 | } |
| 4289 | } |
| 4290 | |
| 4291 | /* set the default case in choice */ |
| 4292 | ch->dflt = cs; |
| 4293 | cs->flags |= LYS_SET_DFLT; |
| 4294 | |
| 4295 | return LY_SUCCESS; |
| 4296 | } |
| 4297 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4298 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4299 | * @brief Compile parsed choice node information. |
| 4300 | * @param[in] ctx Compile context |
| 4301 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4302 | * @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] | 4303 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4304 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4305 | */ |
| 4306 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4307 | 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] | 4308 | { |
| 4309 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4310 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4311 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4312 | LY_ERR ret = LY_SUCCESS; |
| 4313 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4314 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4315 | if (child_p->nodetype == LYS_CASE) { |
| 4316 | 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] | 4317 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4318 | } |
| 4319 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4320 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4321 | } |
| 4322 | } |
| 4323 | |
| 4324 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4325 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4326 | 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] | 4327 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4328 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4329 | return ret; |
| 4330 | } |
| 4331 | |
| 4332 | /** |
| 4333 | * @brief Compile parsed anydata or anyxml node information. |
| 4334 | * @param[in] ctx Compile context |
| 4335 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4336 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4337 | * is enriched with the any-specific information. |
| 4338 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4339 | */ |
| 4340 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4341 | 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] | 4342 | { |
| 4343 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4344 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4345 | unsigned int u; |
| 4346 | LY_ERR ret = LY_SUCCESS; |
| 4347 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4348 | 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] | 4349 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4350 | /* do not check "must" semantics in a grouping */ |
| 4351 | ly_set_add(&ctx->unres, any, 0); |
| 4352 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4353 | |
| 4354 | if (any->flags & LYS_CONFIG_W) { |
| 4355 | 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] | 4356 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4357 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4358 | done: |
| 4359 | return ret; |
| 4360 | } |
| 4361 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4362 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4363 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4364 | * |
| 4365 | * @param[in] ctx Compile context |
| 4366 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4367 | * the choice itself is expected instead of a specific case node. |
| 4368 | * @param[in] node Schema node to connect into the list. |
| 4369 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4370 | * 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] | 4371 | */ |
| 4372 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4373 | 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] | 4374 | { |
| 4375 | struct lysc_node **children; |
| 4376 | |
| 4377 | if (node->nodetype == LYS_CASE) { |
| 4378 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4379 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4380 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4381 | } |
| 4382 | if (children) { |
| 4383 | if (!(*children)) { |
| 4384 | /* first child */ |
| 4385 | *children = node; |
| 4386 | } else if (*children != node) { |
| 4387 | /* by the condition in previous branch we cover the choice/case children |
| 4388 | * - the children list is shared by the choice and the the first case, in addition |
| 4389 | * the first child of each case must be referenced from the case node. So the node is |
| 4390 | * actually always already inserted in case it is the first children - so here such |
| 4391 | * a situation actually corresponds to the first branch */ |
| 4392 | /* insert at the end of the parent's children list */ |
| 4393 | (*children)->prev->next = node; |
| 4394 | node->prev = (*children)->prev; |
| 4395 | (*children)->prev = node; |
| 4396 | |
| 4397 | /* check the name uniqueness */ |
| 4398 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4399 | lysc_node_notifs(parent), node->name, node)) { |
| 4400 | return LY_EEXIST; |
| 4401 | } |
| 4402 | } |
| 4403 | } |
| 4404 | return LY_SUCCESS; |
| 4405 | } |
| 4406 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4407 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4408 | * @brief Prepare the case structure in choice node for the new data node. |
| 4409 | * |
| 4410 | * 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 |
| 4411 | * created in the choice when the first child was processed. |
| 4412 | * |
| 4413 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4414 | * @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, |
| 4415 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4416 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4417 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4418 | * @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, |
| 4419 | * 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] | 4420 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4421 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4422 | 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] | 4423 | { |
| 4424 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4425 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4426 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4427 | unsigned int u; |
| 4428 | LY_ERR ret; |
| 4429 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4430 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4431 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4432 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4433 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4434 | return NULL; \ |
| 4435 | } \ |
| 4436 | } |
| 4437 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4438 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4439 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4440 | |
| 4441 | /* we have to add an implicit case node into the parent choice */ |
| 4442 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4443 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4444 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4445 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4446 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4447 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4448 | /* the case is already present since the child is not its first children */ |
| 4449 | return (struct lysc_node_case*)ch->cases->prev; |
| 4450 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4451 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4452 | |
| 4453 | /* explicit parent case is not present (this is its first child) */ |
| 4454 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4455 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4456 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4457 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4458 | cs->sp = node_p; |
| 4459 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4460 | /* 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] | 4461 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4462 | |
| 4463 | if (node_p->when) { |
| 4464 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4465 | 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] | 4466 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4467 | |
| 4468 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4469 | /* do not check "when" semantics in a grouping */ |
| 4470 | ly_set_add(&ctx->unres, cs, 0); |
| 4471 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4472 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4473 | 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] | 4474 | } else { |
| 4475 | LOGINT(ctx->ctx); |
| 4476 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4477 | } |
| 4478 | cs->module = ctx->mod; |
| 4479 | cs->prev = (struct lysc_node*)cs; |
| 4480 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4481 | 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] | 4482 | cs->child = child; |
| 4483 | |
| 4484 | return cs; |
| 4485 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4486 | if (cs) { |
| 4487 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4488 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4489 | return NULL; |
| 4490 | |
| 4491 | #undef UNIQUE_CHECK |
| 4492 | } |
| 4493 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4494 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4495 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4496 | * |
| 4497 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4498 | * @param[in] node Target node where the config is supposed to be changed. |
| 4499 | * @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] | 4500 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4501 | * 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] | 4502 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4503 | * @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] | 4504 | * @return LY_ERR value. |
| 4505 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4506 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4507 | 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] | 4508 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4509 | { |
| 4510 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4511 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4512 | |
| 4513 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4514 | /* nothing to do */ |
| 4515 | return LY_SUCCESS; |
| 4516 | } |
| 4517 | |
| 4518 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4519 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4520 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4521 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4522 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4523 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4524 | return LY_EVALID; |
| 4525 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4526 | node->flags |= LYS_SET_CONFIG; |
| 4527 | } else { |
| 4528 | if (node->flags & LYS_SET_CONFIG) { |
| 4529 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4530 | /* setting config flags, but have node with explicit config true */ |
| 4531 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4532 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4533 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4534 | return LY_EVALID; |
| 4535 | } |
| 4536 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4537 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4538 | return LY_SUCCESS; |
| 4539 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4540 | } |
| 4541 | node->flags &= ~LYS_CONFIG_MASK; |
| 4542 | node->flags |= config; |
| 4543 | |
| 4544 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4545 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4546 | 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] | 4547 | } |
| 4548 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4549 | return LY_SUCCESS; |
| 4550 | } |
| 4551 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4552 | /** |
| 4553 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4554 | * |
| 4555 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4556 | * the flag to such parents from a mandatory children. |
| 4557 | * |
| 4558 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4559 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4560 | * (mandatory children was removed). |
| 4561 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4562 | void |
| 4563 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4564 | { |
| 4565 | struct lysc_node *iter; |
| 4566 | |
| 4567 | if (add) { /* set flag */ |
| 4568 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4569 | parent = parent->parent) { |
| 4570 | parent->flags |= LYS_MAND_TRUE; |
| 4571 | } |
| 4572 | } else { /* unset flag */ |
| 4573 | 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] | 4574 | 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] | 4575 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4576 | /* there is another mandatory node */ |
| 4577 | return; |
| 4578 | } |
| 4579 | } |
| 4580 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4581 | parent->flags &= ~LYS_MAND_TRUE; |
| 4582 | } |
| 4583 | } |
| 4584 | } |
| 4585 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4586 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4587 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4588 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4589 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4590 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4591 | */ |
| 4592 | static void |
| 4593 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4594 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4595 | LY_ARRAY_SIZE_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4596 | size_t len; |
| 4597 | |
| 4598 | len = strlen(aug_p->nodeid); |
| 4599 | LY_ARRAY_FOR(result, v) { |
| 4600 | if (strlen(result[v]->nodeid) <= len) { |
| 4601 | continue; |
| 4602 | } |
| 4603 | if (v < LY_ARRAY_SIZE(result)) { |
| 4604 | /* move the rest of array */ |
| 4605 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4606 | break; |
| 4607 | } |
| 4608 | } |
| 4609 | result[v] = aug_p; |
| 4610 | LY_ARRAY_INCREMENT(result); |
| 4611 | } |
| 4612 | |
| 4613 | /** |
| 4614 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4615 | * |
| 4616 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4617 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4618 | * |
| 4619 | * @param[in] ctx Compile context. |
| 4620 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4621 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4622 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4623 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4624 | * @return LY_ERR value. |
| 4625 | */ |
| 4626 | LY_ERR |
| 4627 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4628 | { |
| 4629 | struct lysp_augment **result = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4630 | LY_ARRAY_SIZE_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4631 | |
| 4632 | assert(augments); |
| 4633 | |
| 4634 | /* get count of the augments in module and all its submodules */ |
| 4635 | if (aug_p) { |
| 4636 | count += LY_ARRAY_SIZE(aug_p); |
| 4637 | } |
| 4638 | LY_ARRAY_FOR(inc_p, u) { |
| 4639 | if (inc_p[u].submodule->augments) { |
| 4640 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4641 | } |
| 4642 | } |
| 4643 | |
| 4644 | if (!count) { |
| 4645 | *augments = NULL; |
| 4646 | return LY_SUCCESS; |
| 4647 | } |
| 4648 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4649 | |
| 4650 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4651 | * together, so there can be even /z/y betwwen them. */ |
| 4652 | LY_ARRAY_FOR(aug_p, u) { |
| 4653 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4654 | } |
| 4655 | LY_ARRAY_FOR(inc_p, u) { |
| 4656 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4657 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4658 | } |
| 4659 | } |
| 4660 | |
| 4661 | *augments = result; |
| 4662 | return LY_SUCCESS; |
| 4663 | } |
| 4664 | |
| 4665 | /** |
| 4666 | * @brief Compile the parsed augment connecting it into its target. |
| 4667 | * |
| 4668 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4669 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4670 | * are already implemented and compiled. |
| 4671 | * |
| 4672 | * @param[in] ctx Compile context. |
| 4673 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4674 | * @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 |
| 4675 | * children in case of the augmenting uses data. |
| 4676 | * @return LY_SUCCESS on success. |
| 4677 | * @return LY_EVALID on failure. |
| 4678 | */ |
| 4679 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4680 | 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] | 4681 | { |
| 4682 | LY_ERR ret = LY_SUCCESS; |
| 4683 | struct lysp_node *node_p, *case_node_p; |
| 4684 | struct lysc_node *target; /* target target of the augment */ |
| 4685 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4686 | struct lysc_when **when, *when_shared; |
| 4687 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4688 | uint16_t flags = 0; |
| 4689 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4690 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4691 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4692 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4693 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4694 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4695 | 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] | 4696 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4697 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4698 | if (ret != LY_SUCCESS) { |
| 4699 | if (ret == LY_EDENIED) { |
| 4700 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4701 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4702 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4703 | } |
| 4704 | return LY_EVALID; |
| 4705 | } |
| 4706 | |
| 4707 | /* check for mandatory nodes |
| 4708 | * - new cases augmenting some choice can have mandatory nodes |
| 4709 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4710 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4711 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4712 | allow_mandatory = 1; |
| 4713 | } |
| 4714 | |
| 4715 | when_shared = NULL; |
| 4716 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4717 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4718 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4719 | && !((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] | 4720 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4721 | && !(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] | 4722 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4723 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4724 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4725 | return LY_EVALID; |
| 4726 | } |
| 4727 | |
| 4728 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4729 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4730 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4731 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4732 | } else { |
| 4733 | 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] | 4734 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4735 | } |
| 4736 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4737 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4738 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4739 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4740 | * 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] | 4741 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4742 | /* 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] | 4743 | 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] | 4744 | } else if (target->nodetype == LYS_CHOICE) { |
| 4745 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4746 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4747 | } else { |
| 4748 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4749 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4750 | if (!node) { |
| 4751 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4752 | break; |
| 4753 | } |
| 4754 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4755 | } |
| 4756 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4757 | 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] | 4758 | node->flags &= ~LYS_MAND_TRUE; |
| 4759 | lys_compile_mandatory_parents(target, 0); |
| 4760 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4761 | "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] | 4762 | return LY_EVALID; |
| 4763 | } |
| 4764 | |
| 4765 | /* pass augment's when to all the children */ |
| 4766 | if (aug_p->when) { |
| 4767 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4768 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4769 | 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] | 4770 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4771 | |
| 4772 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4773 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4774 | ly_set_add(&ctx->unres, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4775 | } |
| 4776 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4777 | when_shared = *when; |
| 4778 | } else { |
| 4779 | ++when_shared->refcount; |
| 4780 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4781 | |
| 4782 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4783 | /* in this case check "when" again for all children because of dummy node check */ |
| 4784 | ly_set_add(&ctx->unres, node, 0); |
| 4785 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4786 | } |
| 4787 | } |
| 4788 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4789 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4790 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4791 | switch (target->nodetype) { |
| 4792 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4793 | 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] | 4794 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4795 | 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] | 4796 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4797 | break; |
| 4798 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4799 | 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] | 4800 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4801 | 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] | 4802 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4803 | break; |
| 4804 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4805 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4806 | if (aug_p->actions) { |
| 4807 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4808 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4809 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4810 | return LY_EVALID; |
| 4811 | } |
| 4812 | if (aug_p->notifs) { |
| 4813 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4814 | "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] | 4815 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4816 | return LY_EVALID; |
| 4817 | } |
| 4818 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4819 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4820 | lysc_update_path(ctx, NULL, NULL); |
| 4821 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4822 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4823 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4824 | return ret; |
| 4825 | } |
| 4826 | |
| 4827 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4828 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4829 | * |
| 4830 | * @param[in] ctx Compile context. |
| 4831 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4832 | * @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] | 4833 | * @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] | 4834 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4835 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4836 | * 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] | 4837 | * @return LY_ERR value. |
| 4838 | */ |
| 4839 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4840 | 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] | 4841 | { |
| 4842 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4843 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4844 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4845 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4846 | return LY_EVALID; |
| 4847 | } |
| 4848 | |
| 4849 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4850 | /* check if node has default value */ |
| 4851 | if (node->nodetype & LYS_LEAF) { |
| 4852 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4853 | if (refine_flag) { |
| 4854 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4855 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4856 | return LY_EVALID; |
| 4857 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4858 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4859 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4860 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4861 | |
| 4862 | /* update the list of incomplete default values if needed */ |
| 4863 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4864 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4865 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4866 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4867 | free(leaf->dflt); |
| 4868 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4869 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4870 | } |
| 4871 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4872 | if (refine_flag) { |
| 4873 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4874 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4875 | return LY_EVALID; |
| 4876 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4877 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4878 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4879 | 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] | 4880 | return LY_EVALID; |
| 4881 | } |
| 4882 | |
| 4883 | node->flags &= ~LYS_MAND_FALSE; |
| 4884 | node->flags |= LYS_MAND_TRUE; |
| 4885 | lys_compile_mandatory_parents(node->parent, 1); |
| 4886 | } else { |
| 4887 | /* make mandatory false */ |
| 4888 | node->flags &= ~LYS_MAND_TRUE; |
| 4889 | node->flags |= LYS_MAND_FALSE; |
| 4890 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4891 | 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] | 4892 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4893 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4894 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4895 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4896 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4897 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4898 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4899 | } |
| 4900 | } |
| 4901 | return LY_SUCCESS; |
| 4902 | } |
| 4903 | |
| 4904 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4905 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4906 | * If present, also apply uses's modificators. |
| 4907 | * |
| 4908 | * @param[in] ctx Compile context |
| 4909 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4910 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4911 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4912 | * the compile context. |
| 4913 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4914 | */ |
| 4915 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4916 | 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] | 4917 | { |
| 4918 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4919 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4920 | /* 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] | 4921 | struct lysc_node_container context_node_fake = |
| 4922 | {.nodetype = LYS_CONTAINER, |
| 4923 | .module = ctx->mod, |
| 4924 | .flags = parent ? parent->flags : 0, |
| 4925 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4926 | .prev = (struct lysc_node*)&context_node_fake, |
| 4927 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4928 | struct lysp_grp *grp = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4929 | LY_ARRAY_SIZE_TYPE u, v; |
| 4930 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4931 | int found; |
| 4932 | const char *id, *name, *prefix; |
| 4933 | size_t prefix_len, name_len; |
| 4934 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4935 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4936 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4937 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4938 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4939 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4940 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4941 | struct lysp_augment **augments = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4942 | LY_ARRAY_SIZE_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4943 | struct lysc_notif **notifs = NULL; |
| 4944 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4945 | |
| 4946 | /* search for the grouping definition */ |
| 4947 | found = 0; |
| 4948 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4949 | 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] | 4950 | if (prefix) { |
| 4951 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4952 | if (!mod) { |
| 4953 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4954 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4955 | return LY_EVALID; |
| 4956 | } |
| 4957 | } else { |
| 4958 | mod = ctx->mod_def; |
| 4959 | } |
| 4960 | if (mod == ctx->mod_def) { |
| 4961 | 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] | 4962 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4963 | LY_ARRAY_FOR(grp, u) { |
| 4964 | if (!strcmp(grp[u].name, name)) { |
| 4965 | grp = &grp[u]; |
| 4966 | found = 1; |
| 4967 | break; |
| 4968 | } |
| 4969 | } |
| 4970 | } |
| 4971 | } |
| 4972 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4973 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4974 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4975 | if (grp) { |
| 4976 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4977 | if (!strcmp(grp[u].name, name)) { |
| 4978 | grp = &grp[u]; |
| 4979 | found = 1; |
| 4980 | } |
| 4981 | } |
| 4982 | } |
| 4983 | if (!found && mod->parsed->includes) { |
| 4984 | /* ... and all the submodules */ |
| 4985 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4986 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4987 | if (grp) { |
| 4988 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4989 | if (!strcmp(grp[v].name, name)) { |
| 4990 | grp = &grp[v]; |
| 4991 | found = 1; |
| 4992 | } |
| 4993 | } |
| 4994 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4995 | } |
| 4996 | } |
| 4997 | } |
| 4998 | if (!found) { |
| 4999 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5000 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 5001 | return LY_EVALID; |
| 5002 | } |
| 5003 | |
| 5004 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 5005 | grp_stack_count = ctx->groupings.count; |
| 5006 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 5007 | if (grp_stack_count == ctx->groupings.count) { |
| 5008 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 5009 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5010 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 5011 | return LY_EVALID; |
| 5012 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5013 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5014 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 5015 | grp->flags |= LYS_USED_GRP; |
| 5016 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5017 | |
| 5018 | /* switch context's mod_def */ |
| 5019 | mod_old = ctx->mod_def; |
| 5020 | ctx->mod_def = mod; |
| 5021 | |
| 5022 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5023 | 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] | 5024 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5025 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5026 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5027 | /* 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] | 5028 | 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] | 5029 | |
| 5030 | /* some preparation for applying refines */ |
| 5031 | if (grp->data == node_p) { |
| 5032 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 5033 | if (parent) { |
| 5034 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 5035 | } else if (ctx->mod->compiled->data) { |
| 5036 | child = ctx->mod->compiled->data; |
| 5037 | } else { |
| 5038 | child = NULL; |
| 5039 | } |
| 5040 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5041 | } |
| 5042 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5043 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5044 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5045 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5046 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5047 | /* 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] | 5048 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5049 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5050 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5051 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 5052 | |
| 5053 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5054 | /* do not check "when" semantics in a grouping */ |
| 5055 | ly_set_add(&ctx->unres, child, 0); |
| 5056 | } |
| 5057 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5058 | when_shared = *when; |
| 5059 | } else { |
| 5060 | ++when_shared->refcount; |
| 5061 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5062 | |
| 5063 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5064 | /* in this case check "when" again for all children because of dummy node check */ |
| 5065 | ly_set_add(&ctx->unres, child, 0); |
| 5066 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5067 | } |
| 5068 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5069 | } |
| 5070 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5071 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5072 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5073 | /* 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] | 5074 | 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] | 5075 | } |
| 5076 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5077 | /* compile actions */ |
| 5078 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 5079 | if (actions) { |
| 5080 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5081 | 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] | 5082 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 5083 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 5084 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 5085 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 5086 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5087 | } |
| 5088 | } |
| 5089 | |
| 5090 | /* compile notifications */ |
| 5091 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 5092 | if (notifs) { |
| 5093 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5094 | 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] | 5095 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 5096 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 5097 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 5098 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 5099 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5100 | } |
| 5101 | } |
| 5102 | |
| 5103 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5104 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5105 | 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] | 5106 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5107 | 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] | 5108 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5109 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5110 | /* reload previous context's mod_def */ |
| 5111 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5112 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5113 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5114 | /* apply refine */ |
| 5115 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5116 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 5117 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5118 | 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] | 5119 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5120 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5121 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5122 | |
| 5123 | /* default value */ |
| 5124 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5125 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5127 | "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] | 5128 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5129 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5130 | } |
| 5131 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 5132 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5133 | "Invalid refine of default - %s cannot hold default value(s).", |
| 5134 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5135 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5136 | } |
| 5137 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5138 | struct ly_err_item *err = NULL; |
| 5139 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 5140 | if (leaf->dflt) { |
| 5141 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5142 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5143 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5144 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5145 | } else { |
| 5146 | /* prepare a new one */ |
| 5147 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 5148 | leaf->dflt->realtype = leaf->type; |
| 5149 | } |
| 5150 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5151 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5152 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 5153 | 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] | 5154 | 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] | 5155 | leaf->dflt->realtype->refcount++; |
| 5156 | if (err) { |
| 5157 | ly_err_print(err); |
| 5158 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5159 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 5160 | ly_err_free(err); |
| 5161 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5162 | if (rc == LY_EINCOMPLETE) { |
| 5163 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5164 | 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] | 5165 | |
| 5166 | /* but in general result is so far ok */ |
| 5167 | rc = LY_SUCCESS; |
| 5168 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5169 | LY_CHECK_GOTO(rc, cleanup); |
| 5170 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5171 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5172 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 5173 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5174 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5175 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5176 | "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] | 5177 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5178 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5179 | |
| 5180 | /* remove previous set of default values */ |
| 5181 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5182 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5183 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 5184 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 5185 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5186 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5187 | LY_ARRAY_FREE(llist->dflts); |
| 5188 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5189 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5190 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5191 | |
| 5192 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5193 | 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] | 5194 | 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] | 5195 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5196 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5197 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5198 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5199 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5200 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 5201 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5202 | 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] | 5203 | 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] | 5204 | 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] | 5205 | llist->dflts[u]->realtype->refcount++; |
| 5206 | if (err) { |
| 5207 | ly_err_print(err); |
| 5208 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5209 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 5210 | ly_err_free(err); |
| 5211 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5212 | if (rc == LY_EINCOMPLETE) { |
| 5213 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5214 | 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] | 5215 | |
| 5216 | /* but in general result is so far ok */ |
| 5217 | rc = LY_SUCCESS; |
| 5218 | } |
| 5219 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5220 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5221 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5222 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5223 | if (((struct lysc_node_choice*)node)->dflt) { |
| 5224 | /* unset LYS_SET_DFLT from the current default case */ |
| 5225 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 5226 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5227 | 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] | 5228 | } |
| 5229 | } |
| 5230 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5231 | /* description */ |
| 5232 | if (rfn->dsc) { |
| 5233 | FREE_STRING(ctx->ctx, node->dsc); |
| 5234 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 5235 | } |
| 5236 | |
| 5237 | /* reference */ |
| 5238 | if (rfn->ref) { |
| 5239 | FREE_STRING(ctx->ctx, node->ref); |
| 5240 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 5241 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5242 | |
| 5243 | /* config */ |
| 5244 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5245 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5246 | 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] | 5247 | } else { |
| 5248 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5249 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5250 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5251 | } |
| 5252 | |
| 5253 | /* mandatory */ |
| 5254 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5255 | 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] | 5256 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5257 | |
| 5258 | /* presence */ |
| 5259 | if (rfn->presence) { |
| 5260 | if (node->nodetype != LYS_CONTAINER) { |
| 5261 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5262 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5263 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5264 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5265 | } |
| 5266 | node->flags |= LYS_PRESENCE; |
| 5267 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5268 | |
| 5269 | /* must */ |
| 5270 | if (rfn->musts) { |
| 5271 | switch (node->nodetype) { |
| 5272 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5273 | 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] | 5274 | break; |
| 5275 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5276 | 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] | 5277 | break; |
| 5278 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5279 | 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] | 5280 | break; |
| 5281 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5282 | 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] | 5283 | break; |
| 5284 | case LYS_ANYXML: |
| 5285 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5286 | 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] | 5287 | break; |
| 5288 | default: |
| 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 must statement - %s cannot hold any must statement.", |
| 5291 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5292 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5293 | } |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 5294 | ly_set_add(&ctx->unres, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5295 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5296 | |
| 5297 | /* min/max-elements */ |
| 5298 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5299 | switch (node->nodetype) { |
| 5300 | case LYS_LEAFLIST: |
| 5301 | if (rfn->flags & LYS_SET_MAX) { |
| 5302 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5303 | } |
| 5304 | if (rfn->flags & LYS_SET_MIN) { |
| 5305 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5306 | if (rfn->min) { |
| 5307 | node->flags |= LYS_MAND_TRUE; |
| 5308 | lys_compile_mandatory_parents(node->parent, 1); |
| 5309 | } else { |
| 5310 | node->flags &= ~LYS_MAND_TRUE; |
| 5311 | lys_compile_mandatory_parents(node->parent, 0); |
| 5312 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5313 | } |
| 5314 | break; |
| 5315 | case LYS_LIST: |
| 5316 | if (rfn->flags & LYS_SET_MAX) { |
| 5317 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5318 | } |
| 5319 | if (rfn->flags & LYS_SET_MIN) { |
| 5320 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5321 | if (rfn->min) { |
| 5322 | node->flags |= LYS_MAND_TRUE; |
| 5323 | lys_compile_mandatory_parents(node->parent, 1); |
| 5324 | } else { |
| 5325 | node->flags &= ~LYS_MAND_TRUE; |
| 5326 | lys_compile_mandatory_parents(node->parent, 0); |
| 5327 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5328 | } |
| 5329 | break; |
| 5330 | default: |
| 5331 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5332 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5333 | (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] | 5334 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5335 | } |
| 5336 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5337 | |
| 5338 | /* if-feature */ |
| 5339 | if (rfn->iffeatures) { |
| 5340 | /* 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] | 5341 | 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] | 5342 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5343 | |
| 5344 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5345 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5346 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5347 | /* 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] | 5348 | for (uint32_t i = 0; i < refined.count; ++i) { |
| 5349 | node = (struct lysc_node*)refined.objs[i]; |
| 5350 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5351 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5352 | |
| 5353 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5354 | if ((node->flags & LYS_MAND_TRUE) && |
| 5355 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5356 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5357 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5358 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5359 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5360 | } |
| 5361 | |
| 5362 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5363 | if (node->nodetype == LYS_LIST) { |
| 5364 | min = ((struct lysc_node_list*)node)->min; |
| 5365 | max = ((struct lysc_node_list*)node)->max; |
| 5366 | } else { |
| 5367 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5368 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5369 | } |
| 5370 | if (min > max) { |
| 5371 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5372 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5373 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5374 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5375 | } |
| 5376 | } |
| 5377 | } |
| 5378 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5379 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5380 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5381 | |
| 5382 | cleanup: |
| 5383 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5384 | if (context_node_fake.child) { |
| 5385 | context_node_fake.child->prev = child; |
| 5386 | } |
| 5387 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5388 | child->parent = parent; |
| 5389 | } |
| 5390 | |
| 5391 | if (uses_p->augments || uses_p->refines) { |
| 5392 | /* 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] | 5393 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5394 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5395 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5396 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5397 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5398 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5399 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5400 | } |
| 5401 | } |
| 5402 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5403 | /* reload previous context's mod_def */ |
| 5404 | ctx->mod_def = mod_old; |
| 5405 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5406 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5407 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5408 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5409 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5410 | |
| 5411 | return ret; |
| 5412 | } |
| 5413 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5414 | static int |
| 5415 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5416 | { |
| 5417 | struct lysp_node *iter; |
| 5418 | int len = 0; |
| 5419 | |
| 5420 | *path = NULL; |
| 5421 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5422 | char *s = *path; |
| 5423 | char *id; |
| 5424 | |
| 5425 | switch (iter->nodetype) { |
| 5426 | case LYS_USES: |
| 5427 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5428 | break; |
| 5429 | case LYS_GROUPING: |
| 5430 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5431 | break; |
| 5432 | case LYS_AUGMENT: |
| 5433 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5434 | break; |
| 5435 | default: |
| 5436 | id = strdup(iter->name); |
| 5437 | break; |
| 5438 | } |
| 5439 | |
| 5440 | if (!iter->parent) { |
| 5441 | /* print prefix */ |
| 5442 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5443 | } else { |
| 5444 | /* prefix is the same as in parent */ |
| 5445 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5446 | } |
| 5447 | free(s); |
| 5448 | free(id); |
| 5449 | } |
| 5450 | |
| 5451 | if (len < 0) { |
| 5452 | free(*path); |
| 5453 | *path = NULL; |
| 5454 | } else if (len == 0) { |
| 5455 | *path = strdup("/"); |
| 5456 | len = 1; |
| 5457 | } |
| 5458 | return len; |
| 5459 | } |
| 5460 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5461 | /** |
| 5462 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5463 | * |
| 5464 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5465 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5466 | */ |
| 5467 | static LY_ERR |
| 5468 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5469 | { |
| 5470 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5471 | char *path; |
| 5472 | int len; |
| 5473 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5474 | struct lysp_node_uses fake_uses = { |
| 5475 | .parent = node_p, |
| 5476 | .nodetype = LYS_USES, |
| 5477 | .flags = 0, .next = NULL, |
| 5478 | .name = grp->name, |
| 5479 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5480 | .refines = NULL, .augments = NULL |
| 5481 | }; |
| 5482 | struct lysc_node_container fake_container = { |
| 5483 | .nodetype = LYS_CONTAINER, |
| 5484 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5485 | .module = ctx->mod, |
| 5486 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5487 | .prev = (struct lysc_node*)&fake_container, |
| 5488 | .name = "fake", |
| 5489 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5490 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5491 | }; |
| 5492 | |
| 5493 | if (grp->parent) { |
| 5494 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5495 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5496 | |
| 5497 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5498 | if (len < 0) { |
| 5499 | LOGMEM(ctx->ctx); |
| 5500 | return LY_EMEM; |
| 5501 | } |
| 5502 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5503 | ctx->path_len = (uint16_t)len; |
| 5504 | free(path); |
| 5505 | |
| 5506 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5507 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5508 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5509 | lysc_update_path(ctx, NULL, NULL); |
| 5510 | lysc_update_path(ctx, NULL, NULL); |
| 5511 | |
| 5512 | ctx->path_len = 1; |
| 5513 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5514 | |
| 5515 | /* cleanup */ |
| 5516 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5517 | |
| 5518 | return ret; |
| 5519 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5520 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5521 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5522 | * @brief Compile parsed schema node information. |
| 5523 | * @param[in] ctx Compile context |
| 5524 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5525 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5526 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5527 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5528 | * @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). |
| 5529 | * 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] | 5530 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5531 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5532 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5533 | 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] | 5534 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5535 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5536 | struct lysc_node *node; |
| 5537 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5538 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5539 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5540 | 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] | 5541 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5542 | if (node_p->nodetype != LYS_USES) { |
| 5543 | lysc_update_path(ctx, parent, node_p->name); |
| 5544 | } else { |
| 5545 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5546 | lysc_update_path(ctx, NULL, node_p->name); |
| 5547 | } |
| 5548 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5549 | switch (node_p->nodetype) { |
| 5550 | case LYS_CONTAINER: |
| 5551 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5552 | node_compile_spec = lys_compile_node_container; |
| 5553 | break; |
| 5554 | case LYS_LEAF: |
| 5555 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5556 | node_compile_spec = lys_compile_node_leaf; |
| 5557 | break; |
| 5558 | case LYS_LIST: |
| 5559 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5560 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5561 | break; |
| 5562 | case LYS_LEAFLIST: |
| 5563 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5564 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5565 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5566 | case LYS_CHOICE: |
| 5567 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5568 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5569 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5570 | case LYS_ANYXML: |
| 5571 | case LYS_ANYDATA: |
| 5572 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5573 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5574 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5575 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5576 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5577 | lysc_update_path(ctx, NULL, NULL); |
| 5578 | lysc_update_path(ctx, NULL, NULL); |
| 5579 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5580 | default: |
| 5581 | LOGINT(ctx->ctx); |
| 5582 | return LY_EINT; |
| 5583 | } |
| 5584 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5585 | node->nodetype = node_p->nodetype; |
| 5586 | node->module = ctx->mod; |
| 5587 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5588 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5589 | |
| 5590 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5591 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5592 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5593 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5594 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5595 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5596 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5597 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5598 | node->flags |= LYS_CONFIG_R; |
| 5599 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5600 | /* config not explicitely set, inherit it from parent */ |
| 5601 | if (parent) { |
| 5602 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5603 | } else { |
| 5604 | /* default is config true */ |
| 5605 | node->flags |= LYS_CONFIG_W; |
| 5606 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5607 | } else { |
| 5608 | /* config set explicitely */ |
| 5609 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5610 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5611 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5612 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5613 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5614 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5615 | goto error; |
| 5616 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5617 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5618 | /* *list ordering */ |
| 5619 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5620 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5621 | 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] | 5622 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5623 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5624 | node->flags &= ~LYS_ORDBY_MASK; |
| 5625 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5626 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5627 | /* default ordering is system */ |
| 5628 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5629 | } |
| 5630 | } |
| 5631 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5632 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5633 | * 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] | 5634 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5635 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5636 | * 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] | 5637 | 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] | 5638 | } |
| 5639 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5640 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5641 | node->sp = node_p; |
| 5642 | } |
| 5643 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5644 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5645 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5646 | if (node_p->when) { |
| 5647 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5648 | 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] | 5649 | |
| 5650 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5651 | /* do not check "when" semantics in a grouping */ |
| 5652 | ly_set_add(&ctx->unres, node, 0); |
| 5653 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5654 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5655 | 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] | 5656 | |
| 5657 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5658 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5659 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5660 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5661 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5662 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5663 | if (node->flags & LYS_MAND_TRUE) { |
| 5664 | lys_compile_mandatory_parents(parent, 1); |
| 5665 | } |
| 5666 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5667 | lysc_update_path(ctx, NULL, NULL); |
| 5668 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5669 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5670 | if (parent) { |
| 5671 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5672 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5673 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5674 | } else { |
| 5675 | lysc_update_path(ctx, parent, node->name); |
| 5676 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5677 | 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] | 5678 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5679 | if (uses_status) { |
| 5680 | |
| 5681 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5682 | /* 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] | 5683 | * it directly gets the same status flags as the choice; |
| 5684 | * 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] | 5685 | 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] | 5686 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5687 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5688 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5689 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5690 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5691 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5692 | 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] | 5693 | |
| 5694 | if (parent->nodetype == LYS_CHOICE) { |
| 5695 | lysc_update_path(ctx, NULL, NULL); |
| 5696 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5697 | } else { |
| 5698 | /* top-level element */ |
| 5699 | if (!ctx->mod->compiled->data) { |
| 5700 | ctx->mod->compiled->data = node; |
| 5701 | } else { |
| 5702 | /* insert at the end of the module's top-level nodes list */ |
| 5703 | ctx->mod->compiled->data->prev->next = node; |
| 5704 | node->prev = ctx->mod->compiled->data->prev; |
| 5705 | ctx->mod->compiled->data->prev = node; |
| 5706 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5707 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5708 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5709 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5710 | return LY_EVALID; |
| 5711 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5712 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5713 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5714 | |
| 5715 | return LY_SUCCESS; |
| 5716 | |
| 5717 | error: |
| 5718 | lysc_node_free(ctx->ctx, node); |
| 5719 | return ret; |
| 5720 | } |
| 5721 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5722 | static void |
| 5723 | lysc_disconnect(struct lysc_node *node) |
| 5724 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5725 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5726 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5727 | int remove_cs = 0; |
| 5728 | |
| 5729 | parent = node->parent; |
| 5730 | |
| 5731 | /* parent's first child */ |
| 5732 | if (!parent) { |
| 5733 | return; |
| 5734 | } |
| 5735 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5736 | cs = (struct lysc_node_case*)node; |
| 5737 | } else if (parent->nodetype == LYS_CASE) { |
| 5738 | /* disconnecting some node in a case */ |
| 5739 | cs = (struct lysc_node_case*)parent; |
| 5740 | parent = cs->parent; |
| 5741 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5742 | if (child == node) { |
| 5743 | if (cs->child == child) { |
| 5744 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5745 | /* case with a single child -> remove also the case */ |
| 5746 | child->parent = NULL; |
| 5747 | remove_cs = 1; |
| 5748 | } else { |
| 5749 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5750 | } |
| 5751 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5752 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5753 | } |
| 5754 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5755 | if (!remove_cs) { |
| 5756 | cs = NULL; |
| 5757 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5758 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5759 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | if (cs) { |
| 5763 | if (remove_cs) { |
| 5764 | /* cs has only one child which is being also removed */ |
| 5765 | lysc_disconnect((struct lysc_node*)cs); |
| 5766 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5767 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5768 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5769 | /* default case removed */ |
| 5770 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5771 | } |
| 5772 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5773 | /* first case removed */ |
| 5774 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5775 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5776 | if (cs->child) { |
| 5777 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5778 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5779 | prev = cs->child->prev; |
| 5780 | } /* else all the children are under a single case */ |
| 5781 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5782 | if (child->parent != (struct lysc_node*)cs) { |
| 5783 | break; |
| 5784 | } |
| 5785 | lysc_node_free(node->module->ctx, child); |
| 5786 | } |
| 5787 | if (prev) { |
| 5788 | if (prev->next) { |
| 5789 | prev->next = child; |
| 5790 | } |
| 5791 | if (child) { |
| 5792 | child->prev = prev; |
| 5793 | } else { |
| 5794 | /* link from the first child under the cases */ |
| 5795 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5796 | } |
| 5797 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5798 | } |
| 5799 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5800 | } |
| 5801 | |
| 5802 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5803 | if (node->prev->next) { |
| 5804 | node->prev->next = node->next; |
| 5805 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5806 | if (node->next) { |
| 5807 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5808 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5809 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5810 | if (child) { |
| 5811 | child->prev = node->prev; |
| 5812 | } |
| 5813 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5814 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5815 | } |
| 5816 | } |
| 5817 | |
| 5818 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5819 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5820 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5821 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5822 | struct ly_set devs_p = {0}; |
| 5823 | struct ly_set targets = {0}; |
| 5824 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5825 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5826 | struct lysc_action *rpcs; |
| 5827 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5828 | struct lysp_deviation *dev; |
| 5829 | struct lysp_deviate *d, **dp_new; |
| 5830 | struct lysp_deviate_add *d_add; |
| 5831 | struct lysp_deviate_del *d_del; |
| 5832 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5833 | LY_ARRAY_SIZE_TYPE u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5834 | struct lysc_deviation { |
| 5835 | const char *nodeid; |
| 5836 | struct lysc_node *target; /* target node of the deviation */ |
| 5837 | 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] | 5838 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5839 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5840 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5841 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5842 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5843 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5844 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5845 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5846 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5847 | |
| 5848 | /* get all deviations from the module and all its submodules ... */ |
| 5849 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5850 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5851 | } |
| 5852 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5853 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5854 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5855 | } |
| 5856 | } |
| 5857 | if (!devs_p.count) { |
| 5858 | /* nothing to do */ |
| 5859 | return LY_SUCCESS; |
| 5860 | } |
| 5861 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5862 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5863 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5864 | /* ... and group them by the target node */ |
| 5865 | devs = calloc(devs_p.count, sizeof *devs); |
| 5866 | for (u = 0; u < devs_p.count; ++u) { |
| 5867 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5868 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5869 | |
| 5870 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5871 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5872 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5873 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5874 | /* move the target pointer to input/output to make them different from the action and |
| 5875 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5876 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5877 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5878 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5879 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5880 | flags |= LYSC_OPT_INTERNAL; |
| 5881 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5882 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5883 | flags |= LYSC_OPT_INTERNAL; |
| 5884 | } |
| 5885 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5886 | /* insert into the set of targets with duplicity detection */ |
| 5887 | i = ly_set_add(&targets, target, 0); |
| 5888 | if (!devs[i]) { |
| 5889 | /* new record */ |
| 5890 | devs[i] = calloc(1, sizeof **devs); |
| 5891 | devs[i]->target = target; |
| 5892 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5893 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5894 | } |
| 5895 | /* add deviates into the deviation's list of deviates */ |
| 5896 | for (d = dev->deviates; d; d = d->next) { |
| 5897 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5898 | *dp_new = d; |
| 5899 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5900 | devs[i]->not_supported = 1; |
| 5901 | } |
| 5902 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5903 | |
| 5904 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5905 | } |
| 5906 | |
| 5907 | /* MACROS for deviates checking */ |
| 5908 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5909 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5910 | 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] | 5911 | goto cleanup; \ |
| 5912 | } |
| 5913 | |
| 5914 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5915 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5916 | 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] | 5917 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5918 | goto cleanup; \ |
| 5919 | } |
| 5920 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5921 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5922 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5923 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5924 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5925 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5926 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5927 | goto cleanup; \ |
| 5928 | } |
| 5929 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5930 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5931 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5932 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5933 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5934 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5935 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5936 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5937 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5938 | goto cleanup; \ |
| 5939 | } |
| 5940 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5941 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5942 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5943 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5944 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5945 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5946 | goto cleanup; \ |
| 5947 | } |
| 5948 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5949 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5950 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5951 | 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] | 5952 | goto cleanup; \ |
| 5953 | } |
| 5954 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5955 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5956 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5957 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5958 | "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] | 5959 | goto cleanup; \ |
| 5960 | } |
| 5961 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5962 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5963 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5964 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5965 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5966 | 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] | 5967 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5968 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5969 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5970 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5971 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5972 | goto cleanup; \ |
| 5973 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5974 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5975 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5976 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5977 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5978 | (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] | 5979 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5980 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5981 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5982 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5983 | } |
| 5984 | |
| 5985 | /* apply deviations */ |
| 5986 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5987 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5988 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5989 | struct ly_err_item *err = NULL; |
| 5990 | |
| 5991 | dflt = NULL; |
| 5992 | changed_type = 0; |
| 5993 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5994 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5995 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5996 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5997 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5998 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5999 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 6000 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6001 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 6002 | } |
| 6003 | } |
| 6004 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6005 | /* not-supported */ |
| 6006 | if (devs[u]->not_supported) { |
| 6007 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6008 | 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] | 6009 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 6010 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6011 | |
| 6012 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6013 | if (devs[u]->target->parent) { \ |
| 6014 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 6015 | } else { \ |
| 6016 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 6017 | } \ |
| 6018 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6019 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 6020 | } \ |
| 6021 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 6022 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6023 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6024 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6025 | } |
| 6026 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6027 | if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6028 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6029 | /* remove RPC's/action's input */ |
| 6030 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 6031 | 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] | 6032 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 6033 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6034 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6035 | /* remove RPC's/action's output */ |
| 6036 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 6037 | 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] | 6038 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 6039 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6040 | } else { |
| 6041 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6042 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6043 | } |
| 6044 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6045 | /* remove Notification */ |
| 6046 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6047 | } else { |
| 6048 | /* remove the target node */ |
| 6049 | lysc_disconnect(devs[u]->target); |
| 6050 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 6051 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6052 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6053 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6054 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6055 | continue; |
| 6056 | } |
| 6057 | |
| 6058 | /* list of deviates (not-supported is not present in the list) */ |
| 6059 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 6060 | d = devs[u]->deviates[v]; |
| 6061 | |
| 6062 | switch (d->mod) { |
| 6063 | case LYS_DEV_ADD: |
| 6064 | d_add = (struct lysp_deviate_add*)d; |
| 6065 | /* [units-stmt] */ |
| 6066 | if (d_add->units) { |
| 6067 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 6068 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 6069 | |
| 6070 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6071 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6072 | } |
| 6073 | |
| 6074 | /* *must-stmt */ |
| 6075 | if (d_add->musts) { |
| 6076 | switch (devs[u]->target->nodetype) { |
| 6077 | case LYS_CONTAINER: |
| 6078 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6079 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 6080 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6081 | break; |
| 6082 | case LYS_LEAF: |
| 6083 | case LYS_LEAFLIST: |
| 6084 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6085 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 6086 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6087 | break; |
| 6088 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6089 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 6090 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6091 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6092 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6093 | case LYS_ACTION: |
| 6094 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6095 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 6096 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6097 | break; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 6098 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6099 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 6100 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6101 | break; |
| 6102 | } |
| 6103 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6104 | default: |
| 6105 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6106 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6107 | goto cleanup; |
| 6108 | } |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6109 | ly_set_add(&ctx->unres, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6110 | } |
| 6111 | |
| 6112 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6113 | if (d_add->uniques) { |
| 6114 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 6115 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 6116 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6117 | |
| 6118 | /* *default-stmt */ |
| 6119 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6120 | switch (devs[u]->target->nodetype) { |
| 6121 | case LYS_LEAF: |
| 6122 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6123 | 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] | 6124 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6125 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6126 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6127 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6128 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6129 | } else { |
| 6130 | /* prepare new default value storage */ |
| 6131 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6132 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6133 | dflt = d_add->dflts[0]; |
| 6134 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 6135 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6136 | /* mark the new default values as leaf's own */ |
| 6137 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6138 | break; |
| 6139 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6140 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6141 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6142 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6143 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6144 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6145 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6146 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6147 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6148 | LY_ARRAY_FREE(llist->dflts); |
| 6149 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6150 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6151 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6152 | } |
| 6153 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6154 | 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] | 6155 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 6156 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6157 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6158 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 6159 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6160 | LY_ARRAY_INCREMENT(llist->dflts); |
| 6161 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 6162 | llist->dflts[x]->realtype = llist->type; |
| 6163 | 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] | 6164 | 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] | 6165 | (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] | 6166 | llist->dflts[x]->realtype->refcount++; |
| 6167 | if (err) { |
| 6168 | ly_err_print(err); |
| 6169 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6170 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 6171 | d_add->dflts[x - y], err->msg); |
| 6172 | ly_err_free(err); |
| 6173 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6174 | if (rc == LY_EINCOMPLETE) { |
| 6175 | /* postpone default compilation when the tree is complete */ |
| 6176 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6177 | |
| 6178 | /* but in general result is so far ok */ |
| 6179 | rc = LY_SUCCESS; |
| 6180 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6181 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6182 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6183 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6184 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 6185 | break; |
| 6186 | case LYS_CHOICE: |
| 6187 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 6188 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 6189 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 6190 | * 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] | 6191 | 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] | 6192 | goto cleanup; |
| 6193 | } |
| 6194 | break; |
| 6195 | default: |
| 6196 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6197 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6198 | goto cleanup; |
| 6199 | } |
| 6200 | } |
| 6201 | |
| 6202 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6203 | if (d_add->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6204 | 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] | 6205 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6206 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 6207 | goto cleanup; |
| 6208 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6209 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6210 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 6211 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6212 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6213 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 6214 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6215 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 6216 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6217 | goto cleanup; |
| 6218 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6219 | 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] | 6220 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6221 | |
| 6222 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6223 | if (d_add->flags & LYS_MAND_MASK) { |
| 6224 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 6225 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6226 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 6227 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6228 | goto cleanup; |
| 6229 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6230 | 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] | 6231 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6232 | |
| 6233 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6234 | if (d_add->flags & LYS_SET_MIN) { |
| 6235 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6236 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6237 | /* change value */ |
| 6238 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 6239 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6240 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6241 | /* change value */ |
| 6242 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 6243 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6244 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6245 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 6246 | goto cleanup; |
| 6247 | } |
| 6248 | if (d_add->min) { |
| 6249 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6250 | } |
| 6251 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6252 | |
| 6253 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6254 | if (d_add->flags & LYS_SET_MAX) { |
| 6255 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6256 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6257 | /* change value */ |
| 6258 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6259 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6260 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6261 | /* change value */ |
| 6262 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6263 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6264 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6265 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6266 | goto cleanup; |
| 6267 | } |
| 6268 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6269 | |
| 6270 | break; |
| 6271 | case LYS_DEV_DELETE: |
| 6272 | d_del = (struct lysp_deviate_del*)d; |
| 6273 | |
| 6274 | /* [units-stmt] */ |
| 6275 | if (d_del->units) { |
| 6276 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6277 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6278 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6279 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6280 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6281 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6282 | goto cleanup; |
| 6283 | } |
| 6284 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6285 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6286 | } |
| 6287 | |
| 6288 | /* *must-stmt */ |
| 6289 | if (d_del->musts) { |
| 6290 | switch (devs[u]->target->nodetype) { |
| 6291 | case LYS_CONTAINER: |
| 6292 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6293 | 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] | 6294 | break; |
| 6295 | case LYS_LEAF: |
| 6296 | case LYS_LEAFLIST: |
| 6297 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6298 | 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] | 6299 | break; |
| 6300 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6301 | 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] | 6302 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6303 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6304 | case LYS_ACTION: |
| 6305 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6306 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6307 | break; |
| 6308 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6309 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6310 | break; |
| 6311 | } |
| 6312 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6313 | default: |
| 6314 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6315 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6316 | goto cleanup; |
| 6317 | } |
| 6318 | } |
| 6319 | |
| 6320 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6321 | if (d_del->uniques) { |
| 6322 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6323 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6324 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6325 | LY_ARRAY_FOR(list->uniques, z) { |
| 6326 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6327 | nodeid = strpbrk(name, " \t\n"); |
| 6328 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 6329 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6330 | break; |
| 6331 | } |
| 6332 | while (isspace(*nodeid)) { |
| 6333 | ++nodeid; |
| 6334 | } |
| 6335 | } else { |
| 6336 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6337 | break; |
| 6338 | } |
| 6339 | } |
| 6340 | } |
| 6341 | if (!name) { |
| 6342 | /* complete match - remove the unique */ |
| 6343 | LY_ARRAY_DECREMENT(list->uniques); |
| 6344 | LY_ARRAY_FREE(list->uniques[z]); |
| 6345 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6346 | --z; |
| 6347 | break; |
| 6348 | } |
| 6349 | } |
| 6350 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6351 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6352 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6353 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6354 | goto cleanup; |
| 6355 | } |
| 6356 | } |
| 6357 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6358 | LY_ARRAY_FREE(list->uniques); |
| 6359 | list->uniques = NULL; |
| 6360 | } |
| 6361 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6362 | |
| 6363 | /* *default-stmt */ |
| 6364 | if (d_del->dflts) { |
| 6365 | switch (devs[u]->target->nodetype) { |
| 6366 | case LYS_LEAF: |
| 6367 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6368 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6369 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6370 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6371 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6372 | 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] | 6373 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6374 | if (i) { |
| 6375 | free((char*)dflt); |
| 6376 | } |
| 6377 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6378 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6379 | d_del->dflts[0], dflt); |
| 6380 | goto cleanup; |
| 6381 | } |
| 6382 | if (i) { |
| 6383 | free((char*)dflt); |
| 6384 | } |
| 6385 | dflt = NULL; |
| 6386 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6387 | /* update the list of incomplete default values if needed */ |
| 6388 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6389 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6390 | /* remove the default specification */ |
| 6391 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6392 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6393 | free(leaf->dflt); |
| 6394 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6395 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6396 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6397 | break; |
| 6398 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6399 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6400 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6401 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6402 | 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] | 6403 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6404 | if (i) { |
| 6405 | free((char*)dflt); |
| 6406 | } |
| 6407 | dflt = NULL; |
| 6408 | break; |
| 6409 | } |
| 6410 | if (i) { |
| 6411 | free((char*)dflt); |
| 6412 | } |
| 6413 | dflt = NULL; |
| 6414 | } |
| 6415 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6416 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6417 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6418 | goto cleanup; |
| 6419 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6420 | |
| 6421 | /* update the list of incomplete default values if needed */ |
| 6422 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6423 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6424 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6425 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6426 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6427 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6428 | free(llist->dflts[y]); |
| 6429 | 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] | 6430 | 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] | 6431 | } |
| 6432 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6433 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6434 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6435 | LY_ARRAY_FREE(llist->dflts); |
| 6436 | llist->dflts = NULL; |
| 6437 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6438 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6439 | break; |
| 6440 | case LYS_CHOICE: |
| 6441 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6442 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6443 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6444 | 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] | 6445 | if (prefix) { |
| 6446 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6447 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6448 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6449 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6450 | "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] | 6451 | goto cleanup; |
| 6452 | } |
| 6453 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6454 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6455 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6456 | "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] | 6457 | goto cleanup; |
| 6458 | } |
| 6459 | } |
| 6460 | /* else { |
| 6461 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6462 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6463 | * the module of the default case, just matching its name */ |
| 6464 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6465 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6466 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6467 | 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] | 6468 | goto cleanup; |
| 6469 | } |
| 6470 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6471 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6472 | break; |
| 6473 | default: |
| 6474 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6475 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6476 | goto cleanup; |
| 6477 | } |
| 6478 | } |
| 6479 | |
| 6480 | break; |
| 6481 | case LYS_DEV_REPLACE: |
| 6482 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6483 | |
| 6484 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6485 | if (d_rpl->type) { |
| 6486 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6487 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6488 | 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] | 6489 | |
| 6490 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6491 | /* the target has default from the previous type - remove it */ |
| 6492 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6493 | /* update the list of incomplete default values if needed */ |
| 6494 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6495 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6496 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6497 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6498 | free(leaf->dflt); |
| 6499 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6500 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6501 | } else { /* LYS_LEAFLIST */ |
| 6502 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6503 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6504 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6505 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6506 | free(llist->dflts[x]); |
| 6507 | } |
| 6508 | LY_ARRAY_FREE(llist->dflts); |
| 6509 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6510 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6511 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6512 | } |
| 6513 | } |
| 6514 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6515 | /* there is no default value, do not set changed_type after type compilation |
| 6516 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6517 | changed_type = -1; |
| 6518 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6519 | 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] | 6520 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6521 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6522 | |
| 6523 | /* [units-stmt] */ |
| 6524 | if (d_rpl->units) { |
| 6525 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6526 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6527 | units, "replacing", "units", d_rpl->units); |
| 6528 | |
| 6529 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6530 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6531 | } |
| 6532 | |
| 6533 | /* [default-stmt] */ |
| 6534 | if (d_rpl->dflt) { |
| 6535 | switch (devs[u]->target->nodetype) { |
| 6536 | case LYS_LEAF: |
| 6537 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6538 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6539 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6540 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6541 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6542 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6543 | dflt = d_rpl->dflt; |
| 6544 | /* 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] | 6545 | break; |
| 6546 | case LYS_CHOICE: |
| 6547 | 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] | 6548 | 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] | 6549 | goto cleanup; |
| 6550 | } |
| 6551 | break; |
| 6552 | default: |
| 6553 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6554 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6555 | goto cleanup; |
| 6556 | } |
| 6557 | } |
| 6558 | |
| 6559 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6560 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6561 | 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] | 6562 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6563 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6564 | goto cleanup; |
| 6565 | } |
| 6566 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6567 | 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] | 6568 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6569 | goto cleanup; |
| 6570 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6571 | 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] | 6572 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6573 | |
| 6574 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6575 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6576 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6577 | 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] | 6578 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6579 | goto cleanup; |
| 6580 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6581 | 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] | 6582 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6583 | |
| 6584 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6585 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6586 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6587 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6588 | /* change value */ |
| 6589 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6590 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6591 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6592 | /* change value */ |
| 6593 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6594 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6595 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6596 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6597 | goto cleanup; |
| 6598 | } |
| 6599 | if (d_rpl->min) { |
| 6600 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6601 | } |
| 6602 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6603 | |
| 6604 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6605 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6606 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6607 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6608 | /* change value */ |
| 6609 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6610 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6611 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6612 | /* change value */ |
| 6613 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6614 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6615 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6616 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6617 | goto cleanup; |
| 6618 | } |
| 6619 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6620 | |
| 6621 | break; |
| 6622 | default: |
| 6623 | LOGINT(ctx->ctx); |
| 6624 | goto cleanup; |
| 6625 | } |
| 6626 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6627 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6628 | /* final check when all deviations of a single target node are applied */ |
| 6629 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6630 | /* check min-max compatibility */ |
| 6631 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6632 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6633 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6634 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6635 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6636 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6637 | } else { |
| 6638 | min = max = 0; |
| 6639 | } |
| 6640 | if (min > max) { |
| 6641 | 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] | 6642 | "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] | 6643 | goto cleanup; |
| 6644 | } |
| 6645 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6646 | if (dflt) { |
| 6647 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6648 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6649 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6650 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6651 | 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] | 6652 | 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] | 6653 | leaf->dflt->realtype->refcount++; |
| 6654 | if (err) { |
| 6655 | ly_err_print(err); |
| 6656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6657 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6658 | ly_err_free(err); |
| 6659 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6660 | if (rc == LY_EINCOMPLETE) { |
| 6661 | /* postpone default compilation when the tree is complete */ |
| 6662 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6663 | |
| 6664 | /* but in general result is so far ok */ |
| 6665 | rc = LY_SUCCESS; |
| 6666 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6667 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6668 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6669 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6670 | int dynamic; |
| 6671 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6672 | 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] | 6673 | |
| 6674 | /* update the list of incomplete default values if needed */ |
| 6675 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6676 | |
| 6677 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6678 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6679 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6680 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6681 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6682 | 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] | 6683 | 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] | 6684 | leaf->dflt->realtype->refcount++; |
| 6685 | if (err) { |
| 6686 | ly_err_print(err); |
| 6687 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6688 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6689 | ly_err_free(err); |
| 6690 | } |
| 6691 | if (dynamic) { |
| 6692 | free((void*)dflt); |
| 6693 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6694 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6695 | if (rc == LY_EINCOMPLETE) { |
| 6696 | /* postpone default compilation when the tree is complete */ |
| 6697 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6698 | |
| 6699 | /* but in general result is so far ok */ |
| 6700 | rc = LY_SUCCESS; |
| 6701 | } |
| 6702 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6703 | } else { /* LYS_LEAFLIST */ |
| 6704 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6705 | 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] | 6706 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6707 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6708 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6709 | rc = llist->type->plugin->store(ctx->ctx, llist->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*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6712 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6713 | llist->dflts[x]->realtype->refcount++; |
| 6714 | if (err) { |
| 6715 | ly_err_print(err); |
| 6716 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6717 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6718 | dflt, err->msg); |
| 6719 | ly_err_free(err); |
| 6720 | } |
| 6721 | if (dynamic) { |
| 6722 | free((void*)dflt); |
| 6723 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6724 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6725 | if (rc == LY_EINCOMPLETE) { |
| 6726 | /* postpone default compilation when the tree is complete */ |
| 6727 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6728 | |
| 6729 | /* but in general result is so far ok */ |
| 6730 | rc = LY_SUCCESS; |
| 6731 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6732 | LY_CHECK_GOTO(rc, cleanup); |
| 6733 | } |
| 6734 | } |
| 6735 | } |
| 6736 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6737 | /* check mandatory - default compatibility */ |
| 6738 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6739 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6740 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6742 | "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] | 6743 | goto cleanup; |
| 6744 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6745 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6746 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6747 | 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] | 6748 | goto cleanup; |
| 6749 | } |
| 6750 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6751 | /* mandatory node under a default case */ |
| 6752 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6753 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6754 | 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] | 6755 | goto cleanup; |
| 6756 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6757 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6758 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6759 | } |
| 6760 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6761 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6762 | ret = LY_SUCCESS; |
| 6763 | |
| 6764 | cleanup: |
| 6765 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6766 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6767 | free(devs[u]); |
| 6768 | } |
| 6769 | free(devs); |
| 6770 | ly_set_erase(&targets, NULL); |
| 6771 | ly_set_erase(&devs_p, NULL); |
| 6772 | |
| 6773 | return ret; |
| 6774 | } |
| 6775 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6776 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6777 | * @brief Compile the given YANG submodule into the main module. |
| 6778 | * @param[in] ctx Compile context |
| 6779 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6780 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6781 | */ |
| 6782 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6783 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6784 | { |
| 6785 | unsigned int u; |
| 6786 | LY_ERR ret = LY_SUCCESS; |
| 6787 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6788 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6789 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6790 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6791 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6792 | if (!mainmod->mod->off_features) { |
| 6793 | /* features are compiled directly into the compiled module structure, |
| 6794 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6795 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6796 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6797 | LY_CHECK_GOTO(ret, error); |
| 6798 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6799 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6800 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6801 | 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] | 6802 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6803 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6804 | /* data nodes */ |
| 6805 | LY_LIST_FOR(submod->data, node_p) { |
| 6806 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6807 | LY_CHECK_GOTO(ret, error); |
| 6808 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6809 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6810 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6811 | 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] | 6812 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6813 | error: |
| 6814 | return ret; |
| 6815 | } |
| 6816 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6817 | static void * |
| 6818 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6819 | { |
| 6820 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6821 | if (substmts[u].stmt == stmt) { |
| 6822 | return substmts[u].storage; |
| 6823 | } |
| 6824 | } |
| 6825 | return NULL; |
| 6826 | } |
| 6827 | |
| 6828 | LY_ERR |
| 6829 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6830 | { |
| 6831 | LY_ERR ret = LY_EVALID, r; |
| 6832 | unsigned int u; |
| 6833 | struct lysp_stmt *stmt; |
| 6834 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6835 | |
| 6836 | /* check for invalid substatements */ |
| 6837 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6838 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6839 | continue; |
| 6840 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6841 | for (u = 0; substmts[u].stmt; ++u) { |
| 6842 | if (substmts[u].stmt == stmt->kw) { |
| 6843 | break; |
| 6844 | } |
| 6845 | } |
| 6846 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6847 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6848 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6849 | goto cleanup; |
| 6850 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6851 | } |
| 6852 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6853 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6854 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6855 | /* keep order of the processing the same as the order in the defined substmts, |
| 6856 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6857 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6858 | int stmt_present = 0; |
| 6859 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6860 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6861 | if (substmts[u].stmt != stmt->kw) { |
| 6862 | continue; |
| 6863 | } |
| 6864 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6865 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6866 | if (substmts[u].storage) { |
| 6867 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6868 | case LY_STMT_STATUS: |
| 6869 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6870 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6871 | break; |
| 6872 | case LY_STMT_UNITS: { |
| 6873 | const char **units; |
| 6874 | |
| 6875 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6876 | /* single item */ |
| 6877 | if (*((const char **)substmts[u].storage)) { |
| 6878 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6879 | goto cleanup; |
| 6880 | } |
| 6881 | units = (const char **)substmts[u].storage; |
| 6882 | } else { |
| 6883 | /* sized array */ |
| 6884 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6885 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6886 | } |
| 6887 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6888 | break; |
| 6889 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6890 | case LY_STMT_TYPE: { |
| 6891 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6892 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6893 | |
| 6894 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6895 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6896 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6897 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6898 | goto cleanup; |
| 6899 | } |
| 6900 | compiled = substmts[u].storage; |
| 6901 | } else { |
| 6902 | /* sized array */ |
| 6903 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6904 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6905 | compiled = (void*)type; |
| 6906 | } |
| 6907 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6908 | 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] | 6909 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6910 | 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] | 6911 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6912 | lysp_type_free(ctx->ctx, parsed); |
| 6913 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6914 | break; |
| 6915 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6916 | case LY_STMT_IF_FEATURE: { |
| 6917 | struct lysc_iffeature *iff = NULL; |
| 6918 | |
| 6919 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6920 | /* single item */ |
| 6921 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6922 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6923 | goto cleanup; |
| 6924 | } |
| 6925 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6926 | } else { |
| 6927 | /* sized array */ |
| 6928 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6929 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6930 | } |
| 6931 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6932 | break; |
| 6933 | } |
| 6934 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6935 | * 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] | 6936 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6937 | 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.", |
| 6938 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6939 | goto cleanup; |
| 6940 | } |
| 6941 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6942 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6943 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6944 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6945 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6946 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6947 | goto cleanup; |
| 6948 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6949 | } |
| 6950 | |
| 6951 | ret = LY_SUCCESS; |
| 6952 | |
| 6953 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6954 | return ret; |
| 6955 | } |
| 6956 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6957 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6958 | * @brief Check when for cyclic dependencies. |
| 6959 | * @param[in] set Set with all the referenced nodes. |
| 6960 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6961 | * @return LY_ERR value |
| 6962 | */ |
| 6963 | static LY_ERR |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6964 | 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] | 6965 | { |
| 6966 | struct lyxp_set tmp_set; |
| 6967 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6968 | uint32_t i, j; |
| 6969 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6970 | int idx; |
| 6971 | struct lysc_when *when; |
| 6972 | LY_ERR ret = LY_SUCCESS; |
| 6973 | |
| 6974 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6975 | |
| 6976 | /* prepare in_ctx of the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6977 | for ( i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6978 | xp_scnode = &set->val.scnodes[i]; |
| 6979 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6980 | if (xp_scnode->in_ctx != -1) { |
| 6981 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6982 | xp_scnode->in_ctx = 1; |
| 6983 | } |
| 6984 | } |
| 6985 | |
| 6986 | for (i = 0; i < set->used; ++i) { |
| 6987 | xp_scnode = &set->val.scnodes[i]; |
| 6988 | if (xp_scnode->in_ctx != 1) { |
| 6989 | /* already checked */ |
| 6990 | continue; |
| 6991 | } |
| 6992 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6993 | 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] | 6994 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6995 | /* no when to check */ |
| 6996 | xp_scnode->in_ctx = 0; |
| 6997 | continue; |
| 6998 | } |
| 6999 | |
| 7000 | node = xp_scnode->scnode; |
| 7001 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7002 | LY_ARRAY_FOR(node->when, u) { |
| 7003 | when = node->when[u]; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 7004 | ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7005 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 7006 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7007 | 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] | 7008 | goto cleanup; |
| 7009 | } |
| 7010 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7011 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7012 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7013 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7014 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7015 | 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] | 7016 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7017 | 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] | 7018 | ret = LY_EVALID; |
| 7019 | goto cleanup; |
| 7020 | } |
| 7021 | |
| 7022 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7023 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7024 | } else { |
| 7025 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7026 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7027 | } |
| 7028 | } |
| 7029 | |
| 7030 | /* merge this set into the global when set */ |
| 7031 | lyxp_set_scnode_merge(set, &tmp_set); |
| 7032 | } |
| 7033 | |
| 7034 | /* check when of non-data parents as well */ |
| 7035 | node = node->parent; |
| 7036 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 7037 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7038 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 7039 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7040 | } |
| 7041 | |
| 7042 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7043 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7044 | return ret; |
| 7045 | } |
| 7046 | |
| 7047 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7048 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 7049 | * @param[in] ctx Compile context. |
| 7050 | * @param[in] node Node to check. |
| 7051 | * @return LY_ERR value |
| 7052 | */ |
| 7053 | static LY_ERR |
| 7054 | lys_compile_check_xpath(struct lysc_ctx *ctx, const struct lysc_node *node) |
| 7055 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7056 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7057 | uint32_t i; |
| 7058 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7059 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7060 | struct lysc_when **when = NULL; |
| 7061 | struct lysc_must *musts = NULL; |
| 7062 | LY_ERR ret = LY_SUCCESS; |
| 7063 | |
| 7064 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7065 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7066 | |
| 7067 | switch (node->nodetype) { |
| 7068 | case LYS_CONTAINER: |
| 7069 | when = ((struct lysc_node_container *)node)->when; |
| 7070 | musts = ((struct lysc_node_container *)node)->musts; |
| 7071 | break; |
| 7072 | case LYS_CHOICE: |
| 7073 | when = ((struct lysc_node_choice *)node)->when; |
| 7074 | break; |
| 7075 | case LYS_LEAF: |
| 7076 | when = ((struct lysc_node_leaf *)node)->when; |
| 7077 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 7078 | break; |
| 7079 | case LYS_LEAFLIST: |
| 7080 | when = ((struct lysc_node_leaflist *)node)->when; |
| 7081 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 7082 | break; |
| 7083 | case LYS_LIST: |
| 7084 | when = ((struct lysc_node_list *)node)->when; |
| 7085 | musts = ((struct lysc_node_list *)node)->musts; |
| 7086 | break; |
| 7087 | case LYS_ANYXML: |
| 7088 | case LYS_ANYDATA: |
| 7089 | when = ((struct lysc_node_anydata *)node)->when; |
| 7090 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 7091 | break; |
| 7092 | case LYS_CASE: |
| 7093 | when = ((struct lysc_node_case *)node)->when; |
| 7094 | break; |
| 7095 | case LYS_NOTIF: |
| 7096 | musts = ((struct lysc_notif *)node)->musts; |
| 7097 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7098 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7099 | case LYS_ACTION: |
| 7100 | /* first process input musts */ |
| 7101 | musts = ((struct lysc_action *)node)->input.musts; |
| 7102 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7103 | default: |
| 7104 | /* nothing to check */ |
| 7105 | break; |
| 7106 | } |
| 7107 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7108 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7109 | LY_ARRAY_FOR(when, u) { |
| 7110 | ret = lyxp_atomize(when[u]->cond, LYD_SCHEMA, when[u]->module, when[u]->context, |
| 7111 | 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] | 7112 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7113 | 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] | 7114 | goto cleanup; |
| 7115 | } |
| 7116 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7117 | ctx->path[0] = '\0'; |
| 7118 | 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] | 7119 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7120 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7121 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 7122 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7123 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7124 | /* 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] | 7125 | 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] | 7126 | schema->name); |
| 7127 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7128 | |
| 7129 | /* check dummy node accessing */ |
| 7130 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7131 | 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] | 7132 | ret = LY_EVALID; |
| 7133 | goto cleanup; |
| 7134 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7135 | } |
| 7136 | } |
| 7137 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7138 | /* check cyclic dependencies */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7139 | ret = lys_compile_check_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7140 | LY_CHECK_GOTO(ret, cleanup); |
| 7141 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7142 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7143 | } |
| 7144 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7145 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7146 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7147 | LY_ARRAY_FOR(musts, u) { |
| 7148 | 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] | 7149 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7150 | 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] | 7151 | goto cleanup; |
| 7152 | } |
| 7153 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7154 | ctx->path[0] = '\0'; |
| 7155 | 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] | 7156 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7157 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7158 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7159 | /* 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] | 7160 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 7161 | 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] | 7162 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7163 | } |
| 7164 | } |
| 7165 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7166 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7167 | } |
| 7168 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7169 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7170 | /* now check output musts */ |
| 7171 | input_done = 1; |
| 7172 | musts = ((struct lysc_action *)node)->output.musts; |
| 7173 | opts = LYXP_SCNODE_OUTPUT; |
| 7174 | goto check_musts; |
| 7175 | } |
| 7176 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7177 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7178 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7179 | return ret; |
| 7180 | } |
| 7181 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7182 | static LY_ERR |
| 7183 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7184 | { |
| 7185 | struct lysc_ext_instance *ext; |
| 7186 | struct lysp_ext_instance *ext_p = NULL; |
| 7187 | struct lysp_stmt *stmt; |
| 7188 | const struct lys_module *ext_mod; |
| 7189 | LY_ERR ret = LY_SUCCESS; |
| 7190 | |
| 7191 | /* create the parsed extension instance manually */ |
| 7192 | ext_p = calloc(1, sizeof *ext_p); |
| 7193 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 7194 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 7195 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 7196 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7197 | ext_p->insubstmt_index = 0; |
| 7198 | |
| 7199 | stmt = calloc(1, sizeof *ext_p->child); |
| 7200 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 7201 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 7202 | stmt->kw = LY_STMT_TYPE; |
| 7203 | ext_p->child = stmt; |
| 7204 | |
| 7205 | /* allocate new extension instance */ |
| 7206 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7207 | |
| 7208 | /* manually get extension definition module */ |
| 7209 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7210 | |
| 7211 | /* compile the extension instance */ |
| 7212 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7213 | |
| 7214 | cleanup: |
| 7215 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7216 | free(ext_p); |
| 7217 | return ret; |
| 7218 | } |
| 7219 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7220 | LY_ERR |
| 7221 | lys_compile(struct lys_module *mod, int options) |
| 7222 | { |
| 7223 | struct lysc_ctx ctx = {0}; |
| 7224 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7225 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7226 | struct lysp_module *sp; |
| 7227 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7228 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7229 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7230 | struct lys_module *m; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7231 | LY_ARRAY_SIZE_TYPE u, v; |
| 7232 | uint32_t i; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7233 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7234 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7235 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7236 | |
| 7237 | if (!mod->implemented) { |
| 7238 | /* just imported modules are not compiled */ |
| 7239 | return LY_SUCCESS; |
| 7240 | } |
| 7241 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7242 | sp = mod->parsed; |
| 7243 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7244 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7245 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7246 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7247 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7248 | ctx.path_len = 1; |
| 7249 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7250 | |
| 7251 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7252 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 7253 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7254 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7255 | 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] | 7256 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7257 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7258 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7259 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7260 | |
| 7261 | /* features */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7262 | if (mod->off_features) { |
| 7263 | /* there is already precompiled array of features */ |
| 7264 | mod_c->features = mod->off_features; |
| 7265 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7266 | } else { |
| 7267 | /* features are compiled directly into the compiled module structure, |
| 7268 | * 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] | 7269 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7270 | LY_CHECK_GOTO(ret, error); |
| 7271 | } |
| 7272 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7273 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7274 | * are present. */ |
| 7275 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7276 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7277 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7278 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7279 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7280 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7281 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7282 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7283 | 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] | 7284 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7285 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7286 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7287 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7288 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7289 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7290 | /* identities */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7291 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7292 | 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] | 7293 | if (sp->identities) { |
| 7294 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 7295 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7296 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7297 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7298 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7299 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7300 | 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] | 7301 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7302 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7303 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7304 | 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] | 7305 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7306 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7307 | 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] | 7308 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7309 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7310 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7311 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7312 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7313 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7314 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7315 | /* extension instances TODO cover extension instances from submodules */ |
| 7316 | 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] | 7317 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7318 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7319 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7320 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7321 | * 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] | 7322 | for (i = 0; i < ctx.unres.count; ++i) { |
| 7323 | if (((struct lysc_node*)ctx.unres.objs[i])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 7324 | type = ((struct lysc_node_leaf*)ctx.unres.objs[i])->type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7325 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7326 | /* validate the path */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7327 | 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] | 7328 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7329 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7330 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7331 | /* validate the path */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7332 | 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] | 7333 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v], NULL); |
| 7334 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7335 | } |
| 7336 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7337 | } |
| 7338 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7339 | |
| 7340 | /* check xpath */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7341 | 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] | 7342 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7343 | for (i = 0; i < ctx.unres.count; ++i) { |
| 7344 | if (((struct lysc_node*)ctx.unres.objs[i])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 7345 | type = ((struct lysc_node_leaf*)ctx.unres.objs[i])->type; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7346 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7347 | /* store pointer to the real type */ |
| 7348 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7349 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7350 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7351 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7352 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7353 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7354 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7355 | /* store pointer to the real type */ |
| 7356 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7357 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7358 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7359 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7360 | } |
| 7361 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7362 | } |
| 7363 | } |
| 7364 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7365 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7366 | /* finish incomplete default values compilation */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7367 | for (i = 0; i < ctx.dflts.count; ++i) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7368 | struct ly_err_item *err = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7369 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[i]; |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 7370 | 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] | 7371 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7372 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7373 | if (err) { |
| 7374 | ly_err_print(err); |
| 7375 | ctx.path[0] = '\0'; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7376 | 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] | 7377 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 7378 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7379 | ly_err_free(err); |
| 7380 | } |
| 7381 | LY_CHECK_GOTO(ret, error); |
| 7382 | } |
| 7383 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7384 | /* validate non-instantiated groupings from the parsed schema, |
| 7385 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7386 | ctx.options |= LYSC_OPT_GROUPING; |
| 7387 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7388 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7389 | 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] | 7390 | } |
| 7391 | } |
| 7392 | LY_LIST_FOR(sp->data, node_p) { |
| 7393 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7394 | LY_ARRAY_FOR(grps, u) { |
| 7395 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7396 | 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] | 7397 | } |
| 7398 | } |
| 7399 | } |
| 7400 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7401 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7402 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7403 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7404 | } |
| 7405 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7406 | #if 0 |
| 7407 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7408 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7409 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7410 | * the anotation definitions available in the internal schema structure. */ |
| 7411 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7412 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7413 | lys_free(mod, NULL, 1, 1); |
| 7414 | return NULL; |
| 7415 | } |
| 7416 | } |
| 7417 | #endif |
| 7418 | |
| 7419 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
| 7420 | if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
| 7421 | LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error); |
| 7422 | } |
| 7423 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7424 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7425 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7426 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7427 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7428 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7429 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7430 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7431 | lysp_module_free(mod->parsed); |
| 7432 | ((struct lys_module*)mod)->parsed = NULL; |
| 7433 | } |
| 7434 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7435 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7436 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7437 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7438 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7439 | if (m->implemented == 2) { |
| 7440 | m->implemented = 1; |
| 7441 | } |
| 7442 | } |
| 7443 | } |
| 7444 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7445 | ((struct lys_module*)mod)->compiled = mod_c; |
| 7446 | return LY_SUCCESS; |
| 7447 | |
| 7448 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7449 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7450 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7451 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7452 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7453 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7454 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7455 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7456 | mod->compiled = NULL; |
| 7457 | |
| 7458 | /* revert compilation of modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7459 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7460 | m = ctx.ctx->list.objs[i]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7461 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7462 | /* revert features list to the precompiled state */ |
| 7463 | lys_feature_precompile_revert(&ctx, m); |
| 7464 | /* mark module as imported-only / not-implemented */ |
| 7465 | m->implemented = 0; |
| 7466 | /* free the compiled version of the module */ |
| 7467 | lysc_module_free(m->compiled, NULL); |
| 7468 | m->compiled = NULL; |
| 7469 | } |
| 7470 | } |
| 7471 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7472 | return ret; |
| 7473 | } |