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 | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 50 | size_t __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); \ |
| 61 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 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); \ |
| 72 | for (uint32_t __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \ |
| 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); \ |
| 82 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 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); \ |
| 101 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 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 | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 109 | for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
| 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) { \ |
| 119 | for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
| 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 | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 274 | unsigned int u; |
| 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 | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 417 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 418 | |
| 419 | for (i = 0; i < len; ++i) { |
| 420 | if (name[i] == ':') { |
| 421 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 422 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 423 | LY_CHECK_RET(!mod, NULL); |
| 424 | |
| 425 | name = &name[i + 1]; |
| 426 | len = len - i - 1; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 431 | if (mod->implemented) { |
| 432 | /* module is implemented so there is already the compiled schema */ |
| 433 | flist = mod->compiled->features; |
| 434 | } else { |
| 435 | flist = mod->off_features; |
| 436 | } |
| 437 | LY_ARRAY_FOR(flist, i) { |
| 438 | f = &flist[i]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 439 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 440 | return f; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return NULL; |
| 445 | } |
| 446 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 447 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 448 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 449 | */ |
| 450 | static LY_ERR |
| 451 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 452 | { |
| 453 | LY_ERR ret = LY_SUCCESS; |
| 454 | |
| 455 | if (!ext_p->compiled) { |
| 456 | lysc_update_path(ctx, NULL, "{extension}"); |
| 457 | lysc_update_path(ctx, NULL, ext_p->name); |
| 458 | |
| 459 | /* compile the extension definition */ |
| 460 | ext_p->compiled = calloc(1, sizeof **ext); |
| 461 | ext_p->compiled->refcount = 1; |
| 462 | DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name); |
| 463 | DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument); |
| 464 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 465 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 466 | |
| 467 | lysc_update_path(ctx, NULL, NULL); |
| 468 | lysc_update_path(ctx, NULL, NULL); |
| 469 | |
| 470 | /* find extension definition plugin */ |
| 471 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 472 | } |
| 473 | |
| 474 | *ext = lysc_ext_dup(ext_p->compiled); |
| 475 | |
| 476 | done: |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 481 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 482 | * |
| 483 | * @param[in] ctx Compilation context. |
| 484 | * @param[in] ext_p Parsed extension instance. |
| 485 | * @param[in,out] ext Prepared compiled extension instance. |
| 486 | * @param[in] parent Extension instance parent. |
| 487 | * @param[in] parent_type Extension instance parent type. |
| 488 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 489 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 490 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 491 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
| 492 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 493 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 494 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 495 | const char *name; |
| 496 | unsigned int u; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 497 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 498 | |
| 499 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 500 | ext->insubstmt = ext_p->insubstmt; |
| 501 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 502 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 503 | ext->parent = parent; |
| 504 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 505 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 506 | 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] | 507 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 508 | /* get module where the extension definition should be placed */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 509 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u); |
| 510 | if (ext_p->yin) { |
| 511 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 512 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 513 | * YANG (import) prefix must be done here. */ |
| 514 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 515 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 516 | u = 0; |
| 517 | } else if (ctx->mod_def->compiled) { |
| 518 | unsigned int v; |
| 519 | LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) { |
| 520 | if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) { |
| 521 | char *s; |
| 522 | asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]); |
| 523 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
| 524 | u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */ |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | if (!prefixed_name) { |
| 530 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 531 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 532 | goto cleanup; |
| 533 | } |
| 534 | } else { |
| 535 | prefixed_name = ext_p->name; |
| 536 | } |
| 537 | lysc_update_path(ctx, NULL, prefixed_name); |
| 538 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 539 | if (!ext_mod) { |
| 540 | ext_mod = lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1); |
| 541 | if (!ext_mod) { |
| 542 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 543 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 544 | goto cleanup; |
| 545 | } else if (!ext_mod->parsed->extensions) { |
| 546 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 547 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 548 | prefixed_name, ext_mod->name); |
| 549 | goto cleanup; |
| 550 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 551 | } |
| 552 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 553 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 554 | /* find the parsed extension definition there */ |
| 555 | LY_ARRAY_FOR(ext_mod->parsed->extensions, u) { |
| 556 | if (!strcmp(name, ext_mod->parsed->extensions[u].name)) { |
| 557 | /* compile extension definition and assign it */ |
| 558 | LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[u], &ext->def)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 562 | if (!ext->def) { |
| 563 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 564 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 565 | goto cleanup; |
| 566 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 567 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 568 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 569 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 570 | * 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] | 571 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 572 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 573 | struct lysp_stmt *stmt = NULL; |
| 574 | |
| 575 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 576 | /* ... argument was the first XML child element */ |
| 577 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 578 | /* TODO check namespace of the statement */ |
| 579 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 580 | stmt = ext_p->child; |
| 581 | } |
| 582 | } |
| 583 | } else { |
| 584 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 585 | * with LYS_YIN_ATTR flag */ |
| 586 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 587 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 588 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 589 | break; |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | if (!stmt) { |
| 594 | /* missing extension's argument */ |
| 595 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 596 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 597 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 598 | |
| 599 | } |
| 600 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 601 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 602 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 603 | if (prefixed_name != ext_p->name) { |
| 604 | lydict_remove(ctx->ctx, ext_p->name); |
| 605 | ext_p->name = prefixed_name; |
| 606 | if (!ext_p->argument && ext->argument) { |
| 607 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 608 | } |
| 609 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 610 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 611 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 612 | if (ext->argument) { |
| 613 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 614 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 615 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 616 | if (ext->argument) { |
| 617 | lysc_update_path(ctx, NULL, NULL); |
| 618 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 619 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 620 | ext_p->compiled = ext; |
| 621 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 622 | ret = LY_SUCCESS; |
| 623 | cleanup: |
| 624 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 625 | lydict_remove(ctx->ctx, prefixed_name); |
| 626 | } |
| 627 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 628 | lysc_update_path(ctx, NULL, NULL); |
| 629 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 630 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 631 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 635 | * @brief Compile information from the if-feature statement |
| 636 | * @param[in] ctx Compile context. |
| 637 | * @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] | 638 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 639 | * @return LY_ERR value. |
| 640 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 641 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 642 | 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] | 643 | { |
| 644 | const char *c = *value; |
| 645 | int r, rc = EXIT_FAILURE; |
| 646 | int i, j, last_not, checkversion = 0; |
| 647 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 648 | uint8_t op; |
| 649 | struct iff_stack stack = {0, 0, NULL}; |
| 650 | struct lysc_feature *f; |
| 651 | |
| 652 | assert(c); |
| 653 | |
| 654 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 655 | for (i = j = last_not = 0; c[i]; i++) { |
| 656 | if (c[i] == '(') { |
| 657 | j++; |
| 658 | checkversion = 1; |
| 659 | continue; |
| 660 | } else if (c[i] == ')') { |
| 661 | j--; |
| 662 | continue; |
| 663 | } else if (isspace(c[i])) { |
| 664 | checkversion = 1; |
| 665 | continue; |
| 666 | } |
| 667 | |
| 668 | 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] | 669 | int sp; |
| 670 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 671 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 672 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 673 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 674 | return LY_EVALID; |
| 675 | } else if (!isspace(c[i + r])) { |
| 676 | /* feature name starting with the not/and/or */ |
| 677 | last_not = 0; |
| 678 | f_size++; |
| 679 | } else if (c[i] == 'n') { /* not operation */ |
| 680 | if (last_not) { |
| 681 | /* double not */ |
| 682 | expr_size = expr_size - 2; |
| 683 | last_not = 0; |
| 684 | } else { |
| 685 | last_not = 1; |
| 686 | } |
| 687 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 688 | if (f_exp != f_size) { |
| 689 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 690 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 691 | return LY_EVALID; |
| 692 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 693 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 694 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 695 | /* not a not operation */ |
| 696 | last_not = 0; |
| 697 | } |
| 698 | i += r; |
| 699 | } else { |
| 700 | f_size++; |
| 701 | last_not = 0; |
| 702 | } |
| 703 | expr_size++; |
| 704 | |
| 705 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 706 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 707 | i--; |
| 708 | break; |
| 709 | } |
| 710 | i++; |
| 711 | } |
| 712 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 713 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 714 | /* not matching count of ( and ) */ |
| 715 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 716 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 717 | return LY_EVALID; |
| 718 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 719 | if (f_exp != f_size) { |
| 720 | /* features do not match the needed arguments for the logical operations */ |
| 721 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 722 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 723 | "the required number of operands for the operations.", *value); |
| 724 | return LY_EVALID; |
| 725 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 726 | |
| 727 | if (checkversion || expr_size > 1) { |
| 728 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 729 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 730 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 731 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 732 | return LY_EVALID; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | /* allocate the memory */ |
| 737 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 738 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 739 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 740 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 741 | |
| 742 | stack.size = expr_size; |
| 743 | f_size--; expr_size--; /* used as indexes from now */ |
| 744 | |
| 745 | for (i--; i >= 0; i--) { |
| 746 | if (c[i] == ')') { |
| 747 | /* push it on stack */ |
| 748 | iff_stack_push(&stack, LYS_IFF_RP); |
| 749 | continue; |
| 750 | } else if (c[i] == '(') { |
| 751 | /* pop from the stack into result all operators until ) */ |
| 752 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 753 | iff_setop(iff->expr, op, expr_size--); |
| 754 | } |
| 755 | continue; |
| 756 | } else if (isspace(c[i])) { |
| 757 | continue; |
| 758 | } |
| 759 | |
| 760 | /* end of operator or operand -> find beginning and get what is it */ |
| 761 | j = i + 1; |
| 762 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 763 | i--; |
| 764 | } |
| 765 | i++; /* go back by one step */ |
| 766 | |
| 767 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 768 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 769 | /* double not */ |
| 770 | iff_stack_pop(&stack); |
| 771 | } else { |
| 772 | /* not has the highest priority, so do not pop from the stack |
| 773 | * as in case of AND and OR */ |
| 774 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 775 | } |
| 776 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 777 | /* as for OR - pop from the stack all operators with the same or higher |
| 778 | * priority and store them to the result, then push the AND to the stack */ |
| 779 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 780 | op = iff_stack_pop(&stack); |
| 781 | iff_setop(iff->expr, op, expr_size--); |
| 782 | } |
| 783 | iff_stack_push(&stack, LYS_IFF_AND); |
| 784 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 785 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 786 | op = iff_stack_pop(&stack); |
| 787 | iff_setop(iff->expr, op, expr_size--); |
| 788 | } |
| 789 | iff_stack_push(&stack, LYS_IFF_OR); |
| 790 | } else { |
| 791 | /* feature name, length is j - i */ |
| 792 | |
| 793 | /* add it to the expression */ |
| 794 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 795 | |
| 796 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 797 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 798 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 799 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 800 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 801 | iff->features[f_size] = f; |
| 802 | LY_ARRAY_INCREMENT(iff->features); |
| 803 | f_size--; |
| 804 | } |
| 805 | } |
| 806 | while (stack.index) { |
| 807 | op = iff_stack_pop(&stack); |
| 808 | iff_setop(iff->expr, op, expr_size--); |
| 809 | } |
| 810 | |
| 811 | if (++expr_size || ++f_size) { |
| 812 | /* not all expected operators and operands found */ |
| 813 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 814 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 815 | rc = LY_EINT; |
| 816 | } else { |
| 817 | rc = LY_SUCCESS; |
| 818 | } |
| 819 | |
| 820 | error: |
| 821 | /* cleanup */ |
| 822 | iff_stack_clean(&stack); |
| 823 | |
| 824 | return rc; |
| 825 | } |
| 826 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 827 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 828 | * @brief Get the XPath context node for the given schema node. |
| 829 | * @param[in] start The schema node where the XPath expression appears. |
| 830 | * @return The context node to evaluate XPath expression in given schema node. |
| 831 | * @return NULL in case the context node is the root node. |
| 832 | */ |
| 833 | static struct lysc_node * |
| 834 | lysc_xpath_context(struct lysc_node *start) |
| 835 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 836 | 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] | 837 | start = start->parent); |
| 838 | return start; |
| 839 | } |
| 840 | |
| 841 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 842 | * @brief Compile information from the when statement |
| 843 | * @param[in] ctx Compile context. |
| 844 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 845 | * @param[in] flags Flags of the node with the "when" defiition. |
| 846 | * @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] | 847 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 848 | * @return LY_ERR value. |
| 849 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 850 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 851 | 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] | 852 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 853 | LY_ERR ret = LY_SUCCESS; |
| 854 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 855 | *when = calloc(1, sizeof **when); |
| 856 | (*when)->refcount = 1; |
| 857 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 858 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 859 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 860 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 861 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 862 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 863 | 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] | 864 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 865 | |
| 866 | done: |
| 867 | return ret; |
| 868 | } |
| 869 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 870 | /** |
| 871 | * @brief Compile information from the must statement |
| 872 | * @param[in] ctx Compile context. |
| 873 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 874 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 875 | * @return LY_ERR value. |
| 876 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 877 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 878 | 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] | 879 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 880 | LY_ERR ret = LY_SUCCESS; |
| 881 | |
| 882 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 883 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 884 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 885 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 886 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 887 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 888 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 889 | 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] | 890 | |
| 891 | done: |
| 892 | return ret; |
| 893 | } |
| 894 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 895 | /** |
| 896 | * @brief Compile information from the import statement |
| 897 | * @param[in] ctx Compile context. |
| 898 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 899 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 900 | * @return LY_ERR value. |
| 901 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 902 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 903 | 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] | 904 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 905 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 906 | LY_ERR ret = LY_SUCCESS; |
| 907 | |
| 908 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 909 | 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] | 910 | imp->module = imp_p->module; |
| 911 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 912 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 913 | * 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] | 914 | * 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] | 915 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 916 | /* try to use filepath if present */ |
| 917 | if (imp->module->filepath) { |
| 918 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 919 | !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] | 920 | if (mod != imp->module) { |
| 921 | 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] | 922 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 923 | mod = NULL; |
| 924 | } |
| 925 | } |
| 926 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 927 | 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] | 928 | 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] | 929 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 930 | return LY_ENOTFOUND; |
| 931 | } |
| 932 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | done: |
| 936 | return ret; |
| 937 | } |
| 938 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 939 | /** |
| 940 | * @brief Compile information from the identity statement |
| 941 | * |
| 942 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 943 | * |
| 944 | * @param[in] ctx Compile context. |
| 945 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 946 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 947 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 948 | * @return LY_ERR value. |
| 949 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 950 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 951 | 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] | 952 | { |
| 953 | unsigned int u; |
| 954 | LY_ERR ret = LY_SUCCESS; |
| 955 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 956 | lysc_update_path(ctx, NULL, ident_p->name); |
| 957 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 958 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 959 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 960 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 961 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 962 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 963 | 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] | 964 | /* 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] | 965 | 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] | 966 | ident->flags = ident_p->flags; |
| 967 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 968 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 969 | done: |
| 970 | return ret; |
| 971 | } |
| 972 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 973 | /** |
| 974 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 975 | * |
| 976 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 977 | * |
| 978 | * @param[in] ctx Compile context for logging. |
| 979 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 980 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 981 | * @return LY_SUCCESS if everything is ok. |
| 982 | * @return LY_EVALID if the identity is derived from itself. |
| 983 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 984 | static LY_ERR |
| 985 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 986 | { |
| 987 | LY_ERR ret = LY_EVALID; |
| 988 | unsigned int u, v; |
| 989 | struct ly_set recursion = {0}; |
| 990 | struct lysc_ident *drv; |
| 991 | |
| 992 | if (!derived) { |
| 993 | return LY_SUCCESS; |
| 994 | } |
| 995 | |
| 996 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 997 | if (ident == derived[u]) { |
| 998 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 999 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1000 | goto cleanup; |
| 1001 | } |
| 1002 | ly_set_add(&recursion, derived[u], 0); |
| 1003 | } |
| 1004 | |
| 1005 | for (v = 0; v < recursion.count; ++v) { |
| 1006 | drv = recursion.objs[v]; |
| 1007 | if (!drv->derived) { |
| 1008 | continue; |
| 1009 | } |
| 1010 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 1011 | if (ident == drv->derived[u]) { |
| 1012 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1013 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1014 | goto cleanup; |
| 1015 | } |
| 1016 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1017 | } |
| 1018 | } |
| 1019 | ret = LY_SUCCESS; |
| 1020 | |
| 1021 | cleanup: |
| 1022 | ly_set_erase(&recursion, NULL); |
| 1023 | return ret; |
| 1024 | } |
| 1025 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1026 | /** |
| 1027 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1028 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1029 | * 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] | 1030 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1031 | * to distinguish these two use cases. |
| 1032 | * |
| 1033 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1034 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 1035 | * @param[in] ident Referencing identity to work with. |
| 1036 | * @param[in] bases Array of bases of identityref to fill in. |
| 1037 | * @return LY_ERR value. |
| 1038 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1039 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1040 | 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] | 1041 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1042 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1043 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1044 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1045 | struct lysc_ident **idref; |
| 1046 | |
| 1047 | assert(ident || bases); |
| 1048 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1049 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1050 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1051 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1052 | return LY_EVALID; |
| 1053 | } |
| 1054 | |
| 1055 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 1056 | s = strchr(bases_p[u], ':'); |
| 1057 | if (s) { |
| 1058 | /* prefixed identity */ |
| 1059 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1060 | 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] | 1061 | } else { |
| 1062 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1063 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1064 | } |
| 1065 | if (!mod) { |
| 1066 | if (ident) { |
| 1067 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1068 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1069 | } else { |
| 1070 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1071 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1072 | } |
| 1073 | return LY_EVALID; |
| 1074 | } |
| 1075 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1076 | if (mod->compiled && mod->compiled->identities) { |
| 1077 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 1078 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1079 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1080 | if (ident == &mod->compiled->identities[v]) { |
| 1081 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1082 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1083 | return LY_EVALID; |
| 1084 | } |
| 1085 | 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] | 1086 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1087 | 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] | 1088 | *idref = ident; |
| 1089 | } else { |
| 1090 | /* we have match! store the found identity */ |
| 1091 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1092 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1093 | } |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | if (!idref || !(*idref)) { |
| 1099 | if (ident) { |
| 1100 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1101 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1102 | } else { |
| 1103 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1104 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1105 | } |
| 1106 | return LY_EVALID; |
| 1107 | } |
| 1108 | } |
| 1109 | return LY_SUCCESS; |
| 1110 | } |
| 1111 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1112 | /** |
| 1113 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1114 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1115 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1116 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1117 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1118 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1119 | static LY_ERR |
| 1120 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1121 | { |
| 1122 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1123 | |
| 1124 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 1125 | if (!idents_p[i].bases) { |
| 1126 | continue; |
| 1127 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1128 | lysc_update_path(ctx, NULL, idents[i].name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1129 | LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL)); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1130 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1131 | } |
| 1132 | return LY_SUCCESS; |
| 1133 | } |
| 1134 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1135 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1136 | 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] | 1137 | { |
| 1138 | unsigned int offset = 0, u; |
| 1139 | struct lysc_ctx context = {0}; |
| 1140 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1141 | assert(ctx_sc || ctx); |
| 1142 | |
| 1143 | if (!ctx_sc) { |
| 1144 | context.ctx = ctx; |
| 1145 | context.mod = module; |
| 1146 | context.path_len = 1; |
| 1147 | context.path[0] = '/'; |
| 1148 | ctx_sc = &context; |
| 1149 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1150 | |
| 1151 | if (!features_p) { |
| 1152 | return LY_SUCCESS; |
| 1153 | } |
| 1154 | if (*features) { |
| 1155 | offset = LY_ARRAY_SIZE(*features); |
| 1156 | } |
| 1157 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1158 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 1159 | 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] | 1160 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1161 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1162 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1163 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1164 | 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] | 1165 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1166 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1167 | 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] | 1168 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1169 | (*features)[offset + u].module = ctx_sc->mod; |
| 1170 | |
| 1171 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1172 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1173 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1174 | |
| 1175 | return LY_SUCCESS; |
| 1176 | } |
| 1177 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1178 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1179 | * @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] | 1180 | * |
| 1181 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1182 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1183 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1184 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1185 | * being currently processed). |
| 1186 | * @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] | 1187 | * @return LY_SUCCESS if everything is ok. |
| 1188 | * @return LY_EVALID if the feature references indirectly itself. |
| 1189 | */ |
| 1190 | static LY_ERR |
| 1191 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1192 | { |
| 1193 | LY_ERR ret = LY_EVALID; |
| 1194 | unsigned int u, v; |
| 1195 | struct ly_set recursion = {0}; |
| 1196 | struct lysc_feature *drv; |
| 1197 | |
| 1198 | if (!depfeatures) { |
| 1199 | return LY_SUCCESS; |
| 1200 | } |
| 1201 | |
| 1202 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1203 | if (feature == depfeatures[u]) { |
| 1204 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1205 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1206 | goto cleanup; |
| 1207 | } |
| 1208 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1209 | } |
| 1210 | |
| 1211 | for (v = 0; v < recursion.count; ++v) { |
| 1212 | drv = recursion.objs[v]; |
| 1213 | if (!drv->depfeatures) { |
| 1214 | continue; |
| 1215 | } |
| 1216 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1217 | if (feature == drv->depfeatures[u]) { |
| 1218 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1219 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1220 | goto cleanup; |
| 1221 | } |
| 1222 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1223 | } |
| 1224 | } |
| 1225 | ret = LY_SUCCESS; |
| 1226 | |
| 1227 | cleanup: |
| 1228 | ly_set_erase(&recursion, NULL); |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1233 | * @brief Create pre-compiled features array. |
| 1234 | * |
| 1235 | * See lys_feature_precompile() for more details. |
| 1236 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1237 | * @param[in] ctx Compile context. |
| 1238 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1239 | * @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] | 1240 | * @return LY_ERR value. |
| 1241 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1242 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1243 | 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] | 1244 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1245 | unsigned int u, v, x; |
| 1246 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1247 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1248 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1249 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1250 | /* find the preprecompiled feature */ |
| 1251 | LY_ARRAY_FOR(features, x) { |
| 1252 | if (strcmp(features[x].name, feature_p->name)) { |
| 1253 | continue; |
| 1254 | } |
| 1255 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1256 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1257 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1258 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1259 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1260 | 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] | 1261 | 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] | 1262 | if (feature->iffeatures) { |
| 1263 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1264 | if (feature->iffeatures[u].features) { |
| 1265 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1266 | /* check for circular dependency - direct reference first,... */ |
| 1267 | if (feature == feature->iffeatures[u].features[v]) { |
| 1268 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1269 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1270 | return LY_EVALID; |
| 1271 | } |
| 1272 | /* ... and indirect circular reference */ |
| 1273 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1274 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1275 | /* add itself into the dependants list */ |
| 1276 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1277 | *df = feature; |
| 1278 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1279 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1280 | } |
| 1281 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1282 | lysc_update_path(ctx, NULL, NULL); |
| 1283 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1284 | done: |
| 1285 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1286 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1287 | |
| 1288 | LOGINT(ctx->ctx); |
| 1289 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1290 | } |
| 1291 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1292 | /** |
| 1293 | * @brief Revert compiled list of features back to the precompiled state. |
| 1294 | * |
| 1295 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1296 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1297 | * |
| 1298 | * @param[in] ctx Compilation context. |
| 1299 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1300 | * and supposed to hold the off_features list. |
| 1301 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1302 | static void |
| 1303 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1304 | { |
| 1305 | unsigned int u, v; |
| 1306 | |
| 1307 | /* keep the off_features list until the complete lys_module is freed */ |
| 1308 | mod->off_features = mod->compiled->features; |
| 1309 | mod->compiled->features = NULL; |
| 1310 | |
| 1311 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1312 | * which may points into the data being freed here */ |
| 1313 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1314 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1315 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1316 | } |
| 1317 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1318 | mod->off_features[u].iffeatures = NULL; |
| 1319 | |
| 1320 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1321 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1322 | } |
| 1323 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1324 | mod->off_features[u].exts = NULL; |
| 1325 | } |
| 1326 | } |
| 1327 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1328 | /** |
| 1329 | * @brief Validate and normalize numeric value from a range definition. |
| 1330 | * @param[in] ctx Compile context. |
| 1331 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1332 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1333 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1334 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1335 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1336 | * @param[in] value String value of the range boundary. |
| 1337 | * @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. |
| 1338 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1339 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1340 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1341 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1342 | 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] | 1343 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1344 | size_t fraction = 0, size; |
| 1345 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1346 | *len = 0; |
| 1347 | |
| 1348 | assert(value); |
| 1349 | /* parse value */ |
| 1350 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1351 | return LY_EVALID; |
| 1352 | } |
| 1353 | |
| 1354 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1355 | ++(*len); |
| 1356 | } |
| 1357 | |
| 1358 | while (isdigit(value[*len])) { |
| 1359 | ++(*len); |
| 1360 | } |
| 1361 | |
| 1362 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1363 | if (basetype == LY_TYPE_DEC64) { |
| 1364 | goto decimal; |
| 1365 | } else { |
| 1366 | *valcopy = strndup(value, *len); |
| 1367 | return LY_SUCCESS; |
| 1368 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1369 | } |
| 1370 | fraction = *len; |
| 1371 | |
| 1372 | ++(*len); |
| 1373 | while (isdigit(value[*len])) { |
| 1374 | ++(*len); |
| 1375 | } |
| 1376 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1377 | if (basetype == LY_TYPE_DEC64) { |
| 1378 | decimal: |
| 1379 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1380 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1381 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1382 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1383 | *len, value, frdigits); |
| 1384 | return LY_EINVAL; |
| 1385 | } |
| 1386 | if (fraction) { |
| 1387 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1388 | } else { |
| 1389 | size = (*len) + frdigits + 1; |
| 1390 | } |
| 1391 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1392 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1393 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1394 | (*valcopy)[size - 1] = '\0'; |
| 1395 | if (fraction) { |
| 1396 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1397 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1398 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1399 | } else { |
| 1400 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1401 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1402 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1403 | } |
| 1404 | return LY_SUCCESS; |
| 1405 | } |
| 1406 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1407 | /** |
| 1408 | * @brief Check that values in range are in ascendant order. |
| 1409 | * @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] | 1410 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1411 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1412 | * @param[in] value Current value to check. |
| 1413 | * @param[in] prev_value The last seen value. |
| 1414 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1415 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1416 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1417 | 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] | 1418 | { |
| 1419 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1420 | 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] | 1421 | return LY_EEXIST; |
| 1422 | } |
| 1423 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1424 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1425 | return LY_EEXIST; |
| 1426 | } |
| 1427 | } |
| 1428 | return LY_SUCCESS; |
| 1429 | } |
| 1430 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1431 | /** |
| 1432 | * @brief Set min/max value of the range part. |
| 1433 | * @param[in] ctx Compile context. |
| 1434 | * @param[in] part Range part structure to fill. |
| 1435 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1436 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1437 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1438 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1439 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1440 | * @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] | 1441 | * @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] | 1442 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1443 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1444 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1445 | * frdigits value), LY_EMEM. |
| 1446 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1447 | static LY_ERR |
| 1448 | 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] | 1449 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1450 | { |
| 1451 | LY_ERR ret = LY_SUCCESS; |
| 1452 | char *valcopy = NULL; |
| 1453 | size_t len; |
| 1454 | |
| 1455 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1456 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1457 | LY_CHECK_GOTO(ret, finalize); |
| 1458 | } |
| 1459 | if (!valcopy && base_range) { |
| 1460 | if (max) { |
| 1461 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1462 | } else { |
| 1463 | part->min_64 = base_range->parts[0].min_64; |
| 1464 | } |
| 1465 | if (!first) { |
| 1466 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1467 | } |
| 1468 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1472 | case LY_TYPE_INT8: /* range */ |
| 1473 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1474 | 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] | 1475 | } else if (max) { |
| 1476 | part->max_64 = INT64_C(127); |
| 1477 | } else { |
| 1478 | part->min_64 = INT64_C(-128); |
| 1479 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1480 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1481 | 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] | 1482 | } |
| 1483 | break; |
| 1484 | case LY_TYPE_INT16: /* range */ |
| 1485 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1486 | 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] | 1487 | } else if (max) { |
| 1488 | part->max_64 = INT64_C(32767); |
| 1489 | } else { |
| 1490 | part->min_64 = INT64_C(-32768); |
| 1491 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1492 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1493 | 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] | 1494 | } |
| 1495 | break; |
| 1496 | case LY_TYPE_INT32: /* range */ |
| 1497 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1498 | 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] | 1499 | } else if (max) { |
| 1500 | part->max_64 = INT64_C(2147483647); |
| 1501 | } else { |
| 1502 | part->min_64 = INT64_C(-2147483648); |
| 1503 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1504 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1505 | 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] | 1506 | } |
| 1507 | break; |
| 1508 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1509 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1510 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1511 | 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] | 1512 | max ? &part->max_64 : &part->min_64); |
| 1513 | } else if (max) { |
| 1514 | part->max_64 = INT64_C(9223372036854775807); |
| 1515 | } else { |
| 1516 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1517 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1518 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1519 | 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] | 1520 | } |
| 1521 | break; |
| 1522 | case LY_TYPE_UINT8: /* range */ |
| 1523 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1524 | 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] | 1525 | } else if (max) { |
| 1526 | part->max_u64 = UINT64_C(255); |
| 1527 | } else { |
| 1528 | part->min_u64 = UINT64_C(0); |
| 1529 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1530 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1531 | 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] | 1532 | } |
| 1533 | break; |
| 1534 | case LY_TYPE_UINT16: /* range */ |
| 1535 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1536 | 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] | 1537 | } else if (max) { |
| 1538 | part->max_u64 = UINT64_C(65535); |
| 1539 | } else { |
| 1540 | part->min_u64 = UINT64_C(0); |
| 1541 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1542 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1543 | 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] | 1544 | } |
| 1545 | break; |
| 1546 | case LY_TYPE_UINT32: /* range */ |
| 1547 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1548 | 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] | 1549 | } else if (max) { |
| 1550 | part->max_u64 = UINT64_C(4294967295); |
| 1551 | } else { |
| 1552 | part->min_u64 = UINT64_C(0); |
| 1553 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1554 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1555 | 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] | 1556 | } |
| 1557 | break; |
| 1558 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1559 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1560 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1561 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1562 | 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] | 1563 | } else if (max) { |
| 1564 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1565 | } else { |
| 1566 | part->min_u64 = UINT64_C(0); |
| 1567 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1568 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1569 | 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] | 1570 | } |
| 1571 | break; |
| 1572 | default: |
| 1573 | LOGINT(ctx->ctx); |
| 1574 | ret = LY_EINT; |
| 1575 | } |
| 1576 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1577 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1578 | if (ret == LY_EDENIED) { |
| 1579 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1580 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1581 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1582 | } else if (ret == LY_EVALID) { |
| 1583 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1584 | "Invalid %s restriction - invalid value \"%s\".", |
| 1585 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1586 | } else if (ret == LY_EEXIST) { |
| 1587 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1588 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1589 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1590 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1591 | } else if (!ret && value) { |
| 1592 | *value = *value + len; |
| 1593 | } |
| 1594 | free(valcopy); |
| 1595 | return ret; |
| 1596 | } |
| 1597 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1598 | /** |
| 1599 | * @brief Compile the parsed range restriction. |
| 1600 | * @param[in] ctx Compile context. |
| 1601 | * @param[in] range_p Parsed range structure to compile. |
| 1602 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1603 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1604 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1605 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1606 | * range restriction must be more restrictive than the base_range. |
| 1607 | * @param[in,out] range Pointer to the created current range structure. |
| 1608 | * @return LY_ERR value. |
| 1609 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1610 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1611 | 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] | 1612 | struct lysc_range *base_range, struct lysc_range **range) |
| 1613 | { |
| 1614 | LY_ERR ret = LY_EVALID; |
| 1615 | const char *expr; |
| 1616 | struct lysc_range_part *parts = NULL, *part; |
| 1617 | int range_expected = 0, uns; |
| 1618 | unsigned int parts_done = 0, u, v; |
| 1619 | |
| 1620 | assert(range); |
| 1621 | assert(range_p); |
| 1622 | |
| 1623 | expr = range_p->arg; |
| 1624 | while(1) { |
| 1625 | if (isspace(*expr)) { |
| 1626 | ++expr; |
| 1627 | } else if (*expr == '\0') { |
| 1628 | if (range_expected) { |
| 1629 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1630 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1631 | length_restr ? "length" : "range", range_p->arg); |
| 1632 | goto cleanup; |
| 1633 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1634 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1635 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1636 | length_restr ? "length" : "range", range_p->arg); |
| 1637 | goto cleanup; |
| 1638 | } |
| 1639 | parts_done++; |
| 1640 | break; |
| 1641 | } else if (!strncmp(expr, "min", 3)) { |
| 1642 | if (parts) { |
| 1643 | /* min cannot be used elsewhere than in the first part */ |
| 1644 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1645 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1646 | expr - range_p->arg, range_p->arg); |
| 1647 | goto cleanup; |
| 1648 | } |
| 1649 | expr += 3; |
| 1650 | |
| 1651 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1652 | 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] | 1653 | part->max_64 = part->min_64; |
| 1654 | } else if (*expr == '|') { |
| 1655 | if (!parts || range_expected) { |
| 1656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1657 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1658 | goto cleanup; |
| 1659 | } |
| 1660 | expr++; |
| 1661 | parts_done++; |
| 1662 | /* process next part of the expression */ |
| 1663 | } else if (!strncmp(expr, "..", 2)) { |
| 1664 | expr += 2; |
| 1665 | while (isspace(*expr)) { |
| 1666 | expr++; |
| 1667 | } |
| 1668 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1669 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1670 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1671 | goto cleanup; |
| 1672 | } |
| 1673 | /* continue expecting the upper boundary */ |
| 1674 | range_expected = 1; |
| 1675 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1676 | /* number */ |
| 1677 | if (range_expected) { |
| 1678 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1679 | 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] | 1680 | range_expected = 0; |
| 1681 | } else { |
| 1682 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1683 | 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] | 1684 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1685 | part->max_64 = part->min_64; |
| 1686 | } |
| 1687 | |
| 1688 | /* continue with possible another expression part */ |
| 1689 | } else if (!strncmp(expr, "max", 3)) { |
| 1690 | expr += 3; |
| 1691 | while (isspace(*expr)) { |
| 1692 | expr++; |
| 1693 | } |
| 1694 | if (*expr != '\0') { |
| 1695 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1696 | length_restr ? "length" : "range", expr); |
| 1697 | goto cleanup; |
| 1698 | } |
| 1699 | if (range_expected) { |
| 1700 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1701 | 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] | 1702 | range_expected = 0; |
| 1703 | } else { |
| 1704 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1705 | 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] | 1706 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1707 | part->min_64 = part->max_64; |
| 1708 | } |
| 1709 | } else { |
| 1710 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1711 | length_restr ? "length" : "range", expr); |
| 1712 | goto cleanup; |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | /* check with the previous range/length restriction */ |
| 1717 | if (base_range) { |
| 1718 | switch (basetype) { |
| 1719 | case LY_TYPE_BINARY: |
| 1720 | case LY_TYPE_UINT8: |
| 1721 | case LY_TYPE_UINT16: |
| 1722 | case LY_TYPE_UINT32: |
| 1723 | case LY_TYPE_UINT64: |
| 1724 | case LY_TYPE_STRING: |
| 1725 | uns = 1; |
| 1726 | break; |
| 1727 | case LY_TYPE_DEC64: |
| 1728 | case LY_TYPE_INT8: |
| 1729 | case LY_TYPE_INT16: |
| 1730 | case LY_TYPE_INT32: |
| 1731 | case LY_TYPE_INT64: |
| 1732 | uns = 0; |
| 1733 | break; |
| 1734 | default: |
| 1735 | LOGINT(ctx->ctx); |
| 1736 | ret = LY_EINT; |
| 1737 | goto cleanup; |
| 1738 | } |
| 1739 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1740 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1741 | goto baseerror; |
| 1742 | } |
| 1743 | /* current lower bound is not lower than the base */ |
| 1744 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1745 | /* base has single value */ |
| 1746 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1747 | /* both lower bounds are the same */ |
| 1748 | if (parts[u].min_64 != parts[u].max_64) { |
| 1749 | /* current continues with a range */ |
| 1750 | goto baseerror; |
| 1751 | } else { |
| 1752 | /* equal single values, move both forward */ |
| 1753 | ++v; |
| 1754 | continue; |
| 1755 | } |
| 1756 | } else { |
| 1757 | /* base is single value lower than current range, so the |
| 1758 | * value from base range is removed in the current, |
| 1759 | * move only base and repeat checking */ |
| 1760 | ++v; |
| 1761 | --u; |
| 1762 | continue; |
| 1763 | } |
| 1764 | } else { |
| 1765 | /* base is the range */ |
| 1766 | if (parts[u].min_64 == parts[u].max_64) { |
| 1767 | /* current is a single value */ |
| 1768 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1769 | /* current is behind the base range, so base range is omitted, |
| 1770 | * move the base and keep the current for further check */ |
| 1771 | ++v; |
| 1772 | --u; |
| 1773 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1774 | continue; |
| 1775 | } else { |
| 1776 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1777 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1778 | /* higher bound is higher than the current higher bound */ |
| 1779 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1780 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1781 | * continue with the same current, but move the base */ |
| 1782 | --u; |
| 1783 | ++v; |
| 1784 | continue; |
| 1785 | } |
| 1786 | /* current range starts within the base range but end behind it */ |
| 1787 | goto baseerror; |
| 1788 | } else { |
| 1789 | /* current range is smaller than the base, |
| 1790 | * move current, but stay with the base */ |
| 1791 | continue; |
| 1792 | } |
| 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | if (u != parts_done) { |
| 1797 | baseerror: |
| 1798 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1799 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1800 | length_restr ? "length" : "range", range_p->arg); |
| 1801 | goto cleanup; |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | if (!(*range)) { |
| 1806 | *range = calloc(1, sizeof **range); |
| 1807 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1808 | } |
| 1809 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1810 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1811 | if (range_p->eapptag) { |
| 1812 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1813 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1814 | } |
| 1815 | if (range_p->emsg) { |
| 1816 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1817 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1818 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1819 | if (range_p->dsc) { |
| 1820 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1821 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1822 | } |
| 1823 | if (range_p->ref) { |
| 1824 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1825 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1826 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1827 | /* extensions are taken only from the last range by the caller */ |
| 1828 | |
| 1829 | (*range)->parts = parts; |
| 1830 | parts = NULL; |
| 1831 | ret = LY_SUCCESS; |
| 1832 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1833 | LY_ARRAY_FREE(parts); |
| 1834 | |
| 1835 | return ret; |
| 1836 | } |
| 1837 | |
| 1838 | /** |
| 1839 | * @brief Checks pattern syntax. |
| 1840 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1841 | * @param[in] ctx Context. |
| 1842 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1843 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1844 | * @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] | 1845 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1846 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1847 | LY_ERR |
| 1848 | 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] | 1849 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1850 | int idx, idx2, start, end, count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1851 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1852 | int err_code; |
| 1853 | const char *orig_ptr; |
| 1854 | PCRE2_SIZE err_offset; |
| 1855 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1856 | #define URANGE_LEN 19 |
| 1857 | char *ublock2urange[][2] = { |
| 1858 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1859 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1860 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1861 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1862 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1863 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1864 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1865 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1866 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1867 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1868 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1869 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1870 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1871 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1872 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1873 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1874 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1875 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1876 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1877 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1878 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1879 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1880 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1881 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1882 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1883 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1884 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1885 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1886 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1887 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1888 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1889 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1890 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1891 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1892 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1893 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1894 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1895 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1896 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1897 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1898 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1899 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1900 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1901 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1902 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1903 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1904 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1905 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1906 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1907 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1908 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1909 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1910 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1911 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1912 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1913 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1914 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1915 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1916 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1917 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1918 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1919 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1920 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1921 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1922 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1923 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1924 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1925 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1926 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1927 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1928 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1929 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1930 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1931 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1932 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1933 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1934 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1935 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1936 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1937 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1938 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1939 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1940 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1941 | {NULL, NULL} |
| 1942 | }; |
| 1943 | |
| 1944 | /* adjust the expression to a Perl equivalent |
| 1945 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1946 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1947 | /* we need to replace all "$" and "^" with "\$" and "\^", count them now */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1948 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1949 | |
| 1950 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1951 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1952 | perl_regex[0] = '\0'; |
| 1953 | |
| 1954 | ptr = perl_regex; |
| 1955 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1956 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1957 | if (orig_ptr[0] == '$') { |
| 1958 | ptr += sprintf(ptr, "\\$"); |
| 1959 | } else if (orig_ptr[0] == '^') { |
| 1960 | ptr += sprintf(ptr, "\\^"); |
| 1961 | } else { |
| 1962 | ptr[0] = orig_ptr[0]; |
| 1963 | ++ptr; |
| 1964 | } |
| 1965 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1966 | ptr[0] = '\0'; |
| 1967 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1968 | |
| 1969 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1970 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1971 | start = ptr - perl_regex; |
| 1972 | |
| 1973 | ptr = strchr(ptr, '}'); |
| 1974 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1975 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1976 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1977 | free(perl_regex); |
| 1978 | return LY_EVALID; |
| 1979 | } |
| 1980 | end = (ptr - perl_regex) + 1; |
| 1981 | |
| 1982 | /* need more space */ |
| 1983 | if (end - start < URANGE_LEN) { |
| 1984 | 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] | 1985 | 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] | 1986 | } |
| 1987 | |
| 1988 | /* find our range */ |
| 1989 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1990 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1991 | break; |
| 1992 | } |
| 1993 | } |
| 1994 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1995 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1996 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1997 | free(perl_regex); |
| 1998 | return LY_EVALID; |
| 1999 | } |
| 2000 | |
| 2001 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 2002 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 2003 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 2004 | ++count; |
| 2005 | } |
| 2006 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 2007 | --count; |
| 2008 | } |
| 2009 | } |
| 2010 | if (count) { |
| 2011 | /* skip brackets */ |
| 2012 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2013 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2014 | } else { |
| 2015 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2016 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2021 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2022 | 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] | 2023 | &err_code, &err_offset, NULL); |
| 2024 | if (!code_local) { |
| 2025 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2026 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2027 | 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] | 2028 | free(perl_regex); |
| 2029 | return LY_EVALID; |
| 2030 | } |
| 2031 | free(perl_regex); |
| 2032 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2033 | if (code) { |
| 2034 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2035 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2036 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | return LY_SUCCESS; |
| 2040 | |
| 2041 | #undef URANGE_LEN |
| 2042 | } |
| 2043 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2044 | /** |
| 2045 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2046 | * @param[in] ctx Compile context. |
| 2047 | * @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] | 2048 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2049 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2050 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2051 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2052 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2053 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2054 | 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] | 2055 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2056 | { |
| 2057 | struct lysc_pattern **pattern; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2058 | unsigned int u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2059 | LY_ERR ret = LY_SUCCESS; |
| 2060 | |
| 2061 | /* first, copy the patterns from the base type */ |
| 2062 | if (base_patterns) { |
| 2063 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2064 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2065 | } |
| 2066 | |
| 2067 | LY_ARRAY_FOR(patterns_p, u) { |
| 2068 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2069 | *pattern = calloc(1, sizeof **pattern); |
| 2070 | ++(*pattern)->refcount; |
| 2071 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | 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] | 2073 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2074 | |
| 2075 | if (patterns_p[u].arg[0] == 0x15) { |
| 2076 | (*pattern)->inverted = 1; |
| 2077 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2078 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2079 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2080 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2081 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2082 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2083 | 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] | 2084 | } |
| 2085 | done: |
| 2086 | return ret; |
| 2087 | } |
| 2088 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2089 | /** |
| 2090 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2091 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2092 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2093 | 0 /* LY_TYPE_UNKNOWN */, |
| 2094 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2095 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2096 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2097 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2098 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2099 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2100 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2101 | 0 /* LY_TYPE_BOOL */, |
| 2102 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2103 | 0 /* LY_TYPE_EMPTY */, |
| 2104 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2105 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2106 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2107 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2108 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2109 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2110 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2111 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2112 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2113 | }; |
| 2114 | |
| 2115 | /** |
| 2116 | * @brief stringification of the YANG built-in data types |
| 2117 | */ |
| 2118 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2119 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2120 | "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] | 2121 | }; |
| 2122 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2123 | /** |
| 2124 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2125 | * @param[in] ctx Compile context. |
| 2126 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2127 | * @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] | 2128 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2129 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2130 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2131 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2132 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2133 | 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] | 2134 | 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] | 2135 | { |
| 2136 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 2137 | unsigned int u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2138 | int32_t value = 0; |
| 2139 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2140 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2141 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2142 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2143 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2144 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2145 | return LY_EVALID; |
| 2146 | } |
| 2147 | |
| 2148 | LY_ARRAY_FOR(enums_p, u) { |
| 2149 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2150 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2151 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2152 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2153 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2154 | if (base_enums) { |
| 2155 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2156 | LY_ARRAY_FOR(base_enums, v) { |
| 2157 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2158 | break; |
| 2159 | } |
| 2160 | } |
| 2161 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 2162 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2163 | "Invalid %s - derived type adds new item \"%s\".", |
| 2164 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2165 | return LY_EVALID; |
| 2166 | } |
| 2167 | match = v; |
| 2168 | } |
| 2169 | |
| 2170 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2171 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2172 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2173 | e->value = (int32_t)enums_p[u].value; |
| 2174 | if (!u || e->value >= value) { |
| 2175 | value = e->value + 1; |
| 2176 | } |
| 2177 | /* check collision with other values */ |
| 2178 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2179 | if (e->value == (*enums)[v].value) { |
| 2180 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2181 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2182 | e->value, e->name, (*enums)[v].name); |
| 2183 | return LY_EVALID; |
| 2184 | } |
| 2185 | } |
| 2186 | } else if (base_enums) { |
| 2187 | /* inherit the assigned value */ |
| 2188 | e->value = base_enums[match].value; |
| 2189 | if (!u || e->value >= value) { |
| 2190 | value = e->value + 1; |
| 2191 | } |
| 2192 | } else { |
| 2193 | /* assign value automatically */ |
| 2194 | if (u && value == INT32_MIN) { |
| 2195 | /* counter overflow */ |
| 2196 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2197 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2198 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2199 | return LY_EVALID; |
| 2200 | } |
| 2201 | e->value = value++; |
| 2202 | } |
| 2203 | } else { /* LY_TYPE_BITS */ |
| 2204 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2205 | e->value = (int32_t)enums_p[u].value; |
| 2206 | if (!u || (uint32_t)e->value >= position) { |
| 2207 | position = (uint32_t)e->value + 1; |
| 2208 | } |
| 2209 | /* check collision with other values */ |
| 2210 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2211 | if (e->value == (*enums)[v].value) { |
| 2212 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2213 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2214 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2215 | return LY_EVALID; |
| 2216 | } |
| 2217 | } |
| 2218 | } else if (base_enums) { |
| 2219 | /* inherit the assigned value */ |
| 2220 | e->value = base_enums[match].value; |
| 2221 | if (!u || (uint32_t)e->value >= position) { |
| 2222 | position = (uint32_t)e->value + 1; |
| 2223 | } |
| 2224 | } else { |
| 2225 | /* assign value automatically */ |
| 2226 | if (u && position == 0) { |
| 2227 | /* counter overflow */ |
| 2228 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2229 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2230 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2231 | return LY_EVALID; |
| 2232 | } |
| 2233 | e->value = position++; |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | if (base_enums) { |
| 2238 | /* the assigned values must not change from the derived type */ |
| 2239 | if (e->value != base_enums[match].value) { |
| 2240 | if (basetype == LY_TYPE_ENUM) { |
| 2241 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2242 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2243 | e->name, base_enums[match].value, e->value); |
| 2244 | } else { |
| 2245 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2246 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2247 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2248 | } |
| 2249 | return LY_EVALID; |
| 2250 | } |
| 2251 | } |
| 2252 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2253 | 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] | 2254 | 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] | 2255 | |
| 2256 | if (basetype == LY_TYPE_BITS) { |
| 2257 | /* keep bits ordered by position */ |
| 2258 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2259 | if (v != u) { |
| 2260 | memcpy(&storage, e, sizeof *e); |
| 2261 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2262 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2263 | } |
| 2264 | } |
| 2265 | } |
| 2266 | |
| 2267 | done: |
| 2268 | return ret; |
| 2269 | } |
| 2270 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2271 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2272 | for ((NODE) = (NODE)->parent; \ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 2273 | (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] | 2274 | (NODE) = (NODE)->parent); \ |
| 2275 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2276 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2277 | TERM; \ |
| 2278 | } |
| 2279 | |
| 2280 | /** |
| 2281 | * @brief Validate the predicate(s) from the leafref path. |
| 2282 | * @param[in] ctx Compile context |
| 2283 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2284 | * 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] | 2285 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2286 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2287 | * @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] | 2288 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2289 | */ |
| 2290 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2291 | 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] | 2292 | 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] | 2293 | { |
| 2294 | LY_ERR ret = LY_EVALID; |
| 2295 | const struct lys_module *mod; |
| 2296 | const struct lysc_node *src_node, *dst_node; |
| 2297 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2298 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2299 | unsigned int dest_parent_times, c; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2300 | const char *start, *end, *pke_end; |
| 2301 | struct ly_set keys = {0}; |
| 2302 | int i; |
| 2303 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2304 | assert(path_context); |
| 2305 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2306 | while (**predicate == '[') { |
| 2307 | start = (*predicate)++; |
| 2308 | |
| 2309 | while (isspace(**predicate)) { |
| 2310 | ++(*predicate); |
| 2311 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2312 | 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] | 2313 | while (isspace(**predicate)) { |
| 2314 | ++(*predicate); |
| 2315 | } |
| 2316 | if (**predicate != '=') { |
| 2317 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2318 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2319 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2320 | goto cleanup; |
| 2321 | } |
| 2322 | ++(*predicate); |
| 2323 | while (isspace(**predicate)) { |
| 2324 | ++(*predicate); |
| 2325 | } |
| 2326 | |
| 2327 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2328 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2329 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2330 | goto cleanup; |
| 2331 | } |
| 2332 | --pke_end; |
| 2333 | while (isspace(*pke_end)) { |
| 2334 | --pke_end; |
| 2335 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2336 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2337 | /* localize path-key-expr */ |
| 2338 | pke_start = path_key_expr = *predicate; |
| 2339 | /* move after the current predicate */ |
| 2340 | *predicate = end + 1; |
| 2341 | |
| 2342 | /* source (must be leaf or leaf-list) */ |
| 2343 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2344 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2345 | if (!mod) { |
| 2346 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2347 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2348 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2349 | goto cleanup; |
| 2350 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2351 | if (!mod->implemented) { |
| 2352 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2353 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2354 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2355 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2356 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2357 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2358 | src_node = NULL; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2359 | if (!(context_node->flags & LYS_KEYLESS)) { |
| 2360 | struct lysc_node *key; |
| 2361 | 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] | 2362 | if (!ly_strncmp(key->name, src, src_len)) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2363 | src_node = key; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2364 | break; |
| 2365 | } |
| 2366 | } |
| 2367 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2368 | if (!src_node) { |
| 2369 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2370 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2371 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2372 | goto cleanup; |
| 2373 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2374 | |
| 2375 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2376 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2377 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2378 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2379 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2380 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2381 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2382 | *predicate - start, start, src_node->name); |
| 2383 | goto cleanup; |
| 2384 | } |
| 2385 | |
| 2386 | /* destination */ |
| 2387 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2388 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2389 | |
| 2390 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2391 | if (strncmp(path_key_expr, "current", 7)) { |
| 2392 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2393 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2394 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2395 | *predicate - start, start); |
| 2396 | goto cleanup; |
| 2397 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2398 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2399 | if (*path_key_expr != '(') { |
| 2400 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2401 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2402 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2403 | if (*path_key_expr != ')') { |
| 2404 | goto error_current_function_invocation; |
| 2405 | } |
| 2406 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2407 | |
| 2408 | if (*path_key_expr != '/') { |
| 2409 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2410 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2411 | *predicate - start, start); |
| 2412 | goto cleanup; |
| 2413 | } |
| 2414 | ++path_key_expr; |
| 2415 | while (isspace(*path_key_expr)) { |
| 2416 | ++path_key_expr; |
| 2417 | } |
| 2418 | |
| 2419 | /* rel-path-keyexpr: |
| 2420 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2421 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2422 | ++dest_parent_times; |
| 2423 | path_key_expr += 2; |
| 2424 | while (isspace(*path_key_expr)) { |
| 2425 | ++path_key_expr; |
| 2426 | } |
| 2427 | if (*path_key_expr != '/') { |
| 2428 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2429 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2430 | *predicate - start, start); |
| 2431 | goto cleanup; |
| 2432 | } |
| 2433 | ++path_key_expr; |
| 2434 | while (isspace(*path_key_expr)) { |
| 2435 | ++path_key_expr; |
| 2436 | } |
| 2437 | |
| 2438 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2439 | * all schema nodes that cannot be instantiated in data tree */ |
| 2440 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2441 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2442 | *predicate - start, start); |
| 2443 | } |
| 2444 | if (!dest_parent_times) { |
| 2445 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2446 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2447 | *predicate - start, start); |
| 2448 | goto cleanup; |
| 2449 | } |
| 2450 | if (path_key_expr == pke_end) { |
| 2451 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2452 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2453 | *predicate - start, start); |
| 2454 | goto cleanup; |
| 2455 | } |
| 2456 | |
| 2457 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2458 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2459 | 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] | 2460 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2461 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2462 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2463 | goto cleanup; |
| 2464 | } |
| 2465 | |
| 2466 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2467 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2468 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2469 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2470 | } |
| 2471 | if (!mod) { |
| 2472 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2473 | "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] | 2474 | *predicate - start, start, dst_len, dst); |
| 2475 | goto cleanup; |
| 2476 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2477 | if (!mod->implemented) { |
| 2478 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2479 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2480 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2481 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2482 | 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] | 2483 | if (!dst_node) { |
| 2484 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2485 | "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] | 2486 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2487 | goto cleanup; |
| 2488 | } |
| 2489 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2490 | 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] | 2491 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2492 | "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] | 2493 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2494 | goto cleanup; |
| 2495 | } |
| 2496 | } |
| 2497 | |
| 2498 | ret = LY_SUCCESS; |
| 2499 | cleanup: |
| 2500 | ly_set_erase(&keys, NULL); |
| 2501 | return ret; |
| 2502 | } |
| 2503 | |
| 2504 | /** |
| 2505 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2506 | * |
| 2507 | * path-arg = absolute-path / relative-path |
| 2508 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2509 | * relative-path = 1*(".." "/") descendant-path |
| 2510 | * |
| 2511 | * @param[in,out] path Path to parse. |
| 2512 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2513 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2514 | * @param[out] name Name of the token. |
| 2515 | * @param[out] nam_len Length of the name. |
| 2516 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2517 | * must not be changed between consecutive calls. -1 if the |
| 2518 | * path is absolute. |
| 2519 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2520 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2521 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2522 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2523 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2524 | int *parent_times, int *has_predicate) |
| 2525 | { |
| 2526 | int par_times = 0; |
| 2527 | |
| 2528 | assert(path && *path); |
| 2529 | assert(parent_times); |
| 2530 | assert(prefix); |
| 2531 | assert(prefix_len); |
| 2532 | assert(name); |
| 2533 | assert(name_len); |
| 2534 | assert(has_predicate); |
| 2535 | |
| 2536 | *prefix = NULL; |
| 2537 | *prefix_len = 0; |
| 2538 | *name = NULL; |
| 2539 | *name_len = 0; |
| 2540 | *has_predicate = 0; |
| 2541 | |
| 2542 | if (!*parent_times) { |
| 2543 | if (!strncmp(*path, "..", 2)) { |
| 2544 | *path += 2; |
| 2545 | ++par_times; |
| 2546 | while (!strncmp(*path, "/..", 3)) { |
| 2547 | *path += 3; |
| 2548 | ++par_times; |
| 2549 | } |
| 2550 | } |
| 2551 | if (par_times) { |
| 2552 | *parent_times = par_times; |
| 2553 | } else { |
| 2554 | *parent_times = -1; |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | if (**path != '/') { |
| 2559 | return LY_EINVAL; |
| 2560 | } |
| 2561 | /* skip '/' */ |
| 2562 | ++(*path); |
| 2563 | |
| 2564 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2565 | 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] | 2566 | |
| 2567 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2568 | /* path continues by another token or this is the last token */ |
| 2569 | return LY_SUCCESS; |
| 2570 | } else if ((*path)[0] != '[') { |
| 2571 | /* unexpected character */ |
| 2572 | return LY_EINVAL; |
| 2573 | } else { |
| 2574 | /* predicate starting with [ */ |
| 2575 | *has_predicate = 1; |
| 2576 | return LY_SUCCESS; |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2581 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2582 | * |
| 2583 | * The set of features used for target must be a subset of features used for the leafref. |
| 2584 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2585 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2586 | * |
| 2587 | * @param[in] refnode The leafref node. |
| 2588 | * @param[in] target Tha target node of the leafref. |
| 2589 | * @return LY_SUCCESS or LY_EVALID; |
| 2590 | */ |
| 2591 | static LY_ERR |
| 2592 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2593 | { |
| 2594 | LY_ERR ret = LY_EVALID; |
| 2595 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2596 | unsigned int u, v, count; |
| 2597 | struct ly_set features = {0}; |
| 2598 | |
| 2599 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2600 | if (iter->iffeatures) { |
| 2601 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2602 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2603 | 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] | 2604 | } |
| 2605 | } |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2610 | * So when adding features applicable to the target into the features set, we should not be |
| 2611 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2612 | * to the leafref itself. */ |
| 2613 | count = features.count; |
| 2614 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2615 | if (iter->iffeatures) { |
| 2616 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2617 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2618 | 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] | 2619 | /* new feature was added (or LY_EMEM) */ |
| 2620 | goto cleanup; |
| 2621 | } |
| 2622 | } |
| 2623 | } |
| 2624 | } |
| 2625 | } |
| 2626 | ret = LY_SUCCESS; |
| 2627 | |
| 2628 | cleanup: |
| 2629 | ly_set_erase(&features, NULL); |
| 2630 | return ret; |
| 2631 | } |
| 2632 | |
| 2633 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2634 | * @brief Validate the leafref path. |
| 2635 | * @param[in] ctx Compile context |
| 2636 | * @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] | 2637 | * @param[in] leafref Leafref to validate. |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2638 | * @param[out] target Optional resolved leafref target. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2639 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2640 | */ |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2641 | LY_ERR |
| 2642 | lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref, |
| 2643 | const struct lysc_node **target) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2644 | { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 2645 | const struct lysc_node *node = NULL, *parent = NULL; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2646 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2647 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2648 | const char *id, *prefix, *name; |
| 2649 | size_t prefix_len, name_len; |
| 2650 | int parent_times = 0, has_predicate; |
| 2651 | unsigned int iter, u; |
| 2652 | LY_ERR ret = LY_SUCCESS; |
| 2653 | |
| 2654 | assert(ctx); |
| 2655 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2656 | assert(leafref); |
| 2657 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2658 | ctx->path[0] = '\0'; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2659 | lysc_path(startnode, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2660 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2661 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2662 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2663 | id = leafref->path; |
Juraj Vijtiuk | bd0f2a6 | 2019-12-11 15:58:18 +0100 | [diff] [blame] | 2664 | |
| 2665 | if (!*id) { |
| 2666 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Empty leafref path."); |
| 2667 | return LY_EVALID; |
| 2668 | } |
| 2669 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2670 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2671 | if (!iter) { /* first iteration */ |
| 2672 | /* precess ".." in relative paths */ |
| 2673 | if (parent_times > 0) { |
| 2674 | /* move from the context node */ |
| 2675 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2676 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2677 | * all schema nodes that cannot be instantiated in data tree */ |
| 2678 | 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] | 2679 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2680 | } |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2685 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2686 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2687 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2688 | } |
| 2689 | if (!mod) { |
| 2690 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2691 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2692 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2693 | return LY_EVALID; |
| 2694 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2695 | if (!mod->implemented) { |
| 2696 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2697 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2698 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2699 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2700 | 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] | 2701 | if (!node) { |
| 2702 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2703 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2704 | return LY_EVALID; |
| 2705 | } |
| 2706 | parent = node; |
| 2707 | |
| 2708 | if (has_predicate) { |
| 2709 | /* we have predicate, so the current result must be list */ |
| 2710 | if (node->nodetype != LYS_LIST) { |
| 2711 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2712 | "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] | 2713 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2714 | return LY_EVALID; |
| 2715 | } |
| 2716 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2717 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2718 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | ++iter; |
| 2722 | } |
| 2723 | if (ret) { |
| 2724 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2725 | "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] | 2726 | return LY_EVALID; |
| 2727 | } |
| 2728 | |
| 2729 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2730 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2731 | "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] | 2732 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2733 | return LY_EVALID; |
| 2734 | } |
| 2735 | |
| 2736 | /* check status */ |
| 2737 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2738 | return LY_EVALID; |
| 2739 | } |
| 2740 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2741 | /* check config */ |
| 2742 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2743 | if (node->flags & LYS_CONFIG_R) { |
| 2744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2745 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2746 | leafref->path); |
| 2747 | return LY_EVALID; |
| 2748 | } |
| 2749 | } |
| 2750 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2751 | /* store the target's type and check for circular chain of leafrefs */ |
| 2752 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2753 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2754 | if (type == (struct lysc_type*)leafref) { |
| 2755 | /* circular chain detected */ |
| 2756 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2757 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2758 | return LY_EVALID; |
| 2759 | } |
| 2760 | } |
| 2761 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2762 | /* check if leafref and its target are under common if-features */ |
| 2763 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2764 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2765 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2766 | leafref->path); |
| 2767 | return LY_EVALID; |
| 2768 | } |
| 2769 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2770 | ctx->path_len = 1; |
| 2771 | ctx->path[1] = '\0'; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2772 | if (target) { |
| 2773 | *target = node; |
| 2774 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2775 | return LY_SUCCESS; |
| 2776 | } |
| 2777 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2778 | 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] | 2779 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2780 | /** |
| 2781 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2782 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2783 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2784 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2785 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2786 | * @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] | 2787 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2788 | * @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] | 2789 | * @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] | 2790 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2791 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2792 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2793 | * @return LY_ERR value. |
| 2794 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2795 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2796 | 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] | 2797 | 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] | 2798 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2799 | { |
| 2800 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2801 | unsigned int u, v, additional; |
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; |
| 2810 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2811 | |
| 2812 | switch (basetype) { |
| 2813 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2814 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2815 | |
| 2816 | /* 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] | 2817 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2818 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2819 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2820 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2821 | 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] | 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | if (tpdfname) { |
| 2826 | type_p->compiled = *type; |
| 2827 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2828 | } |
| 2829 | break; |
| 2830 | case LY_TYPE_BITS: |
| 2831 | /* RFC 7950 9.7 - bits */ |
| 2832 | bits = (struct lysc_type_bits*)(*type); |
| 2833 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2834 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2835 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2836 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2837 | } |
| 2838 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2839 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2840 | /* 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] | 2841 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2842 | 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] | 2843 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2844 | 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] | 2845 | free(*type); |
| 2846 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2847 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2848 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | if (tpdfname) { |
| 2852 | type_p->compiled = *type; |
| 2853 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2854 | } |
| 2855 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2856 | case LY_TYPE_DEC64: |
| 2857 | dec = (struct lysc_type_dec*)(*type); |
| 2858 | |
| 2859 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2860 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2861 | if (!type_p->fraction_digits) { |
| 2862 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2863 | 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] | 2864 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2865 | 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] | 2866 | free(*type); |
| 2867 | *type = NULL; |
| 2868 | } |
| 2869 | return LY_EVALID; |
| 2870 | } |
| 2871 | } else if (type_p->fraction_digits) { |
| 2872 | /* 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] | 2873 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2874 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2875 | "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] | 2876 | tpdfname); |
| 2877 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2878 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2879 | "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] | 2880 | free(*type); |
| 2881 | *type = NULL; |
| 2882 | } |
| 2883 | return LY_EVALID; |
| 2884 | } |
| 2885 | dec->fraction_digits = type_p->fraction_digits; |
| 2886 | |
| 2887 | /* RFC 7950 9.2.4 - range */ |
| 2888 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2889 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2890 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2891 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2892 | 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] | 2893 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | if (tpdfname) { |
| 2897 | type_p->compiled = *type; |
| 2898 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2899 | } |
| 2900 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2901 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2902 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2903 | |
| 2904 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2905 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2906 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2907 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2908 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2909 | 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] | 2910 | } |
| 2911 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2912 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2913 | } |
| 2914 | |
| 2915 | /* RFC 7950 9.4.5 - pattern */ |
| 2916 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2917 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2918 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2919 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2920 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2921 | } |
| 2922 | |
| 2923 | if (tpdfname) { |
| 2924 | type_p->compiled = *type; |
| 2925 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2926 | } |
| 2927 | break; |
| 2928 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2929 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2930 | |
| 2931 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2932 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2933 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2934 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2935 | } |
| 2936 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2937 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2938 | /* 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] | 2939 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2940 | 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] | 2941 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2942 | 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] | 2943 | free(*type); |
| 2944 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2945 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2946 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | if (tpdfname) { |
| 2950 | type_p->compiled = *type; |
| 2951 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2952 | } |
| 2953 | break; |
| 2954 | case LY_TYPE_INT8: |
| 2955 | case LY_TYPE_UINT8: |
| 2956 | case LY_TYPE_INT16: |
| 2957 | case LY_TYPE_UINT16: |
| 2958 | case LY_TYPE_INT32: |
| 2959 | case LY_TYPE_UINT32: |
| 2960 | case LY_TYPE_INT64: |
| 2961 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2962 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2963 | |
| 2964 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2965 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2966 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2967 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2968 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2969 | 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] | 2970 | } |
| 2971 | } |
| 2972 | |
| 2973 | if (tpdfname) { |
| 2974 | type_p->compiled = *type; |
| 2975 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2976 | } |
| 2977 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2978 | case LY_TYPE_IDENT: |
| 2979 | idref = (struct lysc_type_identityref*)(*type); |
| 2980 | |
| 2981 | /* RFC 7950 9.10.2 - base */ |
| 2982 | if (type_p->bases) { |
| 2983 | if (base) { |
| 2984 | /* only the directly derived identityrefs can contain base specification */ |
| 2985 | if (tpdfname) { |
| 2986 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2987 | "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] | 2988 | tpdfname); |
| 2989 | } else { |
| 2990 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2991 | "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] | 2992 | free(*type); |
| 2993 | *type = NULL; |
| 2994 | } |
| 2995 | return LY_EVALID; |
| 2996 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2997 | 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] | 2998 | } |
| 2999 | |
| 3000 | if (!base && !type_p->flags) { |
| 3001 | /* type derived from identityref built-in type must contain at least one base */ |
| 3002 | if (tpdfname) { |
| 3003 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 3004 | } else { |
| 3005 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 3006 | free(*type); |
| 3007 | *type = NULL; |
| 3008 | } |
| 3009 | return LY_EVALID; |
| 3010 | } |
| 3011 | |
| 3012 | if (tpdfname) { |
| 3013 | type_p->compiled = *type; |
| 3014 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3015 | } |
| 3016 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3017 | case LY_TYPE_LEAFREF: |
| 3018 | /* RFC 7950 9.9.3 - require-instance */ |
| 3019 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3020 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3021 | if (tpdfname) { |
| 3022 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3023 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 3024 | } else { |
| 3025 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3026 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 3027 | free(*type); |
| 3028 | *type = NULL; |
| 3029 | } |
| 3030 | return LY_EVALID; |
| 3031 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3032 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 3033 | } else if (base) { |
| 3034 | /* inherit */ |
| 3035 | ((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] | 3036 | } else { |
| 3037 | /* default is true */ |
| 3038 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 3039 | } |
| 3040 | if (type_p->path) { |
| 3041 | 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] | 3042 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3043 | } else if (base) { |
| 3044 | 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] | 3045 | ((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] | 3046 | } else if (tpdfname) { |
| 3047 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 3048 | return LY_EVALID; |
| 3049 | } else { |
| 3050 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 3051 | free(*type); |
| 3052 | *type = NULL; |
| 3053 | return LY_EVALID; |
| 3054 | } |
| 3055 | if (tpdfname) { |
| 3056 | type_p->compiled = *type; |
| 3057 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3058 | } |
| 3059 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 3060 | case LY_TYPE_INST: |
| 3061 | /* RFC 7950 9.9.3 - require-instance */ |
| 3062 | if (type_p->flags & LYS_SET_REQINST) { |
| 3063 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 3064 | } else { |
| 3065 | /* default is true */ |
| 3066 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 3067 | } |
| 3068 | |
| 3069 | if (tpdfname) { |
| 3070 | type_p->compiled = *type; |
| 3071 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3072 | } |
| 3073 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3074 | case LY_TYPE_UNION: |
| 3075 | un = (struct lysc_type_union*)(*type); |
| 3076 | |
| 3077 | /* RFC 7950 7.4 - type */ |
| 3078 | if (type_p->types) { |
| 3079 | if (base) { |
| 3080 | /* only the directly derived union can contain types specification */ |
| 3081 | if (tpdfname) { |
| 3082 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3083 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 3084 | tpdfname); |
| 3085 | } else { |
| 3086 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3087 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 3088 | free(*type); |
| 3089 | *type = NULL; |
| 3090 | } |
| 3091 | return LY_EVALID; |
| 3092 | } |
| 3093 | /* compile the type */ |
| 3094 | additional = 0; |
| 3095 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 3096 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3097 | 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] | 3098 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 3099 | /* add space for additional types from the union subtype */ |
| 3100 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 3101 | p = ly_realloc(((uint32_t*)(un->types) - 1), sizeof(uint32_t) + ((LY_ARRAY_SIZE(type_p->types) + additional + LY_ARRAY_SIZE(un_aux->types) - 1) * sizeof *(un->types))); |
| 3102 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3103 | un->types = (void*)((uint32_t*)(p) + 1); |
| 3104 | |
| 3105 | /* copy subtypes of the subtype union */ |
| 3106 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 3107 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 3108 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 3109 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3110 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3111 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 3112 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 3113 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 3114 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 3115 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 3116 | /* TODO extensions */ |
| 3117 | |
| 3118 | } else { |
| 3119 | un->types[u + additional] = un_aux->types[v]; |
| 3120 | ++un_aux->types[v]->refcount; |
| 3121 | } |
| 3122 | ++additional; |
| 3123 | LY_ARRAY_INCREMENT(un->types); |
| 3124 | } |
| 3125 | /* compensate u increment in main loop */ |
| 3126 | --additional; |
| 3127 | |
| 3128 | /* free the replaced union subtype */ |
| 3129 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 3130 | } else { |
| 3131 | LY_ARRAY_INCREMENT(un->types); |
| 3132 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3133 | } |
| 3134 | } |
| 3135 | |
| 3136 | if (!base && !type_p->flags) { |
| 3137 | /* type derived from union built-in type must contain at least one type */ |
| 3138 | if (tpdfname) { |
| 3139 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 3140 | } else { |
| 3141 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 3142 | free(*type); |
| 3143 | *type = NULL; |
| 3144 | } |
| 3145 | return LY_EVALID; |
| 3146 | } |
| 3147 | |
| 3148 | if (tpdfname) { |
| 3149 | type_p->compiled = *type; |
| 3150 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3151 | } |
| 3152 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3153 | case LY_TYPE_BOOL: |
| 3154 | case LY_TYPE_EMPTY: |
| 3155 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 3156 | break; |
| 3157 | } |
| 3158 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 3159 | done: |
| 3160 | return ret; |
| 3161 | } |
| 3162 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3163 | /** |
| 3164 | * @brief Compile information about the leaf/leaf-list's type. |
| 3165 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3166 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 3167 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3168 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3169 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 3170 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3171 | * @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] | 3172 | * @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] | 3173 | * @return LY_ERR value. |
| 3174 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3175 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3176 | 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] | 3177 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3178 | { |
| 3179 | LY_ERR ret = LY_SUCCESS; |
| 3180 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3181 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3182 | struct type_context { |
| 3183 | const struct lysp_tpdf *tpdf; |
| 3184 | struct lysp_node *node; |
| 3185 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3186 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3187 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3188 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3189 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3190 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3191 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3192 | |
| 3193 | (*type) = NULL; |
| 3194 | |
| 3195 | tctx = calloc(1, sizeof *tctx); |
| 3196 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3197 | 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] | 3198 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 3199 | ret == LY_SUCCESS; |
| 3200 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 3201 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 3202 | if (basetype) { |
| 3203 | break; |
| 3204 | } |
| 3205 | |
| 3206 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3207 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3208 | 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] | 3209 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3210 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3211 | if (units && !*units) { |
| 3212 | /* inherit units */ |
| 3213 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3214 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3215 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3216 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3217 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3218 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3219 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3220 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3221 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3222 | break; |
| 3223 | } |
| 3224 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3225 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3226 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3227 | * 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] | 3228 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3229 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3230 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3231 | dummyloops = 1; |
| 3232 | goto preparenext; |
| 3233 | } else { |
| 3234 | tctx = NULL; |
| 3235 | break; |
| 3236 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3237 | } |
| 3238 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3239 | /* circular typedef reference detection */ |
| 3240 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3241 | /* local part */ |
| 3242 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3243 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3244 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3245 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3246 | free(tctx); |
| 3247 | ret = LY_EVALID; |
| 3248 | goto cleanup; |
| 3249 | } |
| 3250 | } |
| 3251 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3252 | /* global part for unions corner case */ |
| 3253 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3254 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3255 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3256 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3257 | free(tctx); |
| 3258 | ret = LY_EVALID; |
| 3259 | goto cleanup; |
| 3260 | } |
| 3261 | } |
| 3262 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3263 | /* store information for the following processing */ |
| 3264 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3265 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3266 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3267 | /* prepare next loop */ |
| 3268 | tctx_prev = tctx; |
| 3269 | tctx = calloc(1, sizeof *tctx); |
| 3270 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3271 | } |
| 3272 | free(tctx); |
| 3273 | |
| 3274 | /* allocate type according to the basetype */ |
| 3275 | switch (basetype) { |
| 3276 | case LY_TYPE_BINARY: |
| 3277 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3278 | break; |
| 3279 | case LY_TYPE_BITS: |
| 3280 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3281 | break; |
| 3282 | case LY_TYPE_BOOL: |
| 3283 | case LY_TYPE_EMPTY: |
| 3284 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3285 | break; |
| 3286 | case LY_TYPE_DEC64: |
| 3287 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3288 | break; |
| 3289 | case LY_TYPE_ENUM: |
| 3290 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3291 | break; |
| 3292 | case LY_TYPE_IDENT: |
| 3293 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3294 | break; |
| 3295 | case LY_TYPE_INST: |
| 3296 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3297 | break; |
| 3298 | case LY_TYPE_LEAFREF: |
| 3299 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3300 | break; |
| 3301 | case LY_TYPE_STRING: |
| 3302 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3303 | break; |
| 3304 | case LY_TYPE_UNION: |
| 3305 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3306 | break; |
| 3307 | case LY_TYPE_INT8: |
| 3308 | case LY_TYPE_UINT8: |
| 3309 | case LY_TYPE_INT16: |
| 3310 | case LY_TYPE_UINT16: |
| 3311 | case LY_TYPE_INT32: |
| 3312 | case LY_TYPE_UINT32: |
| 3313 | case LY_TYPE_INT64: |
| 3314 | case LY_TYPE_UINT64: |
| 3315 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3316 | break; |
| 3317 | case LY_TYPE_UNKNOWN: |
| 3318 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3319 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3320 | ret = LY_EVALID; |
| 3321 | goto cleanup; |
| 3322 | } |
| 3323 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3324 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3325 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3326 | ly_data_type2str[basetype]); |
| 3327 | free(*type); |
| 3328 | (*type) = NULL; |
| 3329 | ret = LY_EVALID; |
| 3330 | goto cleanup; |
| 3331 | } |
| 3332 | |
| 3333 | /* get restrictions from the referred typedefs */ |
| 3334 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3335 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3336 | |
| 3337 | /* remember the typedef context for circular check */ |
| 3338 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3339 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3340 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3341 | base = tctx->tpdf->type.compiled; |
| 3342 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3343 | } 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] | 3344 | /* no change, just use the type information from the base */ |
| 3345 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3346 | ++base->refcount; |
| 3347 | continue; |
| 3348 | } |
| 3349 | |
| 3350 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3351 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3352 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3353 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3354 | ret = LY_EVALID; |
| 3355 | goto cleanup; |
| 3356 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3357 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3358 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3359 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3360 | ret = LY_EVALID; |
| 3361 | goto cleanup; |
| 3362 | } |
| 3363 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3364 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3365 | /* TODO user type plugins */ |
| 3366 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3367 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3368 | 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] | 3369 | 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] | 3370 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3371 | LY_CHECK_GOTO(ret, cleanup); |
| 3372 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3373 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3374 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3375 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3376 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3377 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3378 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3379 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3380 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3381 | /* TODO user type plugins */ |
| 3382 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3383 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3384 | 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] | 3385 | LY_CHECK_GOTO(ret, cleanup); |
| 3386 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3387 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3388 | free(*type); |
| 3389 | (*type) = base; |
| 3390 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3391 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3392 | if (dflt && !(*type)->dflt) { |
| 3393 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3394 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3395 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3396 | (*type)->dflt->realtype = (*type); |
| 3397 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3398 | 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] | 3399 | 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] | 3400 | if (err) { |
| 3401 | ly_err_print(err); |
| 3402 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3403 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3404 | ly_err_free(err); |
| 3405 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3406 | if (ret == LY_EINCOMPLETE) { |
| 3407 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3408 | 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] | 3409 | |
| 3410 | /* but in general result is so far ok */ |
| 3411 | ret = LY_SUCCESS; |
| 3412 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3413 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3414 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3415 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3416 | 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] | 3417 | |
| 3418 | cleanup: |
| 3419 | ly_set_erase(&tpdf_chain, free); |
| 3420 | return ret; |
| 3421 | } |
| 3422 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3423 | /** |
| 3424 | * @brief Compile status information of the given node. |
| 3425 | * |
| 3426 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3427 | * has the status correctly set during the compilation. |
| 3428 | * |
| 3429 | * @param[in] ctx Compile context |
| 3430 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3431 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3432 | * the compatibility with the parent's status value. |
| 3433 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3434 | * @return LY_ERR value. |
| 3435 | */ |
| 3436 | static LY_ERR |
| 3437 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3438 | { |
| 3439 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3440 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3441 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3442 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3443 | if ((parent_flags & 0x3) != 0x3) { |
| 3444 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3445 | * combination of bits (0x3) which marks the uses_status value */ |
| 3446 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3447 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3448 | } |
| 3449 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3450 | } else { |
| 3451 | (*node_flags) |= LYS_STATUS_CURR; |
| 3452 | } |
| 3453 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3454 | /* check status compatibility with the parent */ |
| 3455 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3456 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3457 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3458 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3459 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3460 | } else { /* LYS_STATUS_DEPRC */ |
| 3461 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3462 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3463 | } |
| 3464 | return LY_EVALID; |
| 3465 | } |
| 3466 | } |
| 3467 | return LY_SUCCESS; |
| 3468 | } |
| 3469 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3470 | /** |
| 3471 | * @brief Check uniqness of the node/action/notification name. |
| 3472 | * |
| 3473 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3474 | * structures, but they share the namespace so we need to check their name collisions. |
| 3475 | * |
| 3476 | * @param[in] ctx Compile context. |
| 3477 | * @param[in] children List (linked list) of data nodes to go through. |
| 3478 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3479 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3480 | * @param[in] name Name of the item to find in the given lists. |
| 3481 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3482 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3483 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3484 | */ |
| 3485 | static LY_ERR |
| 3486 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3487 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3488 | const char *name, void *exclude) |
| 3489 | { |
| 3490 | const struct lysc_node *iter; |
| 3491 | unsigned int u; |
| 3492 | |
| 3493 | LY_LIST_FOR(children, iter) { |
| 3494 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3495 | goto error; |
| 3496 | } |
| 3497 | } |
| 3498 | LY_ARRAY_FOR(actions, u) { |
| 3499 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3500 | goto error; |
| 3501 | } |
| 3502 | } |
| 3503 | LY_ARRAY_FOR(notifs, u) { |
| 3504 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3505 | goto error; |
| 3506 | } |
| 3507 | } |
| 3508 | return LY_SUCCESS; |
| 3509 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3510 | 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] | 3511 | return LY_EEXIST; |
| 3512 | } |
| 3513 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3514 | 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] | 3515 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3516 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3517 | * @brief Compile parsed RPC/action schema node information. |
| 3518 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3519 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3520 | * @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] | 3521 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3522 | * @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). |
| 3523 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3524 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3525 | */ |
| 3526 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3527 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3528 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3529 | { |
| 3530 | LY_ERR ret = LY_SUCCESS; |
| 3531 | struct lysp_node *child_p; |
| 3532 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3533 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3534 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3535 | lysc_update_path(ctx, parent, action_p->name); |
| 3536 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3537 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3538 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3539 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3540 | action_p->name, action)) { |
| 3541 | return LY_EVALID; |
| 3542 | } |
| 3543 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3544 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3545 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3546 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3547 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3548 | return LY_EVALID; |
| 3549 | } |
| 3550 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3551 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3552 | action->module = ctx->mod; |
| 3553 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3554 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3555 | action->sp = action_p; |
| 3556 | } |
| 3557 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3558 | |
| 3559 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3560 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 3561 | 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] | 3562 | |
| 3563 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3564 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3565 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3566 | 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] | 3567 | 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] | 3568 | |
| 3569 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3570 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3571 | 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] | 3572 | 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] | 3573 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3574 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3575 | 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] | 3576 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3577 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3578 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3579 | |
| 3580 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3581 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3582 | 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] | 3583 | 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] | 3584 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3585 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3586 | 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] | 3587 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3588 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3589 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3590 | |
| 3591 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3592 | /* do not check "must" semantics in a grouping */ |
| 3593 | ly_set_add(&ctx->unres, action, 0); |
| 3594 | } |
| 3595 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3596 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3597 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3598 | return ret; |
| 3599 | } |
| 3600 | |
| 3601 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3602 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3603 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3604 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3605 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3606 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3607 | * @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] | 3608 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3609 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3610 | */ |
| 3611 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3612 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3613 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3614 | { |
| 3615 | LY_ERR ret = LY_SUCCESS; |
| 3616 | struct lysp_node *child_p; |
| 3617 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3618 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3619 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3620 | lysc_update_path(ctx, parent, notif_p->name); |
| 3621 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3622 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3623 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3624 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3625 | notif_p->name, notif)) { |
| 3626 | return LY_EVALID; |
| 3627 | } |
| 3628 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3629 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3630 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3631 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3632 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3633 | return LY_EVALID; |
| 3634 | } |
| 3635 | |
| 3636 | notif->nodetype = LYS_NOTIF; |
| 3637 | notif->module = ctx->mod; |
| 3638 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3639 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3640 | notif->sp = notif_p; |
| 3641 | } |
| 3642 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3643 | |
| 3644 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3645 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3646 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3647 | |
| 3648 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3649 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3650 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3651 | 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] | 3652 | 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] | 3653 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3654 | /* do not check "must" semantics in a grouping */ |
| 3655 | ly_set_add(&ctx->unres, notif, 0); |
| 3656 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3657 | 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] | 3658 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3659 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3660 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3661 | 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] | 3662 | } |
| 3663 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3664 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3665 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3666 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3667 | return ret; |
| 3668 | } |
| 3669 | |
| 3670 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3671 | * @brief Compile parsed container node information. |
| 3672 | * @param[in] ctx Compile context |
| 3673 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3674 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3675 | * is enriched with the container-specific information. |
| 3676 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3677 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3678 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3679 | 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] | 3680 | { |
| 3681 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3682 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3683 | struct lysp_node *child_p; |
| 3684 | unsigned int u; |
| 3685 | LY_ERR ret = LY_SUCCESS; |
| 3686 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3687 | if (cont_p->presence) { |
| 3688 | cont->flags |= LYS_PRESENCE; |
| 3689 | } |
| 3690 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3691 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3692 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3693 | } |
| 3694 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3695 | 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] | 3696 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3697 | /* do not check "must" semantics in a grouping */ |
| 3698 | ly_set_add(&ctx->unres, cont, 0); |
| 3699 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3700 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3701 | 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] | 3702 | |
| 3703 | done: |
| 3704 | return ret; |
| 3705 | } |
| 3706 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3707 | /* |
| 3708 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3709 | * @param[in] ctx Compile context. |
| 3710 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3711 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3712 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3713 | * @return LY_ERR value. |
| 3714 | */ |
| 3715 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3716 | 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] | 3717 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3718 | unsigned int u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3719 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3720 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3721 | 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] | 3722 | leaf->units ? NULL : &leaf->units)); |
| 3723 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3724 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3725 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3726 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3727 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3728 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3729 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3730 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3731 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3732 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3733 | } |
| 3734 | } else { |
| 3735 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3736 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3737 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3738 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3739 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3740 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3741 | } |
| 3742 | } |
| 3743 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3744 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3745 | ly_set_add(&ctx->unres, leaf, 0); |
| 3746 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3747 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3748 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3749 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3750 | ly_set_add(&ctx->unres, leaf, 0); |
| 3751 | } |
| 3752 | } |
| 3753 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3754 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3755 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3756 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3757 | return LY_EVALID; |
| 3758 | } |
| 3759 | } |
| 3760 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3761 | return LY_SUCCESS; |
| 3762 | } |
| 3763 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3764 | /** |
| 3765 | * @brief Compile parsed leaf node information. |
| 3766 | * @param[in] ctx Compile context |
| 3767 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3768 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3769 | * is enriched with the leaf-specific information. |
| 3770 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3771 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3772 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3773 | 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] | 3774 | { |
| 3775 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3776 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3777 | unsigned int u; |
| 3778 | LY_ERR ret = LY_SUCCESS; |
| 3779 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3780 | 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] | 3781 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3782 | /* do not check "must" semantics in a grouping */ |
| 3783 | ly_set_add(&ctx->unres, leaf, 0); |
| 3784 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3785 | if (leaf_p->units) { |
| 3786 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3787 | leaf->flags |= LYS_SET_UNITS; |
| 3788 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3789 | |
| 3790 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3791 | leaf->dflt = (void*)leaf_p->dflt; |
| 3792 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3793 | if (ret) { |
| 3794 | leaf->dflt = NULL; |
| 3795 | return ret; |
| 3796 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3797 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3798 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3799 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3800 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3801 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3802 | leaf->dflt->realtype = leaf->type; |
| 3803 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3804 | 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] | 3805 | 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] | 3806 | leaf->dflt->realtype->refcount++; |
| 3807 | if (err) { |
| 3808 | ly_err_print(err); |
| 3809 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3810 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3811 | ly_err_free(err); |
| 3812 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3813 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3814 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3815 | 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] | 3816 | |
| 3817 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3818 | ret = LY_SUCCESS; |
| 3819 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3820 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3821 | leaf->flags |= LYS_SET_DFLT; |
| 3822 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3823 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3824 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3825 | done: |
| 3826 | return ret; |
| 3827 | } |
| 3828 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3829 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3830 | * @brief Compile parsed leaf-list node information. |
| 3831 | * @param[in] ctx Compile context |
| 3832 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3833 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3834 | * is enriched with the leaf-list-specific information. |
| 3835 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3836 | */ |
| 3837 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3838 | 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] | 3839 | { |
| 3840 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3841 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3842 | unsigned int u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3843 | LY_ERR ret = LY_SUCCESS; |
| 3844 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3845 | 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] | 3846 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3847 | /* do not check "must" semantics in a grouping */ |
| 3848 | ly_set_add(&ctx->unres, llist, 0); |
| 3849 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3850 | if (llist_p->units) { |
| 3851 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3852 | llist->flags |= LYS_SET_UNITS; |
| 3853 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3854 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3855 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3856 | llist->dflts = (void*)llist_p->dflts; |
| 3857 | 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] | 3858 | if (ret != LY_SUCCESS) { |
| 3859 | /* make sure the defaults are freed correctly */ |
| 3860 | if (llist_p->dflts) { |
| 3861 | llist->dflts = NULL; |
| 3862 | } |
| 3863 | return ret; |
| 3864 | } |
| 3865 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3866 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3867 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3868 | 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] | 3869 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3870 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3871 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3872 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3873 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3874 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3875 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3876 | llist->dflts[u]->realtype = llist->type; |
| 3877 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3878 | 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] | 3879 | 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] | 3880 | llist->dflts[u]->realtype->refcount++; |
| 3881 | if (err) { |
| 3882 | ly_err_print(err); |
| 3883 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3884 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3885 | ly_err_free(err); |
| 3886 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3887 | if (ret == LY_EINCOMPLETE) { |
| 3888 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3889 | 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] | 3890 | |
| 3891 | /* but in general result is so far ok */ |
| 3892 | ret = LY_SUCCESS; |
| 3893 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3894 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3895 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3896 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3897 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3898 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3899 | /* configuration data values must be unique - so check the default values */ |
| 3900 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3901 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3902 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3903 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3904 | 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] | 3905 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3906 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3907 | if (dynamic) { |
| 3908 | free((char*)val); |
| 3909 | } |
| 3910 | return LY_EVALID; |
| 3911 | } |
| 3912 | } |
| 3913 | } |
| 3914 | } |
| 3915 | |
| 3916 | /* 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] | 3917 | |
| 3918 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3919 | if (llist->min) { |
| 3920 | llist->flags |= LYS_MAND_TRUE; |
| 3921 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3922 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3923 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3924 | done: |
| 3925 | return ret; |
| 3926 | } |
| 3927 | |
| 3928 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3929 | * @brief Compile information about list's uniques. |
| 3930 | * @param[in] ctx Compile context. |
| 3931 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3932 | * @param[in] uniques Sized array list of unique statements. |
| 3933 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3934 | * @return LY_ERR value. |
| 3935 | */ |
| 3936 | static LY_ERR |
| 3937 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3938 | { |
| 3939 | LY_ERR ret = LY_SUCCESS; |
| 3940 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3941 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3942 | const char *keystr, *delim; |
| 3943 | size_t len; |
| 3944 | unsigned int v; |
| 3945 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3946 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3947 | |
| 3948 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3949 | config = -1; |
| 3950 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3951 | keystr = uniques[v]; |
| 3952 | while (keystr) { |
| 3953 | delim = strpbrk(keystr, " \t\n"); |
| 3954 | if (delim) { |
| 3955 | len = delim - keystr; |
| 3956 | while (isspace(*delim)) { |
| 3957 | ++delim; |
| 3958 | } |
| 3959 | } else { |
| 3960 | len = strlen(keystr); |
| 3961 | } |
| 3962 | |
| 3963 | /* unique node must be present */ |
| 3964 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3965 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3966 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3967 | if (ret != LY_SUCCESS) { |
| 3968 | if (ret == LY_EDENIED) { |
| 3969 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3970 | "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] | 3971 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3972 | } |
| 3973 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3974 | } else if (flags) { |
| 3975 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3976 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3977 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3978 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3979 | } |
| 3980 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3981 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3982 | /* all referenced leafs must be of the same config type */ |
| 3983 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3984 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3985 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3986 | return LY_EVALID; |
| 3987 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3988 | config = 1; |
| 3989 | } else { /* LYS_CONFIG_R */ |
| 3990 | config = 0; |
| 3991 | } |
| 3992 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3993 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3994 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3995 | if (parent->nodetype == LYS_LIST) { |
| 3996 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3997 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3998 | return LY_EVALID; |
| 3999 | } |
| 4000 | } |
| 4001 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4002 | /* check status */ |
| 4003 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 4004 | (*key)->flags, (*key)->module, (*key)->name)); |
| 4005 | |
| 4006 | /* mark leaf as unique */ |
| 4007 | (*key)->flags |= LYS_UNIQUE; |
| 4008 | |
| 4009 | /* next unique value in line */ |
| 4010 | keystr = delim; |
| 4011 | } |
| 4012 | /* next unique definition */ |
| 4013 | } |
| 4014 | |
| 4015 | return LY_SUCCESS; |
| 4016 | } |
| 4017 | |
| 4018 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4019 | * @brief Compile parsed list node information. |
| 4020 | * @param[in] ctx Compile context |
| 4021 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4022 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4023 | * is enriched with the list-specific information. |
| 4024 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4025 | */ |
| 4026 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4027 | 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] | 4028 | { |
| 4029 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 4030 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 4031 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4032 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4033 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4034 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4035 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4036 | LY_ERR ret = LY_SUCCESS; |
| 4037 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4038 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4039 | if (list->min) { |
| 4040 | list->flags |= LYS_MAND_TRUE; |
| 4041 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4042 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 4043 | |
| 4044 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4045 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4046 | } |
| 4047 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4048 | 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] | 4049 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4050 | /* do not check "must" semantics in a grouping */ |
| 4051 | ly_set_add(&ctx->unres, list, 0); |
| 4052 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4053 | |
| 4054 | /* keys */ |
| 4055 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 4056 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 4057 | return LY_EVALID; |
| 4058 | } |
| 4059 | |
| 4060 | /* find all the keys (must be direct children) */ |
| 4061 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4062 | if (!keystr) { |
| 4063 | /* keyless list */ |
| 4064 | list->flags |= LYS_KEYLESS; |
| 4065 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4066 | while (keystr) { |
| 4067 | delim = strpbrk(keystr, " \t\n"); |
| 4068 | if (delim) { |
| 4069 | len = delim - keystr; |
| 4070 | while (isspace(*delim)) { |
| 4071 | ++delim; |
| 4072 | } |
| 4073 | } else { |
| 4074 | len = strlen(keystr); |
| 4075 | } |
| 4076 | |
| 4077 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4078 | 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] | 4079 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4080 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4081 | "The list's key \"%.*s\" not found.", len, keystr); |
| 4082 | return LY_EVALID; |
| 4083 | } |
| 4084 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4085 | if (key->flags & LYS_KEY) { |
| 4086 | /* the node was already marked as a key */ |
| 4087 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4088 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 4089 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4090 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4091 | |
| 4092 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4093 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4094 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4095 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 4096 | return LY_EVALID; |
| 4097 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4098 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4099 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4100 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4101 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4102 | "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] | 4103 | return LY_EVALID; |
| 4104 | } |
| 4105 | } else { |
| 4106 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4107 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4108 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4109 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4110 | return LY_EVALID; |
| 4111 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4112 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4113 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4114 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4115 | return LY_EVALID; |
| 4116 | } |
| 4117 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4118 | |
| 4119 | /* check status */ |
| 4120 | 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] | 4121 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4122 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4123 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4124 | if (key->dflt) { |
| 4125 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 4126 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 4127 | free(key->dflt); |
| 4128 | key->dflt = NULL; |
| 4129 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4130 | } |
| 4131 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4132 | key->flags |= LYS_KEY; |
| 4133 | |
| 4134 | /* move it to the correct position */ |
| 4135 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 4136 | /* fix links in closest previous siblings of the key */ |
| 4137 | if (key->next) { |
| 4138 | key->next->prev = key->prev; |
| 4139 | } else { |
| 4140 | /* last child */ |
| 4141 | list->child->prev = key->prev; |
| 4142 | } |
| 4143 | if (key->prev->next) { |
| 4144 | key->prev->next = key->next; |
| 4145 | } |
| 4146 | /* fix links in the key */ |
| 4147 | if (prev_key) { |
| 4148 | key->prev = (struct lysc_node*)prev_key; |
| 4149 | key->next = prev_key->next; |
| 4150 | } else { |
| 4151 | key->prev = list->child->prev; |
| 4152 | key->next = list->child; |
| 4153 | } |
| 4154 | /* fix links in closes future siblings of the key */ |
| 4155 | if (prev_key) { |
| 4156 | if (prev_key->next) { |
| 4157 | prev_key->next->prev = (struct lysc_node*)key; |
| 4158 | } else { |
| 4159 | list->child->prev = (struct lysc_node*)key; |
| 4160 | } |
| 4161 | prev_key->next = (struct lysc_node*)key; |
| 4162 | } else { |
| 4163 | list->child->prev = (struct lysc_node*)key; |
| 4164 | } |
| 4165 | /* fix links in parent */ |
| 4166 | if (!key->prev->next) { |
| 4167 | list->child = (struct lysc_node*)key; |
| 4168 | } |
| 4169 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4170 | |
| 4171 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4172 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4173 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4174 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | /* uniques */ |
| 4178 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4179 | 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] | 4180 | } |
| 4181 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4182 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 4183 | 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] | 4184 | |
| 4185 | done: |
| 4186 | return ret; |
| 4187 | } |
| 4188 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4189 | /** |
| 4190 | * @brief Do some checks and set the default choice's case. |
| 4191 | * |
| 4192 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 4193 | * |
| 4194 | * @param[in] ctx Compile context. |
| 4195 | * @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, |
| 4196 | * not the reference to the imported module. |
| 4197 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 4198 | * @return LY_ERR value. |
| 4199 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4200 | static LY_ERR |
| 4201 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 4202 | { |
| 4203 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 4204 | const char *prefix = NULL, *name; |
| 4205 | size_t prefix_len = 0; |
| 4206 | |
| 4207 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4208 | name = strchr(dflt, ':'); |
| 4209 | if (name) { |
| 4210 | prefix = dflt; |
| 4211 | prefix_len = name - prefix; |
| 4212 | ++name; |
| 4213 | } else { |
| 4214 | name = dflt; |
| 4215 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 4216 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4217 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 4218 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4219 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 4220 | prefix_len, prefix); |
| 4221 | return LY_EVALID; |
| 4222 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4223 | 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] | 4224 | if (!ch->dflt) { |
| 4225 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4226 | "Default case \"%s\" not found.", dflt); |
| 4227 | return LY_EVALID; |
| 4228 | } |
| 4229 | /* no mandatory nodes directly under the default case */ |
| 4230 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4231 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 4232 | break; |
| 4233 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4234 | if (iter->flags & LYS_MAND_TRUE) { |
| 4235 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4236 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 4237 | return LY_EVALID; |
| 4238 | } |
| 4239 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4240 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4241 | return LY_SUCCESS; |
| 4242 | } |
| 4243 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4244 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4245 | 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] | 4246 | { |
| 4247 | struct lys_module *mod; |
| 4248 | const char *prefix = NULL, *name; |
| 4249 | size_t prefix_len = 0; |
| 4250 | struct lysc_node_case *cs; |
| 4251 | struct lysc_node *node; |
| 4252 | |
| 4253 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4254 | name = strchr(dflt, ':'); |
| 4255 | if (name) { |
| 4256 | prefix = dflt; |
| 4257 | prefix_len = name - prefix; |
| 4258 | ++name; |
| 4259 | } else { |
| 4260 | name = dflt; |
| 4261 | } |
| 4262 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 4263 | if (prefix) { |
| 4264 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 4265 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4266 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 4267 | "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] | 4268 | return LY_EVALID; |
| 4269 | } |
| 4270 | } else { |
| 4271 | mod = ctx->mod; |
| 4272 | } |
| 4273 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4274 | 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] | 4275 | if (!cs) { |
| 4276 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4277 | "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] | 4278 | return LY_EVALID; |
| 4279 | } |
| 4280 | |
| 4281 | /* check that there is no mandatory node */ |
| 4282 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4283 | if (node->parent != (struct lysc_node*)cs) { |
| 4284 | break; |
| 4285 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4286 | if (node->flags & LYS_MAND_TRUE) { |
| 4287 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4288 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4289 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4290 | return LY_EVALID; |
| 4291 | } |
| 4292 | } |
| 4293 | |
| 4294 | /* set the default case in choice */ |
| 4295 | ch->dflt = cs; |
| 4296 | cs->flags |= LYS_SET_DFLT; |
| 4297 | |
| 4298 | return LY_SUCCESS; |
| 4299 | } |
| 4300 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4301 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4302 | * @brief Compile parsed choice node information. |
| 4303 | * @param[in] ctx Compile context |
| 4304 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4305 | * @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] | 4306 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4307 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4308 | */ |
| 4309 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4310 | 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] | 4311 | { |
| 4312 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4313 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4314 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4315 | LY_ERR ret = LY_SUCCESS; |
| 4316 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4317 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4318 | if (child_p->nodetype == LYS_CASE) { |
| 4319 | 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] | 4320 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4321 | } |
| 4322 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4323 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4324 | } |
| 4325 | } |
| 4326 | |
| 4327 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4328 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4329 | 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] | 4330 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4331 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4332 | return ret; |
| 4333 | } |
| 4334 | |
| 4335 | /** |
| 4336 | * @brief Compile parsed anydata or anyxml node information. |
| 4337 | * @param[in] ctx Compile context |
| 4338 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4339 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4340 | * is enriched with the any-specific information. |
| 4341 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4342 | */ |
| 4343 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4344 | 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] | 4345 | { |
| 4346 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4347 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4348 | unsigned int u; |
| 4349 | LY_ERR ret = LY_SUCCESS; |
| 4350 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4351 | 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] | 4352 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4353 | /* do not check "must" semantics in a grouping */ |
| 4354 | ly_set_add(&ctx->unres, any, 0); |
| 4355 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4356 | |
| 4357 | if (any->flags & LYS_CONFIG_W) { |
| 4358 | 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] | 4359 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4360 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4361 | done: |
| 4362 | return ret; |
| 4363 | } |
| 4364 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4365 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4366 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4367 | * |
| 4368 | * @param[in] ctx Compile context |
| 4369 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4370 | * the choice itself is expected instead of a specific case node. |
| 4371 | * @param[in] node Schema node to connect into the list. |
| 4372 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 4373 | */ |
| 4374 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4375 | 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] | 4376 | { |
| 4377 | struct lysc_node **children; |
| 4378 | |
| 4379 | if (node->nodetype == LYS_CASE) { |
| 4380 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4381 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4382 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4383 | } |
| 4384 | if (children) { |
| 4385 | if (!(*children)) { |
| 4386 | /* first child */ |
| 4387 | *children = node; |
| 4388 | } else if (*children != node) { |
| 4389 | /* by the condition in previous branch we cover the choice/case children |
| 4390 | * - the children list is shared by the choice and the the first case, in addition |
| 4391 | * the first child of each case must be referenced from the case node. So the node is |
| 4392 | * actually always already inserted in case it is the first children - so here such |
| 4393 | * a situation actually corresponds to the first branch */ |
| 4394 | /* insert at the end of the parent's children list */ |
| 4395 | (*children)->prev->next = node; |
| 4396 | node->prev = (*children)->prev; |
| 4397 | (*children)->prev = node; |
| 4398 | |
| 4399 | /* check the name uniqueness */ |
| 4400 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4401 | lysc_node_notifs(parent), node->name, node)) { |
| 4402 | return LY_EEXIST; |
| 4403 | } |
| 4404 | } |
| 4405 | } |
| 4406 | return LY_SUCCESS; |
| 4407 | } |
| 4408 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4409 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4410 | * @brief Prepare the case structure in choice node for the new data node. |
| 4411 | * |
| 4412 | * 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 |
| 4413 | * created in the choice when the first child was processed. |
| 4414 | * |
| 4415 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4416 | * @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, |
| 4417 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4418 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4419 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4420 | * @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, |
| 4421 | * 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] | 4422 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4423 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4424 | 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] | 4425 | { |
| 4426 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4427 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4428 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4429 | unsigned int u; |
| 4430 | LY_ERR ret; |
| 4431 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4432 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4433 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4434 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4435 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4436 | return NULL; \ |
| 4437 | } \ |
| 4438 | } |
| 4439 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4440 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4441 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4442 | |
| 4443 | /* we have to add an implicit case node into the parent choice */ |
| 4444 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4445 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4446 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4447 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4448 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4449 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4450 | /* the case is already present since the child is not its first children */ |
| 4451 | return (struct lysc_node_case*)ch->cases->prev; |
| 4452 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4453 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4454 | |
| 4455 | /* explicit parent case is not present (this is its first child) */ |
| 4456 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4457 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4458 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4459 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4460 | cs->sp = node_p; |
| 4461 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4462 | /* 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] | 4463 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4464 | |
| 4465 | if (node_p->when) { |
| 4466 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4467 | 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] | 4468 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4469 | |
| 4470 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4471 | /* do not check "when" semantics in a grouping */ |
| 4472 | ly_set_add(&ctx->unres, cs, 0); |
| 4473 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4474 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4475 | 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] | 4476 | } else { |
| 4477 | LOGINT(ctx->ctx); |
| 4478 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4479 | } |
| 4480 | cs->module = ctx->mod; |
| 4481 | cs->prev = (struct lysc_node*)cs; |
| 4482 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4483 | 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] | 4484 | cs->child = child; |
| 4485 | |
| 4486 | return cs; |
| 4487 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4488 | if (cs) { |
| 4489 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4490 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4491 | return NULL; |
| 4492 | |
| 4493 | #undef UNIQUE_CHECK |
| 4494 | } |
| 4495 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4496 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4497 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4498 | * |
| 4499 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4500 | * @param[in] node Target node where the config is supposed to be changed. |
| 4501 | * @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] | 4502 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4503 | * 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] | 4504 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4505 | * @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] | 4506 | * @return LY_ERR value. |
| 4507 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4508 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4509 | 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] | 4510 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4511 | { |
| 4512 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4513 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4514 | |
| 4515 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4516 | /* nothing to do */ |
| 4517 | return LY_SUCCESS; |
| 4518 | } |
| 4519 | |
| 4520 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4521 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4522 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4523 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4524 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4525 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4526 | return LY_EVALID; |
| 4527 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4528 | node->flags |= LYS_SET_CONFIG; |
| 4529 | } else { |
| 4530 | if (node->flags & LYS_SET_CONFIG) { |
| 4531 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4532 | /* setting config flags, but have node with explicit config true */ |
| 4533 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4534 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4535 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4536 | return LY_EVALID; |
| 4537 | } |
| 4538 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4539 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4540 | return LY_SUCCESS; |
| 4541 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4542 | } |
| 4543 | node->flags &= ~LYS_CONFIG_MASK; |
| 4544 | node->flags |= config; |
| 4545 | |
| 4546 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4547 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4548 | 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] | 4549 | } |
| 4550 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4551 | return LY_SUCCESS; |
| 4552 | } |
| 4553 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4554 | /** |
| 4555 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4556 | * |
| 4557 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4558 | * the flag to such parents from a mandatory children. |
| 4559 | * |
| 4560 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4561 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4562 | * (mandatory children was removed). |
| 4563 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4564 | void |
| 4565 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4566 | { |
| 4567 | struct lysc_node *iter; |
| 4568 | |
| 4569 | if (add) { /* set flag */ |
| 4570 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4571 | parent = parent->parent) { |
| 4572 | parent->flags |= LYS_MAND_TRUE; |
| 4573 | } |
| 4574 | } else { /* unset flag */ |
| 4575 | 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] | 4576 | 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] | 4577 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4578 | /* there is another mandatory node */ |
| 4579 | return; |
| 4580 | } |
| 4581 | } |
| 4582 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4583 | parent->flags &= ~LYS_MAND_TRUE; |
| 4584 | } |
| 4585 | } |
| 4586 | } |
| 4587 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4588 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4589 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4590 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4591 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4592 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4593 | */ |
| 4594 | static void |
| 4595 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4596 | { |
| 4597 | unsigned int v; |
| 4598 | size_t len; |
| 4599 | |
| 4600 | len = strlen(aug_p->nodeid); |
| 4601 | LY_ARRAY_FOR(result, v) { |
| 4602 | if (strlen(result[v]->nodeid) <= len) { |
| 4603 | continue; |
| 4604 | } |
| 4605 | if (v < LY_ARRAY_SIZE(result)) { |
| 4606 | /* move the rest of array */ |
| 4607 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4608 | break; |
| 4609 | } |
| 4610 | } |
| 4611 | result[v] = aug_p; |
| 4612 | LY_ARRAY_INCREMENT(result); |
| 4613 | } |
| 4614 | |
| 4615 | /** |
| 4616 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4617 | * |
| 4618 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4619 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4620 | * |
| 4621 | * @param[in] ctx Compile context. |
| 4622 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4623 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4624 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4625 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4626 | * @return LY_ERR value. |
| 4627 | */ |
| 4628 | LY_ERR |
| 4629 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4630 | { |
| 4631 | struct lysp_augment **result = NULL; |
| 4632 | unsigned int u, v; |
| 4633 | size_t count = 0; |
| 4634 | |
| 4635 | assert(augments); |
| 4636 | |
| 4637 | /* get count of the augments in module and all its submodules */ |
| 4638 | if (aug_p) { |
| 4639 | count += LY_ARRAY_SIZE(aug_p); |
| 4640 | } |
| 4641 | LY_ARRAY_FOR(inc_p, u) { |
| 4642 | if (inc_p[u].submodule->augments) { |
| 4643 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4644 | } |
| 4645 | } |
| 4646 | |
| 4647 | if (!count) { |
| 4648 | *augments = NULL; |
| 4649 | return LY_SUCCESS; |
| 4650 | } |
| 4651 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4652 | |
| 4653 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4654 | * together, so there can be even /z/y betwwen them. */ |
| 4655 | LY_ARRAY_FOR(aug_p, u) { |
| 4656 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4657 | } |
| 4658 | LY_ARRAY_FOR(inc_p, u) { |
| 4659 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4660 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4661 | } |
| 4662 | } |
| 4663 | |
| 4664 | *augments = result; |
| 4665 | return LY_SUCCESS; |
| 4666 | } |
| 4667 | |
| 4668 | /** |
| 4669 | * @brief Compile the parsed augment connecting it into its target. |
| 4670 | * |
| 4671 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4672 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4673 | * are already implemented and compiled. |
| 4674 | * |
| 4675 | * @param[in] ctx Compile context. |
| 4676 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4677 | * @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 |
| 4678 | * children in case of the augmenting uses data. |
| 4679 | * @return LY_SUCCESS on success. |
| 4680 | * @return LY_EVALID on failure. |
| 4681 | */ |
| 4682 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4683 | 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] | 4684 | { |
| 4685 | LY_ERR ret = LY_SUCCESS; |
| 4686 | struct lysp_node *node_p, *case_node_p; |
| 4687 | struct lysc_node *target; /* target target of the augment */ |
| 4688 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4689 | struct lysc_when **when, *when_shared; |
| 4690 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4691 | uint16_t flags = 0; |
| 4692 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4693 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4694 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4695 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4696 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4697 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4698 | 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] | 4699 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4700 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4701 | if (ret != LY_SUCCESS) { |
| 4702 | if (ret == LY_EDENIED) { |
| 4703 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4704 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4705 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4706 | } |
| 4707 | return LY_EVALID; |
| 4708 | } |
| 4709 | |
| 4710 | /* check for mandatory nodes |
| 4711 | * - new cases augmenting some choice can have mandatory nodes |
| 4712 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4713 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4714 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4715 | allow_mandatory = 1; |
| 4716 | } |
| 4717 | |
| 4718 | when_shared = NULL; |
| 4719 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4720 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4721 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4722 | && !((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] | 4723 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4724 | && !(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] | 4725 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4726 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4727 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4728 | return LY_EVALID; |
| 4729 | } |
| 4730 | |
| 4731 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4732 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4733 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4734 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4735 | } else { |
| 4736 | 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] | 4737 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4738 | } |
| 4739 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4740 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4741 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4742 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4743 | * 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] | 4744 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4745 | /* 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] | 4746 | 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] | 4747 | } else if (target->nodetype == LYS_CHOICE) { |
| 4748 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4749 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4750 | } else { |
| 4751 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4752 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4753 | if (!node) { |
| 4754 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4755 | break; |
| 4756 | } |
| 4757 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4758 | } |
| 4759 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4760 | 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] | 4761 | node->flags &= ~LYS_MAND_TRUE; |
| 4762 | lys_compile_mandatory_parents(target, 0); |
| 4763 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4764 | "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] | 4765 | return LY_EVALID; |
| 4766 | } |
| 4767 | |
| 4768 | /* pass augment's when to all the children */ |
| 4769 | if (aug_p->when) { |
| 4770 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4771 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4772 | 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] | 4773 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4774 | |
| 4775 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4776 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4777 | ly_set_add(&ctx->unres, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4778 | } |
| 4779 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4780 | when_shared = *when; |
| 4781 | } else { |
| 4782 | ++when_shared->refcount; |
| 4783 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4784 | |
| 4785 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4786 | /* in this case check "when" again for all children because of dummy node check */ |
| 4787 | ly_set_add(&ctx->unres, node, 0); |
| 4788 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4789 | } |
| 4790 | } |
| 4791 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4792 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4793 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4794 | switch (target->nodetype) { |
| 4795 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4796 | 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] | 4797 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4798 | 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] | 4799 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4800 | break; |
| 4801 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4802 | 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] | 4803 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4804 | 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] | 4805 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4806 | break; |
| 4807 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4808 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4809 | if (aug_p->actions) { |
| 4810 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4811 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4812 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4813 | return LY_EVALID; |
| 4814 | } |
| 4815 | if (aug_p->notifs) { |
| 4816 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4817 | "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] | 4818 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4819 | return LY_EVALID; |
| 4820 | } |
| 4821 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4822 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4823 | lysc_update_path(ctx, NULL, NULL); |
| 4824 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4825 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4826 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4827 | return ret; |
| 4828 | } |
| 4829 | |
| 4830 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4831 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4832 | * |
| 4833 | * @param[in] ctx Compile context. |
| 4834 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4835 | * @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] | 4836 | * @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] | 4837 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4838 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4839 | * 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] | 4840 | * @return LY_ERR value. |
| 4841 | */ |
| 4842 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4843 | 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] | 4844 | { |
| 4845 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4846 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4847 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4848 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4849 | return LY_EVALID; |
| 4850 | } |
| 4851 | |
| 4852 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4853 | /* check if node has default value */ |
| 4854 | if (node->nodetype & LYS_LEAF) { |
| 4855 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4856 | if (refine_flag) { |
| 4857 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4858 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4859 | return LY_EVALID; |
| 4860 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4861 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4862 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4863 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4864 | |
| 4865 | /* update the list of incomplete default values if needed */ |
| 4866 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4867 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4868 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4869 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4870 | free(leaf->dflt); |
| 4871 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4872 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4873 | } |
| 4874 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4875 | if (refine_flag) { |
| 4876 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4877 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4878 | return LY_EVALID; |
| 4879 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4880 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4881 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4882 | 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] | 4883 | return LY_EVALID; |
| 4884 | } |
| 4885 | |
| 4886 | node->flags &= ~LYS_MAND_FALSE; |
| 4887 | node->flags |= LYS_MAND_TRUE; |
| 4888 | lys_compile_mandatory_parents(node->parent, 1); |
| 4889 | } else { |
| 4890 | /* make mandatory false */ |
| 4891 | node->flags &= ~LYS_MAND_TRUE; |
| 4892 | node->flags |= LYS_MAND_FALSE; |
| 4893 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4894 | 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] | 4895 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4896 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4897 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4898 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4899 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4900 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4901 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4902 | } |
| 4903 | } |
| 4904 | return LY_SUCCESS; |
| 4905 | } |
| 4906 | |
| 4907 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4908 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4909 | * If present, also apply uses's modificators. |
| 4910 | * |
| 4911 | * @param[in] ctx Compile context |
| 4912 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4913 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4914 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4915 | * the compile context. |
| 4916 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4917 | */ |
| 4918 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4919 | 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] | 4920 | { |
| 4921 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4922 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4923 | /* 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] | 4924 | struct lysc_node_container context_node_fake = |
| 4925 | {.nodetype = LYS_CONTAINER, |
| 4926 | .module = ctx->mod, |
| 4927 | .flags = parent ? parent->flags : 0, |
| 4928 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4929 | .prev = (struct lysc_node*)&context_node_fake, |
| 4930 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4931 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4932 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4933 | int found; |
| 4934 | const char *id, *name, *prefix; |
| 4935 | size_t prefix_len, name_len; |
| 4936 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4937 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4938 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4939 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4940 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4941 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4942 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4943 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4944 | unsigned int actions_index, notifs_index; |
| 4945 | struct lysc_notif **notifs = NULL; |
| 4946 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4947 | |
| 4948 | /* search for the grouping definition */ |
| 4949 | found = 0; |
| 4950 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4951 | 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] | 4952 | if (prefix) { |
| 4953 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4954 | if (!mod) { |
| 4955 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4956 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4957 | return LY_EVALID; |
| 4958 | } |
| 4959 | } else { |
| 4960 | mod = ctx->mod_def; |
| 4961 | } |
| 4962 | if (mod == ctx->mod_def) { |
| 4963 | 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] | 4964 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4965 | LY_ARRAY_FOR(grp, u) { |
| 4966 | if (!strcmp(grp[u].name, name)) { |
| 4967 | grp = &grp[u]; |
| 4968 | found = 1; |
| 4969 | break; |
| 4970 | } |
| 4971 | } |
| 4972 | } |
| 4973 | } |
| 4974 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4975 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4976 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4977 | if (grp) { |
| 4978 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4979 | if (!strcmp(grp[u].name, name)) { |
| 4980 | grp = &grp[u]; |
| 4981 | found = 1; |
| 4982 | } |
| 4983 | } |
| 4984 | } |
| 4985 | if (!found && mod->parsed->includes) { |
| 4986 | /* ... and all the submodules */ |
| 4987 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4988 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4989 | if (grp) { |
| 4990 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4991 | if (!strcmp(grp[v].name, name)) { |
| 4992 | grp = &grp[v]; |
| 4993 | found = 1; |
| 4994 | } |
| 4995 | } |
| 4996 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4997 | } |
| 4998 | } |
| 4999 | } |
| 5000 | if (!found) { |
| 5001 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5002 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 5003 | return LY_EVALID; |
| 5004 | } |
| 5005 | |
| 5006 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 5007 | grp_stack_count = ctx->groupings.count; |
| 5008 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 5009 | if (grp_stack_count == ctx->groupings.count) { |
| 5010 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 5011 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5012 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 5013 | return LY_EVALID; |
| 5014 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5015 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5016 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 5017 | grp->flags |= LYS_USED_GRP; |
| 5018 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5019 | |
| 5020 | /* switch context's mod_def */ |
| 5021 | mod_old = ctx->mod_def; |
| 5022 | ctx->mod_def = mod; |
| 5023 | |
| 5024 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5025 | 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] | 5026 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5027 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5028 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5029 | /* 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] | 5030 | 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] | 5031 | |
| 5032 | /* some preparation for applying refines */ |
| 5033 | if (grp->data == node_p) { |
| 5034 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 5035 | if (parent) { |
| 5036 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 5037 | } else if (ctx->mod->compiled->data) { |
| 5038 | child = ctx->mod->compiled->data; |
| 5039 | } else { |
| 5040 | child = NULL; |
| 5041 | } |
| 5042 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5043 | } |
| 5044 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5045 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5046 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5047 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5048 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5049 | /* 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] | 5050 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5051 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5052 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5053 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 5054 | |
| 5055 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5056 | /* do not check "when" semantics in a grouping */ |
| 5057 | ly_set_add(&ctx->unres, child, 0); |
| 5058 | } |
| 5059 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5060 | when_shared = *when; |
| 5061 | } else { |
| 5062 | ++when_shared->refcount; |
| 5063 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5064 | |
| 5065 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5066 | /* in this case check "when" again for all children because of dummy node check */ |
| 5067 | ly_set_add(&ctx->unres, child, 0); |
| 5068 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5069 | } |
| 5070 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5071 | } |
| 5072 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5073 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5074 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5075 | /* 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] | 5076 | 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] | 5077 | } |
| 5078 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5079 | /* compile actions */ |
| 5080 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 5081 | if (actions) { |
| 5082 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5083 | 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] | 5084 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 5085 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 5086 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 5087 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 5088 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5089 | } |
| 5090 | } |
| 5091 | |
| 5092 | /* compile notifications */ |
| 5093 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 5094 | if (notifs) { |
| 5095 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5096 | 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] | 5097 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 5098 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 5099 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 5100 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 5101 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5102 | } |
| 5103 | } |
| 5104 | |
| 5105 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5106 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5107 | 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] | 5108 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5109 | 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] | 5110 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5111 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5112 | /* reload previous context's mod_def */ |
| 5113 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5114 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5115 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5116 | /* apply refine */ |
| 5117 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5118 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 5119 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5120 | 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] | 5121 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5122 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5123 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5124 | |
| 5125 | /* default value */ |
| 5126 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5127 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5128 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5129 | "Invalid refine of default - %s cannot hold %d default values.", |
| 5130 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5131 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5132 | } |
| 5133 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 5134 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5135 | "Invalid refine of default - %s cannot hold default value(s).", |
| 5136 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5137 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5138 | } |
| 5139 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5140 | struct ly_err_item *err = NULL; |
| 5141 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 5142 | if (leaf->dflt) { |
| 5143 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5144 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5145 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5146 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5147 | } else { |
| 5148 | /* prepare a new one */ |
| 5149 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 5150 | leaf->dflt->realtype = leaf->type; |
| 5151 | } |
| 5152 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5153 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5154 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 5155 | 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] | 5156 | 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] | 5157 | leaf->dflt->realtype->refcount++; |
| 5158 | if (err) { |
| 5159 | ly_err_print(err); |
| 5160 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5161 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 5162 | ly_err_free(err); |
| 5163 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5164 | if (rc == LY_EINCOMPLETE) { |
| 5165 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5166 | 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] | 5167 | |
| 5168 | /* but in general result is so far ok */ |
| 5169 | rc = LY_SUCCESS; |
| 5170 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5171 | LY_CHECK_GOTO(rc, cleanup); |
| 5172 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5173 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5174 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 5175 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5176 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5177 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5178 | "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] | 5179 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5180 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5181 | |
| 5182 | /* remove previous set of default values */ |
| 5183 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5184 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5185 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 5186 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 5187 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5188 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5189 | LY_ARRAY_FREE(llist->dflts); |
| 5190 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5191 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5192 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5193 | |
| 5194 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5195 | 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] | 5196 | 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] | 5197 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5198 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5199 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5200 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5201 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5202 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 5203 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5204 | 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] | 5205 | 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] | 5206 | 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] | 5207 | llist->dflts[u]->realtype->refcount++; |
| 5208 | if (err) { |
| 5209 | ly_err_print(err); |
| 5210 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5211 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 5212 | ly_err_free(err); |
| 5213 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5214 | if (rc == LY_EINCOMPLETE) { |
| 5215 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5216 | 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] | 5217 | |
| 5218 | /* but in general result is so far ok */ |
| 5219 | rc = LY_SUCCESS; |
| 5220 | } |
| 5221 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5222 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5223 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5224 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5225 | if (((struct lysc_node_choice*)node)->dflt) { |
| 5226 | /* unset LYS_SET_DFLT from the current default case */ |
| 5227 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 5228 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5229 | 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] | 5230 | } |
| 5231 | } |
| 5232 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5233 | /* description */ |
| 5234 | if (rfn->dsc) { |
| 5235 | FREE_STRING(ctx->ctx, node->dsc); |
| 5236 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 5237 | } |
| 5238 | |
| 5239 | /* reference */ |
| 5240 | if (rfn->ref) { |
| 5241 | FREE_STRING(ctx->ctx, node->ref); |
| 5242 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 5243 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5244 | |
| 5245 | /* config */ |
| 5246 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5247 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5248 | 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] | 5249 | } else { |
| 5250 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5251 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5252 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5253 | } |
| 5254 | |
| 5255 | /* mandatory */ |
| 5256 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5257 | 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] | 5258 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5259 | |
| 5260 | /* presence */ |
| 5261 | if (rfn->presence) { |
| 5262 | if (node->nodetype != LYS_CONTAINER) { |
| 5263 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5264 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5265 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5266 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5267 | } |
| 5268 | node->flags |= LYS_PRESENCE; |
| 5269 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5270 | |
| 5271 | /* must */ |
| 5272 | if (rfn->musts) { |
| 5273 | switch (node->nodetype) { |
| 5274 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5275 | 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] | 5276 | break; |
| 5277 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5278 | 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] | 5279 | break; |
| 5280 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5281 | 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] | 5282 | break; |
| 5283 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5284 | 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] | 5285 | break; |
| 5286 | case LYS_ANYXML: |
| 5287 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5288 | 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] | 5289 | break; |
| 5290 | default: |
| 5291 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5292 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5293 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5294 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5295 | } |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 5296 | ly_set_add(&ctx->unres, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5297 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5298 | |
| 5299 | /* min/max-elements */ |
| 5300 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5301 | switch (node->nodetype) { |
| 5302 | case LYS_LEAFLIST: |
| 5303 | if (rfn->flags & LYS_SET_MAX) { |
| 5304 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5305 | } |
| 5306 | if (rfn->flags & LYS_SET_MIN) { |
| 5307 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5308 | if (rfn->min) { |
| 5309 | node->flags |= LYS_MAND_TRUE; |
| 5310 | lys_compile_mandatory_parents(node->parent, 1); |
| 5311 | } else { |
| 5312 | node->flags &= ~LYS_MAND_TRUE; |
| 5313 | lys_compile_mandatory_parents(node->parent, 0); |
| 5314 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5315 | } |
| 5316 | break; |
| 5317 | case LYS_LIST: |
| 5318 | if (rfn->flags & LYS_SET_MAX) { |
| 5319 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5320 | } |
| 5321 | if (rfn->flags & LYS_SET_MIN) { |
| 5322 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5323 | if (rfn->min) { |
| 5324 | node->flags |= LYS_MAND_TRUE; |
| 5325 | lys_compile_mandatory_parents(node->parent, 1); |
| 5326 | } else { |
| 5327 | node->flags &= ~LYS_MAND_TRUE; |
| 5328 | lys_compile_mandatory_parents(node->parent, 0); |
| 5329 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5330 | } |
| 5331 | break; |
| 5332 | default: |
| 5333 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5334 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5335 | (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] | 5336 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5337 | } |
| 5338 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5339 | |
| 5340 | /* if-feature */ |
| 5341 | if (rfn->iffeatures) { |
| 5342 | /* 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] | 5343 | 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] | 5344 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5345 | |
| 5346 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5347 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5348 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5349 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 5350 | for (u = 0; u < refined.count; ++u) { |
| 5351 | node = (struct lysc_node*)refined.objs[u]; |
| 5352 | rfn = &uses_p->refines[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5353 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5354 | |
| 5355 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5356 | if ((node->flags & LYS_MAND_TRUE) && |
| 5357 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5358 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5359 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5360 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5361 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5362 | } |
| 5363 | |
| 5364 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5365 | if (node->nodetype == LYS_LIST) { |
| 5366 | min = ((struct lysc_node_list*)node)->min; |
| 5367 | max = ((struct lysc_node_list*)node)->max; |
| 5368 | } else { |
| 5369 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5370 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5371 | } |
| 5372 | if (min > max) { |
| 5373 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5374 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5375 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5376 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5377 | } |
| 5378 | } |
| 5379 | } |
| 5380 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5381 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5382 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5383 | |
| 5384 | cleanup: |
| 5385 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5386 | if (context_node_fake.child) { |
| 5387 | context_node_fake.child->prev = child; |
| 5388 | } |
| 5389 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5390 | child->parent = parent; |
| 5391 | } |
| 5392 | |
| 5393 | if (uses_p->augments || uses_p->refines) { |
| 5394 | /* 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] | 5395 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5396 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5397 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5398 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5399 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5400 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5401 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5402 | } |
| 5403 | } |
| 5404 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5405 | /* reload previous context's mod_def */ |
| 5406 | ctx->mod_def = mod_old; |
| 5407 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5408 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5409 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5410 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5411 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5412 | |
| 5413 | return ret; |
| 5414 | } |
| 5415 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5416 | static int |
| 5417 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5418 | { |
| 5419 | struct lysp_node *iter; |
| 5420 | int len = 0; |
| 5421 | |
| 5422 | *path = NULL; |
| 5423 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5424 | char *s = *path; |
| 5425 | char *id; |
| 5426 | |
| 5427 | switch (iter->nodetype) { |
| 5428 | case LYS_USES: |
| 5429 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5430 | break; |
| 5431 | case LYS_GROUPING: |
| 5432 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5433 | break; |
| 5434 | case LYS_AUGMENT: |
| 5435 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5436 | break; |
| 5437 | default: |
| 5438 | id = strdup(iter->name); |
| 5439 | break; |
| 5440 | } |
| 5441 | |
| 5442 | if (!iter->parent) { |
| 5443 | /* print prefix */ |
| 5444 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5445 | } else { |
| 5446 | /* prefix is the same as in parent */ |
| 5447 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5448 | } |
| 5449 | free(s); |
| 5450 | free(id); |
| 5451 | } |
| 5452 | |
| 5453 | if (len < 0) { |
| 5454 | free(*path); |
| 5455 | *path = NULL; |
| 5456 | } else if (len == 0) { |
| 5457 | *path = strdup("/"); |
| 5458 | len = 1; |
| 5459 | } |
| 5460 | return len; |
| 5461 | } |
| 5462 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5463 | /** |
| 5464 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5465 | * |
| 5466 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5467 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5468 | */ |
| 5469 | static LY_ERR |
| 5470 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5471 | { |
| 5472 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5473 | char *path; |
| 5474 | int len; |
| 5475 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5476 | struct lysp_node_uses fake_uses = { |
| 5477 | .parent = node_p, |
| 5478 | .nodetype = LYS_USES, |
| 5479 | .flags = 0, .next = NULL, |
| 5480 | .name = grp->name, |
| 5481 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5482 | .refines = NULL, .augments = NULL |
| 5483 | }; |
| 5484 | struct lysc_node_container fake_container = { |
| 5485 | .nodetype = LYS_CONTAINER, |
| 5486 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5487 | .module = ctx->mod, |
| 5488 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5489 | .prev = (struct lysc_node*)&fake_container, |
| 5490 | .name = "fake", |
| 5491 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5492 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5493 | }; |
| 5494 | |
| 5495 | if (grp->parent) { |
| 5496 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5497 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5498 | |
| 5499 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5500 | if (len < 0) { |
| 5501 | LOGMEM(ctx->ctx); |
| 5502 | return LY_EMEM; |
| 5503 | } |
| 5504 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5505 | ctx->path_len = (uint16_t)len; |
| 5506 | free(path); |
| 5507 | |
| 5508 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5509 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5510 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5511 | lysc_update_path(ctx, NULL, NULL); |
| 5512 | lysc_update_path(ctx, NULL, NULL); |
| 5513 | |
| 5514 | ctx->path_len = 1; |
| 5515 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5516 | |
| 5517 | /* cleanup */ |
| 5518 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5519 | |
| 5520 | return ret; |
| 5521 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5522 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5523 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5524 | * @brief Compile parsed schema node information. |
| 5525 | * @param[in] ctx Compile context |
| 5526 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5527 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5528 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5529 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5530 | * @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). |
| 5531 | * 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] | 5532 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5533 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5534 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5535 | 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] | 5536 | { |
| 5537 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5538 | struct lysc_node *node; |
| 5539 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5540 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5541 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5542 | 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] | 5543 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5544 | if (node_p->nodetype != LYS_USES) { |
| 5545 | lysc_update_path(ctx, parent, node_p->name); |
| 5546 | } else { |
| 5547 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5548 | lysc_update_path(ctx, NULL, node_p->name); |
| 5549 | } |
| 5550 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5551 | switch (node_p->nodetype) { |
| 5552 | case LYS_CONTAINER: |
| 5553 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5554 | node_compile_spec = lys_compile_node_container; |
| 5555 | break; |
| 5556 | case LYS_LEAF: |
| 5557 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5558 | node_compile_spec = lys_compile_node_leaf; |
| 5559 | break; |
| 5560 | case LYS_LIST: |
| 5561 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5562 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5563 | break; |
| 5564 | case LYS_LEAFLIST: |
| 5565 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5566 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5567 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5568 | case LYS_CHOICE: |
| 5569 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5570 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5571 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5572 | case LYS_ANYXML: |
| 5573 | case LYS_ANYDATA: |
| 5574 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5575 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5576 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5577 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5578 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5579 | lysc_update_path(ctx, NULL, NULL); |
| 5580 | lysc_update_path(ctx, NULL, NULL); |
| 5581 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5582 | default: |
| 5583 | LOGINT(ctx->ctx); |
| 5584 | return LY_EINT; |
| 5585 | } |
| 5586 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5587 | node->nodetype = node_p->nodetype; |
| 5588 | node->module = ctx->mod; |
| 5589 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5590 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5591 | |
| 5592 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5593 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5594 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5595 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5596 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5597 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5598 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5599 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5600 | node->flags |= LYS_CONFIG_R; |
| 5601 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5602 | /* config not explicitely set, inherit it from parent */ |
| 5603 | if (parent) { |
| 5604 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5605 | } else { |
| 5606 | /* default is config true */ |
| 5607 | node->flags |= LYS_CONFIG_W; |
| 5608 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5609 | } else { |
| 5610 | /* config set explicitely */ |
| 5611 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5612 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5613 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5614 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5615 | "Configuration node cannot be child of any state data node."); |
| 5616 | goto error; |
| 5617 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5618 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5619 | /* *list ordering */ |
| 5620 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5621 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5622 | 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] | 5623 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5624 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5625 | node->flags &= ~LYS_ORDBY_MASK; |
| 5626 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5627 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5628 | /* default ordering is system */ |
| 5629 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5630 | } |
| 5631 | } |
| 5632 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5633 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5634 | * 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] | 5635 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5636 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5637 | * the parent is choice (parent here) or some case (so we have to get its flags to check) */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5638 | LY_CHECK_GOTO(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] | 5639 | } |
| 5640 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5641 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5642 | node->sp = node_p; |
| 5643 | } |
| 5644 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5645 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5646 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5647 | if (node_p->when) { |
| 5648 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5649 | ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5650 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5651 | |
| 5652 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5653 | /* do not check "when" semantics in a grouping */ |
| 5654 | ly_set_add(&ctx->unres, node, 0); |
| 5655 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5656 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5657 | 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] | 5658 | |
| 5659 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5660 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5661 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5662 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5663 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5664 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5665 | if (node->flags & LYS_MAND_TRUE) { |
| 5666 | lys_compile_mandatory_parents(parent, 1); |
| 5667 | } |
| 5668 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5669 | lysc_update_path(ctx, NULL, NULL); |
| 5670 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5671 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5672 | if (parent) { |
| 5673 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5674 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5675 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5676 | } else { |
| 5677 | lysc_update_path(ctx, parent, node->name); |
| 5678 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5679 | 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] | 5680 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5681 | if (uses_status) { |
| 5682 | |
| 5683 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5684 | /* 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] | 5685 | * it directly gets the same status flags as the choice; |
| 5686 | * uses_status cannot be applied here since uses cannot be child statement of choice */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5687 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5688 | node->parent = (struct lysc_node*)cs; |
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 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5691 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5692 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5693 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5694 | 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] | 5695 | |
| 5696 | if (parent->nodetype == LYS_CHOICE) { |
| 5697 | lysc_update_path(ctx, NULL, NULL); |
| 5698 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5699 | } else { |
| 5700 | /* top-level element */ |
| 5701 | if (!ctx->mod->compiled->data) { |
| 5702 | ctx->mod->compiled->data = node; |
| 5703 | } else { |
| 5704 | /* insert at the end of the module's top-level nodes list */ |
| 5705 | ctx->mod->compiled->data->prev->next = node; |
| 5706 | node->prev = ctx->mod->compiled->data->prev; |
| 5707 | ctx->mod->compiled->data->prev = node; |
| 5708 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5709 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5710 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5711 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5712 | return LY_EVALID; |
| 5713 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5714 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5715 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5716 | |
| 5717 | return LY_SUCCESS; |
| 5718 | |
| 5719 | error: |
| 5720 | lysc_node_free(ctx->ctx, node); |
| 5721 | return ret; |
| 5722 | } |
| 5723 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5724 | static void |
| 5725 | lysc_disconnect(struct lysc_node *node) |
| 5726 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5727 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5728 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5729 | int remove_cs = 0; |
| 5730 | |
| 5731 | parent = node->parent; |
| 5732 | |
| 5733 | /* parent's first child */ |
| 5734 | if (!parent) { |
| 5735 | return; |
| 5736 | } |
| 5737 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5738 | cs = (struct lysc_node_case*)node; |
| 5739 | } else if (parent->nodetype == LYS_CASE) { |
| 5740 | /* disconnecting some node in a case */ |
| 5741 | cs = (struct lysc_node_case*)parent; |
| 5742 | parent = cs->parent; |
| 5743 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5744 | if (child == node) { |
| 5745 | if (cs->child == child) { |
| 5746 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5747 | /* case with a single child -> remove also the case */ |
| 5748 | child->parent = NULL; |
| 5749 | remove_cs = 1; |
| 5750 | } else { |
| 5751 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5752 | } |
| 5753 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5754 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5755 | } |
| 5756 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5757 | if (!remove_cs) { |
| 5758 | cs = NULL; |
| 5759 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5760 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5761 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | if (cs) { |
| 5765 | if (remove_cs) { |
| 5766 | /* cs has only one child which is being also removed */ |
| 5767 | lysc_disconnect((struct lysc_node*)cs); |
| 5768 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5769 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5770 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5771 | /* default case removed */ |
| 5772 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5773 | } |
| 5774 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5775 | /* first case removed */ |
| 5776 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5777 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5778 | if (cs->child) { |
| 5779 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5780 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5781 | prev = cs->child->prev; |
| 5782 | } /* else all the children are under a single case */ |
| 5783 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5784 | if (child->parent != (struct lysc_node*)cs) { |
| 5785 | break; |
| 5786 | } |
| 5787 | lysc_node_free(node->module->ctx, child); |
| 5788 | } |
| 5789 | if (prev) { |
| 5790 | if (prev->next) { |
| 5791 | prev->next = child; |
| 5792 | } |
| 5793 | if (child) { |
| 5794 | child->prev = prev; |
| 5795 | } else { |
| 5796 | /* link from the first child under the cases */ |
| 5797 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5798 | } |
| 5799 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5800 | } |
| 5801 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5802 | } |
| 5803 | |
| 5804 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5805 | if (node->prev->next) { |
| 5806 | node->prev->next = node->next; |
| 5807 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5808 | if (node->next) { |
| 5809 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5810 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5811 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5812 | if (child) { |
| 5813 | child->prev = node->prev; |
| 5814 | } |
| 5815 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5816 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5817 | } |
| 5818 | } |
| 5819 | |
| 5820 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5821 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5822 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5823 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5824 | struct ly_set devs_p = {0}; |
| 5825 | struct ly_set targets = {0}; |
| 5826 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5827 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5828 | struct lysc_action *rpcs; |
| 5829 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5830 | struct lysp_deviation *dev; |
| 5831 | struct lysp_deviate *d, **dp_new; |
| 5832 | struct lysp_deviate_add *d_add; |
| 5833 | struct lysp_deviate_del *d_del; |
| 5834 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5835 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5836 | struct lysc_deviation { |
| 5837 | const char *nodeid; |
| 5838 | struct lysc_node *target; /* target node of the deviation */ |
| 5839 | 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] | 5840 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5841 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5842 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5843 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5844 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5845 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5846 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5847 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5848 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5849 | |
| 5850 | /* get all deviations from the module and all its submodules ... */ |
| 5851 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5852 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5853 | } |
| 5854 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5855 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5856 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5857 | } |
| 5858 | } |
| 5859 | if (!devs_p.count) { |
| 5860 | /* nothing to do */ |
| 5861 | return LY_SUCCESS; |
| 5862 | } |
| 5863 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5864 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5865 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5866 | /* ... and group them by the target node */ |
| 5867 | devs = calloc(devs_p.count, sizeof *devs); |
| 5868 | for (u = 0; u < devs_p.count; ++u) { |
| 5869 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5870 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5871 | |
| 5872 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5873 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5874 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5875 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5876 | /* move the target pointer to input/output to make them different from the action and |
| 5877 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5878 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5879 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5880 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5881 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5882 | flags |= LYSC_OPT_INTERNAL; |
| 5883 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5884 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5885 | flags |= LYSC_OPT_INTERNAL; |
| 5886 | } |
| 5887 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5888 | /* insert into the set of targets with duplicity detection */ |
| 5889 | i = ly_set_add(&targets, target, 0); |
| 5890 | if (!devs[i]) { |
| 5891 | /* new record */ |
| 5892 | devs[i] = calloc(1, sizeof **devs); |
| 5893 | devs[i]->target = target; |
| 5894 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5895 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5896 | } |
| 5897 | /* add deviates into the deviation's list of deviates */ |
| 5898 | for (d = dev->deviates; d; d = d->next) { |
| 5899 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5900 | *dp_new = d; |
| 5901 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5902 | devs[i]->not_supported = 1; |
| 5903 | } |
| 5904 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5905 | |
| 5906 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5907 | } |
| 5908 | |
| 5909 | /* MACROS for deviates checking */ |
| 5910 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5911 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5912 | 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] | 5913 | goto cleanup; \ |
| 5914 | } |
| 5915 | |
| 5916 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5917 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5918 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \ |
| 5919 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5920 | goto cleanup; \ |
| 5921 | } |
| 5922 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5923 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5924 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5925 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5926 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5927 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5928 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5929 | goto cleanup; \ |
| 5930 | } |
| 5931 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5932 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5933 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5934 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5935 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5936 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5937 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5938 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5939 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5940 | goto cleanup; \ |
| 5941 | } |
| 5942 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5943 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5944 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5945 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5946 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5947 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5948 | goto cleanup; \ |
| 5949 | } |
| 5950 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5951 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5952 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5953 | 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] | 5954 | goto cleanup; \ |
| 5955 | } |
| 5956 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5957 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5958 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5959 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5960 | "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] | 5961 | goto cleanup; \ |
| 5962 | } |
| 5963 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5964 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5965 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5966 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5967 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5968 | 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] | 5969 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5970 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5971 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5972 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5973 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5974 | goto cleanup; \ |
| 5975 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5976 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5977 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5978 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5979 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5980 | (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] | 5981 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5982 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5983 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5984 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5985 | } |
| 5986 | |
| 5987 | /* apply deviations */ |
| 5988 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5989 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5990 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5991 | struct ly_err_item *err = NULL; |
| 5992 | |
| 5993 | dflt = NULL; |
| 5994 | changed_type = 0; |
| 5995 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5996 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5997 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5998 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5999 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 6000 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6001 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 6002 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6003 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 6004 | } |
| 6005 | } |
| 6006 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6007 | /* not-supported */ |
| 6008 | if (devs[u]->not_supported) { |
| 6009 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 6010 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 6011 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 6012 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6013 | |
| 6014 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6015 | if (devs[u]->target->parent) { \ |
| 6016 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 6017 | } else { \ |
| 6018 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 6019 | } \ |
| 6020 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6021 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 6022 | } \ |
| 6023 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 6024 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6025 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6026 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6027 | } |
| 6028 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6029 | if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6030 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6031 | /* remove RPC's/action's input */ |
| 6032 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 6033 | 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] | 6034 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 6035 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6036 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6037 | /* remove RPC's/action's output */ |
| 6038 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 6039 | 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] | 6040 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 6041 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6042 | } else { |
| 6043 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6044 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6045 | } |
| 6046 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6047 | /* remove Notification */ |
| 6048 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6049 | } else { |
| 6050 | /* remove the target node */ |
| 6051 | lysc_disconnect(devs[u]->target); |
| 6052 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 6053 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6054 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6055 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6056 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6057 | continue; |
| 6058 | } |
| 6059 | |
| 6060 | /* list of deviates (not-supported is not present in the list) */ |
| 6061 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 6062 | d = devs[u]->deviates[v]; |
| 6063 | |
| 6064 | switch (d->mod) { |
| 6065 | case LYS_DEV_ADD: |
| 6066 | d_add = (struct lysp_deviate_add*)d; |
| 6067 | /* [units-stmt] */ |
| 6068 | if (d_add->units) { |
| 6069 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 6070 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 6071 | |
| 6072 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6073 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6074 | } |
| 6075 | |
| 6076 | /* *must-stmt */ |
| 6077 | if (d_add->musts) { |
| 6078 | switch (devs[u]->target->nodetype) { |
| 6079 | case LYS_CONTAINER: |
| 6080 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6081 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 6082 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6083 | break; |
| 6084 | case LYS_LEAF: |
| 6085 | case LYS_LEAFLIST: |
| 6086 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6087 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 6088 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6089 | break; |
| 6090 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6091 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 6092 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6093 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6094 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6095 | case LYS_ACTION: |
| 6096 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6097 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 6098 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6099 | break; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 6100 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6101 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 6102 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6103 | break; |
| 6104 | } |
| 6105 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6106 | default: |
| 6107 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6108 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6109 | goto cleanup; |
| 6110 | } |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6111 | ly_set_add(&ctx->unres, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6112 | } |
| 6113 | |
| 6114 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6115 | if (d_add->uniques) { |
| 6116 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 6117 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 6118 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6119 | |
| 6120 | /* *default-stmt */ |
| 6121 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6122 | switch (devs[u]->target->nodetype) { |
| 6123 | case LYS_LEAF: |
| 6124 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6125 | 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] | 6126 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6127 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6128 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6129 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6130 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6131 | } else { |
| 6132 | /* prepare new default value storage */ |
| 6133 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6134 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6135 | dflt = d_add->dflts[0]; |
| 6136 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 6137 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6138 | /* mark the new default values as leaf's own */ |
| 6139 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6140 | break; |
| 6141 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6142 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6143 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6144 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6145 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6146 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6147 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6148 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6149 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6150 | LY_ARRAY_FREE(llist->dflts); |
| 6151 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6152 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6153 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6154 | } |
| 6155 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6156 | 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] | 6157 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 6158 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6159 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6160 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 6161 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6162 | LY_ARRAY_INCREMENT(llist->dflts); |
| 6163 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 6164 | llist->dflts[x]->realtype = llist->type; |
| 6165 | 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] | 6166 | 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] | 6167 | (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] | 6168 | llist->dflts[x]->realtype->refcount++; |
| 6169 | if (err) { |
| 6170 | ly_err_print(err); |
| 6171 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6172 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 6173 | d_add->dflts[x - y], err->msg); |
| 6174 | ly_err_free(err); |
| 6175 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6176 | if (rc == LY_EINCOMPLETE) { |
| 6177 | /* postpone default compilation when the tree is complete */ |
| 6178 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6179 | |
| 6180 | /* but in general result is so far ok */ |
| 6181 | rc = LY_SUCCESS; |
| 6182 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6183 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6184 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6185 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6186 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 6187 | break; |
| 6188 | case LYS_CHOICE: |
| 6189 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 6190 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 6191 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 6192 | * 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] | 6193 | 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] | 6194 | goto cleanup; |
| 6195 | } |
| 6196 | break; |
| 6197 | default: |
| 6198 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6199 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6200 | goto cleanup; |
| 6201 | } |
| 6202 | } |
| 6203 | |
| 6204 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6205 | if (d_add->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6206 | 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] | 6207 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6208 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 6209 | goto cleanup; |
| 6210 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6211 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6212 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 6213 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6214 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6215 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 6216 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6217 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 6218 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6219 | goto cleanup; |
| 6220 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6221 | 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] | 6222 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6223 | |
| 6224 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6225 | if (d_add->flags & LYS_MAND_MASK) { |
| 6226 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 6227 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6228 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 6229 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6230 | goto cleanup; |
| 6231 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6232 | 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] | 6233 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6234 | |
| 6235 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6236 | if (d_add->flags & LYS_SET_MIN) { |
| 6237 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6238 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6239 | /* change value */ |
| 6240 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 6241 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6242 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6243 | /* change value */ |
| 6244 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 6245 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6246 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6247 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 6248 | goto cleanup; |
| 6249 | } |
| 6250 | if (d_add->min) { |
| 6251 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6252 | } |
| 6253 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6254 | |
| 6255 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6256 | if (d_add->flags & LYS_SET_MAX) { |
| 6257 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6258 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6259 | /* change value */ |
| 6260 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6261 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6262 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6263 | /* change value */ |
| 6264 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6265 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6266 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6267 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6268 | goto cleanup; |
| 6269 | } |
| 6270 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6271 | |
| 6272 | break; |
| 6273 | case LYS_DEV_DELETE: |
| 6274 | d_del = (struct lysp_deviate_del*)d; |
| 6275 | |
| 6276 | /* [units-stmt] */ |
| 6277 | if (d_del->units) { |
| 6278 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6279 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6280 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6281 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6282 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6283 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6284 | goto cleanup; |
| 6285 | } |
| 6286 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6287 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6288 | } |
| 6289 | |
| 6290 | /* *must-stmt */ |
| 6291 | if (d_del->musts) { |
| 6292 | switch (devs[u]->target->nodetype) { |
| 6293 | case LYS_CONTAINER: |
| 6294 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6295 | 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] | 6296 | break; |
| 6297 | case LYS_LEAF: |
| 6298 | case LYS_LEAFLIST: |
| 6299 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6300 | 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] | 6301 | break; |
| 6302 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6303 | 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] | 6304 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6305 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6306 | case LYS_ACTION: |
| 6307 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6308 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6309 | break; |
| 6310 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6311 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6312 | break; |
| 6313 | } |
| 6314 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6315 | default: |
| 6316 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6317 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6318 | goto cleanup; |
| 6319 | } |
| 6320 | } |
| 6321 | |
| 6322 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6323 | if (d_del->uniques) { |
| 6324 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6325 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6326 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6327 | LY_ARRAY_FOR(list->uniques, z) { |
| 6328 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6329 | nodeid = strpbrk(name, " \t\n"); |
| 6330 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 6331 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6332 | break; |
| 6333 | } |
| 6334 | while (isspace(*nodeid)) { |
| 6335 | ++nodeid; |
| 6336 | } |
| 6337 | } else { |
| 6338 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6339 | break; |
| 6340 | } |
| 6341 | } |
| 6342 | } |
| 6343 | if (!name) { |
| 6344 | /* complete match - remove the unique */ |
| 6345 | LY_ARRAY_DECREMENT(list->uniques); |
| 6346 | LY_ARRAY_FREE(list->uniques[z]); |
| 6347 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6348 | --z; |
| 6349 | break; |
| 6350 | } |
| 6351 | } |
| 6352 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6353 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6354 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6355 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6356 | goto cleanup; |
| 6357 | } |
| 6358 | } |
| 6359 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6360 | LY_ARRAY_FREE(list->uniques); |
| 6361 | list->uniques = NULL; |
| 6362 | } |
| 6363 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6364 | |
| 6365 | /* *default-stmt */ |
| 6366 | if (d_del->dflts) { |
| 6367 | switch (devs[u]->target->nodetype) { |
| 6368 | case LYS_LEAF: |
| 6369 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6370 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6371 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6372 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6373 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6374 | 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] | 6375 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6376 | if (i) { |
| 6377 | free((char*)dflt); |
| 6378 | } |
| 6379 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6380 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6381 | d_del->dflts[0], dflt); |
| 6382 | goto cleanup; |
| 6383 | } |
| 6384 | if (i) { |
| 6385 | free((char*)dflt); |
| 6386 | } |
| 6387 | dflt = NULL; |
| 6388 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6389 | /* update the list of incomplete default values if needed */ |
| 6390 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6391 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6392 | /* remove the default specification */ |
| 6393 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6394 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6395 | free(leaf->dflt); |
| 6396 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6397 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6398 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6399 | break; |
| 6400 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6401 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6402 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6403 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6404 | 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] | 6405 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6406 | if (i) { |
| 6407 | free((char*)dflt); |
| 6408 | } |
| 6409 | dflt = NULL; |
| 6410 | break; |
| 6411 | } |
| 6412 | if (i) { |
| 6413 | free((char*)dflt); |
| 6414 | } |
| 6415 | dflt = NULL; |
| 6416 | } |
| 6417 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6418 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6419 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6420 | goto cleanup; |
| 6421 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6422 | |
| 6423 | /* update the list of incomplete default values if needed */ |
| 6424 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6425 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6426 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6427 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6428 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6429 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6430 | free(llist->dflts[y]); |
| 6431 | 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] | 6432 | 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] | 6433 | } |
| 6434 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6435 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6436 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6437 | LY_ARRAY_FREE(llist->dflts); |
| 6438 | llist->dflts = NULL; |
| 6439 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6440 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6441 | break; |
| 6442 | case LYS_CHOICE: |
| 6443 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6444 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6445 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6446 | 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] | 6447 | if (prefix) { |
| 6448 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6449 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6450 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6451 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6452 | "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] | 6453 | goto cleanup; |
| 6454 | } |
| 6455 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6456 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6457 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6458 | "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] | 6459 | goto cleanup; |
| 6460 | } |
| 6461 | } |
| 6462 | /* else { |
| 6463 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6464 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6465 | * the module of the default case, just matching its name */ |
| 6466 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6467 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6468 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6469 | 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] | 6470 | goto cleanup; |
| 6471 | } |
| 6472 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6473 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6474 | break; |
| 6475 | default: |
| 6476 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6477 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6478 | goto cleanup; |
| 6479 | } |
| 6480 | } |
| 6481 | |
| 6482 | break; |
| 6483 | case LYS_DEV_REPLACE: |
| 6484 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6485 | |
| 6486 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6487 | if (d_rpl->type) { |
| 6488 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6489 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6490 | 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] | 6491 | |
| 6492 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6493 | /* the target has default from the previous type - remove it */ |
| 6494 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6495 | /* update the list of incomplete default values if needed */ |
| 6496 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6497 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6498 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6499 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6500 | free(leaf->dflt); |
| 6501 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6502 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6503 | } else { /* LYS_LEAFLIST */ |
| 6504 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6505 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6506 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6507 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6508 | free(llist->dflts[x]); |
| 6509 | } |
| 6510 | LY_ARRAY_FREE(llist->dflts); |
| 6511 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6512 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6513 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6514 | } |
| 6515 | } |
| 6516 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6517 | /* there is no default value, do not set changed_type after type compilation |
| 6518 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6519 | changed_type = -1; |
| 6520 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6521 | 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] | 6522 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6523 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6524 | |
| 6525 | /* [units-stmt] */ |
| 6526 | if (d_rpl->units) { |
| 6527 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6528 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6529 | units, "replacing", "units", d_rpl->units); |
| 6530 | |
| 6531 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6532 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6533 | } |
| 6534 | |
| 6535 | /* [default-stmt] */ |
| 6536 | if (d_rpl->dflt) { |
| 6537 | switch (devs[u]->target->nodetype) { |
| 6538 | case LYS_LEAF: |
| 6539 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6540 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6541 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6542 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6543 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6544 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6545 | dflt = d_rpl->dflt; |
| 6546 | /* 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] | 6547 | break; |
| 6548 | case LYS_CHOICE: |
| 6549 | 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] | 6550 | 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] | 6551 | goto cleanup; |
| 6552 | } |
| 6553 | break; |
| 6554 | default: |
| 6555 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6556 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6557 | goto cleanup; |
| 6558 | } |
| 6559 | } |
| 6560 | |
| 6561 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6562 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6563 | 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] | 6564 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6565 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6566 | goto cleanup; |
| 6567 | } |
| 6568 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6569 | 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] | 6570 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6571 | goto cleanup; |
| 6572 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6573 | 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] | 6574 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6575 | |
| 6576 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6577 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6578 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6579 | 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] | 6580 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6581 | goto cleanup; |
| 6582 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6583 | 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] | 6584 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6585 | |
| 6586 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6587 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6588 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6589 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6590 | /* change value */ |
| 6591 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6592 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6593 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6594 | /* change value */ |
| 6595 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6596 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6597 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6598 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6599 | goto cleanup; |
| 6600 | } |
| 6601 | if (d_rpl->min) { |
| 6602 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6603 | } |
| 6604 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6605 | |
| 6606 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6607 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6608 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6609 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6610 | /* change value */ |
| 6611 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6612 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6613 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6614 | /* change value */ |
| 6615 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6616 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6617 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6618 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6619 | goto cleanup; |
| 6620 | } |
| 6621 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6622 | |
| 6623 | break; |
| 6624 | default: |
| 6625 | LOGINT(ctx->ctx); |
| 6626 | goto cleanup; |
| 6627 | } |
| 6628 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6629 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6630 | /* final check when all deviations of a single target node are applied */ |
| 6631 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6632 | /* check min-max compatibility */ |
| 6633 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6634 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6635 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6636 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6637 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6638 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6639 | } else { |
| 6640 | min = max = 0; |
| 6641 | } |
| 6642 | if (min > max) { |
| 6643 | 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] | 6644 | "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] | 6645 | goto cleanup; |
| 6646 | } |
| 6647 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6648 | if (dflt) { |
| 6649 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6650 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6651 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6652 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6653 | 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] | 6654 | 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] | 6655 | leaf->dflt->realtype->refcount++; |
| 6656 | if (err) { |
| 6657 | ly_err_print(err); |
| 6658 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6659 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6660 | ly_err_free(err); |
| 6661 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6662 | if (rc == LY_EINCOMPLETE) { |
| 6663 | /* postpone default compilation when the tree is complete */ |
| 6664 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6665 | |
| 6666 | /* but in general result is so far ok */ |
| 6667 | rc = LY_SUCCESS; |
| 6668 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6669 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6670 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6671 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6672 | int dynamic; |
| 6673 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6674 | 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] | 6675 | |
| 6676 | /* update the list of incomplete default values if needed */ |
| 6677 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6678 | |
| 6679 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6680 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6681 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6682 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6683 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6684 | 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] | 6685 | 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] | 6686 | leaf->dflt->realtype->refcount++; |
| 6687 | if (err) { |
| 6688 | ly_err_print(err); |
| 6689 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6690 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6691 | ly_err_free(err); |
| 6692 | } |
| 6693 | if (dynamic) { |
| 6694 | free((void*)dflt); |
| 6695 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6696 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6697 | if (rc == LY_EINCOMPLETE) { |
| 6698 | /* postpone default compilation when the tree is complete */ |
| 6699 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6700 | |
| 6701 | /* but in general result is so far ok */ |
| 6702 | rc = LY_SUCCESS; |
| 6703 | } |
| 6704 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6705 | } else { /* LYS_LEAFLIST */ |
| 6706 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6707 | 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] | 6708 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6709 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6710 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6711 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6712 | 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] | 6713 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6714 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6715 | llist->dflts[x]->realtype->refcount++; |
| 6716 | if (err) { |
| 6717 | ly_err_print(err); |
| 6718 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6719 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6720 | dflt, err->msg); |
| 6721 | ly_err_free(err); |
| 6722 | } |
| 6723 | if (dynamic) { |
| 6724 | free((void*)dflt); |
| 6725 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6726 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6727 | if (rc == LY_EINCOMPLETE) { |
| 6728 | /* postpone default compilation when the tree is complete */ |
| 6729 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6730 | |
| 6731 | /* but in general result is so far ok */ |
| 6732 | rc = LY_SUCCESS; |
| 6733 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6734 | LY_CHECK_GOTO(rc, cleanup); |
| 6735 | } |
| 6736 | } |
| 6737 | } |
| 6738 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6739 | /* check mandatory - default compatibility */ |
| 6740 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6741 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6742 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6744 | "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] | 6745 | goto cleanup; |
| 6746 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6747 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6748 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6749 | 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] | 6750 | goto cleanup; |
| 6751 | } |
| 6752 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6753 | /* mandatory node under a default case */ |
| 6754 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6755 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6756 | 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] | 6757 | goto cleanup; |
| 6758 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6759 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6760 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6761 | } |
| 6762 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6763 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6764 | ret = LY_SUCCESS; |
| 6765 | |
| 6766 | cleanup: |
| 6767 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6768 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6769 | free(devs[u]); |
| 6770 | } |
| 6771 | free(devs); |
| 6772 | ly_set_erase(&targets, NULL); |
| 6773 | ly_set_erase(&devs_p, NULL); |
| 6774 | |
| 6775 | return ret; |
| 6776 | } |
| 6777 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6778 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6779 | * @brief Compile the given YANG submodule into the main module. |
| 6780 | * @param[in] ctx Compile context |
| 6781 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6782 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6783 | */ |
| 6784 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6785 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6786 | { |
| 6787 | unsigned int u; |
| 6788 | LY_ERR ret = LY_SUCCESS; |
| 6789 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6790 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6791 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6792 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6793 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6794 | if (!mainmod->mod->off_features) { |
| 6795 | /* features are compiled directly into the compiled module structure, |
| 6796 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6797 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6798 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6799 | LY_CHECK_GOTO(ret, error); |
| 6800 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6801 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6802 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6803 | 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] | 6804 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6805 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6806 | /* data nodes */ |
| 6807 | LY_LIST_FOR(submod->data, node_p) { |
| 6808 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6809 | LY_CHECK_GOTO(ret, error); |
| 6810 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6811 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6812 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6813 | 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] | 6814 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6815 | error: |
| 6816 | return ret; |
| 6817 | } |
| 6818 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6819 | static void * |
| 6820 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6821 | { |
| 6822 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6823 | if (substmts[u].stmt == stmt) { |
| 6824 | return substmts[u].storage; |
| 6825 | } |
| 6826 | } |
| 6827 | return NULL; |
| 6828 | } |
| 6829 | |
| 6830 | LY_ERR |
| 6831 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6832 | { |
| 6833 | LY_ERR ret = LY_EVALID, r; |
| 6834 | unsigned int u; |
| 6835 | struct lysp_stmt *stmt; |
| 6836 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6837 | |
| 6838 | /* check for invalid substatements */ |
| 6839 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6840 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6841 | continue; |
| 6842 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6843 | for (u = 0; substmts[u].stmt; ++u) { |
| 6844 | if (substmts[u].stmt == stmt->kw) { |
| 6845 | break; |
| 6846 | } |
| 6847 | } |
| 6848 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6849 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6850 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6851 | goto cleanup; |
| 6852 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6853 | } |
| 6854 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6855 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6856 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6857 | /* keep order of the processing the same as the order in the defined substmts, |
| 6858 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6859 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6860 | int stmt_present = 0; |
| 6861 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6862 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6863 | if (substmts[u].stmt != stmt->kw) { |
| 6864 | continue; |
| 6865 | } |
| 6866 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6867 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6868 | if (substmts[u].storage) { |
| 6869 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6870 | case LY_STMT_STATUS: |
| 6871 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6872 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6873 | break; |
| 6874 | case LY_STMT_UNITS: { |
| 6875 | const char **units; |
| 6876 | |
| 6877 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6878 | /* single item */ |
| 6879 | if (*((const char **)substmts[u].storage)) { |
| 6880 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6881 | goto cleanup; |
| 6882 | } |
| 6883 | units = (const char **)substmts[u].storage; |
| 6884 | } else { |
| 6885 | /* sized array */ |
| 6886 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6887 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6888 | } |
| 6889 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6890 | break; |
| 6891 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6892 | case LY_STMT_TYPE: { |
| 6893 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6894 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6895 | |
| 6896 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6897 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6898 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6899 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6900 | goto cleanup; |
| 6901 | } |
| 6902 | compiled = substmts[u].storage; |
| 6903 | } else { |
| 6904 | /* sized array */ |
| 6905 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6906 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6907 | compiled = (void*)type; |
| 6908 | } |
| 6909 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6910 | 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] | 6911 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6912 | 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] | 6913 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6914 | lysp_type_free(ctx->ctx, parsed); |
| 6915 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6916 | break; |
| 6917 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6918 | case LY_STMT_IF_FEATURE: { |
| 6919 | struct lysc_iffeature *iff = NULL; |
| 6920 | |
| 6921 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6922 | /* single item */ |
| 6923 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6924 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6925 | goto cleanup; |
| 6926 | } |
| 6927 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6928 | } else { |
| 6929 | /* sized array */ |
| 6930 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6931 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6932 | } |
| 6933 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6934 | break; |
| 6935 | } |
| 6936 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6937 | * 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] | 6938 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6939 | 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.", |
| 6940 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6941 | goto cleanup; |
| 6942 | } |
| 6943 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6944 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6945 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6946 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6947 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6948 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6949 | goto cleanup; |
| 6950 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6951 | } |
| 6952 | |
| 6953 | ret = LY_SUCCESS; |
| 6954 | |
| 6955 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6956 | return ret; |
| 6957 | } |
| 6958 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6959 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6960 | * @brief Check when for cyclic dependencies. |
| 6961 | * @param[in] set Set with all the referenced nodes. |
| 6962 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6963 | * @return LY_ERR value |
| 6964 | */ |
| 6965 | static LY_ERR |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6966 | 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] | 6967 | { |
| 6968 | struct lyxp_set tmp_set; |
| 6969 | struct lyxp_set_scnode *xp_scnode; |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6970 | uint32_t i, j, k; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6971 | int idx; |
| 6972 | struct lysc_when *when; |
| 6973 | LY_ERR ret = LY_SUCCESS; |
| 6974 | |
| 6975 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6976 | |
| 6977 | /* prepare in_ctx of the set */ |
| 6978 | for (i = 0; i < set->used; ++i) { |
| 6979 | xp_scnode = &set->val.scnodes[i]; |
| 6980 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6981 | if (xp_scnode->in_ctx != -1) { |
| 6982 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6983 | xp_scnode->in_ctx = 1; |
| 6984 | } |
| 6985 | } |
| 6986 | |
| 6987 | for (i = 0; i < set->used; ++i) { |
| 6988 | xp_scnode = &set->val.scnodes[i]; |
| 6989 | if (xp_scnode->in_ctx != 1) { |
| 6990 | /* already checked */ |
| 6991 | continue; |
| 6992 | } |
| 6993 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6994 | 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] | 6995 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6996 | /* no when to check */ |
| 6997 | xp_scnode->in_ctx = 0; |
| 6998 | continue; |
| 6999 | } |
| 7000 | |
| 7001 | node = xp_scnode->scnode; |
| 7002 | do { |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7003 | LY_ARRAY_FOR(node->when, j) { |
| 7004 | when = node->when[j]; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 7005 | ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7006 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 7007 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7008 | 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] | 7009 | goto cleanup; |
| 7010 | } |
| 7011 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7012 | for (k = 0; k < tmp_set.used; ++k) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7013 | /* skip roots'n'stuff */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7014 | if (tmp_set.val.scnodes[k].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7015 | /* try to find this node in our set */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7016 | idx = lyxp_set_scnode_dup_node_check(set, tmp_set.val.scnodes[k].scnode, LYXP_NODE_ELEM, -1); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7017 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7018 | 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] | 7019 | ret = LY_EVALID; |
| 7020 | goto cleanup; |
| 7021 | } |
| 7022 | |
| 7023 | /* needs to be checked, if in both sets, will be ignored */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7024 | tmp_set.val.scnodes[k].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7025 | } else { |
| 7026 | /* no when, nothing to check */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7027 | tmp_set.val.scnodes[k].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7028 | } |
| 7029 | } |
| 7030 | |
| 7031 | /* merge this set into the global when set */ |
| 7032 | lyxp_set_scnode_merge(set, &tmp_set); |
| 7033 | } |
| 7034 | |
| 7035 | /* check when of non-data parents as well */ |
| 7036 | node = node->parent; |
| 7037 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 7038 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7039 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 7040 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7041 | } |
| 7042 | |
| 7043 | cleanup: |
| 7044 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 7045 | return ret; |
| 7046 | } |
| 7047 | |
| 7048 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7049 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 7050 | * @param[in] ctx Compile context. |
| 7051 | * @param[in] node Node to check. |
| 7052 | * @return LY_ERR value |
| 7053 | */ |
| 7054 | static LY_ERR |
| 7055 | lys_compile_check_xpath(struct lysc_ctx *ctx, const struct lysc_node *node) |
| 7056 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7057 | struct lyxp_set tmp_set; |
| 7058 | uint32_t i, j; |
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" */ |
| 7109 | LY_ARRAY_FOR(when, i) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 7110 | ret = lyxp_atomize(when[i]->cond, LYD_SCHEMA, when[i]->module, when[i]->context, |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7111 | when[i]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts); |
| 7112 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7113 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[i]->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); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7119 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7120 | /* skip roots'n'stuff */ |
| 7121 | if ((tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[j].in_ctx != -1)) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7122 | struct lysc_node *schema = tmp_set.val.scnodes[j].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 */ |
| 7125 | ret = lysc_check_status(ctx, when[i]->flags, when[i]->module, node->name, schema->flags, schema->module, |
| 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 | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7142 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 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" */ |
| 7147 | LY_ARRAY_FOR(musts, i) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 7148 | ret = lyxp_atomize(musts[i].cond, LYD_SCHEMA, musts[i].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) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7150 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[i].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); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7156 | for (j = 0; j < tmp_set.used; ++j) { |
| 7157 | /* skip roots'n'stuff */ |
| 7158 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
| 7159 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7160 | ret = lysc_check_status(ctx, node->flags, musts[i].module, node->name, tmp_set.val.scnodes[j].scnode->flags, |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7161 | tmp_set.val.scnodes[j].scnode->module, tmp_set.val.scnodes[j].scnode->name); |
| 7162 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7163 | } |
| 7164 | } |
| 7165 | |
| 7166 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 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: |
| 7178 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 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 | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7231 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7232 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7233 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7234 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7235 | |
| 7236 | if (!mod->implemented) { |
| 7237 | /* just imported modules are not compiled */ |
| 7238 | return LY_SUCCESS; |
| 7239 | } |
| 7240 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7241 | sp = mod->parsed; |
| 7242 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7243 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7244 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7245 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7246 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7247 | ctx.path_len = 1; |
| 7248 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7249 | |
| 7250 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7251 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 7252 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7253 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7254 | 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] | 7255 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7256 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7257 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7258 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7259 | |
| 7260 | /* features */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7261 | if (mod->off_features) { |
| 7262 | /* there is already precompiled array of features */ |
| 7263 | mod_c->features = mod->off_features; |
| 7264 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7265 | } else { |
| 7266 | /* features are compiled directly into the compiled module structure, |
| 7267 | * 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] | 7268 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7269 | LY_CHECK_GOTO(ret, error); |
| 7270 | } |
| 7271 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7272 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7273 | * are present. */ |
| 7274 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7275 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7276 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7277 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7278 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7279 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7280 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7281 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7282 | 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] | 7283 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7284 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7285 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7286 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7287 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7288 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7289 | /* identities */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7290 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7291 | 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] | 7292 | if (sp->identities) { |
| 7293 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 7294 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7295 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7296 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7297 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7298 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7299 | 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] | 7300 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7301 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7302 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7303 | 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] | 7304 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7305 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7306 | 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] | 7307 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7308 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7309 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7310 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7311 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7312 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7313 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7314 | /* extension instances TODO cover extension instances from submodules */ |
| 7315 | 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] | 7316 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7317 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7318 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7319 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7320 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7321 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 7322 | if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7323 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 7324 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7325 | /* validate the path */ |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 7326 | LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type, NULL), error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7327 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7328 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7329 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7330 | /* validate the path */ |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7331 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 7332 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v], NULL); |
| 7333 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7334 | } |
| 7335 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7336 | } |
| 7337 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7338 | |
| 7339 | /* check xpath */ |
| 7340 | LY_CHECK_GOTO(ret = lys_compile_check_xpath(&ctx, ctx.unres.objs[u]), error); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7341 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7342 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 7343 | if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7344 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 7345 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7346 | /* store pointer to the real type */ |
| 7347 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7348 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7349 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7350 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7351 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7352 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7353 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7354 | /* store pointer to the real type */ |
| 7355 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7356 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7357 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7358 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7359 | } |
| 7360 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7361 | } |
| 7362 | } |
| 7363 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7364 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7365 | /* finish incomplete default values compilation */ |
| 7366 | for (u = 0; u < ctx.dflts.count; ++u) { |
| 7367 | struct ly_err_item *err = NULL; |
| 7368 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[u]; |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 7369 | 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] | 7370 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7371 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7372 | if (err) { |
| 7373 | ly_err_print(err); |
| 7374 | ctx.path[0] = '\0'; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7375 | 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] | 7376 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 7377 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7378 | ly_err_free(err); |
| 7379 | } |
| 7380 | LY_CHECK_GOTO(ret, error); |
| 7381 | } |
| 7382 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7383 | /* validate non-instantiated groupings from the parsed schema, |
| 7384 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7385 | ctx.options |= LYSC_OPT_GROUPING; |
| 7386 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7387 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7388 | 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] | 7389 | } |
| 7390 | } |
| 7391 | LY_LIST_FOR(sp->data, node_p) { |
| 7392 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7393 | LY_ARRAY_FOR(grps, u) { |
| 7394 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7395 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7396 | } |
| 7397 | } |
| 7398 | } |
| 7399 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7400 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7401 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7402 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7403 | } |
| 7404 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7405 | #if 0 |
| 7406 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7407 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7408 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7409 | * the anotation definitions available in the internal schema structure. */ |
| 7410 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7411 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7412 | lys_free(mod, NULL, 1, 1); |
| 7413 | return NULL; |
| 7414 | } |
| 7415 | } |
| 7416 | #endif |
| 7417 | |
| 7418 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
| 7419 | if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
| 7420 | LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error); |
| 7421 | } |
| 7422 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7423 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7424 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7425 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7426 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7427 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7428 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7429 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7430 | lysp_module_free(mod->parsed); |
| 7431 | ((struct lys_module*)mod)->parsed = NULL; |
| 7432 | } |
| 7433 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7434 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7435 | /* remove flag of the modules implemented by dependency */ |
| 7436 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 7437 | m = ctx.ctx->list.objs[u]; |
| 7438 | if (m->implemented == 2) { |
| 7439 | m->implemented = 1; |
| 7440 | } |
| 7441 | } |
| 7442 | } |
| 7443 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7444 | ((struct lys_module*)mod)->compiled = mod_c; |
| 7445 | return LY_SUCCESS; |
| 7446 | |
| 7447 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7448 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7449 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7450 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7451 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7452 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7453 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7454 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7455 | mod->compiled = NULL; |
| 7456 | |
| 7457 | /* revert compilation of modules implemented by dependency */ |
| 7458 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 7459 | m = ctx.ctx->list.objs[u]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7460 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7461 | /* revert features list to the precompiled state */ |
| 7462 | lys_feature_precompile_revert(&ctx, m); |
| 7463 | /* mark module as imported-only / not-implemented */ |
| 7464 | m->implemented = 0; |
| 7465 | /* free the compiled version of the module */ |
| 7466 | lysc_module_free(m->compiled, NULL); |
| 7467 | m->compiled = NULL; |
| 7468 | } |
| 7469 | } |
| 7470 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7471 | return ret; |
| 7472 | } |