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 | |
| 36 | /** |
| 37 | * @brief Duplicate string into dictionary |
| 38 | * @param[in] CTX libyang context of the dictionary. |
| 39 | * @param[in] ORIG String to duplicate. |
| 40 | * @param[out] DUP Where to store the result. |
| 41 | */ |
| 42 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 43 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 44 | #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] | 45 | if (ARRAY_P) { \ |
| 46 | 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] | 47 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 48 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 49 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 50 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 51 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 52 | } \ |
| 53 | } |
| 54 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 55 | #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] | 56 | if (ARRAY_P) { \ |
| 57 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 58 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 59 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 60 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 61 | RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 62 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 63 | } \ |
| 64 | } |
| 65 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 66 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 67 | if (EXTS_P) { \ |
| 68 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \ |
| 69 | for (uint32_t __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \ |
| 70 | LY_ARRAY_INCREMENT(EXT_C); \ |
| 71 | RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE); \ |
| 72 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 73 | } \ |
| 74 | } |
| 75 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 76 | #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] | 77 | if (ARRAY_P) { \ |
| 78 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 79 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 80 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 81 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 82 | 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] | 83 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 84 | } \ |
| 85 | } |
| 86 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 87 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 88 | if (MEMBER_P) { \ |
| 89 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 90 | 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] | 91 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 92 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 93 | } |
| 94 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 95 | #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] | 96 | if (MEMBER_P) { \ |
| 97 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
| 98 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 99 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 100 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 101 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 102 | } |
| 103 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 104 | #define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 105 | if (ARRAY) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 106 | for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
| 107 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 108 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 109 | return LY_EVALID; \ |
| 110 | } \ |
| 111 | } \ |
| 112 | } |
| 113 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 114 | static struct lysc_ext_instance * |
| 115 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 116 | { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 117 | /* TODO - extensions */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 118 | (void) ctx; |
| 119 | (void) orig; |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 123 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 124 | * @brief Add record into the compile context's list of incomplete default values. |
| 125 | * @param[in] ctx Compile context with the incomplete default values list. |
| 126 | * @param[in] context_node Context schema node to store in the record. |
| 127 | * @param[in] dflt Incomplete default value to store in the record. |
| 128 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 129 | * @return LY_EMEM in case of memory allocation failure. |
| 130 | * @return LY_SUCCESS |
| 131 | */ |
| 132 | static LY_ERR |
| 133 | lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod) |
| 134 | { |
| 135 | struct lysc_incomplete_dflt *r; |
| 136 | r = malloc(sizeof *r); |
| 137 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 138 | r->context_node = context_node; |
| 139 | r->dflt = dflt; |
| 140 | r->dflt_mod = dflt_mod; |
| 141 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 142 | |
| 143 | return LY_SUCCESS; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 148 | * @param[in] ctx Compile context with the incomplete default values list. |
| 149 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 150 | */ |
| 151 | static void |
| 152 | lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt) |
| 153 | { |
| 154 | unsigned int u; |
| 155 | struct lysc_incomplete_dflt *r; |
| 156 | |
| 157 | for (u = 0; u < ctx->dflts.count; ++u) { |
| 158 | r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u]; |
| 159 | if (r->dflt == dflt) { |
| 160 | free(ctx->dflts.objs[u]); |
| 161 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 162 | --ctx->dflts.count; |
| 163 | return; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 168 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 169 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 170 | { |
| 171 | int len; |
| 172 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 173 | |
| 174 | if (!name) { |
| 175 | /* removing last path segment */ |
| 176 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 177 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 178 | if (ctx->path[ctx->path_len] == '=') { |
| 179 | ctx->path[ctx->path_len++] = '}'; |
| 180 | } else { |
| 181 | /* not a top-level special tag, remove also preceiding '/' */ |
| 182 | goto remove_nodelevel; |
| 183 | } |
| 184 | } else { |
| 185 | remove_nodelevel: |
| 186 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 187 | if (ctx->path_len == 0) { |
| 188 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 189 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | /* set new terminating NULL-byte */ |
| 193 | ctx->path[ctx->path_len] = '\0'; |
| 194 | } else { |
| 195 | if (ctx->path_len > 1) { |
| 196 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 197 | /* extension of the special tag */ |
| 198 | nextlevel = 2; |
| 199 | --ctx->path_len; |
| 200 | } else { |
| 201 | /* there is already some path, so add next level */ |
| 202 | nextlevel = 1; |
| 203 | } |
| 204 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 205 | |
| 206 | if (nextlevel != 2) { |
| 207 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 208 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 209 | 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] | 210 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 211 | 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] | 212 | } |
| 213 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 214 | 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] | 215 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 216 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 217 | /* output truncated */ |
| 218 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 219 | } else { |
| 220 | ctx->path_len += len; |
| 221 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 226 | * @brief Duplicate the compiled pattern structure. |
| 227 | * |
| 228 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 229 | * |
| 230 | * @param[in] orig The pattern structure to duplicate. |
| 231 | * @return The duplicated structure to use. |
| 232 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 233 | static struct lysc_pattern* |
| 234 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 235 | { |
| 236 | ++orig->refcount; |
| 237 | return orig; |
| 238 | } |
| 239 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 240 | /** |
| 241 | * @brief Duplicate the array of compiled patterns. |
| 242 | * |
| 243 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 244 | * |
| 245 | * @param[in] ctx Libyang context for logging. |
| 246 | * @param[in] orig The patterns sized array to duplicate. |
| 247 | * @return New sized array as a copy of @p orig. |
| 248 | * @return NULL in case of memory allocation error. |
| 249 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 250 | static struct lysc_pattern** |
| 251 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 252 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 253 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 254 | unsigned int u; |
| 255 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 256 | assert(orig); |
| 257 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 258 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 259 | LY_ARRAY_FOR(orig, u) { |
| 260 | dup[u] = lysc_pattern_dup(orig[u]); |
| 261 | LY_ARRAY_INCREMENT(dup); |
| 262 | } |
| 263 | return dup; |
| 264 | } |
| 265 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 266 | /** |
| 267 | * @brief Duplicate compiled range structure. |
| 268 | * |
| 269 | * @param[in] ctx Libyang context for logging. |
| 270 | * @param[in] orig The range structure to be duplicated. |
| 271 | * @return New compiled range structure as a copy of @p orig. |
| 272 | * @return NULL in case of memory allocation error. |
| 273 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 274 | struct lysc_range* |
| 275 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 276 | { |
| 277 | struct lysc_range *dup; |
| 278 | LY_ERR ret; |
| 279 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 280 | assert(orig); |
| 281 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 282 | dup = calloc(1, sizeof *dup); |
| 283 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 284 | if (orig->parts) { |
| 285 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 286 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 287 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 288 | } |
| 289 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 290 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 291 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 292 | |
| 293 | return dup; |
| 294 | cleanup: |
| 295 | free(dup); |
| 296 | (void) ret; /* set but not used due to the return type */ |
| 297 | return NULL; |
| 298 | } |
| 299 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 300 | /** |
| 301 | * @brief Stack for processing if-feature expressions. |
| 302 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 303 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 304 | int size; /**< number of items in the stack */ |
| 305 | int index; /**< first empty item */ |
| 306 | 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] | 307 | }; |
| 308 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 309 | /** |
| 310 | * @brief Add @ref ifftokens into the stack. |
| 311 | * @param[in] stack The if-feature stack to use. |
| 312 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 313 | * @return LY_EMEM in case of memory allocation error |
| 314 | * @return LY_ESUCCESS if the value successfully stored. |
| 315 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 316 | static LY_ERR |
| 317 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 318 | { |
| 319 | if (stack->index == stack->size) { |
| 320 | stack->size += 4; |
| 321 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 322 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 323 | } |
| 324 | stack->stack[stack->index++] = value; |
| 325 | return LY_SUCCESS; |
| 326 | } |
| 327 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 328 | /** |
| 329 | * @brief Get (and remove) the last item form the stack. |
| 330 | * @param[in] stack The if-feature stack to use. |
| 331 | * @return The value from the top of the stack. |
| 332 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 333 | static uint8_t |
| 334 | iff_stack_pop(struct iff_stack *stack) |
| 335 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 336 | assert(stack && stack->index); |
| 337 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 338 | stack->index--; |
| 339 | return stack->stack[stack->index]; |
| 340 | } |
| 341 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 342 | /** |
| 343 | * @brief Clean up the stack. |
| 344 | * @param[in] stack The if-feature stack to use. |
| 345 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 346 | static void |
| 347 | iff_stack_clean(struct iff_stack *stack) |
| 348 | { |
| 349 | stack->size = 0; |
| 350 | free(stack->stack); |
| 351 | } |
| 352 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 353 | /** |
| 354 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 355 | * (libyang format of the if-feature expression). |
| 356 | * @param[in,out] list The 2bits array to modify. |
| 357 | * @param[in] op The operand (@ref ifftokens) to store. |
| 358 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 359 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 360 | static void |
| 361 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 362 | { |
| 363 | uint8_t *item; |
| 364 | uint8_t mask = 3; |
| 365 | |
| 366 | assert(pos >= 0); |
| 367 | assert(op <= 3); /* max 2 bits */ |
| 368 | |
| 369 | item = &list[pos / 4]; |
| 370 | mask = mask << 2 * (pos % 4); |
| 371 | *item = (*item) & ~mask; |
| 372 | *item = (*item) | (op << 2 * (pos % 4)); |
| 373 | } |
| 374 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 375 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 376 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 377 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 378 | /** |
| 379 | * @brief Find a feature of the given name and referenced in the given module. |
| 380 | * |
| 381 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 382 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 383 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 384 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 385 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 386 | * assigned till that time will be still valid. |
| 387 | * |
| 388 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 389 | * @param[in] name Name of the feature including possible prefix. |
| 390 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 391 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 392 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 393 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 394 | 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] | 395 | { |
| 396 | size_t i; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 397 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 398 | |
| 399 | for (i = 0; i < len; ++i) { |
| 400 | if (name[i] == ':') { |
| 401 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 402 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 403 | LY_CHECK_RET(!mod, NULL); |
| 404 | |
| 405 | name = &name[i + 1]; |
| 406 | len = len - i - 1; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 411 | if (mod->implemented) { |
| 412 | /* module is implemented so there is already the compiled schema */ |
| 413 | flist = mod->compiled->features; |
| 414 | } else { |
| 415 | flist = mod->off_features; |
| 416 | } |
| 417 | LY_ARRAY_FOR(flist, i) { |
| 418 | f = &flist[i]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 419 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 420 | return f; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | static LY_ERR |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 428 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, LYEXT_PARENT parent_type) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 429 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 430 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 431 | const char *name; |
| 432 | unsigned int u; |
| 433 | const struct lys_module *mod; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 434 | struct lysc_ext *elist = NULL; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 435 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 436 | |
| 437 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 438 | ext->insubstmt = ext_p->insubstmt; |
| 439 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 440 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 441 | ext->parent = parent; |
| 442 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 443 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 444 | 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] | 445 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 446 | /* get module where the extension definition should be placed */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 447 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u); |
| 448 | if (ext_p->yin) { |
| 449 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 450 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 451 | * YANG (import) prefix must be done here. */ |
| 452 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 453 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 454 | u = 0; |
| 455 | } else if (ctx->mod_def->compiled) { |
| 456 | unsigned int v; |
| 457 | LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) { |
| 458 | if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) { |
| 459 | char *s; |
| 460 | asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]); |
| 461 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
| 462 | u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */ |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | if (!prefixed_name) { |
| 468 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 469 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 470 | goto cleanup; |
| 471 | } |
| 472 | } else { |
| 473 | prefixed_name = ext_p->name; |
| 474 | } |
| 475 | lysc_update_path(ctx, NULL, prefixed_name); |
| 476 | |
| 477 | mod = lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1); |
| 478 | if (!mod) { |
| 479 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 480 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 481 | goto cleanup; |
| 482 | } else if (!mod->parsed->extensions) { |
| 483 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 484 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 485 | prefixed_name, mod->name); |
| 486 | goto cleanup; |
| 487 | } |
| 488 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 489 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 490 | /* find the extension definition there */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 491 | if (mod->off_extensions) { |
| 492 | elist = mod->off_extensions; |
| 493 | } else { |
| 494 | elist = mod->compiled->extensions; |
| 495 | } |
| 496 | LY_ARRAY_FOR(elist, u) { |
| 497 | if (!strcmp(name, elist[u].name)) { |
| 498 | ext->def = &elist[u]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 499 | break; |
| 500 | } |
| 501 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 502 | if (!ext->def) { |
| 503 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 504 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 505 | goto cleanup; |
| 506 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 507 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 508 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 509 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 510 | * 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] | 511 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 512 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 513 | struct lysp_stmt *stmt = NULL; |
| 514 | |
| 515 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 516 | /* ... argument was the first XML child element */ |
| 517 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 518 | /* TODO check namespace of the statement */ |
| 519 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 520 | stmt = ext_p->child; |
| 521 | } |
| 522 | } |
| 523 | } else { |
| 524 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 525 | * with LYS_YIN_ATTR flag */ |
| 526 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 527 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 528 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 529 | break; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | if (!stmt) { |
| 534 | /* missing extension's argument */ |
| 535 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 536 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 537 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 538 | |
| 539 | } |
| 540 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 541 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 542 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 543 | if (prefixed_name != ext_p->name) { |
| 544 | lydict_remove(ctx->ctx, ext_p->name); |
| 545 | ext_p->name = prefixed_name; |
| 546 | if (!ext_p->argument && ext->argument) { |
| 547 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 548 | } |
| 549 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 550 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 551 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 552 | if (ext->argument) { |
| 553 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 554 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 555 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 556 | if (ext->argument) { |
| 557 | lysc_update_path(ctx, NULL, NULL); |
| 558 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 559 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 560 | ext_p->compiled = ext; |
| 561 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 562 | ret = LY_SUCCESS; |
| 563 | cleanup: |
| 564 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 565 | lydict_remove(ctx->ctx, prefixed_name); |
| 566 | } |
| 567 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 568 | lysc_update_path(ctx, NULL, NULL); |
| 569 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 570 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 571 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
| 575 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 576 | */ |
| 577 | static LY_ERR |
| 578 | lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext) |
| 579 | { |
| 580 | LY_ERR ret = LY_SUCCESS; |
| 581 | |
| 582 | DUP_STRING(ctx->ctx, ext_p->name, ext->name); |
| 583 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 584 | ext->module = ctx->mod_def; |
| 585 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done); |
| 586 | |
| 587 | done: |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * @brief Link the extensions definitions with the available extension plugins. |
| 593 | * |
| 594 | * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules |
| 595 | * are not connected with even available extension plugins. |
| 596 | * |
| 597 | * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)). |
| 598 | */ |
| 599 | static void |
| 600 | lys_compile_extension_plugins(struct lysc_ext *extensions) |
| 601 | { |
| 602 | unsigned int u; |
| 603 | |
| 604 | LY_ARRAY_FOR(extensions, u) { |
| 605 | extensions[u].plugin = lyext_get_plugin(&extensions[u]); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | LY_ERR |
| 610 | lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 611 | struct lysp_ext *extensions_p, struct lysc_ext **extensions) |
| 612 | { |
| 613 | unsigned int offset = 0, u; |
| 614 | struct lysc_ctx context = {0}; |
| 615 | |
| 616 | assert(ctx_sc || ctx); |
| 617 | |
| 618 | if (!ctx_sc) { |
| 619 | context.ctx = ctx; |
| 620 | context.mod = module; |
Michal Vasko | b541af0 | 2019-12-18 12:11:23 +0100 | [diff] [blame] | 621 | context.mod_def = module; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 622 | context.path_len = 1; |
| 623 | context.path[0] = '/'; |
| 624 | ctx_sc = &context; |
| 625 | } |
| 626 | |
| 627 | if (!extensions_p) { |
| 628 | return LY_SUCCESS; |
| 629 | } |
| 630 | if (*extensions) { |
| 631 | offset = LY_ARRAY_SIZE(*extensions); |
| 632 | } |
| 633 | |
| 634 | lysc_update_path(ctx_sc, NULL, "{extension}"); |
| 635 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM); |
| 636 | LY_ARRAY_FOR(extensions_p, u) { |
| 637 | lysc_update_path(ctx_sc, NULL, extensions_p[u].name); |
| 638 | LY_ARRAY_INCREMENT(*extensions); |
| 639 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name); |
| 640 | LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u])); |
| 641 | lysc_update_path(ctx_sc, NULL, NULL); |
| 642 | } |
| 643 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 644 | |
| 645 | return LY_SUCCESS; |
| 646 | } |
| 647 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 648 | /** |
| 649 | * @brief Compile information from the if-feature statement |
| 650 | * @param[in] ctx Compile context. |
| 651 | * @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] | 652 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 653 | * @return LY_ERR value. |
| 654 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 655 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 656 | 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] | 657 | { |
| 658 | const char *c = *value; |
| 659 | int r, rc = EXIT_FAILURE; |
| 660 | int i, j, last_not, checkversion = 0; |
| 661 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 662 | uint8_t op; |
| 663 | struct iff_stack stack = {0, 0, NULL}; |
| 664 | struct lysc_feature *f; |
| 665 | |
| 666 | assert(c); |
| 667 | |
| 668 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 669 | for (i = j = last_not = 0; c[i]; i++) { |
| 670 | if (c[i] == '(') { |
| 671 | j++; |
| 672 | checkversion = 1; |
| 673 | continue; |
| 674 | } else if (c[i] == ')') { |
| 675 | j--; |
| 676 | continue; |
| 677 | } else if (isspace(c[i])) { |
| 678 | checkversion = 1; |
| 679 | continue; |
| 680 | } |
| 681 | |
| 682 | 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] | 683 | int sp; |
| 684 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 685 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 686 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 687 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 688 | return LY_EVALID; |
| 689 | } else if (!isspace(c[i + r])) { |
| 690 | /* feature name starting with the not/and/or */ |
| 691 | last_not = 0; |
| 692 | f_size++; |
| 693 | } else if (c[i] == 'n') { /* not operation */ |
| 694 | if (last_not) { |
| 695 | /* double not */ |
| 696 | expr_size = expr_size - 2; |
| 697 | last_not = 0; |
| 698 | } else { |
| 699 | last_not = 1; |
| 700 | } |
| 701 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 702 | if (f_exp != f_size) { |
| 703 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 704 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 705 | return LY_EVALID; |
| 706 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 707 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 708 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 709 | /* not a not operation */ |
| 710 | last_not = 0; |
| 711 | } |
| 712 | i += r; |
| 713 | } else { |
| 714 | f_size++; |
| 715 | last_not = 0; |
| 716 | } |
| 717 | expr_size++; |
| 718 | |
| 719 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 720 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 721 | i--; |
| 722 | break; |
| 723 | } |
| 724 | i++; |
| 725 | } |
| 726 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 727 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 728 | /* not matching count of ( and ) */ |
| 729 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 730 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 731 | return LY_EVALID; |
| 732 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 733 | if (f_exp != f_size) { |
| 734 | /* features do not match the needed arguments for the logical operations */ |
| 735 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 736 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 737 | "the required number of operands for the operations.", *value); |
| 738 | return LY_EVALID; |
| 739 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 740 | |
| 741 | if (checkversion || expr_size > 1) { |
| 742 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 743 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 745 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 746 | return LY_EVALID; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /* allocate the memory */ |
| 751 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 752 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 753 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 754 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 755 | |
| 756 | stack.size = expr_size; |
| 757 | f_size--; expr_size--; /* used as indexes from now */ |
| 758 | |
| 759 | for (i--; i >= 0; i--) { |
| 760 | if (c[i] == ')') { |
| 761 | /* push it on stack */ |
| 762 | iff_stack_push(&stack, LYS_IFF_RP); |
| 763 | continue; |
| 764 | } else if (c[i] == '(') { |
| 765 | /* pop from the stack into result all operators until ) */ |
| 766 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 767 | iff_setop(iff->expr, op, expr_size--); |
| 768 | } |
| 769 | continue; |
| 770 | } else if (isspace(c[i])) { |
| 771 | continue; |
| 772 | } |
| 773 | |
| 774 | /* end of operator or operand -> find beginning and get what is it */ |
| 775 | j = i + 1; |
| 776 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 777 | i--; |
| 778 | } |
| 779 | i++; /* go back by one step */ |
| 780 | |
| 781 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 782 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 783 | /* double not */ |
| 784 | iff_stack_pop(&stack); |
| 785 | } else { |
| 786 | /* not has the highest priority, so do not pop from the stack |
| 787 | * as in case of AND and OR */ |
| 788 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 789 | } |
| 790 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 791 | /* as for OR - pop from the stack all operators with the same or higher |
| 792 | * priority and store them to the result, then push the AND to the stack */ |
| 793 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 794 | op = iff_stack_pop(&stack); |
| 795 | iff_setop(iff->expr, op, expr_size--); |
| 796 | } |
| 797 | iff_stack_push(&stack, LYS_IFF_AND); |
| 798 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 799 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 800 | op = iff_stack_pop(&stack); |
| 801 | iff_setop(iff->expr, op, expr_size--); |
| 802 | } |
| 803 | iff_stack_push(&stack, LYS_IFF_OR); |
| 804 | } else { |
| 805 | /* feature name, length is j - i */ |
| 806 | |
| 807 | /* add it to the expression */ |
| 808 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 809 | |
| 810 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 811 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 812 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 813 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 814 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 815 | iff->features[f_size] = f; |
| 816 | LY_ARRAY_INCREMENT(iff->features); |
| 817 | f_size--; |
| 818 | } |
| 819 | } |
| 820 | while (stack.index) { |
| 821 | op = iff_stack_pop(&stack); |
| 822 | iff_setop(iff->expr, op, expr_size--); |
| 823 | } |
| 824 | |
| 825 | if (++expr_size || ++f_size) { |
| 826 | /* not all expected operators and operands found */ |
| 827 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 828 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 829 | rc = LY_EINT; |
| 830 | } else { |
| 831 | rc = LY_SUCCESS; |
| 832 | } |
| 833 | |
| 834 | error: |
| 835 | /* cleanup */ |
| 836 | iff_stack_clean(&stack); |
| 837 | |
| 838 | return rc; |
| 839 | } |
| 840 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 841 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 842 | * @brief Get the XPath context node for the given schema node. |
| 843 | * @param[in] start The schema node where the XPath expression appears. |
| 844 | * @return The context node to evaluate XPath expression in given schema node. |
| 845 | * @return NULL in case the context node is the root node. |
| 846 | */ |
| 847 | static struct lysc_node * |
| 848 | lysc_xpath_context(struct lysc_node *start) |
| 849 | { |
| 850 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 851 | start = start->parent); |
| 852 | return start; |
| 853 | } |
| 854 | |
| 855 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 856 | * @brief Compile information from the when statement |
| 857 | * @param[in] ctx Compile context. |
| 858 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 859 | * @param[in] flags Flags of the node with the "when" defiition. |
| 860 | * @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] | 861 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 862 | * @return LY_ERR value. |
| 863 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 864 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 865 | 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] | 866 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 867 | LY_ERR ret = LY_SUCCESS; |
| 868 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 869 | *when = calloc(1, sizeof **when); |
| 870 | (*when)->refcount = 1; |
| 871 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 872 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 873 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 874 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 875 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 876 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 877 | 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] | 878 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 879 | |
| 880 | done: |
| 881 | return ret; |
| 882 | } |
| 883 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 884 | /** |
| 885 | * @brief Compile information from the must statement |
| 886 | * @param[in] ctx Compile context. |
| 887 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 888 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 889 | * @return LY_ERR value. |
| 890 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 891 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 892 | 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] | 893 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 894 | LY_ERR ret = LY_SUCCESS; |
| 895 | |
| 896 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 897 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 898 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 899 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 900 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 901 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 902 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 903 | 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] | 904 | |
| 905 | done: |
| 906 | return ret; |
| 907 | } |
| 908 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 909 | /** |
| 910 | * @brief Compile information from the import statement |
| 911 | * @param[in] ctx Compile context. |
| 912 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 913 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 914 | * @return LY_ERR value. |
| 915 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 916 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 917 | 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] | 918 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 919 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 920 | LY_ERR ret = LY_SUCCESS; |
| 921 | |
| 922 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 923 | 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] | 924 | imp->module = imp_p->module; |
| 925 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 926 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 927 | * 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] | 928 | * 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] | 929 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 930 | /* try to use filepath if present */ |
| 931 | if (imp->module->filepath) { |
| 932 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 933 | !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] | 934 | if (mod != imp->module) { |
| 935 | 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] | 936 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 937 | mod = NULL; |
| 938 | } |
| 939 | } |
| 940 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 941 | 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] | 942 | 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] | 943 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 944 | return LY_ENOTFOUND; |
| 945 | } |
| 946 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | done: |
| 950 | return ret; |
| 951 | } |
| 952 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 953 | /** |
| 954 | * @brief Compile information from the identity statement |
| 955 | * |
| 956 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 957 | * |
| 958 | * @param[in] ctx Compile context. |
| 959 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 960 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 961 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 962 | * @return LY_ERR value. |
| 963 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 964 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 965 | 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] | 966 | { |
| 967 | unsigned int u; |
| 968 | LY_ERR ret = LY_SUCCESS; |
| 969 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 970 | lysc_update_path(ctx, NULL, ident_p->name); |
| 971 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 972 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 973 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 974 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 975 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 976 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 977 | 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] | 978 | /* 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] | 979 | 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] | 980 | ident->flags = ident_p->flags; |
| 981 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 982 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 983 | done: |
| 984 | return ret; |
| 985 | } |
| 986 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 987 | /** |
| 988 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 989 | * |
| 990 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 991 | * |
| 992 | * @param[in] ctx Compile context for logging. |
| 993 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 994 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 995 | * @return LY_SUCCESS if everything is ok. |
| 996 | * @return LY_EVALID if the identity is derived from itself. |
| 997 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 998 | static LY_ERR |
| 999 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1000 | { |
| 1001 | LY_ERR ret = LY_EVALID; |
| 1002 | unsigned int u, v; |
| 1003 | struct ly_set recursion = {0}; |
| 1004 | struct lysc_ident *drv; |
| 1005 | |
| 1006 | if (!derived) { |
| 1007 | return LY_SUCCESS; |
| 1008 | } |
| 1009 | |
| 1010 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 1011 | if (ident == 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, derived[u], 0); |
| 1017 | } |
| 1018 | |
| 1019 | for (v = 0; v < recursion.count; ++v) { |
| 1020 | drv = recursion.objs[v]; |
| 1021 | if (!drv->derived) { |
| 1022 | continue; |
| 1023 | } |
| 1024 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 1025 | if (ident == drv->derived[u]) { |
| 1026 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1027 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1028 | goto cleanup; |
| 1029 | } |
| 1030 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1031 | } |
| 1032 | } |
| 1033 | ret = LY_SUCCESS; |
| 1034 | |
| 1035 | cleanup: |
| 1036 | ly_set_erase(&recursion, NULL); |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1040 | /** |
| 1041 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1042 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1043 | * 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] | 1044 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1045 | * to distinguish these two use cases. |
| 1046 | * |
| 1047 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1048 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 1049 | * @param[in] ident Referencing identity to work with. |
| 1050 | * @param[in] bases Array of bases of identityref to fill in. |
| 1051 | * @return LY_ERR value. |
| 1052 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1053 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1054 | 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] | 1055 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1056 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1057 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1058 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1059 | struct lysc_ident **idref; |
| 1060 | |
| 1061 | assert(ident || bases); |
| 1062 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1063 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1064 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1065 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1066 | return LY_EVALID; |
| 1067 | } |
| 1068 | |
| 1069 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 1070 | s = strchr(bases_p[u], ':'); |
| 1071 | if (s) { |
| 1072 | /* prefixed identity */ |
| 1073 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1074 | 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] | 1075 | } else { |
| 1076 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1077 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1078 | } |
| 1079 | if (!mod) { |
| 1080 | if (ident) { |
| 1081 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1082 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1083 | } else { |
| 1084 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1085 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1086 | } |
| 1087 | return LY_EVALID; |
| 1088 | } |
| 1089 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1090 | if (mod->compiled && mod->compiled->identities) { |
| 1091 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 1092 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1093 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1094 | if (ident == &mod->compiled->identities[v]) { |
| 1095 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1096 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1097 | return LY_EVALID; |
| 1098 | } |
| 1099 | 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] | 1100 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1101 | 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] | 1102 | *idref = ident; |
| 1103 | } else { |
| 1104 | /* we have match! store the found identity */ |
| 1105 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1106 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1107 | } |
| 1108 | break; |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | if (!idref || !(*idref)) { |
| 1113 | if (ident) { |
| 1114 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1115 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1116 | } else { |
| 1117 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1118 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1119 | } |
| 1120 | return LY_EVALID; |
| 1121 | } |
| 1122 | } |
| 1123 | return LY_SUCCESS; |
| 1124 | } |
| 1125 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1126 | /** |
| 1127 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1128 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1129 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1130 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1131 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1132 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1133 | static LY_ERR |
| 1134 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1135 | { |
| 1136 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1137 | |
| 1138 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 1139 | if (!idents_p[i].bases) { |
| 1140 | continue; |
| 1141 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1142 | lysc_update_path(ctx, NULL, idents[i].name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1143 | 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] | 1144 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1145 | } |
| 1146 | return LY_SUCCESS; |
| 1147 | } |
| 1148 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1149 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1150 | 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] | 1151 | { |
| 1152 | unsigned int offset = 0, u; |
| 1153 | struct lysc_ctx context = {0}; |
| 1154 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1155 | assert(ctx_sc || ctx); |
| 1156 | |
| 1157 | if (!ctx_sc) { |
| 1158 | context.ctx = ctx; |
| 1159 | context.mod = module; |
| 1160 | context.path_len = 1; |
| 1161 | context.path[0] = '/'; |
| 1162 | ctx_sc = &context; |
| 1163 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1164 | |
| 1165 | if (!features_p) { |
| 1166 | return LY_SUCCESS; |
| 1167 | } |
| 1168 | if (*features) { |
| 1169 | offset = LY_ARRAY_SIZE(*features); |
| 1170 | } |
| 1171 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1172 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 1173 | 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] | 1174 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1175 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1176 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1177 | LY_ARRAY_INCREMENT(*features); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1178 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 1179 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1180 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1181 | 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] | 1182 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1183 | (*features)[offset + u].module = ctx_sc->mod; |
| 1184 | |
| 1185 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1186 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1187 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1188 | |
| 1189 | return LY_SUCCESS; |
| 1190 | } |
| 1191 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1192 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1193 | * @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] | 1194 | * |
| 1195 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1196 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1197 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1198 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1199 | * being currently processed). |
| 1200 | * @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] | 1201 | * @return LY_SUCCESS if everything is ok. |
| 1202 | * @return LY_EVALID if the feature references indirectly itself. |
| 1203 | */ |
| 1204 | static LY_ERR |
| 1205 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1206 | { |
| 1207 | LY_ERR ret = LY_EVALID; |
| 1208 | unsigned int u, v; |
| 1209 | struct ly_set recursion = {0}; |
| 1210 | struct lysc_feature *drv; |
| 1211 | |
| 1212 | if (!depfeatures) { |
| 1213 | return LY_SUCCESS; |
| 1214 | } |
| 1215 | |
| 1216 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1217 | if (feature == 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, depfeatures[u], 0); |
| 1223 | } |
| 1224 | |
| 1225 | for (v = 0; v < recursion.count; ++v) { |
| 1226 | drv = recursion.objs[v]; |
| 1227 | if (!drv->depfeatures) { |
| 1228 | continue; |
| 1229 | } |
| 1230 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1231 | if (feature == drv->depfeatures[u]) { |
| 1232 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1233 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1234 | goto cleanup; |
| 1235 | } |
| 1236 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1237 | } |
| 1238 | } |
| 1239 | ret = LY_SUCCESS; |
| 1240 | |
| 1241 | cleanup: |
| 1242 | ly_set_erase(&recursion, NULL); |
| 1243 | return ret; |
| 1244 | } |
| 1245 | |
| 1246 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1247 | * @brief Create pre-compiled features array. |
| 1248 | * |
| 1249 | * See lys_feature_precompile() for more details. |
| 1250 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1251 | * @param[in] ctx Compile context. |
| 1252 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1253 | * @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] | 1254 | * @return LY_ERR value. |
| 1255 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1256 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1257 | 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] | 1258 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1259 | unsigned int u, v, x; |
| 1260 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1261 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1262 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1263 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1264 | /* find the preprecompiled feature */ |
| 1265 | LY_ARRAY_FOR(features, x) { |
| 1266 | if (strcmp(features[x].name, feature_p->name)) { |
| 1267 | continue; |
| 1268 | } |
| 1269 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1270 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1271 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1272 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1273 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1274 | 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] | 1275 | 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] | 1276 | if (feature->iffeatures) { |
| 1277 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1278 | if (feature->iffeatures[u].features) { |
| 1279 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1280 | /* check for circular dependency - direct reference first,... */ |
| 1281 | if (feature == feature->iffeatures[u].features[v]) { |
| 1282 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1283 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1284 | return LY_EVALID; |
| 1285 | } |
| 1286 | /* ... and indirect circular reference */ |
| 1287 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1288 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1289 | /* add itself into the dependants list */ |
| 1290 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1291 | *df = feature; |
| 1292 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1293 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1294 | } |
| 1295 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1296 | lysc_update_path(ctx, NULL, NULL); |
| 1297 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1298 | done: |
| 1299 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1300 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1301 | |
| 1302 | LOGINT(ctx->ctx); |
| 1303 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1304 | } |
| 1305 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1306 | /** |
| 1307 | * @brief Revert compiled list of features back to the precompiled state. |
| 1308 | * |
| 1309 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1310 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1311 | * |
| 1312 | * @param[in] ctx Compilation context. |
| 1313 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1314 | * and supposed to hold the off_features list. |
| 1315 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1316 | static void |
| 1317 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1318 | { |
| 1319 | unsigned int u, v; |
| 1320 | |
| 1321 | /* keep the off_features list until the complete lys_module is freed */ |
| 1322 | mod->off_features = mod->compiled->features; |
| 1323 | mod->compiled->features = NULL; |
| 1324 | |
| 1325 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1326 | * which may points into the data being freed here */ |
| 1327 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1328 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1329 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1330 | } |
| 1331 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1332 | mod->off_features[u].iffeatures = NULL; |
| 1333 | |
| 1334 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1335 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1336 | } |
| 1337 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1338 | mod->off_features[u].exts = NULL; |
| 1339 | } |
| 1340 | } |
| 1341 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1342 | /** |
| 1343 | * @brief Validate and normalize numeric value from a range definition. |
| 1344 | * @param[in] ctx Compile context. |
| 1345 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1346 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1347 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1348 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1349 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1350 | * @param[in] value String value of the range boundary. |
| 1351 | * @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. |
| 1352 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1353 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1354 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1355 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1356 | 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] | 1357 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1358 | size_t fraction = 0, size; |
| 1359 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1360 | *len = 0; |
| 1361 | |
| 1362 | assert(value); |
| 1363 | /* parse value */ |
| 1364 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1365 | return LY_EVALID; |
| 1366 | } |
| 1367 | |
| 1368 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1369 | ++(*len); |
| 1370 | } |
| 1371 | |
| 1372 | while (isdigit(value[*len])) { |
| 1373 | ++(*len); |
| 1374 | } |
| 1375 | |
| 1376 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1377 | if (basetype == LY_TYPE_DEC64) { |
| 1378 | goto decimal; |
| 1379 | } else { |
| 1380 | *valcopy = strndup(value, *len); |
| 1381 | return LY_SUCCESS; |
| 1382 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1383 | } |
| 1384 | fraction = *len; |
| 1385 | |
| 1386 | ++(*len); |
| 1387 | while (isdigit(value[*len])) { |
| 1388 | ++(*len); |
| 1389 | } |
| 1390 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1391 | if (basetype == LY_TYPE_DEC64) { |
| 1392 | decimal: |
| 1393 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1394 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1395 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1396 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1397 | *len, value, frdigits); |
| 1398 | return LY_EINVAL; |
| 1399 | } |
| 1400 | if (fraction) { |
| 1401 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1402 | } else { |
| 1403 | size = (*len) + frdigits + 1; |
| 1404 | } |
| 1405 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1406 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1407 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1408 | (*valcopy)[size - 1] = '\0'; |
| 1409 | if (fraction) { |
| 1410 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1411 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1412 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1413 | } else { |
| 1414 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1415 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1416 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1417 | } |
| 1418 | return LY_SUCCESS; |
| 1419 | } |
| 1420 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1421 | /** |
| 1422 | * @brief Check that values in range are in ascendant order. |
| 1423 | * @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] | 1424 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1425 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1426 | * @param[in] value Current value to check. |
| 1427 | * @param[in] prev_value The last seen value. |
| 1428 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1429 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1430 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1431 | 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] | 1432 | { |
| 1433 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1434 | 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] | 1435 | return LY_EEXIST; |
| 1436 | } |
| 1437 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1438 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1439 | return LY_EEXIST; |
| 1440 | } |
| 1441 | } |
| 1442 | return LY_SUCCESS; |
| 1443 | } |
| 1444 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1445 | /** |
| 1446 | * @brief Set min/max value of the range part. |
| 1447 | * @param[in] ctx Compile context. |
| 1448 | * @param[in] part Range part structure to fill. |
| 1449 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1450 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1451 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1452 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1453 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1454 | * @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] | 1455 | * @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] | 1456 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1457 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1458 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1459 | * frdigits value), LY_EMEM. |
| 1460 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1461 | static LY_ERR |
| 1462 | 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] | 1463 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1464 | { |
| 1465 | LY_ERR ret = LY_SUCCESS; |
| 1466 | char *valcopy = NULL; |
| 1467 | size_t len; |
| 1468 | |
| 1469 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1470 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1471 | LY_CHECK_GOTO(ret, finalize); |
| 1472 | } |
| 1473 | if (!valcopy && base_range) { |
| 1474 | if (max) { |
| 1475 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1476 | } else { |
| 1477 | part->min_64 = base_range->parts[0].min_64; |
| 1478 | } |
| 1479 | if (!first) { |
| 1480 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1481 | } |
| 1482 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1486 | case LY_TYPE_INT8: /* range */ |
| 1487 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1488 | 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] | 1489 | } else if (max) { |
| 1490 | part->max_64 = INT64_C(127); |
| 1491 | } else { |
| 1492 | part->min_64 = INT64_C(-128); |
| 1493 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1494 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1495 | 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] | 1496 | } |
| 1497 | break; |
| 1498 | case LY_TYPE_INT16: /* range */ |
| 1499 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1500 | 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] | 1501 | } else if (max) { |
| 1502 | part->max_64 = INT64_C(32767); |
| 1503 | } else { |
| 1504 | part->min_64 = INT64_C(-32768); |
| 1505 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1506 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1507 | 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] | 1508 | } |
| 1509 | break; |
| 1510 | case LY_TYPE_INT32: /* range */ |
| 1511 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1512 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1513 | } else if (max) { |
| 1514 | part->max_64 = INT64_C(2147483647); |
| 1515 | } else { |
| 1516 | part->min_64 = INT64_C(-2147483648); |
| 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_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1523 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1524 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1525 | 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] | 1526 | max ? &part->max_64 : &part->min_64); |
| 1527 | } else if (max) { |
| 1528 | part->max_64 = INT64_C(9223372036854775807); |
| 1529 | } else { |
| 1530 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1531 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1532 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1533 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1534 | } |
| 1535 | break; |
| 1536 | case LY_TYPE_UINT8: /* range */ |
| 1537 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1538 | 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] | 1539 | } else if (max) { |
| 1540 | part->max_u64 = UINT64_C(255); |
| 1541 | } else { |
| 1542 | part->min_u64 = UINT64_C(0); |
| 1543 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1544 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1545 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1546 | } |
| 1547 | break; |
| 1548 | case LY_TYPE_UINT16: /* range */ |
| 1549 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1550 | 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] | 1551 | } else if (max) { |
| 1552 | part->max_u64 = UINT64_C(65535); |
| 1553 | } else { |
| 1554 | part->min_u64 = UINT64_C(0); |
| 1555 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1556 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1557 | 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] | 1558 | } |
| 1559 | break; |
| 1560 | case LY_TYPE_UINT32: /* range */ |
| 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(4294967295), 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(4294967295); |
| 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 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1573 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1574 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1575 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1576 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1577 | } else if (max) { |
| 1578 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1579 | } else { |
| 1580 | part->min_u64 = UINT64_C(0); |
| 1581 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1582 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1583 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1584 | } |
| 1585 | break; |
| 1586 | default: |
| 1587 | LOGINT(ctx->ctx); |
| 1588 | ret = LY_EINT; |
| 1589 | } |
| 1590 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1591 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1592 | if (ret == LY_EDENIED) { |
| 1593 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1594 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1595 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1596 | } else if (ret == LY_EVALID) { |
| 1597 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1598 | "Invalid %s restriction - invalid value \"%s\".", |
| 1599 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1600 | } else if (ret == LY_EEXIST) { |
| 1601 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1602 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1603 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1604 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1605 | } else if (!ret && value) { |
| 1606 | *value = *value + len; |
| 1607 | } |
| 1608 | free(valcopy); |
| 1609 | return ret; |
| 1610 | } |
| 1611 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1612 | /** |
| 1613 | * @brief Compile the parsed range restriction. |
| 1614 | * @param[in] ctx Compile context. |
| 1615 | * @param[in] range_p Parsed range structure to compile. |
| 1616 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1617 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1618 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1619 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1620 | * range restriction must be more restrictive than the base_range. |
| 1621 | * @param[in,out] range Pointer to the created current range structure. |
| 1622 | * @return LY_ERR value. |
| 1623 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1624 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1625 | 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] | 1626 | struct lysc_range *base_range, struct lysc_range **range) |
| 1627 | { |
| 1628 | LY_ERR ret = LY_EVALID; |
| 1629 | const char *expr; |
| 1630 | struct lysc_range_part *parts = NULL, *part; |
| 1631 | int range_expected = 0, uns; |
| 1632 | unsigned int parts_done = 0, u, v; |
| 1633 | |
| 1634 | assert(range); |
| 1635 | assert(range_p); |
| 1636 | |
| 1637 | expr = range_p->arg; |
| 1638 | while(1) { |
| 1639 | if (isspace(*expr)) { |
| 1640 | ++expr; |
| 1641 | } else if (*expr == '\0') { |
| 1642 | if (range_expected) { |
| 1643 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1644 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1645 | length_restr ? "length" : "range", range_p->arg); |
| 1646 | goto cleanup; |
| 1647 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1648 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1649 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1650 | length_restr ? "length" : "range", range_p->arg); |
| 1651 | goto cleanup; |
| 1652 | } |
| 1653 | parts_done++; |
| 1654 | break; |
| 1655 | } else if (!strncmp(expr, "min", 3)) { |
| 1656 | if (parts) { |
| 1657 | /* min cannot be used elsewhere than in the first part */ |
| 1658 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1659 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1660 | expr - range_p->arg, range_p->arg); |
| 1661 | goto cleanup; |
| 1662 | } |
| 1663 | expr += 3; |
| 1664 | |
| 1665 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1666 | 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] | 1667 | part->max_64 = part->min_64; |
| 1668 | } else if (*expr == '|') { |
| 1669 | if (!parts || range_expected) { |
| 1670 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1671 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1672 | goto cleanup; |
| 1673 | } |
| 1674 | expr++; |
| 1675 | parts_done++; |
| 1676 | /* process next part of the expression */ |
| 1677 | } else if (!strncmp(expr, "..", 2)) { |
| 1678 | expr += 2; |
| 1679 | while (isspace(*expr)) { |
| 1680 | expr++; |
| 1681 | } |
| 1682 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1683 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1684 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1685 | goto cleanup; |
| 1686 | } |
| 1687 | /* continue expecting the upper boundary */ |
| 1688 | range_expected = 1; |
| 1689 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1690 | /* number */ |
| 1691 | if (range_expected) { |
| 1692 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1693 | 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] | 1694 | range_expected = 0; |
| 1695 | } else { |
| 1696 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1697 | 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] | 1698 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1699 | part->max_64 = part->min_64; |
| 1700 | } |
| 1701 | |
| 1702 | /* continue with possible another expression part */ |
| 1703 | } else if (!strncmp(expr, "max", 3)) { |
| 1704 | expr += 3; |
| 1705 | while (isspace(*expr)) { |
| 1706 | expr++; |
| 1707 | } |
| 1708 | if (*expr != '\0') { |
| 1709 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1710 | length_restr ? "length" : "range", expr); |
| 1711 | goto cleanup; |
| 1712 | } |
| 1713 | if (range_expected) { |
| 1714 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1715 | 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] | 1716 | range_expected = 0; |
| 1717 | } else { |
| 1718 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1719 | 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] | 1720 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1721 | part->min_64 = part->max_64; |
| 1722 | } |
| 1723 | } else { |
| 1724 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1725 | length_restr ? "length" : "range", expr); |
| 1726 | goto cleanup; |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | /* check with the previous range/length restriction */ |
| 1731 | if (base_range) { |
| 1732 | switch (basetype) { |
| 1733 | case LY_TYPE_BINARY: |
| 1734 | case LY_TYPE_UINT8: |
| 1735 | case LY_TYPE_UINT16: |
| 1736 | case LY_TYPE_UINT32: |
| 1737 | case LY_TYPE_UINT64: |
| 1738 | case LY_TYPE_STRING: |
| 1739 | uns = 1; |
| 1740 | break; |
| 1741 | case LY_TYPE_DEC64: |
| 1742 | case LY_TYPE_INT8: |
| 1743 | case LY_TYPE_INT16: |
| 1744 | case LY_TYPE_INT32: |
| 1745 | case LY_TYPE_INT64: |
| 1746 | uns = 0; |
| 1747 | break; |
| 1748 | default: |
| 1749 | LOGINT(ctx->ctx); |
| 1750 | ret = LY_EINT; |
| 1751 | goto cleanup; |
| 1752 | } |
| 1753 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1754 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1755 | goto baseerror; |
| 1756 | } |
| 1757 | /* current lower bound is not lower than the base */ |
| 1758 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1759 | /* base has single value */ |
| 1760 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1761 | /* both lower bounds are the same */ |
| 1762 | if (parts[u].min_64 != parts[u].max_64) { |
| 1763 | /* current continues with a range */ |
| 1764 | goto baseerror; |
| 1765 | } else { |
| 1766 | /* equal single values, move both forward */ |
| 1767 | ++v; |
| 1768 | continue; |
| 1769 | } |
| 1770 | } else { |
| 1771 | /* base is single value lower than current range, so the |
| 1772 | * value from base range is removed in the current, |
| 1773 | * move only base and repeat checking */ |
| 1774 | ++v; |
| 1775 | --u; |
| 1776 | continue; |
| 1777 | } |
| 1778 | } else { |
| 1779 | /* base is the range */ |
| 1780 | if (parts[u].min_64 == parts[u].max_64) { |
| 1781 | /* current is a single value */ |
| 1782 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1783 | /* current is behind the base range, so base range is omitted, |
| 1784 | * move the base and keep the current for further check */ |
| 1785 | ++v; |
| 1786 | --u; |
| 1787 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1788 | continue; |
| 1789 | } else { |
| 1790 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1791 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1792 | /* higher bound is higher than the current higher bound */ |
| 1793 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1794 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1795 | * continue with the same current, but move the base */ |
| 1796 | --u; |
| 1797 | ++v; |
| 1798 | continue; |
| 1799 | } |
| 1800 | /* current range starts within the base range but end behind it */ |
| 1801 | goto baseerror; |
| 1802 | } else { |
| 1803 | /* current range is smaller than the base, |
| 1804 | * move current, but stay with the base */ |
| 1805 | continue; |
| 1806 | } |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | if (u != parts_done) { |
| 1811 | baseerror: |
| 1812 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1813 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1814 | length_restr ? "length" : "range", range_p->arg); |
| 1815 | goto cleanup; |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | if (!(*range)) { |
| 1820 | *range = calloc(1, sizeof **range); |
| 1821 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1822 | } |
| 1823 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1824 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1825 | if (range_p->eapptag) { |
| 1826 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1827 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1828 | } |
| 1829 | if (range_p->emsg) { |
| 1830 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1831 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1832 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1833 | if (range_p->dsc) { |
| 1834 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1835 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1836 | } |
| 1837 | if (range_p->ref) { |
| 1838 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1839 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1840 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1841 | /* extensions are taken only from the last range by the caller */ |
| 1842 | |
| 1843 | (*range)->parts = parts; |
| 1844 | parts = NULL; |
| 1845 | ret = LY_SUCCESS; |
| 1846 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1847 | LY_ARRAY_FREE(parts); |
| 1848 | |
| 1849 | return ret; |
| 1850 | } |
| 1851 | |
| 1852 | /** |
| 1853 | * @brief Checks pattern syntax. |
| 1854 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1855 | * @param[in] ctx Context. |
| 1856 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1857 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1858 | * @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] | 1859 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1860 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1861 | LY_ERR |
| 1862 | 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] | 1863 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1864 | int idx, idx2, start, end, count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1865 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1866 | int err_code; |
| 1867 | const char *orig_ptr; |
| 1868 | PCRE2_SIZE err_offset; |
| 1869 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1870 | #define URANGE_LEN 19 |
| 1871 | char *ublock2urange[][2] = { |
| 1872 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1873 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1874 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1875 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1876 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1877 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1878 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1879 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1880 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1881 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1882 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1883 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1884 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1885 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1886 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1887 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1888 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1889 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1890 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1891 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1892 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1893 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1894 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1895 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1896 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1897 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1898 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1899 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1900 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1901 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1902 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1903 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1904 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1905 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1906 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1907 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1908 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1909 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1910 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1911 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1912 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1913 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1914 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1915 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1916 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1917 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1918 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1919 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1920 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1921 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1922 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1923 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1924 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1925 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1926 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1927 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1928 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1929 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1930 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1931 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1932 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1933 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1934 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1935 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1936 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1937 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1938 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1939 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1940 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1941 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1942 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1943 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1944 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1945 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1946 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1947 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1948 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1949 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1950 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1951 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1952 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1953 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1954 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1955 | {NULL, NULL} |
| 1956 | }; |
| 1957 | |
| 1958 | /* adjust the expression to a Perl equivalent |
| 1959 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1960 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1961 | /* we need to replace all "$" and "^" with "\$" and "\^", count them now */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1962 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1963 | |
| 1964 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1965 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1966 | perl_regex[0] = '\0'; |
| 1967 | |
| 1968 | ptr = perl_regex; |
| 1969 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1970 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1971 | if (orig_ptr[0] == '$') { |
| 1972 | ptr += sprintf(ptr, "\\$"); |
| 1973 | } else if (orig_ptr[0] == '^') { |
| 1974 | ptr += sprintf(ptr, "\\^"); |
| 1975 | } else { |
| 1976 | ptr[0] = orig_ptr[0]; |
| 1977 | ++ptr; |
| 1978 | } |
| 1979 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1980 | ptr[0] = '\0'; |
| 1981 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1982 | |
| 1983 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1984 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1985 | start = ptr - perl_regex; |
| 1986 | |
| 1987 | ptr = strchr(ptr, '}'); |
| 1988 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1989 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1990 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1991 | free(perl_regex); |
| 1992 | return LY_EVALID; |
| 1993 | } |
| 1994 | end = (ptr - perl_regex) + 1; |
| 1995 | |
| 1996 | /* need more space */ |
| 1997 | if (end - start < URANGE_LEN) { |
| 1998 | 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] | 1999 | 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] | 2000 | } |
| 2001 | |
| 2002 | /* find our range */ |
| 2003 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2004 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2005 | break; |
| 2006 | } |
| 2007 | } |
| 2008 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2009 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2010 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2011 | free(perl_regex); |
| 2012 | return LY_EVALID; |
| 2013 | } |
| 2014 | |
| 2015 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 2016 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 2017 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 2018 | ++count; |
| 2019 | } |
| 2020 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 2021 | --count; |
| 2022 | } |
| 2023 | } |
| 2024 | if (count) { |
| 2025 | /* skip brackets */ |
| 2026 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2027 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2028 | } else { |
| 2029 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2030 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2035 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2036 | 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] | 2037 | &err_code, &err_offset, NULL); |
| 2038 | if (!code_local) { |
| 2039 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2040 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2041 | 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] | 2042 | free(perl_regex); |
| 2043 | return LY_EVALID; |
| 2044 | } |
| 2045 | free(perl_regex); |
| 2046 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2047 | if (code) { |
| 2048 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2049 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2050 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | return LY_SUCCESS; |
| 2054 | |
| 2055 | #undef URANGE_LEN |
| 2056 | } |
| 2057 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2058 | /** |
| 2059 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2060 | * @param[in] ctx Compile context. |
| 2061 | * @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] | 2062 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2063 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2064 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2065 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2066 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2067 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2068 | 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] | 2069 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2070 | { |
| 2071 | struct lysc_pattern **pattern; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2072 | unsigned int u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2073 | LY_ERR ret = LY_SUCCESS; |
| 2074 | |
| 2075 | /* first, copy the patterns from the base type */ |
| 2076 | if (base_patterns) { |
| 2077 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2078 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2079 | } |
| 2080 | |
| 2081 | LY_ARRAY_FOR(patterns_p, u) { |
| 2082 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2083 | *pattern = calloc(1, sizeof **pattern); |
| 2084 | ++(*pattern)->refcount; |
| 2085 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2086 | 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] | 2087 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2088 | |
| 2089 | if (patterns_p[u].arg[0] == 0x15) { |
| 2090 | (*pattern)->inverted = 1; |
| 2091 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2092 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2093 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2094 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2095 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2096 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2097 | 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] | 2098 | } |
| 2099 | done: |
| 2100 | return ret; |
| 2101 | } |
| 2102 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2103 | /** |
| 2104 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2105 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2106 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2107 | 0 /* LY_TYPE_UNKNOWN */, |
| 2108 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2109 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2110 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2111 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2112 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2113 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2114 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2115 | 0 /* LY_TYPE_BOOL */, |
| 2116 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2117 | 0 /* LY_TYPE_EMPTY */, |
| 2118 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2119 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2120 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2121 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2122 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2123 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2124 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2125 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2126 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2127 | }; |
| 2128 | |
| 2129 | /** |
| 2130 | * @brief stringification of the YANG built-in data types |
| 2131 | */ |
| 2132 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2133 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2134 | "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] | 2135 | }; |
| 2136 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2137 | /** |
| 2138 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2139 | * @param[in] ctx Compile context. |
| 2140 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2141 | * @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] | 2142 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2143 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2144 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2145 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2146 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2147 | 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] | 2148 | 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] | 2149 | { |
| 2150 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 2151 | unsigned int u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2152 | int32_t value = 0; |
| 2153 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2154 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2155 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2156 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2157 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2158 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2159 | return LY_EVALID; |
| 2160 | } |
| 2161 | |
| 2162 | LY_ARRAY_FOR(enums_p, u) { |
| 2163 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2164 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2165 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2166 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2167 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2168 | if (base_enums) { |
| 2169 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2170 | LY_ARRAY_FOR(base_enums, v) { |
| 2171 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2172 | break; |
| 2173 | } |
| 2174 | } |
| 2175 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 2176 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2177 | "Invalid %s - derived type adds new item \"%s\".", |
| 2178 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2179 | return LY_EVALID; |
| 2180 | } |
| 2181 | match = v; |
| 2182 | } |
| 2183 | |
| 2184 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2185 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2186 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2187 | e->value = (int32_t)enums_p[u].value; |
| 2188 | if (!u || e->value >= value) { |
| 2189 | value = e->value + 1; |
| 2190 | } |
| 2191 | /* check collision with other values */ |
| 2192 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2193 | if (e->value == (*enums)[v].value) { |
| 2194 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2195 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2196 | e->value, e->name, (*enums)[v].name); |
| 2197 | return LY_EVALID; |
| 2198 | } |
| 2199 | } |
| 2200 | } else if (base_enums) { |
| 2201 | /* inherit the assigned value */ |
| 2202 | e->value = base_enums[match].value; |
| 2203 | if (!u || e->value >= value) { |
| 2204 | value = e->value + 1; |
| 2205 | } |
| 2206 | } else { |
| 2207 | /* assign value automatically */ |
| 2208 | if (u && value == INT32_MIN) { |
| 2209 | /* counter overflow */ |
| 2210 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2211 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2212 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2213 | return LY_EVALID; |
| 2214 | } |
| 2215 | e->value = value++; |
| 2216 | } |
| 2217 | } else { /* LY_TYPE_BITS */ |
| 2218 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2219 | e->value = (int32_t)enums_p[u].value; |
| 2220 | if (!u || (uint32_t)e->value >= position) { |
| 2221 | position = (uint32_t)e->value + 1; |
| 2222 | } |
| 2223 | /* check collision with other values */ |
| 2224 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2225 | if (e->value == (*enums)[v].value) { |
| 2226 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2227 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2228 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2229 | return LY_EVALID; |
| 2230 | } |
| 2231 | } |
| 2232 | } else if (base_enums) { |
| 2233 | /* inherit the assigned value */ |
| 2234 | e->value = base_enums[match].value; |
| 2235 | if (!u || (uint32_t)e->value >= position) { |
| 2236 | position = (uint32_t)e->value + 1; |
| 2237 | } |
| 2238 | } else { |
| 2239 | /* assign value automatically */ |
| 2240 | if (u && position == 0) { |
| 2241 | /* counter overflow */ |
| 2242 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2243 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2244 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2245 | return LY_EVALID; |
| 2246 | } |
| 2247 | e->value = position++; |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | if (base_enums) { |
| 2252 | /* the assigned values must not change from the derived type */ |
| 2253 | if (e->value != base_enums[match].value) { |
| 2254 | if (basetype == LY_TYPE_ENUM) { |
| 2255 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2256 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2257 | e->name, base_enums[match].value, e->value); |
| 2258 | } else { |
| 2259 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2260 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2261 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2262 | } |
| 2263 | return LY_EVALID; |
| 2264 | } |
| 2265 | } |
| 2266 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2267 | 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] | 2268 | 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] | 2269 | |
| 2270 | if (basetype == LY_TYPE_BITS) { |
| 2271 | /* keep bits ordered by position */ |
| 2272 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2273 | if (v != u) { |
| 2274 | memcpy(&storage, e, sizeof *e); |
| 2275 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2276 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2277 | } |
| 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | done: |
| 2282 | return ret; |
| 2283 | } |
| 2284 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2285 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2286 | for ((NODE) = (NODE)->parent; \ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2287 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_ACTION)); \ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2288 | (NODE) = (NODE)->parent); \ |
| 2289 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2290 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2291 | TERM; \ |
| 2292 | } |
| 2293 | |
| 2294 | /** |
| 2295 | * @brief Validate the predicate(s) from the leafref path. |
| 2296 | * @param[in] ctx Compile context |
| 2297 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2298 | * 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] | 2299 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2300 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2301 | * @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] | 2302 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2303 | */ |
| 2304 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2305 | 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] | 2306 | 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] | 2307 | { |
| 2308 | LY_ERR ret = LY_EVALID; |
| 2309 | const struct lys_module *mod; |
| 2310 | const struct lysc_node *src_node, *dst_node; |
| 2311 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2312 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2313 | unsigned int dest_parent_times, c; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2314 | const char *start, *end, *pke_end; |
| 2315 | struct ly_set keys = {0}; |
| 2316 | int i; |
| 2317 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2318 | assert(path_context); |
| 2319 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2320 | while (**predicate == '[') { |
| 2321 | start = (*predicate)++; |
| 2322 | |
| 2323 | while (isspace(**predicate)) { |
| 2324 | ++(*predicate); |
| 2325 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2326 | 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] | 2327 | while (isspace(**predicate)) { |
| 2328 | ++(*predicate); |
| 2329 | } |
| 2330 | if (**predicate != '=') { |
| 2331 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2332 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2333 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2334 | goto cleanup; |
| 2335 | } |
| 2336 | ++(*predicate); |
| 2337 | while (isspace(**predicate)) { |
| 2338 | ++(*predicate); |
| 2339 | } |
| 2340 | |
| 2341 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2342 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2343 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2344 | goto cleanup; |
| 2345 | } |
| 2346 | --pke_end; |
| 2347 | while (isspace(*pke_end)) { |
| 2348 | --pke_end; |
| 2349 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2350 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2351 | /* localize path-key-expr */ |
| 2352 | pke_start = path_key_expr = *predicate; |
| 2353 | /* move after the current predicate */ |
| 2354 | *predicate = end + 1; |
| 2355 | |
| 2356 | /* source (must be leaf or leaf-list) */ |
| 2357 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2358 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2359 | if (!mod) { |
| 2360 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2361 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2362 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2363 | goto cleanup; |
| 2364 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2365 | if (!mod->implemented) { |
| 2366 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2367 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2368 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2369 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2370 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2371 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2372 | src_node = NULL; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2373 | if (!(context_node->flags & LYS_KEYLESS)) { |
| 2374 | struct lysc_node *key; |
| 2375 | 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] | 2376 | if (!ly_strncmp(key->name, src, src_len)) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2377 | src_node = key; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2378 | break; |
| 2379 | } |
| 2380 | } |
| 2381 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2382 | if (!src_node) { |
| 2383 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2384 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2385 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2386 | goto cleanup; |
| 2387 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2388 | |
| 2389 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2390 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2391 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2392 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2393 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2394 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2395 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2396 | *predicate - start, start, src_node->name); |
| 2397 | goto cleanup; |
| 2398 | } |
| 2399 | |
| 2400 | /* destination */ |
| 2401 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2402 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2403 | |
| 2404 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2405 | if (strncmp(path_key_expr, "current", 7)) { |
| 2406 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2407 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2408 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2409 | *predicate - start, start); |
| 2410 | goto cleanup; |
| 2411 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2412 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2413 | if (*path_key_expr != '(') { |
| 2414 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2415 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2416 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2417 | if (*path_key_expr != ')') { |
| 2418 | goto error_current_function_invocation; |
| 2419 | } |
| 2420 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2421 | |
| 2422 | if (*path_key_expr != '/') { |
| 2423 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2424 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2425 | *predicate - start, start); |
| 2426 | goto cleanup; |
| 2427 | } |
| 2428 | ++path_key_expr; |
| 2429 | while (isspace(*path_key_expr)) { |
| 2430 | ++path_key_expr; |
| 2431 | } |
| 2432 | |
| 2433 | /* rel-path-keyexpr: |
| 2434 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2435 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2436 | ++dest_parent_times; |
| 2437 | path_key_expr += 2; |
| 2438 | while (isspace(*path_key_expr)) { |
| 2439 | ++path_key_expr; |
| 2440 | } |
| 2441 | if (*path_key_expr != '/') { |
| 2442 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2443 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2444 | *predicate - start, start); |
| 2445 | goto cleanup; |
| 2446 | } |
| 2447 | ++path_key_expr; |
| 2448 | while (isspace(*path_key_expr)) { |
| 2449 | ++path_key_expr; |
| 2450 | } |
| 2451 | |
| 2452 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2453 | * all schema nodes that cannot be instantiated in data tree */ |
| 2454 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2455 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2456 | *predicate - start, start); |
| 2457 | } |
| 2458 | if (!dest_parent_times) { |
| 2459 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2460 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2461 | *predicate - start, start); |
| 2462 | goto cleanup; |
| 2463 | } |
| 2464 | if (path_key_expr == pke_end) { |
| 2465 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2466 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2467 | *predicate - start, start); |
| 2468 | goto cleanup; |
| 2469 | } |
| 2470 | |
| 2471 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2472 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2473 | 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] | 2474 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2475 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2476 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2477 | goto cleanup; |
| 2478 | } |
| 2479 | |
| 2480 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2481 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2482 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2483 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2484 | } |
| 2485 | if (!mod) { |
| 2486 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2487 | "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] | 2488 | *predicate - start, start, dst_len, dst); |
| 2489 | goto cleanup; |
| 2490 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2491 | if (!mod->implemented) { |
| 2492 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2493 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2494 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2495 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame^] | 2496 | 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] | 2497 | if (!dst_node) { |
| 2498 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2499 | "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] | 2500 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2501 | goto cleanup; |
| 2502 | } |
| 2503 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2504 | 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] | 2505 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2506 | "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] | 2507 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2508 | goto cleanup; |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | ret = LY_SUCCESS; |
| 2513 | cleanup: |
| 2514 | ly_set_erase(&keys, NULL); |
| 2515 | return ret; |
| 2516 | } |
| 2517 | |
| 2518 | /** |
| 2519 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2520 | * |
| 2521 | * path-arg = absolute-path / relative-path |
| 2522 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2523 | * relative-path = 1*(".." "/") descendant-path |
| 2524 | * |
| 2525 | * @param[in,out] path Path to parse. |
| 2526 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2527 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2528 | * @param[out] name Name of the token. |
| 2529 | * @param[out] nam_len Length of the name. |
| 2530 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2531 | * must not be changed between consecutive calls. -1 if the |
| 2532 | * path is absolute. |
| 2533 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2534 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2535 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2536 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2537 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2538 | int *parent_times, int *has_predicate) |
| 2539 | { |
| 2540 | int par_times = 0; |
| 2541 | |
| 2542 | assert(path && *path); |
| 2543 | assert(parent_times); |
| 2544 | assert(prefix); |
| 2545 | assert(prefix_len); |
| 2546 | assert(name); |
| 2547 | assert(name_len); |
| 2548 | assert(has_predicate); |
| 2549 | |
| 2550 | *prefix = NULL; |
| 2551 | *prefix_len = 0; |
| 2552 | *name = NULL; |
| 2553 | *name_len = 0; |
| 2554 | *has_predicate = 0; |
| 2555 | |
| 2556 | if (!*parent_times) { |
| 2557 | if (!strncmp(*path, "..", 2)) { |
| 2558 | *path += 2; |
| 2559 | ++par_times; |
| 2560 | while (!strncmp(*path, "/..", 3)) { |
| 2561 | *path += 3; |
| 2562 | ++par_times; |
| 2563 | } |
| 2564 | } |
| 2565 | if (par_times) { |
| 2566 | *parent_times = par_times; |
| 2567 | } else { |
| 2568 | *parent_times = -1; |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | if (**path != '/') { |
| 2573 | return LY_EINVAL; |
| 2574 | } |
| 2575 | /* skip '/' */ |
| 2576 | ++(*path); |
| 2577 | |
| 2578 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2579 | 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] | 2580 | |
| 2581 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2582 | /* path continues by another token or this is the last token */ |
| 2583 | return LY_SUCCESS; |
| 2584 | } else if ((*path)[0] != '[') { |
| 2585 | /* unexpected character */ |
| 2586 | return LY_EINVAL; |
| 2587 | } else { |
| 2588 | /* predicate starting with [ */ |
| 2589 | *has_predicate = 1; |
| 2590 | return LY_SUCCESS; |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2595 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2596 | * |
| 2597 | * The set of features used for target must be a subset of features used for the leafref. |
| 2598 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2599 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2600 | * |
| 2601 | * @param[in] refnode The leafref node. |
| 2602 | * @param[in] target Tha target node of the leafref. |
| 2603 | * @return LY_SUCCESS or LY_EVALID; |
| 2604 | */ |
| 2605 | static LY_ERR |
| 2606 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2607 | { |
| 2608 | LY_ERR ret = LY_EVALID; |
| 2609 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2610 | unsigned int u, v, count; |
| 2611 | struct ly_set features = {0}; |
| 2612 | |
| 2613 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2614 | if (iter->iffeatures) { |
| 2615 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2616 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2617 | 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] | 2618 | } |
| 2619 | } |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2624 | * So when adding features applicable to the target into the features set, we should not be |
| 2625 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2626 | * to the leafref itself. */ |
| 2627 | count = features.count; |
| 2628 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2629 | if (iter->iffeatures) { |
| 2630 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2631 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2632 | 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] | 2633 | /* new feature was added (or LY_EMEM) */ |
| 2634 | goto cleanup; |
| 2635 | } |
| 2636 | } |
| 2637 | } |
| 2638 | } |
| 2639 | } |
| 2640 | ret = LY_SUCCESS; |
| 2641 | |
| 2642 | cleanup: |
| 2643 | ly_set_erase(&features, NULL); |
| 2644 | return ret; |
| 2645 | } |
| 2646 | |
| 2647 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2648 | * @brief Validate the leafref path. |
| 2649 | * @param[in] ctx Compile context |
| 2650 | * @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] | 2651 | * @param[in] leafref Leafref to validate. |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2652 | * @param[out] target Optional resolved leafref target. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2653 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2654 | */ |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2655 | LY_ERR |
| 2656 | lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref, |
| 2657 | const struct lysc_node **target) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2658 | { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 2659 | const struct lysc_node *node = NULL, *parent = NULL; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2660 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2661 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2662 | const char *id, *prefix, *name; |
| 2663 | size_t prefix_len, name_len; |
| 2664 | int parent_times = 0, has_predicate; |
| 2665 | unsigned int iter, u; |
| 2666 | LY_ERR ret = LY_SUCCESS; |
| 2667 | |
| 2668 | assert(ctx); |
| 2669 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2670 | assert(leafref); |
| 2671 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2672 | ctx->path[0] = '\0'; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2673 | lysc_path(startnode, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2674 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2675 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2676 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2677 | id = leafref->path; |
Juraj Vijtiuk | bd0f2a6 | 2019-12-11 15:58:18 +0100 | [diff] [blame] | 2678 | |
| 2679 | if (!*id) { |
| 2680 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Empty leafref path."); |
| 2681 | return LY_EVALID; |
| 2682 | } |
| 2683 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2684 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2685 | if (!iter) { /* first iteration */ |
| 2686 | /* precess ".." in relative paths */ |
| 2687 | if (parent_times > 0) { |
| 2688 | /* move from the context node */ |
| 2689 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2690 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2691 | * all schema nodes that cannot be instantiated in data tree */ |
| 2692 | 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] | 2693 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2694 | } |
| 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2699 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2700 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2701 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2702 | } |
| 2703 | if (!mod) { |
| 2704 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2705 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2706 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2707 | return LY_EVALID; |
| 2708 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2709 | if (!mod->implemented) { |
| 2710 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2711 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2712 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2713 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame^] | 2714 | 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] | 2715 | if (!node) { |
| 2716 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2717 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2718 | return LY_EVALID; |
| 2719 | } |
| 2720 | parent = node; |
| 2721 | |
| 2722 | if (has_predicate) { |
| 2723 | /* we have predicate, so the current result must be list */ |
| 2724 | if (node->nodetype != LYS_LIST) { |
| 2725 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2726 | "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] | 2727 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2728 | return LY_EVALID; |
| 2729 | } |
| 2730 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2731 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2732 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | ++iter; |
| 2736 | } |
| 2737 | if (ret) { |
| 2738 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2739 | "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] | 2740 | return LY_EVALID; |
| 2741 | } |
| 2742 | |
| 2743 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2745 | "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] | 2746 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2747 | return LY_EVALID; |
| 2748 | } |
| 2749 | |
| 2750 | /* check status */ |
| 2751 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2752 | return LY_EVALID; |
| 2753 | } |
| 2754 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2755 | /* check config */ |
| 2756 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2757 | if (node->flags & LYS_CONFIG_R) { |
| 2758 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2759 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2760 | leafref->path); |
| 2761 | return LY_EVALID; |
| 2762 | } |
| 2763 | } |
| 2764 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2765 | /* store the target's type and check for circular chain of leafrefs */ |
| 2766 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2767 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2768 | if (type == (struct lysc_type*)leafref) { |
| 2769 | /* circular chain detected */ |
| 2770 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2771 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2772 | return LY_EVALID; |
| 2773 | } |
| 2774 | } |
| 2775 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2776 | /* check if leafref and its target are under common if-features */ |
| 2777 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2778 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2779 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2780 | leafref->path); |
| 2781 | return LY_EVALID; |
| 2782 | } |
| 2783 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2784 | ctx->path_len = 1; |
| 2785 | ctx->path[1] = '\0'; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2786 | if (target) { |
| 2787 | *target = node; |
| 2788 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2789 | return LY_SUCCESS; |
| 2790 | } |
| 2791 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2792 | 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] | 2793 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2794 | /** |
| 2795 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2796 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2797 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2798 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2799 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2800 | * @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] | 2801 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2802 | * @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] | 2803 | * @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] | 2804 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2805 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2806 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2807 | * @return LY_ERR value. |
| 2808 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2809 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2810 | 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] | 2811 | 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] | 2812 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2813 | { |
| 2814 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2815 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2816 | struct lysc_type_bin *bin; |
| 2817 | struct lysc_type_num *num; |
| 2818 | struct lysc_type_str *str; |
| 2819 | struct lysc_type_bits *bits; |
| 2820 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2821 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2822 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2823 | struct lysc_type_union *un, *un_aux; |
| 2824 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2825 | |
| 2826 | switch (basetype) { |
| 2827 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2828 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2829 | |
| 2830 | /* 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] | 2831 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2832 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2833 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2834 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2835 | 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] | 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | if (tpdfname) { |
| 2840 | type_p->compiled = *type; |
| 2841 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2842 | } |
| 2843 | break; |
| 2844 | case LY_TYPE_BITS: |
| 2845 | /* RFC 7950 9.7 - bits */ |
| 2846 | bits = (struct lysc_type_bits*)(*type); |
| 2847 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2848 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2849 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2850 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2851 | } |
| 2852 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2853 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2854 | /* 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] | 2855 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2856 | 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] | 2857 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2858 | 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] | 2859 | free(*type); |
| 2860 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2861 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2862 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | if (tpdfname) { |
| 2866 | type_p->compiled = *type; |
| 2867 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2868 | } |
| 2869 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2870 | case LY_TYPE_DEC64: |
| 2871 | dec = (struct lysc_type_dec*)(*type); |
| 2872 | |
| 2873 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2874 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2875 | if (!type_p->fraction_digits) { |
| 2876 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2877 | 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] | 2878 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2879 | 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] | 2880 | free(*type); |
| 2881 | *type = NULL; |
| 2882 | } |
| 2883 | return LY_EVALID; |
| 2884 | } |
| 2885 | } else if (type_p->fraction_digits) { |
| 2886 | /* 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] | 2887 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2888 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2889 | "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] | 2890 | tpdfname); |
| 2891 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2892 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2893 | "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] | 2894 | free(*type); |
| 2895 | *type = NULL; |
| 2896 | } |
| 2897 | return LY_EVALID; |
| 2898 | } |
| 2899 | dec->fraction_digits = type_p->fraction_digits; |
| 2900 | |
| 2901 | /* RFC 7950 9.2.4 - range */ |
| 2902 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2903 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2904 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2905 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2906 | 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] | 2907 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2908 | } |
| 2909 | |
| 2910 | if (tpdfname) { |
| 2911 | type_p->compiled = *type; |
| 2912 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2913 | } |
| 2914 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2915 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2916 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2917 | |
| 2918 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2919 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2920 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2921 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2922 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2923 | 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] | 2924 | } |
| 2925 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2926 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2927 | } |
| 2928 | |
| 2929 | /* RFC 7950 9.4.5 - pattern */ |
| 2930 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2931 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2932 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2933 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2934 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2935 | } |
| 2936 | |
| 2937 | if (tpdfname) { |
| 2938 | type_p->compiled = *type; |
| 2939 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2940 | } |
| 2941 | break; |
| 2942 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2943 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2944 | |
| 2945 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2946 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2947 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2948 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2949 | } |
| 2950 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2951 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2952 | /* 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] | 2953 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2954 | 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] | 2955 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2956 | 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] | 2957 | free(*type); |
| 2958 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2959 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2960 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | if (tpdfname) { |
| 2964 | type_p->compiled = *type; |
| 2965 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2966 | } |
| 2967 | break; |
| 2968 | case LY_TYPE_INT8: |
| 2969 | case LY_TYPE_UINT8: |
| 2970 | case LY_TYPE_INT16: |
| 2971 | case LY_TYPE_UINT16: |
| 2972 | case LY_TYPE_INT32: |
| 2973 | case LY_TYPE_UINT32: |
| 2974 | case LY_TYPE_INT64: |
| 2975 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2976 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2977 | |
| 2978 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2979 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2980 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2981 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2982 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2983 | 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] | 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | if (tpdfname) { |
| 2988 | type_p->compiled = *type; |
| 2989 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2990 | } |
| 2991 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2992 | case LY_TYPE_IDENT: |
| 2993 | idref = (struct lysc_type_identityref*)(*type); |
| 2994 | |
| 2995 | /* RFC 7950 9.10.2 - base */ |
| 2996 | if (type_p->bases) { |
| 2997 | if (base) { |
| 2998 | /* only the directly derived identityrefs can contain base specification */ |
| 2999 | if (tpdfname) { |
| 3000 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3001 | "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] | 3002 | tpdfname); |
| 3003 | } else { |
| 3004 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3005 | "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] | 3006 | free(*type); |
| 3007 | *type = NULL; |
| 3008 | } |
| 3009 | return LY_EVALID; |
| 3010 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3011 | 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] | 3012 | } |
| 3013 | |
| 3014 | if (!base && !type_p->flags) { |
| 3015 | /* type derived from identityref built-in type must contain at least one base */ |
| 3016 | if (tpdfname) { |
| 3017 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 3018 | } else { |
| 3019 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 3020 | free(*type); |
| 3021 | *type = NULL; |
| 3022 | } |
| 3023 | return LY_EVALID; |
| 3024 | } |
| 3025 | |
| 3026 | if (tpdfname) { |
| 3027 | type_p->compiled = *type; |
| 3028 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3029 | } |
| 3030 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3031 | case LY_TYPE_LEAFREF: |
| 3032 | /* RFC 7950 9.9.3 - require-instance */ |
| 3033 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3034 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3035 | if (tpdfname) { |
| 3036 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3037 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 3038 | } else { |
| 3039 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3040 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 3041 | free(*type); |
| 3042 | *type = NULL; |
| 3043 | } |
| 3044 | return LY_EVALID; |
| 3045 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3046 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 3047 | } else if (base) { |
| 3048 | /* inherit */ |
| 3049 | ((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] | 3050 | } else { |
| 3051 | /* default is true */ |
| 3052 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 3053 | } |
| 3054 | if (type_p->path) { |
| 3055 | 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] | 3056 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3057 | } else if (base) { |
| 3058 | 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] | 3059 | ((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] | 3060 | } else if (tpdfname) { |
| 3061 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 3062 | return LY_EVALID; |
| 3063 | } else { |
| 3064 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 3065 | free(*type); |
| 3066 | *type = NULL; |
| 3067 | return LY_EVALID; |
| 3068 | } |
| 3069 | if (tpdfname) { |
| 3070 | type_p->compiled = *type; |
| 3071 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3072 | } |
| 3073 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 3074 | case LY_TYPE_INST: |
| 3075 | /* RFC 7950 9.9.3 - require-instance */ |
| 3076 | if (type_p->flags & LYS_SET_REQINST) { |
| 3077 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 3078 | } else { |
| 3079 | /* default is true */ |
| 3080 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 3081 | } |
| 3082 | |
| 3083 | if (tpdfname) { |
| 3084 | type_p->compiled = *type; |
| 3085 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3086 | } |
| 3087 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3088 | case LY_TYPE_UNION: |
| 3089 | un = (struct lysc_type_union*)(*type); |
| 3090 | |
| 3091 | /* RFC 7950 7.4 - type */ |
| 3092 | if (type_p->types) { |
| 3093 | if (base) { |
| 3094 | /* only the directly derived union can contain types specification */ |
| 3095 | if (tpdfname) { |
| 3096 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3097 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 3098 | tpdfname); |
| 3099 | } else { |
| 3100 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3101 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 3102 | free(*type); |
| 3103 | *type = NULL; |
| 3104 | } |
| 3105 | return LY_EVALID; |
| 3106 | } |
| 3107 | /* compile the type */ |
| 3108 | additional = 0; |
| 3109 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 3110 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3111 | 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] | 3112 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 3113 | /* add space for additional types from the union subtype */ |
| 3114 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 3115 | 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))); |
| 3116 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3117 | un->types = (void*)((uint32_t*)(p) + 1); |
| 3118 | |
| 3119 | /* copy subtypes of the subtype union */ |
| 3120 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 3121 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 3122 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 3123 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3124 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3125 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 3126 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 3127 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 3128 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 3129 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 3130 | /* TODO extensions */ |
| 3131 | |
| 3132 | } else { |
| 3133 | un->types[u + additional] = un_aux->types[v]; |
| 3134 | ++un_aux->types[v]->refcount; |
| 3135 | } |
| 3136 | ++additional; |
| 3137 | LY_ARRAY_INCREMENT(un->types); |
| 3138 | } |
| 3139 | /* compensate u increment in main loop */ |
| 3140 | --additional; |
| 3141 | |
| 3142 | /* free the replaced union subtype */ |
| 3143 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 3144 | } else { |
| 3145 | LY_ARRAY_INCREMENT(un->types); |
| 3146 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | if (!base && !type_p->flags) { |
| 3151 | /* type derived from union built-in type must contain at least one type */ |
| 3152 | if (tpdfname) { |
| 3153 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 3154 | } else { |
| 3155 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 3156 | free(*type); |
| 3157 | *type = NULL; |
| 3158 | } |
| 3159 | return LY_EVALID; |
| 3160 | } |
| 3161 | |
| 3162 | if (tpdfname) { |
| 3163 | type_p->compiled = *type; |
| 3164 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3165 | } |
| 3166 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3167 | case LY_TYPE_BOOL: |
| 3168 | case LY_TYPE_EMPTY: |
| 3169 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 3170 | break; |
| 3171 | } |
| 3172 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 3173 | done: |
| 3174 | return ret; |
| 3175 | } |
| 3176 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3177 | /** |
| 3178 | * @brief Compile information about the leaf/leaf-list's type. |
| 3179 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3180 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 3181 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3182 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3183 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 3184 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3185 | * @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] | 3186 | * @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] | 3187 | * @return LY_ERR value. |
| 3188 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3189 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3190 | 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] | 3191 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3192 | { |
| 3193 | LY_ERR ret = LY_SUCCESS; |
| 3194 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3195 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3196 | struct type_context { |
| 3197 | const struct lysp_tpdf *tpdf; |
| 3198 | struct lysp_node *node; |
| 3199 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3200 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3201 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3202 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3203 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3204 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3205 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3206 | |
| 3207 | (*type) = NULL; |
| 3208 | |
| 3209 | tctx = calloc(1, sizeof *tctx); |
| 3210 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3211 | 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] | 3212 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 3213 | ret == LY_SUCCESS; |
| 3214 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 3215 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 3216 | if (basetype) { |
| 3217 | break; |
| 3218 | } |
| 3219 | |
| 3220 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3221 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3222 | 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] | 3223 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3224 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3225 | if (units && !*units) { |
| 3226 | /* inherit units */ |
| 3227 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3228 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3229 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3230 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3231 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3232 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3233 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3234 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3235 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3236 | break; |
| 3237 | } |
| 3238 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3239 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3240 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3241 | * 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] | 3242 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3243 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3244 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3245 | dummyloops = 1; |
| 3246 | goto preparenext; |
| 3247 | } else { |
| 3248 | tctx = NULL; |
| 3249 | break; |
| 3250 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3251 | } |
| 3252 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3253 | /* circular typedef reference detection */ |
| 3254 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3255 | /* local part */ |
| 3256 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3257 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3258 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3259 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3260 | free(tctx); |
| 3261 | ret = LY_EVALID; |
| 3262 | goto cleanup; |
| 3263 | } |
| 3264 | } |
| 3265 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3266 | /* global part for unions corner case */ |
| 3267 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3268 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3269 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3270 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3271 | free(tctx); |
| 3272 | ret = LY_EVALID; |
| 3273 | goto cleanup; |
| 3274 | } |
| 3275 | } |
| 3276 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3277 | /* store information for the following processing */ |
| 3278 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3279 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3280 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3281 | /* prepare next loop */ |
| 3282 | tctx_prev = tctx; |
| 3283 | tctx = calloc(1, sizeof *tctx); |
| 3284 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3285 | } |
| 3286 | free(tctx); |
| 3287 | |
| 3288 | /* allocate type according to the basetype */ |
| 3289 | switch (basetype) { |
| 3290 | case LY_TYPE_BINARY: |
| 3291 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3292 | break; |
| 3293 | case LY_TYPE_BITS: |
| 3294 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3295 | break; |
| 3296 | case LY_TYPE_BOOL: |
| 3297 | case LY_TYPE_EMPTY: |
| 3298 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3299 | break; |
| 3300 | case LY_TYPE_DEC64: |
| 3301 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3302 | break; |
| 3303 | case LY_TYPE_ENUM: |
| 3304 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3305 | break; |
| 3306 | case LY_TYPE_IDENT: |
| 3307 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3308 | break; |
| 3309 | case LY_TYPE_INST: |
| 3310 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3311 | break; |
| 3312 | case LY_TYPE_LEAFREF: |
| 3313 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3314 | break; |
| 3315 | case LY_TYPE_STRING: |
| 3316 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3317 | break; |
| 3318 | case LY_TYPE_UNION: |
| 3319 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3320 | break; |
| 3321 | case LY_TYPE_INT8: |
| 3322 | case LY_TYPE_UINT8: |
| 3323 | case LY_TYPE_INT16: |
| 3324 | case LY_TYPE_UINT16: |
| 3325 | case LY_TYPE_INT32: |
| 3326 | case LY_TYPE_UINT32: |
| 3327 | case LY_TYPE_INT64: |
| 3328 | case LY_TYPE_UINT64: |
| 3329 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3330 | break; |
| 3331 | case LY_TYPE_UNKNOWN: |
| 3332 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3333 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3334 | ret = LY_EVALID; |
| 3335 | goto cleanup; |
| 3336 | } |
| 3337 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3338 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3339 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3340 | ly_data_type2str[basetype]); |
| 3341 | free(*type); |
| 3342 | (*type) = NULL; |
| 3343 | ret = LY_EVALID; |
| 3344 | goto cleanup; |
| 3345 | } |
| 3346 | |
| 3347 | /* get restrictions from the referred typedefs */ |
| 3348 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3349 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3350 | |
| 3351 | /* remember the typedef context for circular check */ |
| 3352 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3353 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3354 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3355 | base = tctx->tpdf->type.compiled; |
| 3356 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3357 | } 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] | 3358 | /* no change, just use the type information from the base */ |
| 3359 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3360 | ++base->refcount; |
| 3361 | continue; |
| 3362 | } |
| 3363 | |
| 3364 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3365 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3366 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3367 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3368 | ret = LY_EVALID; |
| 3369 | goto cleanup; |
| 3370 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3371 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3372 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3373 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3374 | ret = LY_EVALID; |
| 3375 | goto cleanup; |
| 3376 | } |
| 3377 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3378 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3379 | /* TODO user type plugins */ |
| 3380 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3381 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3382 | 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] | 3383 | 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] | 3384 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3385 | LY_CHECK_GOTO(ret, cleanup); |
| 3386 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3387 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3388 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3389 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3390 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3391 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3392 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3393 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3394 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3395 | /* TODO user type plugins */ |
| 3396 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3397 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3398 | 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] | 3399 | LY_CHECK_GOTO(ret, cleanup); |
| 3400 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3401 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3402 | free(*type); |
| 3403 | (*type) = base; |
| 3404 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3405 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3406 | if (dflt && !(*type)->dflt) { |
| 3407 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3408 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3409 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3410 | (*type)->dflt->realtype = (*type); |
| 3411 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3412 | 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] | 3413 | 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] | 3414 | if (err) { |
| 3415 | ly_err_print(err); |
| 3416 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3417 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3418 | ly_err_free(err); |
| 3419 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3420 | if (ret == LY_EINCOMPLETE) { |
| 3421 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3422 | 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] | 3423 | |
| 3424 | /* but in general result is so far ok */ |
| 3425 | ret = LY_SUCCESS; |
| 3426 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3427 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3428 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3429 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3430 | 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] | 3431 | |
| 3432 | cleanup: |
| 3433 | ly_set_erase(&tpdf_chain, free); |
| 3434 | return ret; |
| 3435 | } |
| 3436 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3437 | /** |
| 3438 | * @brief Compile status information of the given node. |
| 3439 | * |
| 3440 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3441 | * has the status correctly set during the compilation. |
| 3442 | * |
| 3443 | * @param[in] ctx Compile context |
| 3444 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3445 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3446 | * the compatibility with the parent's status value. |
| 3447 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3448 | * @return LY_ERR value. |
| 3449 | */ |
| 3450 | static LY_ERR |
| 3451 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3452 | { |
| 3453 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3454 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3455 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3456 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3457 | if ((parent_flags & 0x3) != 0x3) { |
| 3458 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3459 | * combination of bits (0x3) which marks the uses_status value */ |
| 3460 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3461 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3462 | } |
| 3463 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3464 | } else { |
| 3465 | (*node_flags) |= LYS_STATUS_CURR; |
| 3466 | } |
| 3467 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3468 | /* check status compatibility with the parent */ |
| 3469 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3470 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3471 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3472 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3473 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3474 | } else { /* LYS_STATUS_DEPRC */ |
| 3475 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3476 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3477 | } |
| 3478 | return LY_EVALID; |
| 3479 | } |
| 3480 | } |
| 3481 | return LY_SUCCESS; |
| 3482 | } |
| 3483 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3484 | /** |
| 3485 | * @brief Check uniqness of the node/action/notification name. |
| 3486 | * |
| 3487 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3488 | * structures, but they share the namespace so we need to check their name collisions. |
| 3489 | * |
| 3490 | * @param[in] ctx Compile context. |
| 3491 | * @param[in] children List (linked list) of data nodes to go through. |
| 3492 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3493 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3494 | * @param[in] name Name of the item to find in the given lists. |
| 3495 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3496 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3497 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3498 | */ |
| 3499 | static LY_ERR |
| 3500 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3501 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3502 | const char *name, void *exclude) |
| 3503 | { |
| 3504 | const struct lysc_node *iter; |
| 3505 | unsigned int u; |
| 3506 | |
| 3507 | LY_LIST_FOR(children, iter) { |
| 3508 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3509 | goto error; |
| 3510 | } |
| 3511 | } |
| 3512 | LY_ARRAY_FOR(actions, u) { |
| 3513 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3514 | goto error; |
| 3515 | } |
| 3516 | } |
| 3517 | LY_ARRAY_FOR(notifs, u) { |
| 3518 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3519 | goto error; |
| 3520 | } |
| 3521 | } |
| 3522 | return LY_SUCCESS; |
| 3523 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3524 | 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] | 3525 | return LY_EEXIST; |
| 3526 | } |
| 3527 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3528 | 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] | 3529 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3530 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3531 | * @brief Compile parsed RPC/action schema node information. |
| 3532 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3533 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3534 | * @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] | 3535 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3536 | * @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). |
| 3537 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3538 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3539 | */ |
| 3540 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3541 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3542 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3543 | { |
| 3544 | LY_ERR ret = LY_SUCCESS; |
| 3545 | struct lysp_node *child_p; |
| 3546 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3547 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3548 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3549 | lysc_update_path(ctx, parent, action_p->name); |
| 3550 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3551 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3552 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3553 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3554 | action_p->name, action)) { |
| 3555 | return LY_EVALID; |
| 3556 | } |
| 3557 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3558 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3559 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3560 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3561 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3562 | return LY_EVALID; |
| 3563 | } |
| 3564 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3565 | action->nodetype = LYS_ACTION; |
| 3566 | action->module = ctx->mod; |
| 3567 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3568 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3569 | action->sp = action_p; |
| 3570 | } |
| 3571 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3572 | |
| 3573 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3574 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3575 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3576 | |
| 3577 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3578 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3579 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3580 | 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] | 3581 | 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] | 3582 | |
| 3583 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3584 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3585 | 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] | 3586 | 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] | 3587 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3588 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3589 | 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] | 3590 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3591 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3592 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3593 | |
| 3594 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3595 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3596 | 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] | 3597 | 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] | 3598 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3599 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3600 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3601 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3602 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3603 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3604 | |
| 3605 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3606 | /* do not check "must" semantics in a grouping */ |
| 3607 | ly_set_add(&ctx->unres, action, 0); |
| 3608 | } |
| 3609 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3610 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3611 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3612 | return ret; |
| 3613 | } |
| 3614 | |
| 3615 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3616 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3617 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3618 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3619 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3620 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3621 | * @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] | 3622 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3623 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3624 | */ |
| 3625 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3626 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3627 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3628 | { |
| 3629 | LY_ERR ret = LY_SUCCESS; |
| 3630 | struct lysp_node *child_p; |
| 3631 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3632 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3633 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3634 | lysc_update_path(ctx, parent, notif_p->name); |
| 3635 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3636 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3637 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3638 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3639 | notif_p->name, notif)) { |
| 3640 | return LY_EVALID; |
| 3641 | } |
| 3642 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3643 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3644 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3645 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3646 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3647 | return LY_EVALID; |
| 3648 | } |
| 3649 | |
| 3650 | notif->nodetype = LYS_NOTIF; |
| 3651 | notif->module = ctx->mod; |
| 3652 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3653 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3654 | notif->sp = notif_p; |
| 3655 | } |
| 3656 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3657 | |
| 3658 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3659 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3660 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3661 | |
| 3662 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3663 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3664 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3665 | 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] | 3666 | 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] | 3667 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3668 | /* do not check "must" semantics in a grouping */ |
| 3669 | ly_set_add(&ctx->unres, notif, 0); |
| 3670 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3671 | 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] | 3672 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3673 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3674 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3675 | 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] | 3676 | } |
| 3677 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3678 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3679 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3680 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3681 | return ret; |
| 3682 | } |
| 3683 | |
| 3684 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3685 | * @brief Compile parsed container node information. |
| 3686 | * @param[in] ctx Compile context |
| 3687 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3688 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3689 | * is enriched with the container-specific information. |
| 3690 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3691 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3692 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3693 | 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] | 3694 | { |
| 3695 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3696 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3697 | struct lysp_node *child_p; |
| 3698 | unsigned int u; |
| 3699 | LY_ERR ret = LY_SUCCESS; |
| 3700 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3701 | if (cont_p->presence) { |
| 3702 | cont->flags |= LYS_PRESENCE; |
| 3703 | } |
| 3704 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3705 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3706 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3707 | } |
| 3708 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3709 | 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] | 3710 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3711 | /* do not check "must" semantics in a grouping */ |
| 3712 | ly_set_add(&ctx->unres, cont, 0); |
| 3713 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3714 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3715 | 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] | 3716 | |
| 3717 | done: |
| 3718 | return ret; |
| 3719 | } |
| 3720 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3721 | /* |
| 3722 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3723 | * @param[in] ctx Compile context. |
| 3724 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3725 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3726 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3727 | * @return LY_ERR value. |
| 3728 | */ |
| 3729 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3730 | 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] | 3731 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3732 | unsigned int u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3733 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3734 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3735 | 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] | 3736 | leaf->units ? NULL : &leaf->units)); |
| 3737 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3738 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3739 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3740 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3741 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3742 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3743 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3744 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3745 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3746 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3747 | } |
| 3748 | } else { |
| 3749 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3750 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3751 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3752 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3753 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3754 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3755 | } |
| 3756 | } |
| 3757 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3758 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3759 | ly_set_add(&ctx->unres, leaf, 0); |
| 3760 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3761 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3762 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3763 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3764 | ly_set_add(&ctx->unres, leaf, 0); |
| 3765 | } |
| 3766 | } |
| 3767 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3768 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3769 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3770 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3771 | return LY_EVALID; |
| 3772 | } |
| 3773 | } |
| 3774 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3775 | return LY_SUCCESS; |
| 3776 | } |
| 3777 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3778 | /** |
| 3779 | * @brief Compile parsed leaf node information. |
| 3780 | * @param[in] ctx Compile context |
| 3781 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3782 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3783 | * is enriched with the leaf-specific information. |
| 3784 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3785 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3786 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3787 | 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] | 3788 | { |
| 3789 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3790 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3791 | unsigned int u; |
| 3792 | LY_ERR ret = LY_SUCCESS; |
| 3793 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3794 | 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] | 3795 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3796 | /* do not check "must" semantics in a grouping */ |
| 3797 | ly_set_add(&ctx->unres, leaf, 0); |
| 3798 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3799 | if (leaf_p->units) { |
| 3800 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3801 | leaf->flags |= LYS_SET_UNITS; |
| 3802 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3803 | |
| 3804 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3805 | leaf->dflt = (void*)leaf_p->dflt; |
| 3806 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3807 | if (ret) { |
| 3808 | leaf->dflt = NULL; |
| 3809 | return ret; |
| 3810 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3811 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3812 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3813 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3814 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3815 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3816 | leaf->dflt->realtype = leaf->type; |
| 3817 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3818 | 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] | 3819 | 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] | 3820 | leaf->dflt->realtype->refcount++; |
| 3821 | if (err) { |
| 3822 | ly_err_print(err); |
| 3823 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3824 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3825 | ly_err_free(err); |
| 3826 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3827 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3828 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3829 | 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] | 3830 | |
| 3831 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3832 | ret = LY_SUCCESS; |
| 3833 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3834 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3835 | leaf->flags |= LYS_SET_DFLT; |
| 3836 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3837 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3838 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3839 | done: |
| 3840 | return ret; |
| 3841 | } |
| 3842 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3843 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3844 | * @brief Compile parsed leaf-list node information. |
| 3845 | * @param[in] ctx Compile context |
| 3846 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3847 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3848 | * is enriched with the leaf-list-specific information. |
| 3849 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3850 | */ |
| 3851 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3852 | 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] | 3853 | { |
| 3854 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3855 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3856 | unsigned int u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3857 | LY_ERR ret = LY_SUCCESS; |
| 3858 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3859 | 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] | 3860 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3861 | /* do not check "must" semantics in a grouping */ |
| 3862 | ly_set_add(&ctx->unres, llist, 0); |
| 3863 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3864 | if (llist_p->units) { |
| 3865 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3866 | llist->flags |= LYS_SET_UNITS; |
| 3867 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3868 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3869 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3870 | llist->dflts = (void*)llist_p->dflts; |
| 3871 | 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] | 3872 | if (ret != LY_SUCCESS) { |
| 3873 | /* make sure the defaults are freed correctly */ |
| 3874 | if (llist_p->dflts) { |
| 3875 | llist->dflts = NULL; |
| 3876 | } |
| 3877 | return ret; |
| 3878 | } |
| 3879 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3880 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3881 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3882 | 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] | 3883 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3884 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3885 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3886 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3887 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3888 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3889 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3890 | llist->dflts[u]->realtype = llist->type; |
| 3891 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3892 | 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] | 3893 | 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] | 3894 | llist->dflts[u]->realtype->refcount++; |
| 3895 | if (err) { |
| 3896 | ly_err_print(err); |
| 3897 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3898 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3899 | ly_err_free(err); |
| 3900 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3901 | if (ret == LY_EINCOMPLETE) { |
| 3902 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3903 | 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] | 3904 | |
| 3905 | /* but in general result is so far ok */ |
| 3906 | ret = LY_SUCCESS; |
| 3907 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3908 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3909 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3910 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3911 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3912 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3913 | /* configuration data values must be unique - so check the default values */ |
| 3914 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3915 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3916 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3917 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3918 | 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] | 3919 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3920 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3921 | if (dynamic) { |
| 3922 | free((char*)val); |
| 3923 | } |
| 3924 | return LY_EVALID; |
| 3925 | } |
| 3926 | } |
| 3927 | } |
| 3928 | } |
| 3929 | |
| 3930 | /* 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] | 3931 | |
| 3932 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3933 | if (llist->min) { |
| 3934 | llist->flags |= LYS_MAND_TRUE; |
| 3935 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3936 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3937 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3938 | done: |
| 3939 | return ret; |
| 3940 | } |
| 3941 | |
| 3942 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3943 | * @brief Compile information about list's uniques. |
| 3944 | * @param[in] ctx Compile context. |
| 3945 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3946 | * @param[in] uniques Sized array list of unique statements. |
| 3947 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3948 | * @return LY_ERR value. |
| 3949 | */ |
| 3950 | static LY_ERR |
| 3951 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3952 | { |
| 3953 | LY_ERR ret = LY_SUCCESS; |
| 3954 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3955 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3956 | const char *keystr, *delim; |
| 3957 | size_t len; |
| 3958 | unsigned int v; |
| 3959 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3960 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3961 | |
| 3962 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3963 | config = -1; |
| 3964 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3965 | keystr = uniques[v]; |
| 3966 | while (keystr) { |
| 3967 | delim = strpbrk(keystr, " \t\n"); |
| 3968 | if (delim) { |
| 3969 | len = delim - keystr; |
| 3970 | while (isspace(*delim)) { |
| 3971 | ++delim; |
| 3972 | } |
| 3973 | } else { |
| 3974 | len = strlen(keystr); |
| 3975 | } |
| 3976 | |
| 3977 | /* unique node must be present */ |
| 3978 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3979 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3980 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3981 | if (ret != LY_SUCCESS) { |
| 3982 | if (ret == LY_EDENIED) { |
| 3983 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3984 | "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] | 3985 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3986 | } |
| 3987 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3988 | } else if (flags) { |
| 3989 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3990 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3991 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3992 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3993 | } |
| 3994 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3995 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3996 | /* all referenced leafs must be of the same config type */ |
| 3997 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3998 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3999 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4000 | return LY_EVALID; |
| 4001 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 4002 | config = 1; |
| 4003 | } else { /* LYS_CONFIG_R */ |
| 4004 | config = 0; |
| 4005 | } |
| 4006 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4007 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 4008 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 4009 | if (parent->nodetype == LYS_LIST) { |
| 4010 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4011 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 4012 | return LY_EVALID; |
| 4013 | } |
| 4014 | } |
| 4015 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4016 | /* check status */ |
| 4017 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 4018 | (*key)->flags, (*key)->module, (*key)->name)); |
| 4019 | |
| 4020 | /* mark leaf as unique */ |
| 4021 | (*key)->flags |= LYS_UNIQUE; |
| 4022 | |
| 4023 | /* next unique value in line */ |
| 4024 | keystr = delim; |
| 4025 | } |
| 4026 | /* next unique definition */ |
| 4027 | } |
| 4028 | |
| 4029 | return LY_SUCCESS; |
| 4030 | } |
| 4031 | |
| 4032 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4033 | * @brief Compile parsed list node information. |
| 4034 | * @param[in] ctx Compile context |
| 4035 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4036 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4037 | * is enriched with the list-specific information. |
| 4038 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4039 | */ |
| 4040 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4041 | 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] | 4042 | { |
| 4043 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 4044 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 4045 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4046 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4047 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4048 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4049 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4050 | LY_ERR ret = LY_SUCCESS; |
| 4051 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4052 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4053 | if (list->min) { |
| 4054 | list->flags |= LYS_MAND_TRUE; |
| 4055 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4056 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 4057 | |
| 4058 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4059 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4060 | } |
| 4061 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4062 | 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] | 4063 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4064 | /* do not check "must" semantics in a grouping */ |
| 4065 | ly_set_add(&ctx->unres, list, 0); |
| 4066 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4067 | |
| 4068 | /* keys */ |
| 4069 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 4070 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 4071 | return LY_EVALID; |
| 4072 | } |
| 4073 | |
| 4074 | /* find all the keys (must be direct children) */ |
| 4075 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4076 | if (!keystr) { |
| 4077 | /* keyless list */ |
| 4078 | list->flags |= LYS_KEYLESS; |
| 4079 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4080 | while (keystr) { |
| 4081 | delim = strpbrk(keystr, " \t\n"); |
| 4082 | if (delim) { |
| 4083 | len = delim - keystr; |
| 4084 | while (isspace(*delim)) { |
| 4085 | ++delim; |
| 4086 | } |
| 4087 | } else { |
| 4088 | len = strlen(keystr); |
| 4089 | } |
| 4090 | |
| 4091 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame^] | 4092 | 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] | 4093 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4094 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4095 | "The list's key \"%.*s\" not found.", len, keystr); |
| 4096 | return LY_EVALID; |
| 4097 | } |
| 4098 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4099 | if (key->flags & LYS_KEY) { |
| 4100 | /* the node was already marked as a key */ |
| 4101 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4102 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 4103 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4104 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4105 | |
| 4106 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4107 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4108 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4109 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 4110 | return LY_EVALID; |
| 4111 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4112 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4113 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4114 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4115 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4116 | "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] | 4117 | return LY_EVALID; |
| 4118 | } |
| 4119 | } else { |
| 4120 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4121 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4122 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4123 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4124 | return LY_EVALID; |
| 4125 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4126 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4127 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4128 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4129 | return LY_EVALID; |
| 4130 | } |
| 4131 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4132 | |
| 4133 | /* check status */ |
| 4134 | 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] | 4135 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4136 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4137 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4138 | if (key->dflt) { |
| 4139 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 4140 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 4141 | free(key->dflt); |
| 4142 | key->dflt = NULL; |
| 4143 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4144 | } |
| 4145 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4146 | key->flags |= LYS_KEY; |
| 4147 | |
| 4148 | /* move it to the correct position */ |
| 4149 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 4150 | /* fix links in closest previous siblings of the key */ |
| 4151 | if (key->next) { |
| 4152 | key->next->prev = key->prev; |
| 4153 | } else { |
| 4154 | /* last child */ |
| 4155 | list->child->prev = key->prev; |
| 4156 | } |
| 4157 | if (key->prev->next) { |
| 4158 | key->prev->next = key->next; |
| 4159 | } |
| 4160 | /* fix links in the key */ |
| 4161 | if (prev_key) { |
| 4162 | key->prev = (struct lysc_node*)prev_key; |
| 4163 | key->next = prev_key->next; |
| 4164 | } else { |
| 4165 | key->prev = list->child->prev; |
| 4166 | key->next = list->child; |
| 4167 | } |
| 4168 | /* fix links in closes future siblings of the key */ |
| 4169 | if (prev_key) { |
| 4170 | if (prev_key->next) { |
| 4171 | prev_key->next->prev = (struct lysc_node*)key; |
| 4172 | } else { |
| 4173 | list->child->prev = (struct lysc_node*)key; |
| 4174 | } |
| 4175 | prev_key->next = (struct lysc_node*)key; |
| 4176 | } else { |
| 4177 | list->child->prev = (struct lysc_node*)key; |
| 4178 | } |
| 4179 | /* fix links in parent */ |
| 4180 | if (!key->prev->next) { |
| 4181 | list->child = (struct lysc_node*)key; |
| 4182 | } |
| 4183 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4184 | |
| 4185 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4186 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4187 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4188 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | /* uniques */ |
| 4192 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4193 | 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] | 4194 | } |
| 4195 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4196 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 4197 | 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] | 4198 | |
| 4199 | done: |
| 4200 | return ret; |
| 4201 | } |
| 4202 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4203 | /** |
| 4204 | * @brief Do some checks and set the default choice's case. |
| 4205 | * |
| 4206 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 4207 | * |
| 4208 | * @param[in] ctx Compile context. |
| 4209 | * @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, |
| 4210 | * not the reference to the imported module. |
| 4211 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 4212 | * @return LY_ERR value. |
| 4213 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4214 | static LY_ERR |
| 4215 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 4216 | { |
| 4217 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 4218 | const char *prefix = NULL, *name; |
| 4219 | size_t prefix_len = 0; |
| 4220 | |
| 4221 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4222 | name = strchr(dflt, ':'); |
| 4223 | if (name) { |
| 4224 | prefix = dflt; |
| 4225 | prefix_len = name - prefix; |
| 4226 | ++name; |
| 4227 | } else { |
| 4228 | name = dflt; |
| 4229 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 4230 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4231 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 4232 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4233 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 4234 | prefix_len, prefix); |
| 4235 | return LY_EVALID; |
| 4236 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame^] | 4237 | 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] | 4238 | if (!ch->dflt) { |
| 4239 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4240 | "Default case \"%s\" not found.", dflt); |
| 4241 | return LY_EVALID; |
| 4242 | } |
| 4243 | /* no mandatory nodes directly under the default case */ |
| 4244 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4245 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 4246 | break; |
| 4247 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4248 | if (iter->flags & LYS_MAND_TRUE) { |
| 4249 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4250 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 4251 | return LY_EVALID; |
| 4252 | } |
| 4253 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4254 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4255 | return LY_SUCCESS; |
| 4256 | } |
| 4257 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4258 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4259 | 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] | 4260 | { |
| 4261 | struct lys_module *mod; |
| 4262 | const char *prefix = NULL, *name; |
| 4263 | size_t prefix_len = 0; |
| 4264 | struct lysc_node_case *cs; |
| 4265 | struct lysc_node *node; |
| 4266 | |
| 4267 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4268 | name = strchr(dflt, ':'); |
| 4269 | if (name) { |
| 4270 | prefix = dflt; |
| 4271 | prefix_len = name - prefix; |
| 4272 | ++name; |
| 4273 | } else { |
| 4274 | name = dflt; |
| 4275 | } |
| 4276 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 4277 | if (prefix) { |
| 4278 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 4279 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4280 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 4281 | "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] | 4282 | return LY_EVALID; |
| 4283 | } |
| 4284 | } else { |
| 4285 | mod = ctx->mod; |
| 4286 | } |
| 4287 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame^] | 4288 | 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] | 4289 | if (!cs) { |
| 4290 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4291 | "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4292 | return LY_EVALID; |
| 4293 | } |
| 4294 | |
| 4295 | /* check that there is no mandatory node */ |
| 4296 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4297 | if (node->parent != (struct lysc_node*)cs) { |
| 4298 | break; |
| 4299 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4300 | if (node->flags & LYS_MAND_TRUE) { |
| 4301 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4302 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4303 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4304 | return LY_EVALID; |
| 4305 | } |
| 4306 | } |
| 4307 | |
| 4308 | /* set the default case in choice */ |
| 4309 | ch->dflt = cs; |
| 4310 | cs->flags |= LYS_SET_DFLT; |
| 4311 | |
| 4312 | return LY_SUCCESS; |
| 4313 | } |
| 4314 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4315 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4316 | * @brief Compile parsed choice node information. |
| 4317 | * @param[in] ctx Compile context |
| 4318 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4319 | * @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] | 4320 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4321 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4322 | */ |
| 4323 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4324 | 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] | 4325 | { |
| 4326 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4327 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4328 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4329 | LY_ERR ret = LY_SUCCESS; |
| 4330 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4331 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4332 | if (child_p->nodetype == LYS_CASE) { |
| 4333 | 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] | 4334 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4335 | } |
| 4336 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4337 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4342 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4343 | 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] | 4344 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4345 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4346 | return ret; |
| 4347 | } |
| 4348 | |
| 4349 | /** |
| 4350 | * @brief Compile parsed anydata or anyxml node information. |
| 4351 | * @param[in] ctx Compile context |
| 4352 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4353 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4354 | * is enriched with the any-specific information. |
| 4355 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4356 | */ |
| 4357 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4358 | 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] | 4359 | { |
| 4360 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4361 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4362 | unsigned int u; |
| 4363 | LY_ERR ret = LY_SUCCESS; |
| 4364 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4365 | 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] | 4366 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4367 | /* do not check "must" semantics in a grouping */ |
| 4368 | ly_set_add(&ctx->unres, any, 0); |
| 4369 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4370 | |
| 4371 | if (any->flags & LYS_CONFIG_W) { |
| 4372 | 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] | 4373 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4374 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4375 | done: |
| 4376 | return ret; |
| 4377 | } |
| 4378 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4379 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4380 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4381 | * |
| 4382 | * @param[in] ctx Compile context |
| 4383 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4384 | * the choice itself is expected instead of a specific case node. |
| 4385 | * @param[in] node Schema node to connect into the list. |
| 4386 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 4387 | */ |
| 4388 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4389 | 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] | 4390 | { |
| 4391 | struct lysc_node **children; |
| 4392 | |
| 4393 | if (node->nodetype == LYS_CASE) { |
| 4394 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4395 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4396 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4397 | } |
| 4398 | if (children) { |
| 4399 | if (!(*children)) { |
| 4400 | /* first child */ |
| 4401 | *children = node; |
| 4402 | } else if (*children != node) { |
| 4403 | /* by the condition in previous branch we cover the choice/case children |
| 4404 | * - the children list is shared by the choice and the the first case, in addition |
| 4405 | * the first child of each case must be referenced from the case node. So the node is |
| 4406 | * actually always already inserted in case it is the first children - so here such |
| 4407 | * a situation actually corresponds to the first branch */ |
| 4408 | /* insert at the end of the parent's children list */ |
| 4409 | (*children)->prev->next = node; |
| 4410 | node->prev = (*children)->prev; |
| 4411 | (*children)->prev = node; |
| 4412 | |
| 4413 | /* check the name uniqueness */ |
| 4414 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4415 | lysc_node_notifs(parent), node->name, node)) { |
| 4416 | return LY_EEXIST; |
| 4417 | } |
| 4418 | } |
| 4419 | } |
| 4420 | return LY_SUCCESS; |
| 4421 | } |
| 4422 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4423 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4424 | * @brief Prepare the case structure in choice node for the new data node. |
| 4425 | * |
| 4426 | * 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 |
| 4427 | * created in the choice when the first child was processed. |
| 4428 | * |
| 4429 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4430 | * @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, |
| 4431 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4432 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4433 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4434 | * @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, |
| 4435 | * 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] | 4436 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4437 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4438 | 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] | 4439 | { |
| 4440 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4441 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4442 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4443 | unsigned int u; |
| 4444 | LY_ERR ret; |
| 4445 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4446 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4447 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4448 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4449 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4450 | return NULL; \ |
| 4451 | } \ |
| 4452 | } |
| 4453 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4454 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4455 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4456 | |
| 4457 | /* we have to add an implicit case node into the parent choice */ |
| 4458 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4459 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4460 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4461 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4462 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4463 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4464 | /* the case is already present since the child is not its first children */ |
| 4465 | return (struct lysc_node_case*)ch->cases->prev; |
| 4466 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4467 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4468 | |
| 4469 | /* explicit parent case is not present (this is its first child) */ |
| 4470 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4471 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4472 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4473 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4474 | cs->sp = node_p; |
| 4475 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4476 | /* 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] | 4477 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4478 | |
| 4479 | if (node_p->when) { |
| 4480 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4481 | 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] | 4482 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4483 | |
| 4484 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4485 | /* do not check "when" semantics in a grouping */ |
| 4486 | ly_set_add(&ctx->unres, cs, 0); |
| 4487 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4488 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4489 | 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] | 4490 | } else { |
| 4491 | LOGINT(ctx->ctx); |
| 4492 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4493 | } |
| 4494 | cs->module = ctx->mod; |
| 4495 | cs->prev = (struct lysc_node*)cs; |
| 4496 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4497 | 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] | 4498 | cs->child = child; |
| 4499 | |
| 4500 | return cs; |
| 4501 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4502 | if (cs) { |
| 4503 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4504 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4505 | return NULL; |
| 4506 | |
| 4507 | #undef UNIQUE_CHECK |
| 4508 | } |
| 4509 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4510 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4511 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4512 | * |
| 4513 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4514 | * @param[in] node Target node where the config is supposed to be changed. |
| 4515 | * @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] | 4516 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4517 | * 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] | 4518 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4519 | * @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] | 4520 | * @return LY_ERR value. |
| 4521 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4522 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4523 | 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] | 4524 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4525 | { |
| 4526 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4527 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4528 | |
| 4529 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4530 | /* nothing to do */ |
| 4531 | return LY_SUCCESS; |
| 4532 | } |
| 4533 | |
| 4534 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4535 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4536 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4537 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4538 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4539 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4540 | return LY_EVALID; |
| 4541 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4542 | node->flags |= LYS_SET_CONFIG; |
| 4543 | } else { |
| 4544 | if (node->flags & LYS_SET_CONFIG) { |
| 4545 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4546 | /* setting config flags, but have node with explicit config true */ |
| 4547 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4548 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4549 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4550 | return LY_EVALID; |
| 4551 | } |
| 4552 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4553 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4554 | return LY_SUCCESS; |
| 4555 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4556 | } |
| 4557 | node->flags &= ~LYS_CONFIG_MASK; |
| 4558 | node->flags |= config; |
| 4559 | |
| 4560 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4561 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4562 | 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] | 4563 | } |
| 4564 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4565 | return LY_SUCCESS; |
| 4566 | } |
| 4567 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4568 | /** |
| 4569 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4570 | * |
| 4571 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4572 | * the flag to such parents from a mandatory children. |
| 4573 | * |
| 4574 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4575 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4576 | * (mandatory children was removed). |
| 4577 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4578 | void |
| 4579 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4580 | { |
| 4581 | struct lysc_node *iter; |
| 4582 | |
| 4583 | if (add) { /* set flag */ |
| 4584 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4585 | parent = parent->parent) { |
| 4586 | parent->flags |= LYS_MAND_TRUE; |
| 4587 | } |
| 4588 | } else { /* unset flag */ |
| 4589 | 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] | 4590 | 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] | 4591 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4592 | /* there is another mandatory node */ |
| 4593 | return; |
| 4594 | } |
| 4595 | } |
| 4596 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4597 | parent->flags &= ~LYS_MAND_TRUE; |
| 4598 | } |
| 4599 | } |
| 4600 | } |
| 4601 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4602 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4603 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4604 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4605 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4606 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4607 | */ |
| 4608 | static void |
| 4609 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4610 | { |
| 4611 | unsigned int v; |
| 4612 | size_t len; |
| 4613 | |
| 4614 | len = strlen(aug_p->nodeid); |
| 4615 | LY_ARRAY_FOR(result, v) { |
| 4616 | if (strlen(result[v]->nodeid) <= len) { |
| 4617 | continue; |
| 4618 | } |
| 4619 | if (v < LY_ARRAY_SIZE(result)) { |
| 4620 | /* move the rest of array */ |
| 4621 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4622 | break; |
| 4623 | } |
| 4624 | } |
| 4625 | result[v] = aug_p; |
| 4626 | LY_ARRAY_INCREMENT(result); |
| 4627 | } |
| 4628 | |
| 4629 | /** |
| 4630 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4631 | * |
| 4632 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4633 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4634 | * |
| 4635 | * @param[in] ctx Compile context. |
| 4636 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4637 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4638 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4639 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4640 | * @return LY_ERR value. |
| 4641 | */ |
| 4642 | LY_ERR |
| 4643 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4644 | { |
| 4645 | struct lysp_augment **result = NULL; |
| 4646 | unsigned int u, v; |
| 4647 | size_t count = 0; |
| 4648 | |
| 4649 | assert(augments); |
| 4650 | |
| 4651 | /* get count of the augments in module and all its submodules */ |
| 4652 | if (aug_p) { |
| 4653 | count += LY_ARRAY_SIZE(aug_p); |
| 4654 | } |
| 4655 | LY_ARRAY_FOR(inc_p, u) { |
| 4656 | if (inc_p[u].submodule->augments) { |
| 4657 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4658 | } |
| 4659 | } |
| 4660 | |
| 4661 | if (!count) { |
| 4662 | *augments = NULL; |
| 4663 | return LY_SUCCESS; |
| 4664 | } |
| 4665 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4666 | |
| 4667 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4668 | * together, so there can be even /z/y betwwen them. */ |
| 4669 | LY_ARRAY_FOR(aug_p, u) { |
| 4670 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4671 | } |
| 4672 | LY_ARRAY_FOR(inc_p, u) { |
| 4673 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4674 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4675 | } |
| 4676 | } |
| 4677 | |
| 4678 | *augments = result; |
| 4679 | return LY_SUCCESS; |
| 4680 | } |
| 4681 | |
| 4682 | /** |
| 4683 | * @brief Compile the parsed augment connecting it into its target. |
| 4684 | * |
| 4685 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4686 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4687 | * are already implemented and compiled. |
| 4688 | * |
| 4689 | * @param[in] ctx Compile context. |
| 4690 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4691 | * @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 |
| 4692 | * children in case of the augmenting uses data. |
| 4693 | * @return LY_SUCCESS on success. |
| 4694 | * @return LY_EVALID on failure. |
| 4695 | */ |
| 4696 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4697 | 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] | 4698 | { |
| 4699 | LY_ERR ret = LY_SUCCESS; |
| 4700 | struct lysp_node *node_p, *case_node_p; |
| 4701 | struct lysc_node *target; /* target target of the augment */ |
| 4702 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4703 | struct lysc_when **when, *when_shared; |
| 4704 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4705 | uint16_t flags = 0; |
| 4706 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4707 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4708 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4709 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4710 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4711 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4712 | 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] | 4713 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4714 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4715 | if (ret != LY_SUCCESS) { |
| 4716 | if (ret == LY_EDENIED) { |
| 4717 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4718 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4719 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4720 | } |
| 4721 | return LY_EVALID; |
| 4722 | } |
| 4723 | |
| 4724 | /* check for mandatory nodes |
| 4725 | * - new cases augmenting some choice can have mandatory nodes |
| 4726 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4727 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4728 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4729 | allow_mandatory = 1; |
| 4730 | } |
| 4731 | |
| 4732 | when_shared = NULL; |
| 4733 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4734 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4735 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4736 | && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF))) |
Radek Krejci | 2d56a89 | 2019-02-19 09:05:26 +0100 | [diff] [blame] | 4737 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4738 | && !(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] | 4739 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4740 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4741 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4742 | return LY_EVALID; |
| 4743 | } |
| 4744 | |
| 4745 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4746 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4747 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4748 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4749 | } else { |
| 4750 | 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] | 4751 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4752 | } |
| 4753 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4754 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4755 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4756 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4757 | * 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] | 4758 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4759 | /* 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] | 4760 | 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] | 4761 | } else if (target->nodetype == LYS_CHOICE) { |
| 4762 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4763 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4764 | } else { |
| 4765 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4766 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4767 | } |
| 4768 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4769 | 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] | 4770 | node->flags &= ~LYS_MAND_TRUE; |
| 4771 | lys_compile_mandatory_parents(target, 0); |
| 4772 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4773 | "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] | 4774 | return LY_EVALID; |
| 4775 | } |
| 4776 | |
| 4777 | /* pass augment's when to all the children */ |
| 4778 | if (aug_p->when) { |
| 4779 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4780 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4781 | 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] | 4782 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4783 | |
| 4784 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4785 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4786 | ly_set_add(&ctx->unres, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4787 | } |
| 4788 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4789 | when_shared = *when; |
| 4790 | } else { |
| 4791 | ++when_shared->refcount; |
| 4792 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4793 | |
| 4794 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4795 | /* in this case check "when" again for all children because of dummy node check */ |
| 4796 | ly_set_add(&ctx->unres, node, 0); |
| 4797 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4798 | } |
| 4799 | } |
| 4800 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4801 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4802 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4803 | switch (target->nodetype) { |
| 4804 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4805 | 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] | 4806 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4807 | 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] | 4808 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4809 | break; |
| 4810 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4811 | 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] | 4812 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4813 | 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] | 4814 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4815 | break; |
| 4816 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4817 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4818 | if (aug_p->actions) { |
| 4819 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4820 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4821 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4822 | return LY_EVALID; |
| 4823 | } |
| 4824 | if (aug_p->notifs) { |
| 4825 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4826 | "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] | 4827 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4828 | return LY_EVALID; |
| 4829 | } |
| 4830 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4831 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4832 | lysc_update_path(ctx, NULL, NULL); |
| 4833 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4834 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4835 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4836 | return ret; |
| 4837 | } |
| 4838 | |
| 4839 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4840 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4841 | * |
| 4842 | * @param[in] ctx Compile context. |
| 4843 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4844 | * @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] | 4845 | * @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] | 4846 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4847 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4848 | * 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] | 4849 | * @return LY_ERR value. |
| 4850 | */ |
| 4851 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4852 | 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] | 4853 | { |
| 4854 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4855 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4856 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4857 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4858 | return LY_EVALID; |
| 4859 | } |
| 4860 | |
| 4861 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4862 | /* check if node has default value */ |
| 4863 | if (node->nodetype & LYS_LEAF) { |
| 4864 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4865 | if (refine_flag) { |
| 4866 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4867 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4868 | return LY_EVALID; |
| 4869 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4870 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4871 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4872 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4873 | |
| 4874 | /* update the list of incomplete default values if needed */ |
| 4875 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4876 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4877 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4878 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4879 | free(leaf->dflt); |
| 4880 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4881 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4882 | } |
| 4883 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4884 | if (refine_flag) { |
| 4885 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4886 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4887 | return LY_EVALID; |
| 4888 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4889 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4890 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4891 | 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] | 4892 | return LY_EVALID; |
| 4893 | } |
| 4894 | |
| 4895 | node->flags &= ~LYS_MAND_FALSE; |
| 4896 | node->flags |= LYS_MAND_TRUE; |
| 4897 | lys_compile_mandatory_parents(node->parent, 1); |
| 4898 | } else { |
| 4899 | /* make mandatory false */ |
| 4900 | node->flags &= ~LYS_MAND_TRUE; |
| 4901 | node->flags |= LYS_MAND_FALSE; |
| 4902 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4903 | 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] | 4904 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4905 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4906 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4907 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4908 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4909 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4910 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4911 | } |
| 4912 | } |
| 4913 | return LY_SUCCESS; |
| 4914 | } |
| 4915 | |
| 4916 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4917 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4918 | * If present, also apply uses's modificators. |
| 4919 | * |
| 4920 | * @param[in] ctx Compile context |
| 4921 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4922 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4923 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4924 | * the compile context. |
| 4925 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4926 | */ |
| 4927 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4928 | 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] | 4929 | { |
| 4930 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4931 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4932 | /* 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] | 4933 | struct lysc_node_container context_node_fake = |
| 4934 | {.nodetype = LYS_CONTAINER, |
| 4935 | .module = ctx->mod, |
| 4936 | .flags = parent ? parent->flags : 0, |
| 4937 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4938 | .prev = (struct lysc_node*)&context_node_fake, |
| 4939 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4940 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4941 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4942 | int found; |
| 4943 | const char *id, *name, *prefix; |
| 4944 | size_t prefix_len, name_len; |
| 4945 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4946 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4947 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4948 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4949 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4950 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4951 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4952 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4953 | unsigned int actions_index, notifs_index; |
| 4954 | struct lysc_notif **notifs = NULL; |
| 4955 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4956 | |
| 4957 | /* search for the grouping definition */ |
| 4958 | found = 0; |
| 4959 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4960 | 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] | 4961 | if (prefix) { |
| 4962 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4963 | if (!mod) { |
| 4964 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4965 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4966 | return LY_EVALID; |
| 4967 | } |
| 4968 | } else { |
| 4969 | mod = ctx->mod_def; |
| 4970 | } |
| 4971 | if (mod == ctx->mod_def) { |
| 4972 | 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] | 4973 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4974 | LY_ARRAY_FOR(grp, u) { |
| 4975 | if (!strcmp(grp[u].name, name)) { |
| 4976 | grp = &grp[u]; |
| 4977 | found = 1; |
| 4978 | break; |
| 4979 | } |
| 4980 | } |
| 4981 | } |
| 4982 | } |
| 4983 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4984 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4985 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4986 | if (grp) { |
| 4987 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4988 | if (!strcmp(grp[u].name, name)) { |
| 4989 | grp = &grp[u]; |
| 4990 | found = 1; |
| 4991 | } |
| 4992 | } |
| 4993 | } |
| 4994 | if (!found && mod->parsed->includes) { |
| 4995 | /* ... and all the submodules */ |
| 4996 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4997 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4998 | if (grp) { |
| 4999 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 5000 | if (!strcmp(grp[v].name, name)) { |
| 5001 | grp = &grp[v]; |
| 5002 | found = 1; |
| 5003 | } |
| 5004 | } |
| 5005 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5006 | } |
| 5007 | } |
| 5008 | } |
| 5009 | if (!found) { |
| 5010 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5011 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 5012 | return LY_EVALID; |
| 5013 | } |
| 5014 | |
| 5015 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 5016 | grp_stack_count = ctx->groupings.count; |
| 5017 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 5018 | if (grp_stack_count == ctx->groupings.count) { |
| 5019 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 5020 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5021 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 5022 | return LY_EVALID; |
| 5023 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5024 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5025 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 5026 | grp->flags |= LYS_USED_GRP; |
| 5027 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5028 | |
| 5029 | /* switch context's mod_def */ |
| 5030 | mod_old = ctx->mod_def; |
| 5031 | ctx->mod_def = mod; |
| 5032 | |
| 5033 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5034 | 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] | 5035 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5036 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5037 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5038 | /* 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] | 5039 | 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] | 5040 | |
| 5041 | /* some preparation for applying refines */ |
| 5042 | if (grp->data == node_p) { |
| 5043 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 5044 | if (parent) { |
| 5045 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 5046 | } else if (ctx->mod->compiled->data) { |
| 5047 | child = ctx->mod->compiled->data; |
| 5048 | } else { |
| 5049 | child = NULL; |
| 5050 | } |
| 5051 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5052 | } |
| 5053 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5054 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5055 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5056 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5057 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5058 | /* 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] | 5059 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5060 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5061 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5062 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 5063 | |
| 5064 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5065 | /* do not check "when" semantics in a grouping */ |
| 5066 | ly_set_add(&ctx->unres, child, 0); |
| 5067 | } |
| 5068 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5069 | when_shared = *when; |
| 5070 | } else { |
| 5071 | ++when_shared->refcount; |
| 5072 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5073 | |
| 5074 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5075 | /* in this case check "when" again for all children because of dummy node check */ |
| 5076 | ly_set_add(&ctx->unres, child, 0); |
| 5077 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5078 | } |
| 5079 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5080 | } |
| 5081 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5082 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5083 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5084 | /* 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] | 5085 | 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] | 5086 | } |
| 5087 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5088 | /* compile actions */ |
| 5089 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 5090 | if (actions) { |
| 5091 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5092 | 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] | 5093 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 5094 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 5095 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 5096 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 5097 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | /* compile notifications */ |
| 5102 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 5103 | if (notifs) { |
| 5104 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5105 | 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] | 5106 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 5107 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 5108 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 5109 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 5110 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5111 | } |
| 5112 | } |
| 5113 | |
| 5114 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5115 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5116 | 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] | 5117 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5118 | 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] | 5119 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5120 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5121 | /* reload previous context's mod_def */ |
| 5122 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5123 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5124 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5125 | /* apply refine */ |
| 5126 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5127 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 5128 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5129 | 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] | 5130 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5131 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5132 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5133 | |
| 5134 | /* default value */ |
| 5135 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5136 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5137 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5138 | "Invalid refine of default - %s cannot hold %d default values.", |
| 5139 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5140 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5141 | } |
| 5142 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 5143 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5144 | "Invalid refine of default - %s cannot hold default value(s).", |
| 5145 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5146 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5147 | } |
| 5148 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5149 | struct ly_err_item *err = NULL; |
| 5150 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 5151 | if (leaf->dflt) { |
| 5152 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5153 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5154 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5155 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5156 | } else { |
| 5157 | /* prepare a new one */ |
| 5158 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 5159 | leaf->dflt->realtype = leaf->type; |
| 5160 | } |
| 5161 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5162 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5163 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 5164 | 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] | 5165 | 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] | 5166 | leaf->dflt->realtype->refcount++; |
| 5167 | if (err) { |
| 5168 | ly_err_print(err); |
| 5169 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5170 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 5171 | ly_err_free(err); |
| 5172 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5173 | if (rc == LY_EINCOMPLETE) { |
| 5174 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5175 | 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] | 5176 | |
| 5177 | /* but in general result is so far ok */ |
| 5178 | rc = LY_SUCCESS; |
| 5179 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5180 | LY_CHECK_GOTO(rc, cleanup); |
| 5181 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5182 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5183 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 5184 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5185 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5186 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5187 | "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] | 5188 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5189 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5190 | |
| 5191 | /* remove previous set of default values */ |
| 5192 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5193 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5194 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 5195 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 5196 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5197 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5198 | LY_ARRAY_FREE(llist->dflts); |
| 5199 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5200 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5201 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5202 | |
| 5203 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5204 | 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] | 5205 | 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] | 5206 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5207 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5208 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5209 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5210 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5211 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 5212 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5213 | 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] | 5214 | 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] | 5215 | 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] | 5216 | llist->dflts[u]->realtype->refcount++; |
| 5217 | if (err) { |
| 5218 | ly_err_print(err); |
| 5219 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5220 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 5221 | ly_err_free(err); |
| 5222 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5223 | if (rc == LY_EINCOMPLETE) { |
| 5224 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5225 | 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] | 5226 | |
| 5227 | /* but in general result is so far ok */ |
| 5228 | rc = LY_SUCCESS; |
| 5229 | } |
| 5230 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5231 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5232 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5233 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5234 | if (((struct lysc_node_choice*)node)->dflt) { |
| 5235 | /* unset LYS_SET_DFLT from the current default case */ |
| 5236 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 5237 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5238 | 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] | 5239 | } |
| 5240 | } |
| 5241 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5242 | /* description */ |
| 5243 | if (rfn->dsc) { |
| 5244 | FREE_STRING(ctx->ctx, node->dsc); |
| 5245 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 5246 | } |
| 5247 | |
| 5248 | /* reference */ |
| 5249 | if (rfn->ref) { |
| 5250 | FREE_STRING(ctx->ctx, node->ref); |
| 5251 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 5252 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5253 | |
| 5254 | /* config */ |
| 5255 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5256 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5257 | 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] | 5258 | } else { |
| 5259 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5260 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5261 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5262 | } |
| 5263 | |
| 5264 | /* mandatory */ |
| 5265 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5266 | 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] | 5267 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5268 | |
| 5269 | /* presence */ |
| 5270 | if (rfn->presence) { |
| 5271 | if (node->nodetype != LYS_CONTAINER) { |
| 5272 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5273 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5274 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5275 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5276 | } |
| 5277 | node->flags |= LYS_PRESENCE; |
| 5278 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5279 | |
| 5280 | /* must */ |
| 5281 | if (rfn->musts) { |
| 5282 | switch (node->nodetype) { |
| 5283 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5284 | 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] | 5285 | break; |
| 5286 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5287 | 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] | 5288 | break; |
| 5289 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5290 | 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] | 5291 | break; |
| 5292 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5293 | 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] | 5294 | break; |
| 5295 | case LYS_ANYXML: |
| 5296 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5297 | 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] | 5298 | break; |
| 5299 | default: |
| 5300 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5301 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5302 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5303 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5304 | } |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 5305 | ly_set_add(&ctx->unres, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5306 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5307 | |
| 5308 | /* min/max-elements */ |
| 5309 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5310 | switch (node->nodetype) { |
| 5311 | case LYS_LEAFLIST: |
| 5312 | if (rfn->flags & LYS_SET_MAX) { |
| 5313 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5314 | } |
| 5315 | if (rfn->flags & LYS_SET_MIN) { |
| 5316 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5317 | if (rfn->min) { |
| 5318 | node->flags |= LYS_MAND_TRUE; |
| 5319 | lys_compile_mandatory_parents(node->parent, 1); |
| 5320 | } else { |
| 5321 | node->flags &= ~LYS_MAND_TRUE; |
| 5322 | lys_compile_mandatory_parents(node->parent, 0); |
| 5323 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5324 | } |
| 5325 | break; |
| 5326 | case LYS_LIST: |
| 5327 | if (rfn->flags & LYS_SET_MAX) { |
| 5328 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5329 | } |
| 5330 | if (rfn->flags & LYS_SET_MIN) { |
| 5331 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5332 | if (rfn->min) { |
| 5333 | node->flags |= LYS_MAND_TRUE; |
| 5334 | lys_compile_mandatory_parents(node->parent, 1); |
| 5335 | } else { |
| 5336 | node->flags &= ~LYS_MAND_TRUE; |
| 5337 | lys_compile_mandatory_parents(node->parent, 0); |
| 5338 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5339 | } |
| 5340 | break; |
| 5341 | default: |
| 5342 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5343 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5344 | (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] | 5345 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5346 | } |
| 5347 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5348 | |
| 5349 | /* if-feature */ |
| 5350 | if (rfn->iffeatures) { |
| 5351 | /* 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] | 5352 | 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] | 5353 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5354 | |
| 5355 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5356 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5357 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5358 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 5359 | for (u = 0; u < refined.count; ++u) { |
| 5360 | node = (struct lysc_node*)refined.objs[u]; |
| 5361 | rfn = &uses_p->refines[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5362 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5363 | |
| 5364 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5365 | if ((node->flags & LYS_MAND_TRUE) && |
| 5366 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5367 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5368 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5369 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5370 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5371 | } |
| 5372 | |
| 5373 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5374 | if (node->nodetype == LYS_LIST) { |
| 5375 | min = ((struct lysc_node_list*)node)->min; |
| 5376 | max = ((struct lysc_node_list*)node)->max; |
| 5377 | } else { |
| 5378 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5379 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5380 | } |
| 5381 | if (min > max) { |
| 5382 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5383 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5384 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5385 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5386 | } |
| 5387 | } |
| 5388 | } |
| 5389 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5390 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5391 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5392 | |
| 5393 | cleanup: |
| 5394 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5395 | if (context_node_fake.child) { |
| 5396 | context_node_fake.child->prev = child; |
| 5397 | } |
| 5398 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5399 | child->parent = parent; |
| 5400 | } |
| 5401 | |
| 5402 | if (uses_p->augments || uses_p->refines) { |
| 5403 | /* 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] | 5404 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5405 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5406 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5407 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5408 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5409 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5410 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5411 | } |
| 5412 | } |
| 5413 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5414 | /* reload previous context's mod_def */ |
| 5415 | ctx->mod_def = mod_old; |
| 5416 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5417 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5418 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5419 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5420 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5421 | |
| 5422 | return ret; |
| 5423 | } |
| 5424 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5425 | static int |
| 5426 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5427 | { |
| 5428 | struct lysp_node *iter; |
| 5429 | int len = 0; |
| 5430 | |
| 5431 | *path = NULL; |
| 5432 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5433 | char *s = *path; |
| 5434 | char *id; |
| 5435 | |
| 5436 | switch (iter->nodetype) { |
| 5437 | case LYS_USES: |
| 5438 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5439 | break; |
| 5440 | case LYS_GROUPING: |
| 5441 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5442 | break; |
| 5443 | case LYS_AUGMENT: |
| 5444 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5445 | break; |
| 5446 | default: |
| 5447 | id = strdup(iter->name); |
| 5448 | break; |
| 5449 | } |
| 5450 | |
| 5451 | if (!iter->parent) { |
| 5452 | /* print prefix */ |
| 5453 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5454 | } else { |
| 5455 | /* prefix is the same as in parent */ |
| 5456 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5457 | } |
| 5458 | free(s); |
| 5459 | free(id); |
| 5460 | } |
| 5461 | |
| 5462 | if (len < 0) { |
| 5463 | free(*path); |
| 5464 | *path = NULL; |
| 5465 | } else if (len == 0) { |
| 5466 | *path = strdup("/"); |
| 5467 | len = 1; |
| 5468 | } |
| 5469 | return len; |
| 5470 | } |
| 5471 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5472 | /** |
| 5473 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5474 | * |
| 5475 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5476 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5477 | */ |
| 5478 | static LY_ERR |
| 5479 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5480 | { |
| 5481 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5482 | char *path; |
| 5483 | int len; |
| 5484 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5485 | struct lysp_node_uses fake_uses = { |
| 5486 | .parent = node_p, |
| 5487 | .nodetype = LYS_USES, |
| 5488 | .flags = 0, .next = NULL, |
| 5489 | .name = grp->name, |
| 5490 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5491 | .refines = NULL, .augments = NULL |
| 5492 | }; |
| 5493 | struct lysc_node_container fake_container = { |
| 5494 | .nodetype = LYS_CONTAINER, |
| 5495 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5496 | .module = ctx->mod, |
| 5497 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5498 | .prev = (struct lysc_node*)&fake_container, |
| 5499 | .name = "fake", |
| 5500 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5501 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5502 | }; |
| 5503 | |
| 5504 | if (grp->parent) { |
| 5505 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5506 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5507 | |
| 5508 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5509 | if (len < 0) { |
| 5510 | LOGMEM(ctx->ctx); |
| 5511 | return LY_EMEM; |
| 5512 | } |
| 5513 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5514 | ctx->path_len = (uint16_t)len; |
| 5515 | free(path); |
| 5516 | |
| 5517 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5518 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5519 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5520 | lysc_update_path(ctx, NULL, NULL); |
| 5521 | lysc_update_path(ctx, NULL, NULL); |
| 5522 | |
| 5523 | ctx->path_len = 1; |
| 5524 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5525 | |
| 5526 | /* cleanup */ |
| 5527 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5528 | |
| 5529 | return ret; |
| 5530 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5531 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5532 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5533 | * @brief Compile parsed schema node information. |
| 5534 | * @param[in] ctx Compile context |
| 5535 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5536 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5537 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5538 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5539 | * @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). |
| 5540 | * 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] | 5541 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5542 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5543 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5544 | 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] | 5545 | { |
| 5546 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5547 | struct lysc_node *node; |
| 5548 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5549 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5550 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5551 | 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] | 5552 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5553 | if (node_p->nodetype != LYS_USES) { |
| 5554 | lysc_update_path(ctx, parent, node_p->name); |
| 5555 | } else { |
| 5556 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5557 | lysc_update_path(ctx, NULL, node_p->name); |
| 5558 | } |
| 5559 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5560 | switch (node_p->nodetype) { |
| 5561 | case LYS_CONTAINER: |
| 5562 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5563 | node_compile_spec = lys_compile_node_container; |
| 5564 | break; |
| 5565 | case LYS_LEAF: |
| 5566 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5567 | node_compile_spec = lys_compile_node_leaf; |
| 5568 | break; |
| 5569 | case LYS_LIST: |
| 5570 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5571 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5572 | break; |
| 5573 | case LYS_LEAFLIST: |
| 5574 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5575 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5576 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5577 | case LYS_CHOICE: |
| 5578 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5579 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5580 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5581 | case LYS_ANYXML: |
| 5582 | case LYS_ANYDATA: |
| 5583 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5584 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5585 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5586 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5587 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5588 | lysc_update_path(ctx, NULL, NULL); |
| 5589 | lysc_update_path(ctx, NULL, NULL); |
| 5590 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5591 | default: |
| 5592 | LOGINT(ctx->ctx); |
| 5593 | return LY_EINT; |
| 5594 | } |
| 5595 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5596 | node->nodetype = node_p->nodetype; |
| 5597 | node->module = ctx->mod; |
| 5598 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5599 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5600 | |
| 5601 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5602 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5603 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5604 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5605 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5606 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5607 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5608 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5609 | node->flags |= LYS_CONFIG_R; |
| 5610 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5611 | /* config not explicitely set, inherit it from parent */ |
| 5612 | if (parent) { |
| 5613 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5614 | } else { |
| 5615 | /* default is config true */ |
| 5616 | node->flags |= LYS_CONFIG_W; |
| 5617 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5618 | } else { |
| 5619 | /* config set explicitely */ |
| 5620 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5621 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5622 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5623 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5624 | "Configuration node cannot be child of any state data node."); |
| 5625 | goto error; |
| 5626 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5627 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5628 | /* *list ordering */ |
| 5629 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5630 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5631 | 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] | 5632 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5633 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5634 | node->flags &= ~LYS_ORDBY_MASK; |
| 5635 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5636 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5637 | /* default ordering is system */ |
| 5638 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5639 | } |
| 5640 | } |
| 5641 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5642 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5643 | * 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] | 5644 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5645 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5646 | * 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] | 5647 | 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] | 5648 | } |
| 5649 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5650 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5651 | node->sp = node_p; |
| 5652 | } |
| 5653 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5654 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5655 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5656 | if (node_p->when) { |
| 5657 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5658 | 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] | 5659 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5660 | |
| 5661 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5662 | /* do not check "when" semantics in a grouping */ |
| 5663 | ly_set_add(&ctx->unres, node, 0); |
| 5664 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5665 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5666 | 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] | 5667 | |
| 5668 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5669 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5670 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5671 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5672 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5673 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5674 | if (node->flags & LYS_MAND_TRUE) { |
| 5675 | lys_compile_mandatory_parents(parent, 1); |
| 5676 | } |
| 5677 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5678 | lysc_update_path(ctx, NULL, NULL); |
| 5679 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5680 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5681 | if (parent) { |
| 5682 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5683 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5684 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5685 | } else { |
| 5686 | lysc_update_path(ctx, parent, node->name); |
| 5687 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5688 | 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] | 5689 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5690 | if (uses_status) { |
| 5691 | |
| 5692 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5693 | /* 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] | 5694 | * it directly gets the same status flags as the choice; |
| 5695 | * 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] | 5696 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5697 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5698 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5699 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5700 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5701 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5702 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5703 | 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] | 5704 | |
| 5705 | if (parent->nodetype == LYS_CHOICE) { |
| 5706 | lysc_update_path(ctx, NULL, NULL); |
| 5707 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5708 | } else { |
| 5709 | /* top-level element */ |
| 5710 | if (!ctx->mod->compiled->data) { |
| 5711 | ctx->mod->compiled->data = node; |
| 5712 | } else { |
| 5713 | /* insert at the end of the module's top-level nodes list */ |
| 5714 | ctx->mod->compiled->data->prev->next = node; |
| 5715 | node->prev = ctx->mod->compiled->data->prev; |
| 5716 | ctx->mod->compiled->data->prev = node; |
| 5717 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5718 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5719 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5720 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5721 | return LY_EVALID; |
| 5722 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5723 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5724 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5725 | |
| 5726 | return LY_SUCCESS; |
| 5727 | |
| 5728 | error: |
| 5729 | lysc_node_free(ctx->ctx, node); |
| 5730 | return ret; |
| 5731 | } |
| 5732 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5733 | static void |
| 5734 | lysc_disconnect(struct lysc_node *node) |
| 5735 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5736 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5737 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5738 | int remove_cs = 0; |
| 5739 | |
| 5740 | parent = node->parent; |
| 5741 | |
| 5742 | /* parent's first child */ |
| 5743 | if (!parent) { |
| 5744 | return; |
| 5745 | } |
| 5746 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5747 | cs = (struct lysc_node_case*)node; |
| 5748 | } else if (parent->nodetype == LYS_CASE) { |
| 5749 | /* disconnecting some node in a case */ |
| 5750 | cs = (struct lysc_node_case*)parent; |
| 5751 | parent = cs->parent; |
| 5752 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5753 | if (child == node) { |
| 5754 | if (cs->child == child) { |
| 5755 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5756 | /* case with a single child -> remove also the case */ |
| 5757 | child->parent = NULL; |
| 5758 | remove_cs = 1; |
| 5759 | } else { |
| 5760 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5761 | } |
| 5762 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5763 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5764 | } |
| 5765 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5766 | if (!remove_cs) { |
| 5767 | cs = NULL; |
| 5768 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5769 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5770 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5771 | } |
| 5772 | |
| 5773 | if (cs) { |
| 5774 | if (remove_cs) { |
| 5775 | /* cs has only one child which is being also removed */ |
| 5776 | lysc_disconnect((struct lysc_node*)cs); |
| 5777 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5778 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5779 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5780 | /* default case removed */ |
| 5781 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5782 | } |
| 5783 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5784 | /* first case removed */ |
| 5785 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5786 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5787 | if (cs->child) { |
| 5788 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5789 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5790 | prev = cs->child->prev; |
| 5791 | } /* else all the children are under a single case */ |
| 5792 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5793 | if (child->parent != (struct lysc_node*)cs) { |
| 5794 | break; |
| 5795 | } |
| 5796 | lysc_node_free(node->module->ctx, child); |
| 5797 | } |
| 5798 | if (prev) { |
| 5799 | if (prev->next) { |
| 5800 | prev->next = child; |
| 5801 | } |
| 5802 | if (child) { |
| 5803 | child->prev = prev; |
| 5804 | } else { |
| 5805 | /* link from the first child under the cases */ |
| 5806 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5807 | } |
| 5808 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5809 | } |
| 5810 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5811 | } |
| 5812 | |
| 5813 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5814 | if (node->prev->next) { |
| 5815 | node->prev->next = node->next; |
| 5816 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5817 | if (node->next) { |
| 5818 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5819 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5820 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5821 | if (child) { |
| 5822 | child->prev = node->prev; |
| 5823 | } |
| 5824 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5825 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5826 | } |
| 5827 | } |
| 5828 | |
| 5829 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5830 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5831 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5832 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5833 | struct ly_set devs_p = {0}; |
| 5834 | struct ly_set targets = {0}; |
| 5835 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5836 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5837 | struct lysc_action *rpcs; |
| 5838 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5839 | struct lysp_deviation *dev; |
| 5840 | struct lysp_deviate *d, **dp_new; |
| 5841 | struct lysp_deviate_add *d_add; |
| 5842 | struct lysp_deviate_del *d_del; |
| 5843 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5844 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5845 | struct lysc_deviation { |
| 5846 | const char *nodeid; |
| 5847 | struct lysc_node *target; /* target node of the deviation */ |
| 5848 | 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] | 5849 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5850 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5851 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5852 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5853 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5854 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5855 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5856 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5857 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5858 | |
| 5859 | /* get all deviations from the module and all its submodules ... */ |
| 5860 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5861 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5862 | } |
| 5863 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5864 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5865 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5866 | } |
| 5867 | } |
| 5868 | if (!devs_p.count) { |
| 5869 | /* nothing to do */ |
| 5870 | return LY_SUCCESS; |
| 5871 | } |
| 5872 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5873 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5874 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5875 | /* ... and group them by the target node */ |
| 5876 | devs = calloc(devs_p.count, sizeof *devs); |
| 5877 | for (u = 0; u < devs_p.count; ++u) { |
| 5878 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5879 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5880 | |
| 5881 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5882 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5883 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5884 | if (target->nodetype == LYS_ACTION) { |
| 5885 | /* move the target pointer to input/output to make them different from the action and |
| 5886 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5887 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5888 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5889 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5890 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5891 | flags |= LYSC_OPT_INTERNAL; |
| 5892 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5893 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5894 | flags |= LYSC_OPT_INTERNAL; |
| 5895 | } |
| 5896 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5897 | /* insert into the set of targets with duplicity detection */ |
| 5898 | i = ly_set_add(&targets, target, 0); |
| 5899 | if (!devs[i]) { |
| 5900 | /* new record */ |
| 5901 | devs[i] = calloc(1, sizeof **devs); |
| 5902 | devs[i]->target = target; |
| 5903 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5904 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5905 | } |
| 5906 | /* add deviates into the deviation's list of deviates */ |
| 5907 | for (d = dev->deviates; d; d = d->next) { |
| 5908 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5909 | *dp_new = d; |
| 5910 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5911 | devs[i]->not_supported = 1; |
| 5912 | } |
| 5913 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5914 | |
| 5915 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5916 | } |
| 5917 | |
| 5918 | /* MACROS for deviates checking */ |
| 5919 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5920 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5921 | 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] | 5922 | goto cleanup; \ |
| 5923 | } |
| 5924 | |
| 5925 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5926 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5927 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \ |
| 5928 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5929 | goto cleanup; \ |
| 5930 | } |
| 5931 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5932 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5933 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5934 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5935 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5936 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5937 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5938 | goto cleanup; \ |
| 5939 | } |
| 5940 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5941 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5942 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5943 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5944 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5945 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5946 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5947 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5948 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5949 | goto cleanup; \ |
| 5950 | } |
| 5951 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5952 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5953 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5954 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5955 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5956 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5957 | goto cleanup; \ |
| 5958 | } |
| 5959 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5960 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5961 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5962 | 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] | 5963 | goto cleanup; \ |
| 5964 | } |
| 5965 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5966 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5967 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5968 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5969 | "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] | 5970 | goto cleanup; \ |
| 5971 | } |
| 5972 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5973 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5974 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5975 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5976 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5977 | 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] | 5978 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5979 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5980 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5981 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5982 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5983 | goto cleanup; \ |
| 5984 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5985 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5986 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5987 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5988 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5989 | (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] | 5990 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5991 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5992 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5993 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5994 | } |
| 5995 | |
| 5996 | /* apply deviations */ |
| 5997 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5998 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5999 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 6000 | struct ly_err_item *err = NULL; |
| 6001 | |
| 6002 | dflt = NULL; |
| 6003 | changed_type = 0; |
| 6004 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6005 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 6006 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6007 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 6008 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 6009 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6010 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 6011 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6012 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 6013 | } |
| 6014 | } |
| 6015 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6016 | /* not-supported */ |
| 6017 | if (devs[u]->not_supported) { |
| 6018 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 6019 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 6020 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 6021 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6022 | |
| 6023 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6024 | if (devs[u]->target->parent) { \ |
| 6025 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 6026 | } else { \ |
| 6027 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 6028 | } \ |
| 6029 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6030 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 6031 | } \ |
| 6032 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 6033 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6034 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6035 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6036 | } |
| 6037 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6038 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 6039 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6040 | /* remove RPC's/action's input */ |
| 6041 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 6042 | 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] | 6043 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 6044 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6045 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6046 | /* remove RPC's/action's output */ |
| 6047 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 6048 | 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] | 6049 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 6050 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6051 | } else { |
| 6052 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6053 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6054 | } |
| 6055 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6056 | /* remove Notification */ |
| 6057 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6058 | } else { |
| 6059 | /* remove the target node */ |
| 6060 | lysc_disconnect(devs[u]->target); |
| 6061 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 6062 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6063 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6064 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6065 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6066 | continue; |
| 6067 | } |
| 6068 | |
| 6069 | /* list of deviates (not-supported is not present in the list) */ |
| 6070 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 6071 | d = devs[u]->deviates[v]; |
| 6072 | |
| 6073 | switch (d->mod) { |
| 6074 | case LYS_DEV_ADD: |
| 6075 | d_add = (struct lysp_deviate_add*)d; |
| 6076 | /* [units-stmt] */ |
| 6077 | if (d_add->units) { |
| 6078 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 6079 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 6080 | |
| 6081 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6082 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6083 | } |
| 6084 | |
| 6085 | /* *must-stmt */ |
| 6086 | if (d_add->musts) { |
| 6087 | switch (devs[u]->target->nodetype) { |
| 6088 | case LYS_CONTAINER: |
| 6089 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6090 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 6091 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6092 | break; |
| 6093 | case LYS_LEAF: |
| 6094 | case LYS_LEAFLIST: |
| 6095 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6096 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 6097 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6098 | break; |
| 6099 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6100 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 6101 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6102 | break; |
| 6103 | case LYS_ACTION: |
| 6104 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6105 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 6106 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6107 | break; |
| 6108 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6109 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 6110 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6111 | break; |
| 6112 | } |
| 6113 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6114 | default: |
| 6115 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6116 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6117 | goto cleanup; |
| 6118 | } |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6119 | ly_set_add(&ctx->unres, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6120 | } |
| 6121 | |
| 6122 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6123 | if (d_add->uniques) { |
| 6124 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 6125 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 6126 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6127 | |
| 6128 | /* *default-stmt */ |
| 6129 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6130 | switch (devs[u]->target->nodetype) { |
| 6131 | case LYS_LEAF: |
| 6132 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6133 | 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] | 6134 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6135 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6136 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6137 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6138 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6139 | } else { |
| 6140 | /* prepare new default value storage */ |
| 6141 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6142 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6143 | dflt = d_add->dflts[0]; |
| 6144 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 6145 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6146 | /* mark the new default values as leaf's own */ |
| 6147 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6148 | break; |
| 6149 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6150 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6151 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6152 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6153 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6154 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6155 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6156 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6157 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6158 | LY_ARRAY_FREE(llist->dflts); |
| 6159 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6160 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6161 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6162 | } |
| 6163 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6164 | 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] | 6165 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 6166 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6167 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6168 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 6169 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6170 | LY_ARRAY_INCREMENT(llist->dflts); |
| 6171 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 6172 | llist->dflts[x]->realtype = llist->type; |
| 6173 | 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] | 6174 | 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] | 6175 | (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] | 6176 | llist->dflts[x]->realtype->refcount++; |
| 6177 | if (err) { |
| 6178 | ly_err_print(err); |
| 6179 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6180 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 6181 | d_add->dflts[x - y], err->msg); |
| 6182 | ly_err_free(err); |
| 6183 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6184 | if (rc == LY_EINCOMPLETE) { |
| 6185 | /* postpone default compilation when the tree is complete */ |
| 6186 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6187 | |
| 6188 | /* but in general result is so far ok */ |
| 6189 | rc = LY_SUCCESS; |
| 6190 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6191 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6192 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6193 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6194 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 6195 | break; |
| 6196 | case LYS_CHOICE: |
| 6197 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 6198 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 6199 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 6200 | * 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] | 6201 | 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] | 6202 | goto cleanup; |
| 6203 | } |
| 6204 | break; |
| 6205 | default: |
| 6206 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6207 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6208 | goto cleanup; |
| 6209 | } |
| 6210 | } |
| 6211 | |
| 6212 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6213 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 6214 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6215 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6216 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 6217 | goto cleanup; |
| 6218 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6219 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6220 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 6221 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6222 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6223 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 6224 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6225 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 6226 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6227 | goto cleanup; |
| 6228 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6229 | 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] | 6230 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6231 | |
| 6232 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6233 | if (d_add->flags & LYS_MAND_MASK) { |
| 6234 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 6235 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6236 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 6237 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6238 | goto cleanup; |
| 6239 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6240 | 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] | 6241 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6242 | |
| 6243 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6244 | if (d_add->flags & LYS_SET_MIN) { |
| 6245 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6246 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6247 | /* change value */ |
| 6248 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 6249 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6250 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6251 | /* change value */ |
| 6252 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 6253 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6254 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6255 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 6256 | goto cleanup; |
| 6257 | } |
| 6258 | if (d_add->min) { |
| 6259 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6260 | } |
| 6261 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6262 | |
| 6263 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6264 | if (d_add->flags & LYS_SET_MAX) { |
| 6265 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6266 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6267 | /* change value */ |
| 6268 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6269 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6270 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6271 | /* change value */ |
| 6272 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6273 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6274 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6275 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6276 | goto cleanup; |
| 6277 | } |
| 6278 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6279 | |
| 6280 | break; |
| 6281 | case LYS_DEV_DELETE: |
| 6282 | d_del = (struct lysp_deviate_del*)d; |
| 6283 | |
| 6284 | /* [units-stmt] */ |
| 6285 | if (d_del->units) { |
| 6286 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6287 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6288 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6289 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6290 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6291 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6292 | goto cleanup; |
| 6293 | } |
| 6294 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6295 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6296 | } |
| 6297 | |
| 6298 | /* *must-stmt */ |
| 6299 | if (d_del->musts) { |
| 6300 | switch (devs[u]->target->nodetype) { |
| 6301 | case LYS_CONTAINER: |
| 6302 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6303 | 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] | 6304 | break; |
| 6305 | case LYS_LEAF: |
| 6306 | case LYS_LEAFLIST: |
| 6307 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6308 | 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] | 6309 | break; |
| 6310 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6311 | 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] | 6312 | break; |
| 6313 | case LYS_ACTION: |
| 6314 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6315 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6316 | break; |
| 6317 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6318 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6319 | break; |
| 6320 | } |
| 6321 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6322 | default: |
| 6323 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6324 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6325 | goto cleanup; |
| 6326 | } |
| 6327 | } |
| 6328 | |
| 6329 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6330 | if (d_del->uniques) { |
| 6331 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6332 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6333 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6334 | LY_ARRAY_FOR(list->uniques, z) { |
| 6335 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6336 | nodeid = strpbrk(name, " \t\n"); |
| 6337 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 6338 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6339 | break; |
| 6340 | } |
| 6341 | while (isspace(*nodeid)) { |
| 6342 | ++nodeid; |
| 6343 | } |
| 6344 | } else { |
| 6345 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6346 | break; |
| 6347 | } |
| 6348 | } |
| 6349 | } |
| 6350 | if (!name) { |
| 6351 | /* complete match - remove the unique */ |
| 6352 | LY_ARRAY_DECREMENT(list->uniques); |
| 6353 | LY_ARRAY_FREE(list->uniques[z]); |
| 6354 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6355 | --z; |
| 6356 | break; |
| 6357 | } |
| 6358 | } |
| 6359 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6360 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6361 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6362 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6363 | goto cleanup; |
| 6364 | } |
| 6365 | } |
| 6366 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6367 | LY_ARRAY_FREE(list->uniques); |
| 6368 | list->uniques = NULL; |
| 6369 | } |
| 6370 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6371 | |
| 6372 | /* *default-stmt */ |
| 6373 | if (d_del->dflts) { |
| 6374 | switch (devs[u]->target->nodetype) { |
| 6375 | case LYS_LEAF: |
| 6376 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6377 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6378 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6379 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6380 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6381 | 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] | 6382 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6383 | if (i) { |
| 6384 | free((char*)dflt); |
| 6385 | } |
| 6386 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6387 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6388 | d_del->dflts[0], dflt); |
| 6389 | goto cleanup; |
| 6390 | } |
| 6391 | if (i) { |
| 6392 | free((char*)dflt); |
| 6393 | } |
| 6394 | dflt = NULL; |
| 6395 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6396 | /* update the list of incomplete default values if needed */ |
| 6397 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6398 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6399 | /* remove the default specification */ |
| 6400 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6401 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6402 | free(leaf->dflt); |
| 6403 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6404 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6405 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6406 | break; |
| 6407 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6408 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6409 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6410 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6411 | 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] | 6412 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6413 | if (i) { |
| 6414 | free((char*)dflt); |
| 6415 | } |
| 6416 | dflt = NULL; |
| 6417 | break; |
| 6418 | } |
| 6419 | if (i) { |
| 6420 | free((char*)dflt); |
| 6421 | } |
| 6422 | dflt = NULL; |
| 6423 | } |
| 6424 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6425 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6426 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6427 | goto cleanup; |
| 6428 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6429 | |
| 6430 | /* update the list of incomplete default values if needed */ |
| 6431 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6432 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6433 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6434 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6435 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6436 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6437 | free(llist->dflts[y]); |
| 6438 | 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] | 6439 | 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] | 6440 | } |
| 6441 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6442 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6443 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6444 | LY_ARRAY_FREE(llist->dflts); |
| 6445 | llist->dflts = NULL; |
| 6446 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6447 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6448 | break; |
| 6449 | case LYS_CHOICE: |
| 6450 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6451 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6452 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6453 | 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] | 6454 | if (prefix) { |
| 6455 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6456 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6457 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6458 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6459 | "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] | 6460 | goto cleanup; |
| 6461 | } |
| 6462 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6463 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6464 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6465 | "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] | 6466 | goto cleanup; |
| 6467 | } |
| 6468 | } |
| 6469 | /* else { |
| 6470 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6471 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6472 | * the module of the default case, just matching its name */ |
| 6473 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6474 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6475 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6476 | 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] | 6477 | goto cleanup; |
| 6478 | } |
| 6479 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6480 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6481 | break; |
| 6482 | default: |
| 6483 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6484 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6485 | goto cleanup; |
| 6486 | } |
| 6487 | } |
| 6488 | |
| 6489 | break; |
| 6490 | case LYS_DEV_REPLACE: |
| 6491 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6492 | |
| 6493 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6494 | if (d_rpl->type) { |
| 6495 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6496 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6497 | 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] | 6498 | |
| 6499 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6500 | /* the target has default from the previous type - remove it */ |
| 6501 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6502 | /* update the list of incomplete default values if needed */ |
| 6503 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6504 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6505 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6506 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6507 | free(leaf->dflt); |
| 6508 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6509 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6510 | } else { /* LYS_LEAFLIST */ |
| 6511 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6512 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6513 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6514 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6515 | free(llist->dflts[x]); |
| 6516 | } |
| 6517 | LY_ARRAY_FREE(llist->dflts); |
| 6518 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6519 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6520 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6521 | } |
| 6522 | } |
| 6523 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6524 | /* there is no default value, do not set changed_type after type compilation |
| 6525 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6526 | changed_type = -1; |
| 6527 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6528 | 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] | 6529 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6530 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6531 | |
| 6532 | /* [units-stmt] */ |
| 6533 | if (d_rpl->units) { |
| 6534 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6535 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6536 | units, "replacing", "units", d_rpl->units); |
| 6537 | |
| 6538 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6539 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6540 | } |
| 6541 | |
| 6542 | /* [default-stmt] */ |
| 6543 | if (d_rpl->dflt) { |
| 6544 | switch (devs[u]->target->nodetype) { |
| 6545 | case LYS_LEAF: |
| 6546 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6547 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6548 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6549 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6550 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6551 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6552 | dflt = d_rpl->dflt; |
| 6553 | /* 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] | 6554 | break; |
| 6555 | case LYS_CHOICE: |
| 6556 | 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] | 6557 | 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] | 6558 | goto cleanup; |
| 6559 | } |
| 6560 | break; |
| 6561 | default: |
| 6562 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6563 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6564 | goto cleanup; |
| 6565 | } |
| 6566 | } |
| 6567 | |
| 6568 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6569 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 6570 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6571 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6572 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6573 | goto cleanup; |
| 6574 | } |
| 6575 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6576 | 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] | 6577 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6578 | goto cleanup; |
| 6579 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6580 | 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] | 6581 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6582 | |
| 6583 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6584 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6585 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6586 | 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] | 6587 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6588 | goto cleanup; |
| 6589 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6590 | 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] | 6591 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6592 | |
| 6593 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6594 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6595 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6596 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6597 | /* change value */ |
| 6598 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6599 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6600 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6601 | /* change value */ |
| 6602 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6603 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6604 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6605 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6606 | goto cleanup; |
| 6607 | } |
| 6608 | if (d_rpl->min) { |
| 6609 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6610 | } |
| 6611 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6612 | |
| 6613 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6614 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6615 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6616 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6617 | /* change value */ |
| 6618 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6619 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6620 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6621 | /* change value */ |
| 6622 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6623 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6624 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6625 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6626 | goto cleanup; |
| 6627 | } |
| 6628 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6629 | |
| 6630 | break; |
| 6631 | default: |
| 6632 | LOGINT(ctx->ctx); |
| 6633 | goto cleanup; |
| 6634 | } |
| 6635 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6636 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6637 | /* final check when all deviations of a single target node are applied */ |
| 6638 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6639 | /* check min-max compatibility */ |
| 6640 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6641 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6642 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6643 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6644 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6645 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6646 | } else { |
| 6647 | min = max = 0; |
| 6648 | } |
| 6649 | if (min > max) { |
| 6650 | 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] | 6651 | "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] | 6652 | goto cleanup; |
| 6653 | } |
| 6654 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6655 | if (dflt) { |
| 6656 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6657 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6658 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6659 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6660 | 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] | 6661 | 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] | 6662 | leaf->dflt->realtype->refcount++; |
| 6663 | if (err) { |
| 6664 | ly_err_print(err); |
| 6665 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6666 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6667 | ly_err_free(err); |
| 6668 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6669 | if (rc == LY_EINCOMPLETE) { |
| 6670 | /* postpone default compilation when the tree is complete */ |
| 6671 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6672 | |
| 6673 | /* but in general result is so far ok */ |
| 6674 | rc = LY_SUCCESS; |
| 6675 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6676 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6677 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6678 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6679 | int dynamic; |
| 6680 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6681 | 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] | 6682 | |
| 6683 | /* update the list of incomplete default values if needed */ |
| 6684 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6685 | |
| 6686 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6687 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6688 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6689 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6690 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6691 | 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] | 6692 | 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] | 6693 | leaf->dflt->realtype->refcount++; |
| 6694 | if (err) { |
| 6695 | ly_err_print(err); |
| 6696 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6697 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6698 | ly_err_free(err); |
| 6699 | } |
| 6700 | if (dynamic) { |
| 6701 | free((void*)dflt); |
| 6702 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6703 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6704 | if (rc == LY_EINCOMPLETE) { |
| 6705 | /* postpone default compilation when the tree is complete */ |
| 6706 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6707 | |
| 6708 | /* but in general result is so far ok */ |
| 6709 | rc = LY_SUCCESS; |
| 6710 | } |
| 6711 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6712 | } else { /* LYS_LEAFLIST */ |
| 6713 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6714 | 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] | 6715 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6716 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6717 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6718 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6719 | 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] | 6720 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6721 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6722 | llist->dflts[x]->realtype->refcount++; |
| 6723 | if (err) { |
| 6724 | ly_err_print(err); |
| 6725 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6726 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6727 | dflt, err->msg); |
| 6728 | ly_err_free(err); |
| 6729 | } |
| 6730 | if (dynamic) { |
| 6731 | free((void*)dflt); |
| 6732 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6733 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6734 | if (rc == LY_EINCOMPLETE) { |
| 6735 | /* postpone default compilation when the tree is complete */ |
| 6736 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6737 | |
| 6738 | /* but in general result is so far ok */ |
| 6739 | rc = LY_SUCCESS; |
| 6740 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6741 | LY_CHECK_GOTO(rc, cleanup); |
| 6742 | } |
| 6743 | } |
| 6744 | } |
| 6745 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6746 | /* check mandatory - default compatibility */ |
| 6747 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6748 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6749 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6750 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6751 | "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] | 6752 | goto cleanup; |
| 6753 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6754 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6755 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6756 | 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] | 6757 | goto cleanup; |
| 6758 | } |
| 6759 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6760 | /* mandatory node under a default case */ |
| 6761 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6762 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6763 | 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] | 6764 | goto cleanup; |
| 6765 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6766 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6767 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6768 | } |
| 6769 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6770 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6771 | ret = LY_SUCCESS; |
| 6772 | |
| 6773 | cleanup: |
| 6774 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6775 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6776 | free(devs[u]); |
| 6777 | } |
| 6778 | free(devs); |
| 6779 | ly_set_erase(&targets, NULL); |
| 6780 | ly_set_erase(&devs_p, NULL); |
| 6781 | |
| 6782 | return ret; |
| 6783 | } |
| 6784 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6785 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6786 | * @brief Compile the given YANG submodule into the main module. |
| 6787 | * @param[in] ctx Compile context |
| 6788 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6789 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6790 | */ |
| 6791 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6792 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6793 | { |
| 6794 | unsigned int u; |
| 6795 | LY_ERR ret = LY_SUCCESS; |
| 6796 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6797 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6798 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6799 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6800 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6801 | if (!mainmod->mod->off_features) { |
| 6802 | /* features are compiled directly into the compiled module structure, |
| 6803 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6804 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6805 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6806 | LY_CHECK_GOTO(ret, error); |
| 6807 | } |
| 6808 | if (!mainmod->mod->off_extensions) { |
| 6809 | /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */ |
| 6810 | ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6811 | LY_CHECK_GOTO(ret, error); |
| 6812 | } |
| 6813 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6814 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6815 | 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] | 6816 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6817 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6818 | /* data nodes */ |
| 6819 | LY_LIST_FOR(submod->data, node_p) { |
| 6820 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6821 | LY_CHECK_GOTO(ret, error); |
| 6822 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6823 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6824 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6825 | 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] | 6826 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6827 | error: |
| 6828 | return ret; |
| 6829 | } |
| 6830 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6831 | static void * |
| 6832 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6833 | { |
| 6834 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6835 | if (substmts[u].stmt == stmt) { |
| 6836 | return substmts[u].storage; |
| 6837 | } |
| 6838 | } |
| 6839 | return NULL; |
| 6840 | } |
| 6841 | |
| 6842 | LY_ERR |
| 6843 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6844 | { |
| 6845 | LY_ERR ret = LY_EVALID, r; |
| 6846 | unsigned int u; |
| 6847 | struct lysp_stmt *stmt; |
| 6848 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6849 | |
| 6850 | /* check for invalid substatements */ |
| 6851 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6852 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6853 | continue; |
| 6854 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6855 | for (u = 0; substmts[u].stmt; ++u) { |
| 6856 | if (substmts[u].stmt == stmt->kw) { |
| 6857 | break; |
| 6858 | } |
| 6859 | } |
| 6860 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6861 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6862 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6863 | goto cleanup; |
| 6864 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6865 | } |
| 6866 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6867 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6868 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6869 | /* keep order of the processing the same as the order in the defined substmts, |
| 6870 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6871 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6872 | int stmt_present = 0; |
| 6873 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6874 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6875 | if (substmts[u].stmt != stmt->kw) { |
| 6876 | continue; |
| 6877 | } |
| 6878 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6879 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6880 | if (substmts[u].storage) { |
| 6881 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6882 | case LY_STMT_STATUS: |
| 6883 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6884 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6885 | break; |
| 6886 | case LY_STMT_UNITS: { |
| 6887 | const char **units; |
| 6888 | |
| 6889 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6890 | /* single item */ |
| 6891 | if (*((const char **)substmts[u].storage)) { |
| 6892 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6893 | goto cleanup; |
| 6894 | } |
| 6895 | units = (const char **)substmts[u].storage; |
| 6896 | } else { |
| 6897 | /* sized array */ |
| 6898 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6899 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6900 | } |
| 6901 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6902 | break; |
| 6903 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6904 | case LY_STMT_TYPE: { |
| 6905 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6906 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6907 | |
| 6908 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6909 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6910 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6911 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6912 | goto cleanup; |
| 6913 | } |
| 6914 | compiled = substmts[u].storage; |
| 6915 | } else { |
| 6916 | /* sized array */ |
| 6917 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6918 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6919 | compiled = (void*)type; |
| 6920 | } |
| 6921 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6922 | 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] | 6923 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6924 | 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] | 6925 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6926 | lysp_type_free(ctx->ctx, parsed); |
| 6927 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6928 | break; |
| 6929 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6930 | case LY_STMT_IF_FEATURE: { |
| 6931 | struct lysc_iffeature *iff = NULL; |
| 6932 | |
| 6933 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6934 | /* single item */ |
| 6935 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6936 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6937 | goto cleanup; |
| 6938 | } |
| 6939 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6940 | } else { |
| 6941 | /* sized array */ |
| 6942 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6943 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6944 | } |
| 6945 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6946 | break; |
| 6947 | } |
| 6948 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6949 | * 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] | 6950 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6951 | 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.", |
| 6952 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6953 | goto cleanup; |
| 6954 | } |
| 6955 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6956 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6957 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6958 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6959 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6960 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6961 | goto cleanup; |
| 6962 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6963 | } |
| 6964 | |
| 6965 | ret = LY_SUCCESS; |
| 6966 | |
| 6967 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6968 | return ret; |
| 6969 | } |
| 6970 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6971 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6972 | * @brief Check when for cyclic dependencies. |
| 6973 | * @param[in] set Set with all the referenced nodes. |
| 6974 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6975 | * @return LY_ERR value |
| 6976 | */ |
| 6977 | static LY_ERR |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6978 | 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] | 6979 | { |
| 6980 | struct lyxp_set tmp_set; |
| 6981 | struct lyxp_set_scnode *xp_scnode; |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6982 | uint32_t i, j, k; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6983 | int idx; |
| 6984 | struct lysc_when *when; |
| 6985 | LY_ERR ret = LY_SUCCESS; |
| 6986 | |
| 6987 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6988 | |
| 6989 | /* prepare in_ctx of the set */ |
| 6990 | for (i = 0; i < set->used; ++i) { |
| 6991 | xp_scnode = &set->val.scnodes[i]; |
| 6992 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6993 | if (xp_scnode->in_ctx != -1) { |
| 6994 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6995 | xp_scnode->in_ctx = 1; |
| 6996 | } |
| 6997 | } |
| 6998 | |
| 6999 | for (i = 0; i < set->used; ++i) { |
| 7000 | xp_scnode = &set->val.scnodes[i]; |
| 7001 | if (xp_scnode->in_ctx != 1) { |
| 7002 | /* already checked */ |
| 7003 | continue; |
| 7004 | } |
| 7005 | |
Michal Vasko | 2ff7efe | 2019-12-10 14:50:59 +0100 | [diff] [blame] | 7006 | if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_ACTION | LYS_NOTIF)) |
| 7007 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7008 | /* no when to check */ |
| 7009 | xp_scnode->in_ctx = 0; |
| 7010 | continue; |
| 7011 | } |
| 7012 | |
| 7013 | node = xp_scnode->scnode; |
| 7014 | do { |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7015 | LY_ARRAY_FOR(node->when, j) { |
| 7016 | when = node->when[j]; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7017 | ret = lyxp_atomize(when->cond, LYD_UNKNOWN, when->module, when->context, |
| 7018 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 7019 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7020 | 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] | 7021 | goto cleanup; |
| 7022 | } |
| 7023 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7024 | for (k = 0; k < tmp_set.used; ++k) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7025 | /* skip roots'n'stuff */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7026 | if (tmp_set.val.scnodes[k].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7027 | /* try to find this node in our set */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7028 | 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] | 7029 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7030 | 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] | 7031 | ret = LY_EVALID; |
| 7032 | goto cleanup; |
| 7033 | } |
| 7034 | |
| 7035 | /* needs to be checked, if in both sets, will be ignored */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7036 | tmp_set.val.scnodes[k].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7037 | } else { |
| 7038 | /* no when, nothing to check */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7039 | tmp_set.val.scnodes[k].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7040 | } |
| 7041 | } |
| 7042 | |
| 7043 | /* merge this set into the global when set */ |
| 7044 | lyxp_set_scnode_merge(set, &tmp_set); |
| 7045 | } |
| 7046 | |
| 7047 | /* check when of non-data parents as well */ |
| 7048 | node = node->parent; |
| 7049 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 7050 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7051 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 7052 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7053 | } |
| 7054 | |
| 7055 | cleanup: |
| 7056 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 7057 | return ret; |
| 7058 | } |
| 7059 | |
| 7060 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7061 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 7062 | * @param[in] ctx Compile context. |
| 7063 | * @param[in] node Node to check. |
| 7064 | * @return LY_ERR value |
| 7065 | */ |
| 7066 | static LY_ERR |
| 7067 | lys_compile_check_xpath(struct lysc_ctx *ctx, const struct lysc_node *node) |
| 7068 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7069 | struct lyxp_set tmp_set; |
| 7070 | uint32_t i, j; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7071 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7072 | struct lysc_when **when = NULL; |
| 7073 | struct lysc_must *musts = NULL; |
| 7074 | LY_ERR ret = LY_SUCCESS; |
| 7075 | |
| 7076 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7077 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7078 | |
| 7079 | switch (node->nodetype) { |
| 7080 | case LYS_CONTAINER: |
| 7081 | when = ((struct lysc_node_container *)node)->when; |
| 7082 | musts = ((struct lysc_node_container *)node)->musts; |
| 7083 | break; |
| 7084 | case LYS_CHOICE: |
| 7085 | when = ((struct lysc_node_choice *)node)->when; |
| 7086 | break; |
| 7087 | case LYS_LEAF: |
| 7088 | when = ((struct lysc_node_leaf *)node)->when; |
| 7089 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 7090 | break; |
| 7091 | case LYS_LEAFLIST: |
| 7092 | when = ((struct lysc_node_leaflist *)node)->when; |
| 7093 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 7094 | break; |
| 7095 | case LYS_LIST: |
| 7096 | when = ((struct lysc_node_list *)node)->when; |
| 7097 | musts = ((struct lysc_node_list *)node)->musts; |
| 7098 | break; |
| 7099 | case LYS_ANYXML: |
| 7100 | case LYS_ANYDATA: |
| 7101 | when = ((struct lysc_node_anydata *)node)->when; |
| 7102 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 7103 | break; |
| 7104 | case LYS_CASE: |
| 7105 | when = ((struct lysc_node_case *)node)->when; |
| 7106 | break; |
| 7107 | case LYS_NOTIF: |
| 7108 | musts = ((struct lysc_notif *)node)->musts; |
| 7109 | break; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7110 | case LYS_ACTION: |
| 7111 | /* first process input musts */ |
| 7112 | musts = ((struct lysc_action *)node)->input.musts; |
| 7113 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7114 | default: |
| 7115 | /* nothing to check */ |
| 7116 | break; |
| 7117 | } |
| 7118 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7119 | /* check "when" */ |
| 7120 | LY_ARRAY_FOR(when, i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7121 | ret = lyxp_atomize(when[i]->cond, LYD_UNKNOWN, when[i]->module, when[i]->context, |
| 7122 | when[i]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts); |
| 7123 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7124 | 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] | 7125 | goto cleanup; |
| 7126 | } |
| 7127 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7128 | ctx->path[0] = '\0'; |
| 7129 | 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] | 7130 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7131 | /* skip roots'n'stuff */ |
| 7132 | 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] | 7133 | struct lysc_node *schema = tmp_set.val.scnodes[j].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7134 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7135 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 7136 | ret = lysc_check_status(ctx, when[i]->flags, when[i]->module, node->name, schema->flags, schema->module, |
| 7137 | schema->name); |
| 7138 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7139 | |
| 7140 | /* check dummy node accessing */ |
| 7141 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7142 | 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] | 7143 | ret = LY_EVALID; |
| 7144 | goto cleanup; |
| 7145 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7146 | } |
| 7147 | } |
| 7148 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7149 | /* check cyclic dependencies */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7150 | ret = lys_compile_check_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7151 | LY_CHECK_GOTO(ret, cleanup); |
| 7152 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7153 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 7154 | } |
| 7155 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7156 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7157 | /* check "must" */ |
| 7158 | LY_ARRAY_FOR(musts, i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7159 | ret = lyxp_atomize(musts[i].cond, LYD_UNKNOWN, musts[i].module, node, LYXP_NODE_ELEM, &tmp_set, opts); |
| 7160 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7161 | 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] | 7162 | goto cleanup; |
| 7163 | } |
| 7164 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7165 | ctx->path[0] = '\0'; |
| 7166 | 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] | 7167 | for (j = 0; j < tmp_set.used; ++j) { |
| 7168 | /* skip roots'n'stuff */ |
| 7169 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
| 7170 | /* 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] | 7171 | 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] | 7172 | tmp_set.val.scnodes[j].scnode->module, tmp_set.val.scnodes[j].scnode->name); |
| 7173 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7174 | } |
| 7175 | } |
| 7176 | |
| 7177 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 7178 | } |
| 7179 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7180 | if ((node->nodetype == LYS_ACTION) && !input_done) { |
| 7181 | /* now check output musts */ |
| 7182 | input_done = 1; |
| 7183 | musts = ((struct lysc_action *)node)->output.musts; |
| 7184 | opts = LYXP_SCNODE_OUTPUT; |
| 7185 | goto check_musts; |
| 7186 | } |
| 7187 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7188 | cleanup: |
| 7189 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 7190 | return ret; |
| 7191 | } |
| 7192 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7193 | LY_ERR |
| 7194 | lys_compile(struct lys_module *mod, int options) |
| 7195 | { |
| 7196 | struct lysc_ctx ctx = {0}; |
| 7197 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7198 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7199 | struct lysp_module *sp; |
| 7200 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7201 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7202 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7203 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7204 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7205 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7206 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7207 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7208 | |
| 7209 | if (!mod->implemented) { |
| 7210 | /* just imported modules are not compiled */ |
| 7211 | return LY_SUCCESS; |
| 7212 | } |
| 7213 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7214 | sp = mod->parsed; |
| 7215 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7216 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7217 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7218 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7219 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7220 | ctx.path_len = 1; |
| 7221 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7222 | |
| 7223 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7224 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 7225 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7226 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7227 | 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] | 7228 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7229 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7230 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7231 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7232 | |
| 7233 | /* features */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7234 | if (mod->off_features) { |
| 7235 | /* there is already precompiled array of features */ |
| 7236 | mod_c->features = mod->off_features; |
| 7237 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7238 | } else { |
| 7239 | /* features are compiled directly into the compiled module structure, |
| 7240 | * 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] | 7241 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7242 | LY_CHECK_GOTO(ret, error); |
| 7243 | } |
| 7244 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7245 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7246 | * are present. */ |
| 7247 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7248 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7249 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7250 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7251 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7252 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7253 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7254 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7255 | 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] | 7256 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7257 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7258 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7259 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7260 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7261 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7262 | /* extensions */ |
| 7263 | /* 2-steps: a) prepare compiled structures and ... */ |
| 7264 | if (mod->off_extensions) { |
| 7265 | /* there is already precompiled array of extension definitions */ |
| 7266 | mod_c->extensions = mod->off_extensions; |
| 7267 | mod->off_extensions = NULL; |
| 7268 | } else { |
| 7269 | /* extension definitions are compiled directly into the compiled module structure */ |
| 7270 | ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions); |
Radek Krejci | bfb2e14 | 2019-09-09 14:47:44 +0200 | [diff] [blame] | 7271 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7272 | } |
| 7273 | /* ... b) connect the extension definitions with the appropriate extension plugins */ |
| 7274 | lys_compile_extension_plugins(mod_c->extensions); |
| 7275 | |
| 7276 | /* identities */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7277 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7278 | 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] | 7279 | if (sp->identities) { |
| 7280 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 7281 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7282 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7283 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7284 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7285 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7286 | 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] | 7287 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7288 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7289 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7290 | 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] | 7291 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7292 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7293 | 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] | 7294 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7295 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7296 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7297 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7298 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7299 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7300 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7301 | /* extension instances TODO cover extension instances from submodules */ |
| 7302 | 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] | 7303 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7304 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7305 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7306 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7307 | * 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] | 7308 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 7309 | 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] | 7310 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 7311 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7312 | /* validate the path */ |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 7313 | 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] | 7314 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7315 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7316 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7317 | /* validate the path */ |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7318 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 7319 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v], NULL); |
| 7320 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7321 | } |
| 7322 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7323 | } |
| 7324 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7325 | |
| 7326 | /* check xpath */ |
| 7327 | 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] | 7328 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7329 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 7330 | 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] | 7331 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 7332 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7333 | /* store pointer to the real type */ |
| 7334 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7335 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7336 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7337 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7338 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7339 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7340 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7341 | /* store pointer to the real type */ |
| 7342 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7343 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7344 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7345 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7346 | } |
| 7347 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7348 | } |
| 7349 | } |
| 7350 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7351 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7352 | /* finish incomplete default values compilation */ |
| 7353 | for (u = 0; u < ctx.dflts.count; ++u) { |
| 7354 | struct ly_err_item *err = NULL; |
| 7355 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[u]; |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 7356 | 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] | 7357 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7358 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7359 | if (err) { |
| 7360 | ly_err_print(err); |
| 7361 | ctx.path[0] = '\0'; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7362 | 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] | 7363 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 7364 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7365 | ly_err_free(err); |
| 7366 | } |
| 7367 | LY_CHECK_GOTO(ret, error); |
| 7368 | } |
| 7369 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7370 | /* validate non-instantiated groupings from the parsed schema, |
| 7371 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7372 | ctx.options |= LYSC_OPT_GROUPING; |
| 7373 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7374 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7375 | 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] | 7376 | } |
| 7377 | } |
| 7378 | LY_LIST_FOR(sp->data, node_p) { |
| 7379 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7380 | LY_ARRAY_FOR(grps, u) { |
| 7381 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7382 | 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] | 7383 | } |
| 7384 | } |
| 7385 | } |
| 7386 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7387 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7388 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7389 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7390 | } |
| 7391 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7392 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7393 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7394 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7395 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7396 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7397 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7398 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7399 | lysp_module_free(mod->parsed); |
| 7400 | ((struct lys_module*)mod)->parsed = NULL; |
| 7401 | } |
| 7402 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7403 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7404 | /* remove flag of the modules implemented by dependency */ |
| 7405 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 7406 | m = ctx.ctx->list.objs[u]; |
| 7407 | if (m->implemented == 2) { |
| 7408 | m->implemented = 1; |
| 7409 | } |
| 7410 | } |
| 7411 | } |
| 7412 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7413 | ((struct lys_module*)mod)->compiled = mod_c; |
| 7414 | return LY_SUCCESS; |
| 7415 | |
| 7416 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7417 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7418 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7419 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7420 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7421 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7422 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7423 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7424 | mod->compiled = NULL; |
| 7425 | |
| 7426 | /* revert compilation of modules implemented by dependency */ |
| 7427 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 7428 | m = ctx.ctx->list.objs[u]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7429 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7430 | /* revert features list to the precompiled state */ |
| 7431 | lys_feature_precompile_revert(&ctx, m); |
| 7432 | /* mark module as imported-only / not-implemented */ |
| 7433 | m->implemented = 0; |
| 7434 | /* free the compiled version of the module */ |
| 7435 | lysc_module_free(m->compiled, NULL); |
| 7436 | m->compiled = NULL; |
| 7437 | } |
| 7438 | } |
| 7439 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7440 | return ret; |
| 7441 | } |