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; |
| 621 | context.path_len = 1; |
| 622 | context.path[0] = '/'; |
| 623 | ctx_sc = &context; |
| 624 | } |
| 625 | |
| 626 | if (!extensions_p) { |
| 627 | return LY_SUCCESS; |
| 628 | } |
| 629 | if (*extensions) { |
| 630 | offset = LY_ARRAY_SIZE(*extensions); |
| 631 | } |
| 632 | |
| 633 | lysc_update_path(ctx_sc, NULL, "{extension}"); |
| 634 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM); |
| 635 | LY_ARRAY_FOR(extensions_p, u) { |
| 636 | lysc_update_path(ctx_sc, NULL, extensions_p[u].name); |
| 637 | LY_ARRAY_INCREMENT(*extensions); |
| 638 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name); |
| 639 | LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u])); |
| 640 | lysc_update_path(ctx_sc, NULL, NULL); |
| 641 | } |
| 642 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 643 | |
| 644 | return LY_SUCCESS; |
| 645 | } |
| 646 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 647 | /** |
| 648 | * @brief Compile information from the if-feature statement |
| 649 | * @param[in] ctx Compile context. |
| 650 | * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 651 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 652 | * @return LY_ERR value. |
| 653 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 654 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 655 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 656 | { |
| 657 | const char *c = *value; |
| 658 | int r, rc = EXIT_FAILURE; |
| 659 | int i, j, last_not, checkversion = 0; |
| 660 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 661 | uint8_t op; |
| 662 | struct iff_stack stack = {0, 0, NULL}; |
| 663 | struct lysc_feature *f; |
| 664 | |
| 665 | assert(c); |
| 666 | |
| 667 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 668 | for (i = j = last_not = 0; c[i]; i++) { |
| 669 | if (c[i] == '(') { |
| 670 | j++; |
| 671 | checkversion = 1; |
| 672 | continue; |
| 673 | } else if (c[i] == ')') { |
| 674 | j--; |
| 675 | continue; |
| 676 | } else if (isspace(c[i])) { |
| 677 | checkversion = 1; |
| 678 | continue; |
| 679 | } |
| 680 | |
| 681 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 682 | int sp; |
| 683 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 684 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 685 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 686 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 687 | return LY_EVALID; |
| 688 | } else if (!isspace(c[i + r])) { |
| 689 | /* feature name starting with the not/and/or */ |
| 690 | last_not = 0; |
| 691 | f_size++; |
| 692 | } else if (c[i] == 'n') { /* not operation */ |
| 693 | if (last_not) { |
| 694 | /* double not */ |
| 695 | expr_size = expr_size - 2; |
| 696 | last_not = 0; |
| 697 | } else { |
| 698 | last_not = 1; |
| 699 | } |
| 700 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 701 | if (f_exp != f_size) { |
| 702 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 703 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 704 | return LY_EVALID; |
| 705 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 706 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 707 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 708 | /* not a not operation */ |
| 709 | last_not = 0; |
| 710 | } |
| 711 | i += r; |
| 712 | } else { |
| 713 | f_size++; |
| 714 | last_not = 0; |
| 715 | } |
| 716 | expr_size++; |
| 717 | |
| 718 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 719 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 720 | i--; |
| 721 | break; |
| 722 | } |
| 723 | i++; |
| 724 | } |
| 725 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 726 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 727 | /* not matching count of ( and ) */ |
| 728 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 729 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 730 | return LY_EVALID; |
| 731 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 732 | if (f_exp != f_size) { |
| 733 | /* features do not match the needed arguments for the logical operations */ |
| 734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 735 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 736 | "the required number of operands for the operations.", *value); |
| 737 | return LY_EVALID; |
| 738 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 739 | |
| 740 | if (checkversion || expr_size > 1) { |
| 741 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 742 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 744 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 745 | return LY_EVALID; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /* allocate the memory */ |
| 750 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 751 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 752 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 753 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 754 | |
| 755 | stack.size = expr_size; |
| 756 | f_size--; expr_size--; /* used as indexes from now */ |
| 757 | |
| 758 | for (i--; i >= 0; i--) { |
| 759 | if (c[i] == ')') { |
| 760 | /* push it on stack */ |
| 761 | iff_stack_push(&stack, LYS_IFF_RP); |
| 762 | continue; |
| 763 | } else if (c[i] == '(') { |
| 764 | /* pop from the stack into result all operators until ) */ |
| 765 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 766 | iff_setop(iff->expr, op, expr_size--); |
| 767 | } |
| 768 | continue; |
| 769 | } else if (isspace(c[i])) { |
| 770 | continue; |
| 771 | } |
| 772 | |
| 773 | /* end of operator or operand -> find beginning and get what is it */ |
| 774 | j = i + 1; |
| 775 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 776 | i--; |
| 777 | } |
| 778 | i++; /* go back by one step */ |
| 779 | |
| 780 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 781 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 782 | /* double not */ |
| 783 | iff_stack_pop(&stack); |
| 784 | } else { |
| 785 | /* not has the highest priority, so do not pop from the stack |
| 786 | * as in case of AND and OR */ |
| 787 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 788 | } |
| 789 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 790 | /* as for OR - pop from the stack all operators with the same or higher |
| 791 | * priority and store them to the result, then push the AND to the stack */ |
| 792 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 793 | op = iff_stack_pop(&stack); |
| 794 | iff_setop(iff->expr, op, expr_size--); |
| 795 | } |
| 796 | iff_stack_push(&stack, LYS_IFF_AND); |
| 797 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 798 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 799 | op = iff_stack_pop(&stack); |
| 800 | iff_setop(iff->expr, op, expr_size--); |
| 801 | } |
| 802 | iff_stack_push(&stack, LYS_IFF_OR); |
| 803 | } else { |
| 804 | /* feature name, length is j - i */ |
| 805 | |
| 806 | /* add it to the expression */ |
| 807 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 808 | |
| 809 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 810 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 811 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 812 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 813 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 814 | iff->features[f_size] = f; |
| 815 | LY_ARRAY_INCREMENT(iff->features); |
| 816 | f_size--; |
| 817 | } |
| 818 | } |
| 819 | while (stack.index) { |
| 820 | op = iff_stack_pop(&stack); |
| 821 | iff_setop(iff->expr, op, expr_size--); |
| 822 | } |
| 823 | |
| 824 | if (++expr_size || ++f_size) { |
| 825 | /* not all expected operators and operands found */ |
| 826 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 827 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 828 | rc = LY_EINT; |
| 829 | } else { |
| 830 | rc = LY_SUCCESS; |
| 831 | } |
| 832 | |
| 833 | error: |
| 834 | /* cleanup */ |
| 835 | iff_stack_clean(&stack); |
| 836 | |
| 837 | return rc; |
| 838 | } |
| 839 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 840 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 841 | * @brief Get the XPath context node for the given schema node. |
| 842 | * @param[in] start The schema node where the XPath expression appears. |
| 843 | * @return The context node to evaluate XPath expression in given schema node. |
| 844 | * @return NULL in case the context node is the root node. |
| 845 | */ |
| 846 | static struct lysc_node * |
| 847 | lysc_xpath_context(struct lysc_node *start) |
| 848 | { |
| 849 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 850 | start = start->parent); |
| 851 | return start; |
| 852 | } |
| 853 | |
| 854 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 855 | * @brief Compile information from the when statement |
| 856 | * @param[in] ctx Compile context. |
| 857 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 858 | * @param[in] flags Flags of the node with the "when" defiition. |
| 859 | * @param[in] node Node that inherited the "when" definition, must be connected to parents. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 860 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 861 | * @return LY_ERR value. |
| 862 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 863 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 864 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 865 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 866 | LY_ERR ret = LY_SUCCESS; |
| 867 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 868 | *when = calloc(1, sizeof **when); |
| 869 | (*when)->refcount = 1; |
| 870 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 871 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 872 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 873 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 874 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 875 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 876 | COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 877 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 878 | |
| 879 | done: |
| 880 | return ret; |
| 881 | } |
| 882 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 883 | /** |
| 884 | * @brief Compile information from the must statement |
| 885 | * @param[in] ctx Compile context. |
| 886 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 887 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 888 | * @return LY_ERR value. |
| 889 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 890 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 891 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 892 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 893 | LY_ERR ret = LY_SUCCESS; |
| 894 | |
| 895 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 896 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 897 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 898 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 899 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 900 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 901 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 902 | COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 903 | |
| 904 | done: |
| 905 | return ret; |
| 906 | } |
| 907 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 908 | /** |
| 909 | * @brief Compile information from the import statement |
| 910 | * @param[in] ctx Compile context. |
| 911 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 912 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 913 | * @return LY_ERR value. |
| 914 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 915 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 916 | 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] | 917 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 918 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 919 | LY_ERR ret = LY_SUCCESS; |
| 920 | |
| 921 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 922 | 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] | 923 | imp->module = imp_p->module; |
| 924 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 925 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 926 | * 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] | 927 | * 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] | 928 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 929 | /* try to use filepath if present */ |
| 930 | if (imp->module->filepath) { |
| 931 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 932 | !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] | 933 | if (mod != imp->module) { |
| 934 | 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] | 935 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 936 | mod = NULL; |
| 937 | } |
| 938 | } |
| 939 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 940 | 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] | 941 | 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] | 942 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 943 | return LY_ENOTFOUND; |
| 944 | } |
| 945 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | done: |
| 949 | return ret; |
| 950 | } |
| 951 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 952 | /** |
| 953 | * @brief Compile information from the identity statement |
| 954 | * |
| 955 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 956 | * |
| 957 | * @param[in] ctx Compile context. |
| 958 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 959 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 960 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 961 | * @return LY_ERR value. |
| 962 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 963 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 964 | 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] | 965 | { |
| 966 | unsigned int u; |
| 967 | LY_ERR ret = LY_SUCCESS; |
| 968 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 969 | lysc_update_path(ctx, NULL, ident_p->name); |
| 970 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 971 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 972 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 973 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 974 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 975 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 976 | 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] | 977 | /* 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] | 978 | 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] | 979 | ident->flags = ident_p->flags; |
| 980 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 981 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 982 | done: |
| 983 | return ret; |
| 984 | } |
| 985 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 986 | /** |
| 987 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 988 | * |
| 989 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 990 | * |
| 991 | * @param[in] ctx Compile context for logging. |
| 992 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 993 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 994 | * @return LY_SUCCESS if everything is ok. |
| 995 | * @return LY_EVALID if the identity is derived from itself. |
| 996 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 997 | static LY_ERR |
| 998 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 999 | { |
| 1000 | LY_ERR ret = LY_EVALID; |
| 1001 | unsigned int u, v; |
| 1002 | struct ly_set recursion = {0}; |
| 1003 | struct lysc_ident *drv; |
| 1004 | |
| 1005 | if (!derived) { |
| 1006 | return LY_SUCCESS; |
| 1007 | } |
| 1008 | |
| 1009 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 1010 | if (ident == derived[u]) { |
| 1011 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1012 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1013 | goto cleanup; |
| 1014 | } |
| 1015 | ly_set_add(&recursion, derived[u], 0); |
| 1016 | } |
| 1017 | |
| 1018 | for (v = 0; v < recursion.count; ++v) { |
| 1019 | drv = recursion.objs[v]; |
| 1020 | if (!drv->derived) { |
| 1021 | continue; |
| 1022 | } |
| 1023 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 1024 | if (ident == drv->derived[u]) { |
| 1025 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1026 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1027 | goto cleanup; |
| 1028 | } |
| 1029 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1030 | } |
| 1031 | } |
| 1032 | ret = LY_SUCCESS; |
| 1033 | |
| 1034 | cleanup: |
| 1035 | ly_set_erase(&recursion, NULL); |
| 1036 | return ret; |
| 1037 | } |
| 1038 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1039 | /** |
| 1040 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1041 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1042 | * 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] | 1043 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1044 | * to distinguish these two use cases. |
| 1045 | * |
| 1046 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1047 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 1048 | * @param[in] ident Referencing identity to work with. |
| 1049 | * @param[in] bases Array of bases of identityref to fill in. |
| 1050 | * @return LY_ERR value. |
| 1051 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1052 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1053 | 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] | 1054 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1055 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1056 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1057 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1058 | struct lysc_ident **idref; |
| 1059 | |
| 1060 | assert(ident || bases); |
| 1061 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1062 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1063 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1064 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1065 | return LY_EVALID; |
| 1066 | } |
| 1067 | |
| 1068 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 1069 | s = strchr(bases_p[u], ':'); |
| 1070 | if (s) { |
| 1071 | /* prefixed identity */ |
| 1072 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1073 | 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] | 1074 | } else { |
| 1075 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1076 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1077 | } |
| 1078 | if (!mod) { |
| 1079 | if (ident) { |
| 1080 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1081 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1082 | } else { |
| 1083 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1084 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1085 | } |
| 1086 | return LY_EVALID; |
| 1087 | } |
| 1088 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1089 | if (mod->compiled && mod->compiled->identities) { |
| 1090 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 1091 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1092 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1093 | if (ident == &mod->compiled->identities[v]) { |
| 1094 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1095 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1096 | return LY_EVALID; |
| 1097 | } |
| 1098 | 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] | 1099 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1100 | 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] | 1101 | *idref = ident; |
| 1102 | } else { |
| 1103 | /* we have match! store the found identity */ |
| 1104 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1105 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1106 | } |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | if (!idref || !(*idref)) { |
| 1112 | if (ident) { |
| 1113 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1114 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1115 | } else { |
| 1116 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1117 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1118 | } |
| 1119 | return LY_EVALID; |
| 1120 | } |
| 1121 | } |
| 1122 | return LY_SUCCESS; |
| 1123 | } |
| 1124 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1125 | /** |
| 1126 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1127 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1128 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1129 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1130 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1131 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1132 | static LY_ERR |
| 1133 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1134 | { |
| 1135 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1136 | |
| 1137 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 1138 | if (!idents_p[i].bases) { |
| 1139 | continue; |
| 1140 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1141 | lysc_update_path(ctx, NULL, idents[i].name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1142 | 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] | 1143 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1144 | } |
| 1145 | return LY_SUCCESS; |
| 1146 | } |
| 1147 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1148 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1149 | 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] | 1150 | { |
| 1151 | unsigned int offset = 0, u; |
| 1152 | struct lysc_ctx context = {0}; |
| 1153 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1154 | assert(ctx_sc || ctx); |
| 1155 | |
| 1156 | if (!ctx_sc) { |
| 1157 | context.ctx = ctx; |
| 1158 | context.mod = module; |
| 1159 | context.path_len = 1; |
| 1160 | context.path[0] = '/'; |
| 1161 | ctx_sc = &context; |
| 1162 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1163 | |
| 1164 | if (!features_p) { |
| 1165 | return LY_SUCCESS; |
| 1166 | } |
| 1167 | if (*features) { |
| 1168 | offset = LY_ARRAY_SIZE(*features); |
| 1169 | } |
| 1170 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1171 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 1172 | 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] | 1173 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1174 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1175 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1176 | LY_ARRAY_INCREMENT(*features); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1177 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 1178 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1179 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1180 | 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] | 1181 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1182 | (*features)[offset + u].module = ctx_sc->mod; |
| 1183 | |
| 1184 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1185 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1186 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1187 | |
| 1188 | return LY_SUCCESS; |
| 1189 | } |
| 1190 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1191 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1192 | * @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] | 1193 | * |
| 1194 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1195 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1196 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1197 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1198 | * being currently processed). |
| 1199 | * @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] | 1200 | * @return LY_SUCCESS if everything is ok. |
| 1201 | * @return LY_EVALID if the feature references indirectly itself. |
| 1202 | */ |
| 1203 | static LY_ERR |
| 1204 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1205 | { |
| 1206 | LY_ERR ret = LY_EVALID; |
| 1207 | unsigned int u, v; |
| 1208 | struct ly_set recursion = {0}; |
| 1209 | struct lysc_feature *drv; |
| 1210 | |
| 1211 | if (!depfeatures) { |
| 1212 | return LY_SUCCESS; |
| 1213 | } |
| 1214 | |
| 1215 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1216 | if (feature == depfeatures[u]) { |
| 1217 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1218 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1219 | goto cleanup; |
| 1220 | } |
| 1221 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1222 | } |
| 1223 | |
| 1224 | for (v = 0; v < recursion.count; ++v) { |
| 1225 | drv = recursion.objs[v]; |
| 1226 | if (!drv->depfeatures) { |
| 1227 | continue; |
| 1228 | } |
| 1229 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1230 | if (feature == drv->depfeatures[u]) { |
| 1231 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1232 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1233 | goto cleanup; |
| 1234 | } |
| 1235 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1236 | } |
| 1237 | } |
| 1238 | ret = LY_SUCCESS; |
| 1239 | |
| 1240 | cleanup: |
| 1241 | ly_set_erase(&recursion, NULL); |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
| 1245 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1246 | * @brief Create pre-compiled features array. |
| 1247 | * |
| 1248 | * See lys_feature_precompile() for more details. |
| 1249 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1250 | * @param[in] ctx Compile context. |
| 1251 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1252 | * @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] | 1253 | * @return LY_ERR value. |
| 1254 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1255 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1256 | 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] | 1257 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1258 | unsigned int u, v, x; |
| 1259 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1260 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1261 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1262 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1263 | /* find the preprecompiled feature */ |
| 1264 | LY_ARRAY_FOR(features, x) { |
| 1265 | if (strcmp(features[x].name, feature_p->name)) { |
| 1266 | continue; |
| 1267 | } |
| 1268 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1269 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1270 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1271 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1272 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1273 | 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] | 1274 | 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] | 1275 | if (feature->iffeatures) { |
| 1276 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1277 | if (feature->iffeatures[u].features) { |
| 1278 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1279 | /* check for circular dependency - direct reference first,... */ |
| 1280 | if (feature == feature->iffeatures[u].features[v]) { |
| 1281 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1282 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1283 | return LY_EVALID; |
| 1284 | } |
| 1285 | /* ... and indirect circular reference */ |
| 1286 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1287 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1288 | /* add itself into the dependants list */ |
| 1289 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1290 | *df = feature; |
| 1291 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1292 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1293 | } |
| 1294 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1295 | lysc_update_path(ctx, NULL, NULL); |
| 1296 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1297 | done: |
| 1298 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1299 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1300 | |
| 1301 | LOGINT(ctx->ctx); |
| 1302 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1303 | } |
| 1304 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1305 | /** |
| 1306 | * @brief Revert compiled list of features back to the precompiled state. |
| 1307 | * |
| 1308 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1309 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1310 | * |
| 1311 | * @param[in] ctx Compilation context. |
| 1312 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1313 | * and supposed to hold the off_features list. |
| 1314 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1315 | static void |
| 1316 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1317 | { |
| 1318 | unsigned int u, v; |
| 1319 | |
| 1320 | /* keep the off_features list until the complete lys_module is freed */ |
| 1321 | mod->off_features = mod->compiled->features; |
| 1322 | mod->compiled->features = NULL; |
| 1323 | |
| 1324 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1325 | * which may points into the data being freed here */ |
| 1326 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1327 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1328 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1329 | } |
| 1330 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1331 | mod->off_features[u].iffeatures = NULL; |
| 1332 | |
| 1333 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1334 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1335 | } |
| 1336 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1337 | mod->off_features[u].exts = NULL; |
| 1338 | } |
| 1339 | } |
| 1340 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1341 | /** |
| 1342 | * @brief Validate and normalize numeric value from a range definition. |
| 1343 | * @param[in] ctx Compile context. |
| 1344 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1345 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1346 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1347 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1348 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1349 | * @param[in] value String value of the range boundary. |
| 1350 | * @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. |
| 1351 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1352 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1353 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1354 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1355 | 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] | 1356 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1357 | size_t fraction = 0, size; |
| 1358 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1359 | *len = 0; |
| 1360 | |
| 1361 | assert(value); |
| 1362 | /* parse value */ |
| 1363 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1364 | return LY_EVALID; |
| 1365 | } |
| 1366 | |
| 1367 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1368 | ++(*len); |
| 1369 | } |
| 1370 | |
| 1371 | while (isdigit(value[*len])) { |
| 1372 | ++(*len); |
| 1373 | } |
| 1374 | |
| 1375 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1376 | if (basetype == LY_TYPE_DEC64) { |
| 1377 | goto decimal; |
| 1378 | } else { |
| 1379 | *valcopy = strndup(value, *len); |
| 1380 | return LY_SUCCESS; |
| 1381 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1382 | } |
| 1383 | fraction = *len; |
| 1384 | |
| 1385 | ++(*len); |
| 1386 | while (isdigit(value[*len])) { |
| 1387 | ++(*len); |
| 1388 | } |
| 1389 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1390 | if (basetype == LY_TYPE_DEC64) { |
| 1391 | decimal: |
| 1392 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1393 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1394 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1395 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1396 | *len, value, frdigits); |
| 1397 | return LY_EINVAL; |
| 1398 | } |
| 1399 | if (fraction) { |
| 1400 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1401 | } else { |
| 1402 | size = (*len) + frdigits + 1; |
| 1403 | } |
| 1404 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1405 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1406 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1407 | (*valcopy)[size - 1] = '\0'; |
| 1408 | if (fraction) { |
| 1409 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1410 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1411 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1412 | } else { |
| 1413 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1414 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1415 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1416 | } |
| 1417 | return LY_SUCCESS; |
| 1418 | } |
| 1419 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1420 | /** |
| 1421 | * @brief Check that values in range are in ascendant order. |
| 1422 | * @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] | 1423 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1424 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1425 | * @param[in] value Current value to check. |
| 1426 | * @param[in] prev_value The last seen value. |
| 1427 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1428 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1429 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1430 | 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] | 1431 | { |
| 1432 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1433 | 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] | 1434 | return LY_EEXIST; |
| 1435 | } |
| 1436 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1437 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1438 | return LY_EEXIST; |
| 1439 | } |
| 1440 | } |
| 1441 | return LY_SUCCESS; |
| 1442 | } |
| 1443 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1444 | /** |
| 1445 | * @brief Set min/max value of the range part. |
| 1446 | * @param[in] ctx Compile context. |
| 1447 | * @param[in] part Range part structure to fill. |
| 1448 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1449 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1450 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1451 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1452 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1453 | * @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] | 1454 | * @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] | 1455 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1456 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1457 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1458 | * frdigits value), LY_EMEM. |
| 1459 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1460 | static LY_ERR |
| 1461 | 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] | 1462 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1463 | { |
| 1464 | LY_ERR ret = LY_SUCCESS; |
| 1465 | char *valcopy = NULL; |
| 1466 | size_t len; |
| 1467 | |
| 1468 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1469 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1470 | LY_CHECK_GOTO(ret, finalize); |
| 1471 | } |
| 1472 | if (!valcopy && base_range) { |
| 1473 | if (max) { |
| 1474 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1475 | } else { |
| 1476 | part->min_64 = base_range->parts[0].min_64; |
| 1477 | } |
| 1478 | if (!first) { |
| 1479 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1480 | } |
| 1481 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1485 | case LY_TYPE_INT8: /* range */ |
| 1486 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1487 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1488 | } else if (max) { |
| 1489 | part->max_64 = INT64_C(127); |
| 1490 | } else { |
| 1491 | part->min_64 = INT64_C(-128); |
| 1492 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1493 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1494 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1495 | } |
| 1496 | break; |
| 1497 | case LY_TYPE_INT16: /* range */ |
| 1498 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1499 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1500 | } else if (max) { |
| 1501 | part->max_64 = INT64_C(32767); |
| 1502 | } else { |
| 1503 | part->min_64 = INT64_C(-32768); |
| 1504 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1505 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1506 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1507 | } |
| 1508 | break; |
| 1509 | case LY_TYPE_INT32: /* range */ |
| 1510 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1511 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1512 | } else if (max) { |
| 1513 | part->max_64 = INT64_C(2147483647); |
| 1514 | } else { |
| 1515 | part->min_64 = INT64_C(-2147483648); |
| 1516 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1517 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1518 | 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] | 1519 | } |
| 1520 | break; |
| 1521 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1522 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1523 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1524 | 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] | 1525 | max ? &part->max_64 : &part->min_64); |
| 1526 | } else if (max) { |
| 1527 | part->max_64 = INT64_C(9223372036854775807); |
| 1528 | } else { |
| 1529 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1530 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1531 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1532 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1533 | } |
| 1534 | break; |
| 1535 | case LY_TYPE_UINT8: /* range */ |
| 1536 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1537 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1538 | } else if (max) { |
| 1539 | part->max_u64 = UINT64_C(255); |
| 1540 | } else { |
| 1541 | part->min_u64 = UINT64_C(0); |
| 1542 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1543 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1544 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1545 | } |
| 1546 | break; |
| 1547 | case LY_TYPE_UINT16: /* range */ |
| 1548 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1549 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1550 | } else if (max) { |
| 1551 | part->max_u64 = UINT64_C(65535); |
| 1552 | } else { |
| 1553 | part->min_u64 = UINT64_C(0); |
| 1554 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1555 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1556 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1557 | } |
| 1558 | break; |
| 1559 | case LY_TYPE_UINT32: /* range */ |
| 1560 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1561 | 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] | 1562 | } else if (max) { |
| 1563 | part->max_u64 = UINT64_C(4294967295); |
| 1564 | } else { |
| 1565 | part->min_u64 = UINT64_C(0); |
| 1566 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1567 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1568 | 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] | 1569 | } |
| 1570 | break; |
| 1571 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1572 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1573 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1574 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1575 | 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] | 1576 | } else if (max) { |
| 1577 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1578 | } else { |
| 1579 | part->min_u64 = UINT64_C(0); |
| 1580 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1581 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1582 | 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] | 1583 | } |
| 1584 | break; |
| 1585 | default: |
| 1586 | LOGINT(ctx->ctx); |
| 1587 | ret = LY_EINT; |
| 1588 | } |
| 1589 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1590 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1591 | if (ret == LY_EDENIED) { |
| 1592 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1593 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1594 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1595 | } else if (ret == LY_EVALID) { |
| 1596 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1597 | "Invalid %s restriction - invalid value \"%s\".", |
| 1598 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1599 | } else if (ret == LY_EEXIST) { |
| 1600 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1601 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1602 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1603 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1604 | } else if (!ret && value) { |
| 1605 | *value = *value + len; |
| 1606 | } |
| 1607 | free(valcopy); |
| 1608 | return ret; |
| 1609 | } |
| 1610 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1611 | /** |
| 1612 | * @brief Compile the parsed range restriction. |
| 1613 | * @param[in] ctx Compile context. |
| 1614 | * @param[in] range_p Parsed range structure to compile. |
| 1615 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1616 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1617 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1618 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1619 | * range restriction must be more restrictive than the base_range. |
| 1620 | * @param[in,out] range Pointer to the created current range structure. |
| 1621 | * @return LY_ERR value. |
| 1622 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1623 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1624 | 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] | 1625 | struct lysc_range *base_range, struct lysc_range **range) |
| 1626 | { |
| 1627 | LY_ERR ret = LY_EVALID; |
| 1628 | const char *expr; |
| 1629 | struct lysc_range_part *parts = NULL, *part; |
| 1630 | int range_expected = 0, uns; |
| 1631 | unsigned int parts_done = 0, u, v; |
| 1632 | |
| 1633 | assert(range); |
| 1634 | assert(range_p); |
| 1635 | |
| 1636 | expr = range_p->arg; |
| 1637 | while(1) { |
| 1638 | if (isspace(*expr)) { |
| 1639 | ++expr; |
| 1640 | } else if (*expr == '\0') { |
| 1641 | if (range_expected) { |
| 1642 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1643 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1644 | length_restr ? "length" : "range", range_p->arg); |
| 1645 | goto cleanup; |
| 1646 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1647 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1648 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1649 | length_restr ? "length" : "range", range_p->arg); |
| 1650 | goto cleanup; |
| 1651 | } |
| 1652 | parts_done++; |
| 1653 | break; |
| 1654 | } else if (!strncmp(expr, "min", 3)) { |
| 1655 | if (parts) { |
| 1656 | /* min cannot be used elsewhere than in the first part */ |
| 1657 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1658 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1659 | expr - range_p->arg, range_p->arg); |
| 1660 | goto cleanup; |
| 1661 | } |
| 1662 | expr += 3; |
| 1663 | |
| 1664 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1665 | 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] | 1666 | part->max_64 = part->min_64; |
| 1667 | } else if (*expr == '|') { |
| 1668 | if (!parts || range_expected) { |
| 1669 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1670 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1671 | goto cleanup; |
| 1672 | } |
| 1673 | expr++; |
| 1674 | parts_done++; |
| 1675 | /* process next part of the expression */ |
| 1676 | } else if (!strncmp(expr, "..", 2)) { |
| 1677 | expr += 2; |
| 1678 | while (isspace(*expr)) { |
| 1679 | expr++; |
| 1680 | } |
| 1681 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1682 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1683 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1684 | goto cleanup; |
| 1685 | } |
| 1686 | /* continue expecting the upper boundary */ |
| 1687 | range_expected = 1; |
| 1688 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1689 | /* number */ |
| 1690 | if (range_expected) { |
| 1691 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1692 | 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] | 1693 | range_expected = 0; |
| 1694 | } else { |
| 1695 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1696 | 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] | 1697 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1698 | part->max_64 = part->min_64; |
| 1699 | } |
| 1700 | |
| 1701 | /* continue with possible another expression part */ |
| 1702 | } else if (!strncmp(expr, "max", 3)) { |
| 1703 | expr += 3; |
| 1704 | while (isspace(*expr)) { |
| 1705 | expr++; |
| 1706 | } |
| 1707 | if (*expr != '\0') { |
| 1708 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1709 | length_restr ? "length" : "range", expr); |
| 1710 | goto cleanup; |
| 1711 | } |
| 1712 | if (range_expected) { |
| 1713 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1714 | 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] | 1715 | range_expected = 0; |
| 1716 | } else { |
| 1717 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1718 | 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] | 1719 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1720 | part->min_64 = part->max_64; |
| 1721 | } |
| 1722 | } else { |
| 1723 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1724 | length_restr ? "length" : "range", expr); |
| 1725 | goto cleanup; |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | /* check with the previous range/length restriction */ |
| 1730 | if (base_range) { |
| 1731 | switch (basetype) { |
| 1732 | case LY_TYPE_BINARY: |
| 1733 | case LY_TYPE_UINT8: |
| 1734 | case LY_TYPE_UINT16: |
| 1735 | case LY_TYPE_UINT32: |
| 1736 | case LY_TYPE_UINT64: |
| 1737 | case LY_TYPE_STRING: |
| 1738 | uns = 1; |
| 1739 | break; |
| 1740 | case LY_TYPE_DEC64: |
| 1741 | case LY_TYPE_INT8: |
| 1742 | case LY_TYPE_INT16: |
| 1743 | case LY_TYPE_INT32: |
| 1744 | case LY_TYPE_INT64: |
| 1745 | uns = 0; |
| 1746 | break; |
| 1747 | default: |
| 1748 | LOGINT(ctx->ctx); |
| 1749 | ret = LY_EINT; |
| 1750 | goto cleanup; |
| 1751 | } |
| 1752 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1753 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1754 | goto baseerror; |
| 1755 | } |
| 1756 | /* current lower bound is not lower than the base */ |
| 1757 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1758 | /* base has single value */ |
| 1759 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1760 | /* both lower bounds are the same */ |
| 1761 | if (parts[u].min_64 != parts[u].max_64) { |
| 1762 | /* current continues with a range */ |
| 1763 | goto baseerror; |
| 1764 | } else { |
| 1765 | /* equal single values, move both forward */ |
| 1766 | ++v; |
| 1767 | continue; |
| 1768 | } |
| 1769 | } else { |
| 1770 | /* base is single value lower than current range, so the |
| 1771 | * value from base range is removed in the current, |
| 1772 | * move only base and repeat checking */ |
| 1773 | ++v; |
| 1774 | --u; |
| 1775 | continue; |
| 1776 | } |
| 1777 | } else { |
| 1778 | /* base is the range */ |
| 1779 | if (parts[u].min_64 == parts[u].max_64) { |
| 1780 | /* current is a single value */ |
| 1781 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1782 | /* current is behind the base range, so base range is omitted, |
| 1783 | * move the base and keep the current for further check */ |
| 1784 | ++v; |
| 1785 | --u; |
| 1786 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1787 | continue; |
| 1788 | } else { |
| 1789 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1790 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1791 | /* higher bound is higher than the current higher bound */ |
| 1792 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1793 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1794 | * continue with the same current, but move the base */ |
| 1795 | --u; |
| 1796 | ++v; |
| 1797 | continue; |
| 1798 | } |
| 1799 | /* current range starts within the base range but end behind it */ |
| 1800 | goto baseerror; |
| 1801 | } else { |
| 1802 | /* current range is smaller than the base, |
| 1803 | * move current, but stay with the base */ |
| 1804 | continue; |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | } |
| 1809 | if (u != parts_done) { |
| 1810 | baseerror: |
| 1811 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1812 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1813 | length_restr ? "length" : "range", range_p->arg); |
| 1814 | goto cleanup; |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | if (!(*range)) { |
| 1819 | *range = calloc(1, sizeof **range); |
| 1820 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1821 | } |
| 1822 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1823 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1824 | if (range_p->eapptag) { |
| 1825 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1826 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1827 | } |
| 1828 | if (range_p->emsg) { |
| 1829 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1830 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1831 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1832 | if (range_p->dsc) { |
| 1833 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1834 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1835 | } |
| 1836 | if (range_p->ref) { |
| 1837 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1838 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1839 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1840 | /* extensions are taken only from the last range by the caller */ |
| 1841 | |
| 1842 | (*range)->parts = parts; |
| 1843 | parts = NULL; |
| 1844 | ret = LY_SUCCESS; |
| 1845 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1846 | LY_ARRAY_FREE(parts); |
| 1847 | |
| 1848 | return ret; |
| 1849 | } |
| 1850 | |
| 1851 | /** |
| 1852 | * @brief Checks pattern syntax. |
| 1853 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1854 | * @param[in] ctx Context. |
| 1855 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1856 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1857 | * @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] | 1858 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1859 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1860 | LY_ERR |
| 1861 | 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] | 1862 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1863 | int idx, idx2, start, end, count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1864 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1865 | int err_code; |
| 1866 | const char *orig_ptr; |
| 1867 | PCRE2_SIZE err_offset; |
| 1868 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1869 | #define URANGE_LEN 19 |
| 1870 | char *ublock2urange[][2] = { |
| 1871 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1872 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1873 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1874 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1875 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1876 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1877 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1878 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1879 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1880 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1881 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1882 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1883 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1884 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1885 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1886 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1887 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1888 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1889 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1890 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1891 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1892 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1893 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1894 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1895 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1896 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1897 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1898 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1899 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1900 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1901 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1902 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1903 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1904 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1905 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1906 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1907 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1908 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1909 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1910 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1911 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1912 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1913 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1914 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1915 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1916 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1917 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1918 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1919 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1920 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1921 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1922 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1923 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1924 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1925 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1926 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1927 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1928 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1929 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1930 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1931 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1932 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1933 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1934 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1935 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1936 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1937 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1938 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1939 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1940 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1941 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1942 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1943 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1944 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1945 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1946 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1947 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1948 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1949 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1950 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1951 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1952 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1953 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1954 | {NULL, NULL} |
| 1955 | }; |
| 1956 | |
| 1957 | /* adjust the expression to a Perl equivalent |
| 1958 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1959 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1960 | /* we need to replace all "$" and "^" with "\$" and "\^", count them now */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1961 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1962 | |
| 1963 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1964 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1965 | perl_regex[0] = '\0'; |
| 1966 | |
| 1967 | ptr = perl_regex; |
| 1968 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1969 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1970 | if (orig_ptr[0] == '$') { |
| 1971 | ptr += sprintf(ptr, "\\$"); |
| 1972 | } else if (orig_ptr[0] == '^') { |
| 1973 | ptr += sprintf(ptr, "\\^"); |
| 1974 | } else { |
| 1975 | ptr[0] = orig_ptr[0]; |
| 1976 | ++ptr; |
| 1977 | } |
| 1978 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1979 | ptr[0] = '\0'; |
| 1980 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1981 | |
| 1982 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1983 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1984 | start = ptr - perl_regex; |
| 1985 | |
| 1986 | ptr = strchr(ptr, '}'); |
| 1987 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1988 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1989 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1990 | free(perl_regex); |
| 1991 | return LY_EVALID; |
| 1992 | } |
| 1993 | end = (ptr - perl_regex) + 1; |
| 1994 | |
| 1995 | /* need more space */ |
| 1996 | if (end - start < URANGE_LEN) { |
| 1997 | 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] | 1998 | 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] | 1999 | } |
| 2000 | |
| 2001 | /* find our range */ |
| 2002 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2003 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2004 | break; |
| 2005 | } |
| 2006 | } |
| 2007 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2008 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2009 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2010 | free(perl_regex); |
| 2011 | return LY_EVALID; |
| 2012 | } |
| 2013 | |
| 2014 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 2015 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 2016 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 2017 | ++count; |
| 2018 | } |
| 2019 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 2020 | --count; |
| 2021 | } |
| 2022 | } |
| 2023 | if (count) { |
| 2024 | /* skip brackets */ |
| 2025 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2026 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2027 | } else { |
| 2028 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2029 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2030 | } |
| 2031 | } |
| 2032 | |
| 2033 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2034 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2035 | 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] | 2036 | &err_code, &err_offset, NULL); |
| 2037 | if (!code_local) { |
| 2038 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2039 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2040 | 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] | 2041 | free(perl_regex); |
| 2042 | return LY_EVALID; |
| 2043 | } |
| 2044 | free(perl_regex); |
| 2045 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2046 | if (code) { |
| 2047 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2048 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2049 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | return LY_SUCCESS; |
| 2053 | |
| 2054 | #undef URANGE_LEN |
| 2055 | } |
| 2056 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2057 | /** |
| 2058 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2059 | * @param[in] ctx Compile context. |
| 2060 | * @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] | 2061 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2062 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2063 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2064 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2065 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2066 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2067 | 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] | 2068 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2069 | { |
| 2070 | struct lysc_pattern **pattern; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2071 | unsigned int u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2072 | LY_ERR ret = LY_SUCCESS; |
| 2073 | |
| 2074 | /* first, copy the patterns from the base type */ |
| 2075 | if (base_patterns) { |
| 2076 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2077 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2078 | } |
| 2079 | |
| 2080 | LY_ARRAY_FOR(patterns_p, u) { |
| 2081 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2082 | *pattern = calloc(1, sizeof **pattern); |
| 2083 | ++(*pattern)->refcount; |
| 2084 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2085 | 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] | 2086 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2087 | |
| 2088 | if (patterns_p[u].arg[0] == 0x15) { |
| 2089 | (*pattern)->inverted = 1; |
| 2090 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2091 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2092 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2093 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2094 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2095 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2096 | 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] | 2097 | } |
| 2098 | done: |
| 2099 | return ret; |
| 2100 | } |
| 2101 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2102 | /** |
| 2103 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2104 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2105 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2106 | 0 /* LY_TYPE_UNKNOWN */, |
| 2107 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2108 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2109 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2110 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2111 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2112 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2113 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2114 | 0 /* LY_TYPE_BOOL */, |
| 2115 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2116 | 0 /* LY_TYPE_EMPTY */, |
| 2117 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2118 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2119 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2120 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2121 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2122 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2123 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2124 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2125 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2126 | }; |
| 2127 | |
| 2128 | /** |
| 2129 | * @brief stringification of the YANG built-in data types |
| 2130 | */ |
| 2131 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2132 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2133 | "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] | 2134 | }; |
| 2135 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2136 | /** |
| 2137 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2138 | * @param[in] ctx Compile context. |
| 2139 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2140 | * @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] | 2141 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2142 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2143 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2144 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2145 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2146 | 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] | 2147 | 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] | 2148 | { |
| 2149 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 2150 | unsigned int u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2151 | int32_t value = 0; |
| 2152 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2153 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2154 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2155 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2156 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2157 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2158 | return LY_EVALID; |
| 2159 | } |
| 2160 | |
| 2161 | LY_ARRAY_FOR(enums_p, u) { |
| 2162 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2163 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2164 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2165 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2166 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2167 | if (base_enums) { |
| 2168 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2169 | LY_ARRAY_FOR(base_enums, v) { |
| 2170 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2171 | break; |
| 2172 | } |
| 2173 | } |
| 2174 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 2175 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2176 | "Invalid %s - derived type adds new item \"%s\".", |
| 2177 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2178 | return LY_EVALID; |
| 2179 | } |
| 2180 | match = v; |
| 2181 | } |
| 2182 | |
| 2183 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2184 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2185 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2186 | e->value = (int32_t)enums_p[u].value; |
| 2187 | if (!u || e->value >= value) { |
| 2188 | value = e->value + 1; |
| 2189 | } |
| 2190 | /* check collision with other values */ |
| 2191 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2192 | if (e->value == (*enums)[v].value) { |
| 2193 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2194 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2195 | e->value, e->name, (*enums)[v].name); |
| 2196 | return LY_EVALID; |
| 2197 | } |
| 2198 | } |
| 2199 | } else if (base_enums) { |
| 2200 | /* inherit the assigned value */ |
| 2201 | e->value = base_enums[match].value; |
| 2202 | if (!u || e->value >= value) { |
| 2203 | value = e->value + 1; |
| 2204 | } |
| 2205 | } else { |
| 2206 | /* assign value automatically */ |
| 2207 | if (u && value == INT32_MIN) { |
| 2208 | /* counter overflow */ |
| 2209 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2210 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2211 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2212 | return LY_EVALID; |
| 2213 | } |
| 2214 | e->value = value++; |
| 2215 | } |
| 2216 | } else { /* LY_TYPE_BITS */ |
| 2217 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2218 | e->value = (int32_t)enums_p[u].value; |
| 2219 | if (!u || (uint32_t)e->value >= position) { |
| 2220 | position = (uint32_t)e->value + 1; |
| 2221 | } |
| 2222 | /* check collision with other values */ |
| 2223 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2224 | if (e->value == (*enums)[v].value) { |
| 2225 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2226 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2227 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2228 | return LY_EVALID; |
| 2229 | } |
| 2230 | } |
| 2231 | } else if (base_enums) { |
| 2232 | /* inherit the assigned value */ |
| 2233 | e->value = base_enums[match].value; |
| 2234 | if (!u || (uint32_t)e->value >= position) { |
| 2235 | position = (uint32_t)e->value + 1; |
| 2236 | } |
| 2237 | } else { |
| 2238 | /* assign value automatically */ |
| 2239 | if (u && position == 0) { |
| 2240 | /* counter overflow */ |
| 2241 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2242 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2243 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2244 | return LY_EVALID; |
| 2245 | } |
| 2246 | e->value = position++; |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | if (base_enums) { |
| 2251 | /* the assigned values must not change from the derived type */ |
| 2252 | if (e->value != base_enums[match].value) { |
| 2253 | if (basetype == LY_TYPE_ENUM) { |
| 2254 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2255 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2256 | e->name, base_enums[match].value, e->value); |
| 2257 | } else { |
| 2258 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2259 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2260 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2261 | } |
| 2262 | return LY_EVALID; |
| 2263 | } |
| 2264 | } |
| 2265 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2266 | 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] | 2267 | 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] | 2268 | |
| 2269 | if (basetype == LY_TYPE_BITS) { |
| 2270 | /* keep bits ordered by position */ |
| 2271 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2272 | if (v != u) { |
| 2273 | memcpy(&storage, e, sizeof *e); |
| 2274 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2275 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | done: |
| 2281 | return ret; |
| 2282 | } |
| 2283 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2284 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2285 | for ((NODE) = (NODE)->parent; \ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2286 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_ACTION)); \ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2287 | (NODE) = (NODE)->parent); \ |
| 2288 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2289 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2290 | TERM; \ |
| 2291 | } |
| 2292 | |
| 2293 | /** |
| 2294 | * @brief Validate the predicate(s) from the leafref path. |
| 2295 | * @param[in] ctx Compile context |
| 2296 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2297 | * 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] | 2298 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2299 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2300 | * @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] | 2301 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2302 | */ |
| 2303 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2304 | 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] | 2305 | 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] | 2306 | { |
| 2307 | LY_ERR ret = LY_EVALID; |
| 2308 | const struct lys_module *mod; |
| 2309 | const struct lysc_node *src_node, *dst_node; |
| 2310 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2311 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2312 | unsigned int dest_parent_times, c; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2313 | const char *start, *end, *pke_end; |
| 2314 | struct ly_set keys = {0}; |
| 2315 | int i; |
| 2316 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2317 | assert(path_context); |
| 2318 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2319 | while (**predicate == '[') { |
| 2320 | start = (*predicate)++; |
| 2321 | |
| 2322 | while (isspace(**predicate)) { |
| 2323 | ++(*predicate); |
| 2324 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2325 | 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] | 2326 | while (isspace(**predicate)) { |
| 2327 | ++(*predicate); |
| 2328 | } |
| 2329 | if (**predicate != '=') { |
| 2330 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2331 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2332 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2333 | goto cleanup; |
| 2334 | } |
| 2335 | ++(*predicate); |
| 2336 | while (isspace(**predicate)) { |
| 2337 | ++(*predicate); |
| 2338 | } |
| 2339 | |
| 2340 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2341 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2342 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2343 | goto cleanup; |
| 2344 | } |
| 2345 | --pke_end; |
| 2346 | while (isspace(*pke_end)) { |
| 2347 | --pke_end; |
| 2348 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2349 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2350 | /* localize path-key-expr */ |
| 2351 | pke_start = path_key_expr = *predicate; |
| 2352 | /* move after the current predicate */ |
| 2353 | *predicate = end + 1; |
| 2354 | |
| 2355 | /* source (must be leaf or leaf-list) */ |
| 2356 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2357 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2358 | if (!mod) { |
| 2359 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2360 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2361 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2362 | goto cleanup; |
| 2363 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2364 | if (!mod->implemented) { |
| 2365 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2366 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2367 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2368 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2369 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2370 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2371 | src_node = NULL; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2372 | if (!(context_node->flags & LYS_KEYLESS)) { |
| 2373 | struct lysc_node *key; |
| 2374 | 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] | 2375 | if (!ly_strncmp(key->name, src, src_len)) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2376 | src_node = key; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2377 | break; |
| 2378 | } |
| 2379 | } |
| 2380 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2381 | if (!src_node) { |
| 2382 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2383 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2384 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2385 | goto cleanup; |
| 2386 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2387 | |
| 2388 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2389 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2390 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2391 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2392 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2393 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2394 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2395 | *predicate - start, start, src_node->name); |
| 2396 | goto cleanup; |
| 2397 | } |
| 2398 | |
| 2399 | /* destination */ |
| 2400 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2401 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2402 | |
| 2403 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2404 | if (strncmp(path_key_expr, "current", 7)) { |
| 2405 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2406 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2407 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2408 | *predicate - start, start); |
| 2409 | goto cleanup; |
| 2410 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2411 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2412 | if (*path_key_expr != '(') { |
| 2413 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2414 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2415 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2416 | if (*path_key_expr != ')') { |
| 2417 | goto error_current_function_invocation; |
| 2418 | } |
| 2419 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2420 | |
| 2421 | if (*path_key_expr != '/') { |
| 2422 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2423 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2424 | *predicate - start, start); |
| 2425 | goto cleanup; |
| 2426 | } |
| 2427 | ++path_key_expr; |
| 2428 | while (isspace(*path_key_expr)) { |
| 2429 | ++path_key_expr; |
| 2430 | } |
| 2431 | |
| 2432 | /* rel-path-keyexpr: |
| 2433 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2434 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2435 | ++dest_parent_times; |
| 2436 | path_key_expr += 2; |
| 2437 | while (isspace(*path_key_expr)) { |
| 2438 | ++path_key_expr; |
| 2439 | } |
| 2440 | if (*path_key_expr != '/') { |
| 2441 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2442 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2443 | *predicate - start, start); |
| 2444 | goto cleanup; |
| 2445 | } |
| 2446 | ++path_key_expr; |
| 2447 | while (isspace(*path_key_expr)) { |
| 2448 | ++path_key_expr; |
| 2449 | } |
| 2450 | |
| 2451 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2452 | * all schema nodes that cannot be instantiated in data tree */ |
| 2453 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2454 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2455 | *predicate - start, start); |
| 2456 | } |
| 2457 | if (!dest_parent_times) { |
| 2458 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2459 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2460 | *predicate - start, start); |
| 2461 | goto cleanup; |
| 2462 | } |
| 2463 | if (path_key_expr == pke_end) { |
| 2464 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2465 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2466 | *predicate - start, start); |
| 2467 | goto cleanup; |
| 2468 | } |
| 2469 | |
| 2470 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2471 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2472 | 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] | 2473 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2474 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2475 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2476 | goto cleanup; |
| 2477 | } |
| 2478 | |
| 2479 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2480 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2481 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2482 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2483 | } |
| 2484 | if (!mod) { |
| 2485 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2486 | "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2487 | *predicate - start, start, dst_len, dst); |
| 2488 | goto cleanup; |
| 2489 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2490 | if (!mod->implemented) { |
| 2491 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2492 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2493 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2494 | |
| 2495 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2496 | if (!dst_node) { |
| 2497 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2498 | "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] | 2499 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2500 | goto cleanup; |
| 2501 | } |
| 2502 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2503 | 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] | 2504 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2505 | "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] | 2506 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2507 | goto cleanup; |
| 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | ret = LY_SUCCESS; |
| 2512 | cleanup: |
| 2513 | ly_set_erase(&keys, NULL); |
| 2514 | return ret; |
| 2515 | } |
| 2516 | |
| 2517 | /** |
| 2518 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2519 | * |
| 2520 | * path-arg = absolute-path / relative-path |
| 2521 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2522 | * relative-path = 1*(".." "/") descendant-path |
| 2523 | * |
| 2524 | * @param[in,out] path Path to parse. |
| 2525 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2526 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2527 | * @param[out] name Name of the token. |
| 2528 | * @param[out] nam_len Length of the name. |
| 2529 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2530 | * must not be changed between consecutive calls. -1 if the |
| 2531 | * path is absolute. |
| 2532 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2533 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2534 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2535 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2536 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2537 | int *parent_times, int *has_predicate) |
| 2538 | { |
| 2539 | int par_times = 0; |
| 2540 | |
| 2541 | assert(path && *path); |
| 2542 | assert(parent_times); |
| 2543 | assert(prefix); |
| 2544 | assert(prefix_len); |
| 2545 | assert(name); |
| 2546 | assert(name_len); |
| 2547 | assert(has_predicate); |
| 2548 | |
| 2549 | *prefix = NULL; |
| 2550 | *prefix_len = 0; |
| 2551 | *name = NULL; |
| 2552 | *name_len = 0; |
| 2553 | *has_predicate = 0; |
| 2554 | |
| 2555 | if (!*parent_times) { |
| 2556 | if (!strncmp(*path, "..", 2)) { |
| 2557 | *path += 2; |
| 2558 | ++par_times; |
| 2559 | while (!strncmp(*path, "/..", 3)) { |
| 2560 | *path += 3; |
| 2561 | ++par_times; |
| 2562 | } |
| 2563 | } |
| 2564 | if (par_times) { |
| 2565 | *parent_times = par_times; |
| 2566 | } else { |
| 2567 | *parent_times = -1; |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | if (**path != '/') { |
| 2572 | return LY_EINVAL; |
| 2573 | } |
| 2574 | /* skip '/' */ |
| 2575 | ++(*path); |
| 2576 | |
| 2577 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2578 | 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] | 2579 | |
| 2580 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2581 | /* path continues by another token or this is the last token */ |
| 2582 | return LY_SUCCESS; |
| 2583 | } else if ((*path)[0] != '[') { |
| 2584 | /* unexpected character */ |
| 2585 | return LY_EINVAL; |
| 2586 | } else { |
| 2587 | /* predicate starting with [ */ |
| 2588 | *has_predicate = 1; |
| 2589 | return LY_SUCCESS; |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2594 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2595 | * |
| 2596 | * The set of features used for target must be a subset of features used for the leafref. |
| 2597 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2598 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2599 | * |
| 2600 | * @param[in] refnode The leafref node. |
| 2601 | * @param[in] target Tha target node of the leafref. |
| 2602 | * @return LY_SUCCESS or LY_EVALID; |
| 2603 | */ |
| 2604 | static LY_ERR |
| 2605 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2606 | { |
| 2607 | LY_ERR ret = LY_EVALID; |
| 2608 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2609 | unsigned int u, v, count; |
| 2610 | struct ly_set features = {0}; |
| 2611 | |
| 2612 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2613 | if (iter->iffeatures) { |
| 2614 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2615 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2616 | 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] | 2617 | } |
| 2618 | } |
| 2619 | } |
| 2620 | } |
| 2621 | |
| 2622 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2623 | * So when adding features applicable to the target into the features set, we should not be |
| 2624 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2625 | * to the leafref itself. */ |
| 2626 | count = features.count; |
| 2627 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2628 | if (iter->iffeatures) { |
| 2629 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2630 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2631 | 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] | 2632 | /* new feature was added (or LY_EMEM) */ |
| 2633 | goto cleanup; |
| 2634 | } |
| 2635 | } |
| 2636 | } |
| 2637 | } |
| 2638 | } |
| 2639 | ret = LY_SUCCESS; |
| 2640 | |
| 2641 | cleanup: |
| 2642 | ly_set_erase(&features, NULL); |
| 2643 | return ret; |
| 2644 | } |
| 2645 | |
| 2646 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2647 | * @brief Validate the leafref path. |
| 2648 | * @param[in] ctx Compile context |
| 2649 | * @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] | 2650 | * @param[in] leafref Leafref to validate. |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2651 | * @param[out] target Optional resolved leafref target. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2652 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2653 | */ |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2654 | LY_ERR |
| 2655 | lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref, |
| 2656 | const struct lysc_node **target) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2657 | { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 2658 | const struct lysc_node *node = NULL, *parent = NULL; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2659 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2660 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2661 | const char *id, *prefix, *name; |
| 2662 | size_t prefix_len, name_len; |
| 2663 | int parent_times = 0, has_predicate; |
| 2664 | unsigned int iter, u; |
| 2665 | LY_ERR ret = LY_SUCCESS; |
| 2666 | |
| 2667 | assert(ctx); |
| 2668 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2669 | assert(leafref); |
| 2670 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2671 | ctx->path[0] = '\0'; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2672 | lysc_path(startnode, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2673 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2674 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2675 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2676 | id = leafref->path; |
Juraj Vijtiuk | bd0f2a6 | 2019-12-11 15:58:18 +0100 | [diff] [blame] | 2677 | |
| 2678 | if (!*id) { |
| 2679 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Empty leafref path."); |
| 2680 | return LY_EVALID; |
| 2681 | } |
| 2682 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2683 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2684 | if (!iter) { /* first iteration */ |
| 2685 | /* precess ".." in relative paths */ |
| 2686 | if (parent_times > 0) { |
| 2687 | /* move from the context node */ |
| 2688 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2689 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2690 | * all schema nodes that cannot be instantiated in data tree */ |
| 2691 | 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] | 2692 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2693 | } |
| 2694 | } |
| 2695 | } |
| 2696 | |
| 2697 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2698 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2699 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2700 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2701 | } |
| 2702 | if (!mod) { |
| 2703 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2704 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2705 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2706 | return LY_EVALID; |
| 2707 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2708 | if (!mod->implemented) { |
| 2709 | /* make the module implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 2710 | lys_set_implemented_internal((struct lys_module*)mod, 2); |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2711 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2712 | |
| 2713 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2714 | if (!node) { |
| 2715 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2716 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2717 | return LY_EVALID; |
| 2718 | } |
| 2719 | parent = node; |
| 2720 | |
| 2721 | if (has_predicate) { |
| 2722 | /* we have predicate, so the current result must be list */ |
| 2723 | if (node->nodetype != LYS_LIST) { |
| 2724 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2725 | "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] | 2726 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2727 | return LY_EVALID; |
| 2728 | } |
| 2729 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2730 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2731 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2732 | } |
| 2733 | |
| 2734 | ++iter; |
| 2735 | } |
| 2736 | if (ret) { |
| 2737 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2738 | "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] | 2739 | return LY_EVALID; |
| 2740 | } |
| 2741 | |
| 2742 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2744 | "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] | 2745 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2746 | return LY_EVALID; |
| 2747 | } |
| 2748 | |
| 2749 | /* check status */ |
| 2750 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2751 | return LY_EVALID; |
| 2752 | } |
| 2753 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2754 | /* check config */ |
| 2755 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2756 | if (node->flags & LYS_CONFIG_R) { |
| 2757 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2758 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2759 | leafref->path); |
| 2760 | return LY_EVALID; |
| 2761 | } |
| 2762 | } |
| 2763 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2764 | /* store the target's type and check for circular chain of leafrefs */ |
| 2765 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2766 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2767 | if (type == (struct lysc_type*)leafref) { |
| 2768 | /* circular chain detected */ |
| 2769 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2770 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2771 | return LY_EVALID; |
| 2772 | } |
| 2773 | } |
| 2774 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2775 | /* check if leafref and its target are under common if-features */ |
| 2776 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2777 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2778 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2779 | leafref->path); |
| 2780 | return LY_EVALID; |
| 2781 | } |
| 2782 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2783 | ctx->path_len = 1; |
| 2784 | ctx->path[1] = '\0'; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 2785 | if (target) { |
| 2786 | *target = node; |
| 2787 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2788 | return LY_SUCCESS; |
| 2789 | } |
| 2790 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2791 | 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] | 2792 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2793 | /** |
| 2794 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2795 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2796 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2797 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2798 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2799 | * @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] | 2800 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2801 | * @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] | 2802 | * @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] | 2803 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2804 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2805 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2806 | * @return LY_ERR value. |
| 2807 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2808 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2809 | 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] | 2810 | 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] | 2811 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2812 | { |
| 2813 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2814 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2815 | struct lysc_type_bin *bin; |
| 2816 | struct lysc_type_num *num; |
| 2817 | struct lysc_type_str *str; |
| 2818 | struct lysc_type_bits *bits; |
| 2819 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2820 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2821 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2822 | struct lysc_type_union *un, *un_aux; |
| 2823 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2824 | |
| 2825 | switch (basetype) { |
| 2826 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2827 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2828 | |
| 2829 | /* 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] | 2830 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2831 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2832 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2833 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2834 | 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] | 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | if (tpdfname) { |
| 2839 | type_p->compiled = *type; |
| 2840 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2841 | } |
| 2842 | break; |
| 2843 | case LY_TYPE_BITS: |
| 2844 | /* RFC 7950 9.7 - bits */ |
| 2845 | bits = (struct lysc_type_bits*)(*type); |
| 2846 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2847 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2848 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2849 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2850 | } |
| 2851 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2852 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2853 | /* 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] | 2854 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2855 | 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] | 2856 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2857 | 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] | 2858 | free(*type); |
| 2859 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2860 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2861 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | if (tpdfname) { |
| 2865 | type_p->compiled = *type; |
| 2866 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2867 | } |
| 2868 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2869 | case LY_TYPE_DEC64: |
| 2870 | dec = (struct lysc_type_dec*)(*type); |
| 2871 | |
| 2872 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2873 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2874 | if (!type_p->fraction_digits) { |
| 2875 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2876 | 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] | 2877 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2878 | 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] | 2879 | free(*type); |
| 2880 | *type = NULL; |
| 2881 | } |
| 2882 | return LY_EVALID; |
| 2883 | } |
| 2884 | } else if (type_p->fraction_digits) { |
| 2885 | /* 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] | 2886 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2887 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2888 | "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] | 2889 | tpdfname); |
| 2890 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2891 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2892 | "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] | 2893 | free(*type); |
| 2894 | *type = NULL; |
| 2895 | } |
| 2896 | return LY_EVALID; |
| 2897 | } |
| 2898 | dec->fraction_digits = type_p->fraction_digits; |
| 2899 | |
| 2900 | /* RFC 7950 9.2.4 - range */ |
| 2901 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2902 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2903 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2904 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2905 | 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] | 2906 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2907 | } |
| 2908 | |
| 2909 | if (tpdfname) { |
| 2910 | type_p->compiled = *type; |
| 2911 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2912 | } |
| 2913 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2914 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2915 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2916 | |
| 2917 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2918 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2919 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2920 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2921 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2922 | 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] | 2923 | } |
| 2924 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2925 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2926 | } |
| 2927 | |
| 2928 | /* RFC 7950 9.4.5 - pattern */ |
| 2929 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2930 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2931 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2932 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2933 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2934 | } |
| 2935 | |
| 2936 | if (tpdfname) { |
| 2937 | type_p->compiled = *type; |
| 2938 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2939 | } |
| 2940 | break; |
| 2941 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2942 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2943 | |
| 2944 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2945 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2946 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2947 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2948 | } |
| 2949 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2950 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2951 | /* 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] | 2952 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2953 | 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] | 2954 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2955 | 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] | 2956 | free(*type); |
| 2957 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2958 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2959 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | if (tpdfname) { |
| 2963 | type_p->compiled = *type; |
| 2964 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2965 | } |
| 2966 | break; |
| 2967 | case LY_TYPE_INT8: |
| 2968 | case LY_TYPE_UINT8: |
| 2969 | case LY_TYPE_INT16: |
| 2970 | case LY_TYPE_UINT16: |
| 2971 | case LY_TYPE_INT32: |
| 2972 | case LY_TYPE_UINT32: |
| 2973 | case LY_TYPE_INT64: |
| 2974 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2975 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2976 | |
| 2977 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2978 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2979 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2980 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2981 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2982 | 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] | 2983 | } |
| 2984 | } |
| 2985 | |
| 2986 | if (tpdfname) { |
| 2987 | type_p->compiled = *type; |
| 2988 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2989 | } |
| 2990 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2991 | case LY_TYPE_IDENT: |
| 2992 | idref = (struct lysc_type_identityref*)(*type); |
| 2993 | |
| 2994 | /* RFC 7950 9.10.2 - base */ |
| 2995 | if (type_p->bases) { |
| 2996 | if (base) { |
| 2997 | /* only the directly derived identityrefs can contain base specification */ |
| 2998 | if (tpdfname) { |
| 2999 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3000 | "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] | 3001 | tpdfname); |
| 3002 | } else { |
| 3003 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3004 | "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] | 3005 | free(*type); |
| 3006 | *type = NULL; |
| 3007 | } |
| 3008 | return LY_EVALID; |
| 3009 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3010 | 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] | 3011 | } |
| 3012 | |
| 3013 | if (!base && !type_p->flags) { |
| 3014 | /* type derived from identityref built-in type must contain at least one base */ |
| 3015 | if (tpdfname) { |
| 3016 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 3017 | } else { |
| 3018 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 3019 | free(*type); |
| 3020 | *type = NULL; |
| 3021 | } |
| 3022 | return LY_EVALID; |
| 3023 | } |
| 3024 | |
| 3025 | if (tpdfname) { |
| 3026 | type_p->compiled = *type; |
| 3027 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3028 | } |
| 3029 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3030 | case LY_TYPE_LEAFREF: |
| 3031 | /* RFC 7950 9.9.3 - require-instance */ |
| 3032 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3033 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3034 | if (tpdfname) { |
| 3035 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3036 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 3037 | } else { |
| 3038 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3039 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 3040 | free(*type); |
| 3041 | *type = NULL; |
| 3042 | } |
| 3043 | return LY_EVALID; |
| 3044 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3045 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 3046 | } else if (base) { |
| 3047 | /* inherit */ |
| 3048 | ((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] | 3049 | } else { |
| 3050 | /* default is true */ |
| 3051 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 3052 | } |
| 3053 | if (type_p->path) { |
| 3054 | 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] | 3055 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3056 | } else if (base) { |
| 3057 | 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] | 3058 | ((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] | 3059 | } else if (tpdfname) { |
| 3060 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 3061 | return LY_EVALID; |
| 3062 | } else { |
| 3063 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 3064 | free(*type); |
| 3065 | *type = NULL; |
| 3066 | return LY_EVALID; |
| 3067 | } |
| 3068 | if (tpdfname) { |
| 3069 | type_p->compiled = *type; |
| 3070 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3071 | } |
| 3072 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 3073 | case LY_TYPE_INST: |
| 3074 | /* RFC 7950 9.9.3 - require-instance */ |
| 3075 | if (type_p->flags & LYS_SET_REQINST) { |
| 3076 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 3077 | } else { |
| 3078 | /* default is true */ |
| 3079 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 3080 | } |
| 3081 | |
| 3082 | if (tpdfname) { |
| 3083 | type_p->compiled = *type; |
| 3084 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3085 | } |
| 3086 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3087 | case LY_TYPE_UNION: |
| 3088 | un = (struct lysc_type_union*)(*type); |
| 3089 | |
| 3090 | /* RFC 7950 7.4 - type */ |
| 3091 | if (type_p->types) { |
| 3092 | if (base) { |
| 3093 | /* only the directly derived union can contain types specification */ |
| 3094 | if (tpdfname) { |
| 3095 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3096 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 3097 | tpdfname); |
| 3098 | } else { |
| 3099 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3100 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 3101 | free(*type); |
| 3102 | *type = NULL; |
| 3103 | } |
| 3104 | return LY_EVALID; |
| 3105 | } |
| 3106 | /* compile the type */ |
| 3107 | additional = 0; |
| 3108 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 3109 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3110 | 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] | 3111 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 3112 | /* add space for additional types from the union subtype */ |
| 3113 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 3114 | 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))); |
| 3115 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3116 | un->types = (void*)((uint32_t*)(p) + 1); |
| 3117 | |
| 3118 | /* copy subtypes of the subtype union */ |
| 3119 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 3120 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 3121 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 3122 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3123 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3124 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 3125 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 3126 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 3127 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 3128 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 3129 | /* TODO extensions */ |
| 3130 | |
| 3131 | } else { |
| 3132 | un->types[u + additional] = un_aux->types[v]; |
| 3133 | ++un_aux->types[v]->refcount; |
| 3134 | } |
| 3135 | ++additional; |
| 3136 | LY_ARRAY_INCREMENT(un->types); |
| 3137 | } |
| 3138 | /* compensate u increment in main loop */ |
| 3139 | --additional; |
| 3140 | |
| 3141 | /* free the replaced union subtype */ |
| 3142 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 3143 | } else { |
| 3144 | LY_ARRAY_INCREMENT(un->types); |
| 3145 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | if (!base && !type_p->flags) { |
| 3150 | /* type derived from union built-in type must contain at least one type */ |
| 3151 | if (tpdfname) { |
| 3152 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 3153 | } else { |
| 3154 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 3155 | free(*type); |
| 3156 | *type = NULL; |
| 3157 | } |
| 3158 | return LY_EVALID; |
| 3159 | } |
| 3160 | |
| 3161 | if (tpdfname) { |
| 3162 | type_p->compiled = *type; |
| 3163 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3164 | } |
| 3165 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3166 | case LY_TYPE_BOOL: |
| 3167 | case LY_TYPE_EMPTY: |
| 3168 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 3169 | break; |
| 3170 | } |
| 3171 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 3172 | done: |
| 3173 | return ret; |
| 3174 | } |
| 3175 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3176 | /** |
| 3177 | * @brief Compile information about the leaf/leaf-list's type. |
| 3178 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3179 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 3180 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3181 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3182 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 3183 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3184 | * @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] | 3185 | * @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] | 3186 | * @return LY_ERR value. |
| 3187 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3188 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3189 | 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] | 3190 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3191 | { |
| 3192 | LY_ERR ret = LY_SUCCESS; |
| 3193 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3194 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3195 | struct type_context { |
| 3196 | const struct lysp_tpdf *tpdf; |
| 3197 | struct lysp_node *node; |
| 3198 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3199 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3200 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3201 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3202 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3203 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3204 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3205 | |
| 3206 | (*type) = NULL; |
| 3207 | |
| 3208 | tctx = calloc(1, sizeof *tctx); |
| 3209 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3210 | 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] | 3211 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 3212 | ret == LY_SUCCESS; |
| 3213 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 3214 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 3215 | if (basetype) { |
| 3216 | break; |
| 3217 | } |
| 3218 | |
| 3219 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3220 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3221 | 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] | 3222 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3223 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3224 | if (units && !*units) { |
| 3225 | /* inherit units */ |
| 3226 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3227 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3228 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3229 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3230 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3231 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3232 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3233 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3234 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3235 | break; |
| 3236 | } |
| 3237 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3238 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3239 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3240 | * 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] | 3241 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3242 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3243 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3244 | dummyloops = 1; |
| 3245 | goto preparenext; |
| 3246 | } else { |
| 3247 | tctx = NULL; |
| 3248 | break; |
| 3249 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3250 | } |
| 3251 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3252 | /* circular typedef reference detection */ |
| 3253 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3254 | /* local part */ |
| 3255 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3256 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3257 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3258 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3259 | free(tctx); |
| 3260 | ret = LY_EVALID; |
| 3261 | goto cleanup; |
| 3262 | } |
| 3263 | } |
| 3264 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3265 | /* global part for unions corner case */ |
| 3266 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3267 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3268 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3269 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3270 | free(tctx); |
| 3271 | ret = LY_EVALID; |
| 3272 | goto cleanup; |
| 3273 | } |
| 3274 | } |
| 3275 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3276 | /* store information for the following processing */ |
| 3277 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3278 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3279 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3280 | /* prepare next loop */ |
| 3281 | tctx_prev = tctx; |
| 3282 | tctx = calloc(1, sizeof *tctx); |
| 3283 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3284 | } |
| 3285 | free(tctx); |
| 3286 | |
| 3287 | /* allocate type according to the basetype */ |
| 3288 | switch (basetype) { |
| 3289 | case LY_TYPE_BINARY: |
| 3290 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3291 | break; |
| 3292 | case LY_TYPE_BITS: |
| 3293 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3294 | break; |
| 3295 | case LY_TYPE_BOOL: |
| 3296 | case LY_TYPE_EMPTY: |
| 3297 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3298 | break; |
| 3299 | case LY_TYPE_DEC64: |
| 3300 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3301 | break; |
| 3302 | case LY_TYPE_ENUM: |
| 3303 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3304 | break; |
| 3305 | case LY_TYPE_IDENT: |
| 3306 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3307 | break; |
| 3308 | case LY_TYPE_INST: |
| 3309 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3310 | break; |
| 3311 | case LY_TYPE_LEAFREF: |
| 3312 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3313 | break; |
| 3314 | case LY_TYPE_STRING: |
| 3315 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3316 | break; |
| 3317 | case LY_TYPE_UNION: |
| 3318 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3319 | break; |
| 3320 | case LY_TYPE_INT8: |
| 3321 | case LY_TYPE_UINT8: |
| 3322 | case LY_TYPE_INT16: |
| 3323 | case LY_TYPE_UINT16: |
| 3324 | case LY_TYPE_INT32: |
| 3325 | case LY_TYPE_UINT32: |
| 3326 | case LY_TYPE_INT64: |
| 3327 | case LY_TYPE_UINT64: |
| 3328 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3329 | break; |
| 3330 | case LY_TYPE_UNKNOWN: |
| 3331 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3332 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3333 | ret = LY_EVALID; |
| 3334 | goto cleanup; |
| 3335 | } |
| 3336 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3337 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3338 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3339 | ly_data_type2str[basetype]); |
| 3340 | free(*type); |
| 3341 | (*type) = NULL; |
| 3342 | ret = LY_EVALID; |
| 3343 | goto cleanup; |
| 3344 | } |
| 3345 | |
| 3346 | /* get restrictions from the referred typedefs */ |
| 3347 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3348 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3349 | |
| 3350 | /* remember the typedef context for circular check */ |
| 3351 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3352 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3353 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3354 | base = tctx->tpdf->type.compiled; |
| 3355 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3356 | } 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] | 3357 | /* no change, just use the type information from the base */ |
| 3358 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3359 | ++base->refcount; |
| 3360 | continue; |
| 3361 | } |
| 3362 | |
| 3363 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3364 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3365 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3366 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3367 | ret = LY_EVALID; |
| 3368 | goto cleanup; |
| 3369 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3370 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3371 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3372 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3373 | ret = LY_EVALID; |
| 3374 | goto cleanup; |
| 3375 | } |
| 3376 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3377 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3378 | /* TODO user type plugins */ |
| 3379 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3380 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3381 | 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] | 3382 | 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] | 3383 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3384 | LY_CHECK_GOTO(ret, cleanup); |
| 3385 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3386 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3387 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3388 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3389 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3390 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3391 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3392 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3393 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3394 | /* TODO user type plugins */ |
| 3395 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3396 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3397 | 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] | 3398 | LY_CHECK_GOTO(ret, cleanup); |
| 3399 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3400 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3401 | free(*type); |
| 3402 | (*type) = base; |
| 3403 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3404 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3405 | if (dflt && !(*type)->dflt) { |
| 3406 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3407 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3408 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3409 | (*type)->dflt->realtype = (*type); |
| 3410 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3411 | 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] | 3412 | 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] | 3413 | if (err) { |
| 3414 | ly_err_print(err); |
| 3415 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3416 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3417 | ly_err_free(err); |
| 3418 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3419 | if (ret == LY_EINCOMPLETE) { |
| 3420 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3421 | 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] | 3422 | |
| 3423 | /* but in general result is so far ok */ |
| 3424 | ret = LY_SUCCESS; |
| 3425 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3426 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3427 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3428 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3429 | 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] | 3430 | |
| 3431 | cleanup: |
| 3432 | ly_set_erase(&tpdf_chain, free); |
| 3433 | return ret; |
| 3434 | } |
| 3435 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3436 | /** |
| 3437 | * @brief Compile status information of the given node. |
| 3438 | * |
| 3439 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3440 | * has the status correctly set during the compilation. |
| 3441 | * |
| 3442 | * @param[in] ctx Compile context |
| 3443 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3444 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3445 | * the compatibility with the parent's status value. |
| 3446 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3447 | * @return LY_ERR value. |
| 3448 | */ |
| 3449 | static LY_ERR |
| 3450 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3451 | { |
| 3452 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3453 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3454 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3455 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3456 | if ((parent_flags & 0x3) != 0x3) { |
| 3457 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3458 | * combination of bits (0x3) which marks the uses_status value */ |
| 3459 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3460 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3461 | } |
| 3462 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3463 | } else { |
| 3464 | (*node_flags) |= LYS_STATUS_CURR; |
| 3465 | } |
| 3466 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3467 | /* check status compatibility with the parent */ |
| 3468 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3469 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3470 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3471 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3472 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3473 | } else { /* LYS_STATUS_DEPRC */ |
| 3474 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3475 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3476 | } |
| 3477 | return LY_EVALID; |
| 3478 | } |
| 3479 | } |
| 3480 | return LY_SUCCESS; |
| 3481 | } |
| 3482 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3483 | /** |
| 3484 | * @brief Check uniqness of the node/action/notification name. |
| 3485 | * |
| 3486 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3487 | * structures, but they share the namespace so we need to check their name collisions. |
| 3488 | * |
| 3489 | * @param[in] ctx Compile context. |
| 3490 | * @param[in] children List (linked list) of data nodes to go through. |
| 3491 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3492 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3493 | * @param[in] name Name of the item to find in the given lists. |
| 3494 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3495 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3496 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3497 | */ |
| 3498 | static LY_ERR |
| 3499 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3500 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3501 | const char *name, void *exclude) |
| 3502 | { |
| 3503 | const struct lysc_node *iter; |
| 3504 | unsigned int u; |
| 3505 | |
| 3506 | LY_LIST_FOR(children, iter) { |
| 3507 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3508 | goto error; |
| 3509 | } |
| 3510 | } |
| 3511 | LY_ARRAY_FOR(actions, u) { |
| 3512 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3513 | goto error; |
| 3514 | } |
| 3515 | } |
| 3516 | LY_ARRAY_FOR(notifs, u) { |
| 3517 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3518 | goto error; |
| 3519 | } |
| 3520 | } |
| 3521 | return LY_SUCCESS; |
| 3522 | error: |
| 3523 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification"); |
| 3524 | return LY_EEXIST; |
| 3525 | } |
| 3526 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3527 | 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] | 3528 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3529 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3530 | * @brief Compile parsed RPC/action schema node information. |
| 3531 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3532 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3533 | * @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] | 3534 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3535 | * @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). |
| 3536 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3537 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3538 | */ |
| 3539 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3540 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3541 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3542 | { |
| 3543 | LY_ERR ret = LY_SUCCESS; |
| 3544 | struct lysp_node *child_p; |
| 3545 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3546 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3547 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3548 | lysc_update_path(ctx, parent, action_p->name); |
| 3549 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3550 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3551 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3552 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3553 | action_p->name, action)) { |
| 3554 | return LY_EVALID; |
| 3555 | } |
| 3556 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3557 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3558 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3559 | "Action \"%s\" is placed inside %s.", action_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3560 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3561 | return LY_EVALID; |
| 3562 | } |
| 3563 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3564 | action->nodetype = LYS_ACTION; |
| 3565 | action->module = ctx->mod; |
| 3566 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3567 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3568 | action->sp = action_p; |
| 3569 | } |
| 3570 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3571 | |
| 3572 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3573 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3574 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3575 | |
| 3576 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3577 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3578 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3579 | 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] | 3580 | 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] | 3581 | |
| 3582 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3583 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3584 | 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] | 3585 | 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] | 3586 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3587 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3588 | 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] | 3589 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3590 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3591 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3592 | |
| 3593 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3594 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3595 | 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] | 3596 | 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] | 3597 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3598 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3599 | 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] | 3600 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3601 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3602 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3603 | |
| 3604 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3605 | /* do not check "must" semantics in a grouping */ |
| 3606 | ly_set_add(&ctx->unres, action, 0); |
| 3607 | } |
| 3608 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3609 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3610 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3611 | return ret; |
| 3612 | } |
| 3613 | |
| 3614 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3615 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3616 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3617 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3618 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3619 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3620 | * @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] | 3621 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3622 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3623 | */ |
| 3624 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3625 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3626 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3627 | { |
| 3628 | LY_ERR ret = LY_SUCCESS; |
| 3629 | struct lysp_node *child_p; |
| 3630 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3631 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3632 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3633 | lysc_update_path(ctx, parent, notif_p->name); |
| 3634 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3635 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3636 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3637 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3638 | notif_p->name, notif)) { |
| 3639 | return LY_EVALID; |
| 3640 | } |
| 3641 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3642 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3643 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3644 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3645 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3646 | return LY_EVALID; |
| 3647 | } |
| 3648 | |
| 3649 | notif->nodetype = LYS_NOTIF; |
| 3650 | notif->module = ctx->mod; |
| 3651 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3652 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3653 | notif->sp = notif_p; |
| 3654 | } |
| 3655 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3656 | |
| 3657 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3658 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3659 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3660 | |
| 3661 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3662 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3663 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3664 | 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] | 3665 | 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] | 3666 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3667 | /* do not check "must" semantics in a grouping */ |
| 3668 | ly_set_add(&ctx->unres, notif, 0); |
| 3669 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3670 | 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] | 3671 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3672 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3673 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3674 | 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] | 3675 | } |
| 3676 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3677 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3678 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3679 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3680 | return ret; |
| 3681 | } |
| 3682 | |
| 3683 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3684 | * @brief Compile parsed container node information. |
| 3685 | * @param[in] ctx Compile context |
| 3686 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3687 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3688 | * is enriched with the container-specific information. |
| 3689 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3690 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3691 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3692 | 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] | 3693 | { |
| 3694 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3695 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3696 | struct lysp_node *child_p; |
| 3697 | unsigned int u; |
| 3698 | LY_ERR ret = LY_SUCCESS; |
| 3699 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3700 | if (cont_p->presence) { |
| 3701 | cont->flags |= LYS_PRESENCE; |
| 3702 | } |
| 3703 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3704 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3705 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3706 | } |
| 3707 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3708 | 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] | 3709 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3710 | /* do not check "must" semantics in a grouping */ |
| 3711 | ly_set_add(&ctx->unres, cont, 0); |
| 3712 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3713 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3714 | 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] | 3715 | |
| 3716 | done: |
| 3717 | return ret; |
| 3718 | } |
| 3719 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3720 | /* |
| 3721 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3722 | * @param[in] ctx Compile context. |
| 3723 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3724 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3725 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3726 | * @return LY_ERR value. |
| 3727 | */ |
| 3728 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3729 | 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] | 3730 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3731 | unsigned int u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3732 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3733 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3734 | 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] | 3735 | leaf->units ? NULL : &leaf->units)); |
| 3736 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3737 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3738 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3739 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3740 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3741 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3742 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3743 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3744 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3745 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3746 | } |
| 3747 | } else { |
| 3748 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3749 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3750 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3751 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3752 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3753 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3754 | } |
| 3755 | } |
| 3756 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3757 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3758 | ly_set_add(&ctx->unres, leaf, 0); |
| 3759 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3760 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3761 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3762 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3763 | ly_set_add(&ctx->unres, leaf, 0); |
| 3764 | } |
| 3765 | } |
| 3766 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3767 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3768 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3769 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3770 | return LY_EVALID; |
| 3771 | } |
| 3772 | } |
| 3773 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3774 | return LY_SUCCESS; |
| 3775 | } |
| 3776 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3777 | /** |
| 3778 | * @brief Compile parsed leaf node information. |
| 3779 | * @param[in] ctx Compile context |
| 3780 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3781 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3782 | * is enriched with the leaf-specific information. |
| 3783 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3784 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3785 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3786 | 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] | 3787 | { |
| 3788 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3789 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3790 | unsigned int u; |
| 3791 | LY_ERR ret = LY_SUCCESS; |
| 3792 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3793 | 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] | 3794 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3795 | /* do not check "must" semantics in a grouping */ |
| 3796 | ly_set_add(&ctx->unres, leaf, 0); |
| 3797 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3798 | if (leaf_p->units) { |
| 3799 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3800 | leaf->flags |= LYS_SET_UNITS; |
| 3801 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3802 | |
| 3803 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3804 | leaf->dflt = (void*)leaf_p->dflt; |
| 3805 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3806 | if (ret) { |
| 3807 | leaf->dflt = NULL; |
| 3808 | return ret; |
| 3809 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3810 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3811 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3812 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3813 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3814 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3815 | leaf->dflt->realtype = leaf->type; |
| 3816 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3817 | 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] | 3818 | 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] | 3819 | leaf->dflt->realtype->refcount++; |
| 3820 | if (err) { |
| 3821 | ly_err_print(err); |
| 3822 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3823 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3824 | ly_err_free(err); |
| 3825 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3826 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3827 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3828 | 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] | 3829 | |
| 3830 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3831 | ret = LY_SUCCESS; |
| 3832 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3833 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3834 | leaf->flags |= LYS_SET_DFLT; |
| 3835 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3836 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3837 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3838 | done: |
| 3839 | return ret; |
| 3840 | } |
| 3841 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3842 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3843 | * @brief Compile parsed leaf-list node information. |
| 3844 | * @param[in] ctx Compile context |
| 3845 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3846 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3847 | * is enriched with the leaf-list-specific information. |
| 3848 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3849 | */ |
| 3850 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3851 | 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] | 3852 | { |
| 3853 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3854 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3855 | unsigned int u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3856 | LY_ERR ret = LY_SUCCESS; |
| 3857 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3858 | 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] | 3859 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3860 | /* do not check "must" semantics in a grouping */ |
| 3861 | ly_set_add(&ctx->unres, llist, 0); |
| 3862 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3863 | if (llist_p->units) { |
| 3864 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3865 | llist->flags |= LYS_SET_UNITS; |
| 3866 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3867 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3868 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3869 | llist->dflts = (void*)llist_p->dflts; |
| 3870 | ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3871 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3872 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3873 | 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] | 3874 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3875 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3876 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3877 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3878 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3879 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3880 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3881 | llist->dflts[u]->realtype = llist->type; |
| 3882 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3883 | 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] | 3884 | 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] | 3885 | llist->dflts[u]->realtype->refcount++; |
| 3886 | if (err) { |
| 3887 | ly_err_print(err); |
| 3888 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3889 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3890 | ly_err_free(err); |
| 3891 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3892 | if (ret == LY_EINCOMPLETE) { |
| 3893 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3894 | 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] | 3895 | |
| 3896 | /* but in general result is so far ok */ |
| 3897 | ret = LY_SUCCESS; |
| 3898 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3899 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3900 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3901 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3902 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3903 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3904 | /* configuration data values must be unique - so check the default values */ |
| 3905 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3906 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3907 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3908 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3909 | 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] | 3910 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3911 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3912 | if (dynamic) { |
| 3913 | free((char*)val); |
| 3914 | } |
| 3915 | return LY_EVALID; |
| 3916 | } |
| 3917 | } |
| 3918 | } |
| 3919 | } |
| 3920 | |
| 3921 | /* 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] | 3922 | |
| 3923 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3924 | if (llist->min) { |
| 3925 | llist->flags |= LYS_MAND_TRUE; |
| 3926 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3927 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3928 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3929 | done: |
| 3930 | return ret; |
| 3931 | } |
| 3932 | |
| 3933 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3934 | * @brief Compile information about list's uniques. |
| 3935 | * @param[in] ctx Compile context. |
| 3936 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3937 | * @param[in] uniques Sized array list of unique statements. |
| 3938 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3939 | * @return LY_ERR value. |
| 3940 | */ |
| 3941 | static LY_ERR |
| 3942 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3943 | { |
| 3944 | LY_ERR ret = LY_SUCCESS; |
| 3945 | struct lysc_node_leaf **key, ***unique; |
| 3946 | const char *keystr, *delim; |
| 3947 | size_t len; |
| 3948 | unsigned int v; |
| 3949 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3950 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3951 | |
| 3952 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3953 | config = -1; |
| 3954 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3955 | keystr = uniques[v]; |
| 3956 | while (keystr) { |
| 3957 | delim = strpbrk(keystr, " \t\n"); |
| 3958 | if (delim) { |
| 3959 | len = delim - keystr; |
| 3960 | while (isspace(*delim)) { |
| 3961 | ++delim; |
| 3962 | } |
| 3963 | } else { |
| 3964 | len = strlen(keystr); |
| 3965 | } |
| 3966 | |
| 3967 | /* unique node must be present */ |
| 3968 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3969 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3970 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3971 | if (ret != LY_SUCCESS) { |
| 3972 | if (ret == LY_EDENIED) { |
| 3973 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3974 | "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] | 3975 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3976 | } |
| 3977 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3978 | } else if (flags) { |
| 3979 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3980 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3981 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
| 3982 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3983 | } |
| 3984 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3985 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3986 | /* all referenced leafs must be of the same config type */ |
| 3987 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3988 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3989 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3990 | return LY_EVALID; |
| 3991 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3992 | config = 1; |
| 3993 | } else { /* LYS_CONFIG_R */ |
| 3994 | config = 0; |
| 3995 | } |
| 3996 | |
| 3997 | /* check status */ |
| 3998 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3999 | (*key)->flags, (*key)->module, (*key)->name)); |
| 4000 | |
| 4001 | /* mark leaf as unique */ |
| 4002 | (*key)->flags |= LYS_UNIQUE; |
| 4003 | |
| 4004 | /* next unique value in line */ |
| 4005 | keystr = delim; |
| 4006 | } |
| 4007 | /* next unique definition */ |
| 4008 | } |
| 4009 | |
| 4010 | return LY_SUCCESS; |
| 4011 | } |
| 4012 | |
| 4013 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4014 | * @brief Compile parsed list node information. |
| 4015 | * @param[in] ctx Compile context |
| 4016 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4017 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4018 | * is enriched with the list-specific information. |
| 4019 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4020 | */ |
| 4021 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4022 | 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] | 4023 | { |
| 4024 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 4025 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 4026 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4027 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4028 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4029 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4030 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4031 | LY_ERR ret = LY_SUCCESS; |
| 4032 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4033 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4034 | if (list->min) { |
| 4035 | list->flags |= LYS_MAND_TRUE; |
| 4036 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4037 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 4038 | |
| 4039 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4040 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4041 | } |
| 4042 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4043 | 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] | 4044 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4045 | /* do not check "must" semantics in a grouping */ |
| 4046 | ly_set_add(&ctx->unres, list, 0); |
| 4047 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4048 | |
| 4049 | /* keys */ |
| 4050 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 4051 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 4052 | return LY_EVALID; |
| 4053 | } |
| 4054 | |
| 4055 | /* find all the keys (must be direct children) */ |
| 4056 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4057 | if (!keystr) { |
| 4058 | /* keyless list */ |
| 4059 | list->flags |= LYS_KEYLESS; |
| 4060 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4061 | while (keystr) { |
| 4062 | delim = strpbrk(keystr, " \t\n"); |
| 4063 | if (delim) { |
| 4064 | len = delim - keystr; |
| 4065 | while (isspace(*delim)) { |
| 4066 | ++delim; |
| 4067 | } |
| 4068 | } else { |
| 4069 | len = strlen(keystr); |
| 4070 | } |
| 4071 | |
| 4072 | /* key node must be present */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4073 | key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 4074 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4075 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4076 | "The list's key \"%.*s\" not found.", len, keystr); |
| 4077 | return LY_EVALID; |
| 4078 | } |
| 4079 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4080 | if (key->flags & LYS_KEY) { |
| 4081 | /* the node was already marked as a key */ |
| 4082 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4083 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 4084 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4085 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4086 | |
| 4087 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4088 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4089 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4090 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 4091 | return LY_EVALID; |
| 4092 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4093 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4094 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4095 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4096 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4097 | "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] | 4098 | return LY_EVALID; |
| 4099 | } |
| 4100 | } else { |
| 4101 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4102 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4103 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4104 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4105 | return LY_EVALID; |
| 4106 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4107 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4108 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4109 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4110 | return LY_EVALID; |
| 4111 | } |
| 4112 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4113 | |
| 4114 | /* check status */ |
| 4115 | 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] | 4116 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4117 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4118 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4119 | if (key->dflt) { |
| 4120 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 4121 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 4122 | free(key->dflt); |
| 4123 | key->dflt = NULL; |
| 4124 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4125 | } |
| 4126 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4127 | key->flags |= LYS_KEY; |
| 4128 | |
| 4129 | /* move it to the correct position */ |
| 4130 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 4131 | /* fix links in closest previous siblings of the key */ |
| 4132 | if (key->next) { |
| 4133 | key->next->prev = key->prev; |
| 4134 | } else { |
| 4135 | /* last child */ |
| 4136 | list->child->prev = key->prev; |
| 4137 | } |
| 4138 | if (key->prev->next) { |
| 4139 | key->prev->next = key->next; |
| 4140 | } |
| 4141 | /* fix links in the key */ |
| 4142 | if (prev_key) { |
| 4143 | key->prev = (struct lysc_node*)prev_key; |
| 4144 | key->next = prev_key->next; |
| 4145 | } else { |
| 4146 | key->prev = list->child->prev; |
| 4147 | key->next = list->child; |
| 4148 | } |
| 4149 | /* fix links in closes future siblings of the key */ |
| 4150 | if (prev_key) { |
| 4151 | if (prev_key->next) { |
| 4152 | prev_key->next->prev = (struct lysc_node*)key; |
| 4153 | } else { |
| 4154 | list->child->prev = (struct lysc_node*)key; |
| 4155 | } |
| 4156 | prev_key->next = (struct lysc_node*)key; |
| 4157 | } else { |
| 4158 | list->child->prev = (struct lysc_node*)key; |
| 4159 | } |
| 4160 | /* fix links in parent */ |
| 4161 | if (!key->prev->next) { |
| 4162 | list->child = (struct lysc_node*)key; |
| 4163 | } |
| 4164 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4165 | |
| 4166 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4167 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4168 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4169 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | /* uniques */ |
| 4173 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4174 | 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] | 4175 | } |
| 4176 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4177 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 4178 | 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] | 4179 | |
| 4180 | done: |
| 4181 | return ret; |
| 4182 | } |
| 4183 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4184 | /** |
| 4185 | * @brief Do some checks and set the default choice's case. |
| 4186 | * |
| 4187 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 4188 | * |
| 4189 | * @param[in] ctx Compile context. |
| 4190 | * @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, |
| 4191 | * not the reference to the imported module. |
| 4192 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 4193 | * @return LY_ERR value. |
| 4194 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4195 | static LY_ERR |
| 4196 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 4197 | { |
| 4198 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 4199 | const char *prefix = NULL, *name; |
| 4200 | size_t prefix_len = 0; |
| 4201 | |
| 4202 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4203 | name = strchr(dflt, ':'); |
| 4204 | if (name) { |
| 4205 | prefix = dflt; |
| 4206 | prefix_len = name - prefix; |
| 4207 | ++name; |
| 4208 | } else { |
| 4209 | name = dflt; |
| 4210 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 4211 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4212 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 4213 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4214 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 4215 | prefix_len, prefix); |
| 4216 | return LY_EVALID; |
| 4217 | } |
| 4218 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 4219 | if (!ch->dflt) { |
| 4220 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4221 | "Default case \"%s\" not found.", dflt); |
| 4222 | return LY_EVALID; |
| 4223 | } |
| 4224 | /* no mandatory nodes directly under the default case */ |
| 4225 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4226 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 4227 | break; |
| 4228 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4229 | if (iter->flags & LYS_MAND_TRUE) { |
| 4230 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4231 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 4232 | return LY_EVALID; |
| 4233 | } |
| 4234 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4235 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4236 | return LY_SUCCESS; |
| 4237 | } |
| 4238 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4239 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4240 | 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] | 4241 | { |
| 4242 | struct lys_module *mod; |
| 4243 | const char *prefix = NULL, *name; |
| 4244 | size_t prefix_len = 0; |
| 4245 | struct lysc_node_case *cs; |
| 4246 | struct lysc_node *node; |
| 4247 | |
| 4248 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4249 | name = strchr(dflt, ':'); |
| 4250 | if (name) { |
| 4251 | prefix = dflt; |
| 4252 | prefix_len = name - prefix; |
| 4253 | ++name; |
| 4254 | } else { |
| 4255 | name = dflt; |
| 4256 | } |
| 4257 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 4258 | if (prefix) { |
| 4259 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 4260 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4261 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 4262 | "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] | 4263 | return LY_EVALID; |
| 4264 | } |
| 4265 | } else { |
| 4266 | mod = ctx->mod; |
| 4267 | } |
| 4268 | /* get the default case */ |
| 4269 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 4270 | if (!cs) { |
| 4271 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4272 | "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] | 4273 | return LY_EVALID; |
| 4274 | } |
| 4275 | |
| 4276 | /* check that there is no mandatory node */ |
| 4277 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4278 | if (node->parent != (struct lysc_node*)cs) { |
| 4279 | break; |
| 4280 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4281 | if (node->flags & LYS_MAND_TRUE) { |
| 4282 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4283 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4284 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4285 | return LY_EVALID; |
| 4286 | } |
| 4287 | } |
| 4288 | |
| 4289 | /* set the default case in choice */ |
| 4290 | ch->dflt = cs; |
| 4291 | cs->flags |= LYS_SET_DFLT; |
| 4292 | |
| 4293 | return LY_SUCCESS; |
| 4294 | } |
| 4295 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4296 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4297 | * @brief Compile parsed choice node information. |
| 4298 | * @param[in] ctx Compile context |
| 4299 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4300 | * @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] | 4301 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4302 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4303 | */ |
| 4304 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4305 | 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] | 4306 | { |
| 4307 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4308 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4309 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4310 | LY_ERR ret = LY_SUCCESS; |
| 4311 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4312 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4313 | if (child_p->nodetype == LYS_CASE) { |
| 4314 | 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] | 4315 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4316 | } |
| 4317 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4318 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4319 | } |
| 4320 | } |
| 4321 | |
| 4322 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4323 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4324 | 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] | 4325 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4326 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4327 | return ret; |
| 4328 | } |
| 4329 | |
| 4330 | /** |
| 4331 | * @brief Compile parsed anydata or anyxml node information. |
| 4332 | * @param[in] ctx Compile context |
| 4333 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4334 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4335 | * is enriched with the any-specific information. |
| 4336 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4337 | */ |
| 4338 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4339 | 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] | 4340 | { |
| 4341 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4342 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4343 | unsigned int u; |
| 4344 | LY_ERR ret = LY_SUCCESS; |
| 4345 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4346 | 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] | 4347 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4348 | /* do not check "must" semantics in a grouping */ |
| 4349 | ly_set_add(&ctx->unres, any, 0); |
| 4350 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4351 | |
| 4352 | if (any->flags & LYS_CONFIG_W) { |
| 4353 | 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] | 4354 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4355 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4356 | done: |
| 4357 | return ret; |
| 4358 | } |
| 4359 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4360 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4361 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4362 | * |
| 4363 | * @param[in] ctx Compile context |
| 4364 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4365 | * the choice itself is expected instead of a specific case node. |
| 4366 | * @param[in] node Schema node to connect into the list. |
| 4367 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 4368 | */ |
| 4369 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4370 | 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] | 4371 | { |
| 4372 | struct lysc_node **children; |
| 4373 | |
| 4374 | if (node->nodetype == LYS_CASE) { |
| 4375 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4376 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4377 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4378 | } |
| 4379 | if (children) { |
| 4380 | if (!(*children)) { |
| 4381 | /* first child */ |
| 4382 | *children = node; |
| 4383 | } else if (*children != node) { |
| 4384 | /* by the condition in previous branch we cover the choice/case children |
| 4385 | * - the children list is shared by the choice and the the first case, in addition |
| 4386 | * the first child of each case must be referenced from the case node. So the node is |
| 4387 | * actually always already inserted in case it is the first children - so here such |
| 4388 | * a situation actually corresponds to the first branch */ |
| 4389 | /* insert at the end of the parent's children list */ |
| 4390 | (*children)->prev->next = node; |
| 4391 | node->prev = (*children)->prev; |
| 4392 | (*children)->prev = node; |
| 4393 | |
| 4394 | /* check the name uniqueness */ |
| 4395 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4396 | lysc_node_notifs(parent), node->name, node)) { |
| 4397 | return LY_EEXIST; |
| 4398 | } |
| 4399 | } |
| 4400 | } |
| 4401 | return LY_SUCCESS; |
| 4402 | } |
| 4403 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4404 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4405 | * @brief Prepare the case structure in choice node for the new data node. |
| 4406 | * |
| 4407 | * 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 |
| 4408 | * created in the choice when the first child was processed. |
| 4409 | * |
| 4410 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4411 | * @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, |
| 4412 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4413 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4414 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4415 | * @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, |
| 4416 | * 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] | 4417 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4418 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4419 | 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] | 4420 | { |
| 4421 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4422 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4423 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4424 | unsigned int u; |
| 4425 | LY_ERR ret; |
| 4426 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4427 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4428 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4429 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4430 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4431 | return NULL; \ |
| 4432 | } \ |
| 4433 | } |
| 4434 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4435 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4436 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4437 | |
| 4438 | /* we have to add an implicit case node into the parent choice */ |
| 4439 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4440 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4441 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4442 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4443 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4444 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4445 | /* the case is already present since the child is not its first children */ |
| 4446 | return (struct lysc_node_case*)ch->cases->prev; |
| 4447 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4448 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4449 | |
| 4450 | /* explicit parent case is not present (this is its first child) */ |
| 4451 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4452 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4453 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4454 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4455 | cs->sp = node_p; |
| 4456 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4457 | /* 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] | 4458 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4459 | |
| 4460 | if (node_p->when) { |
| 4461 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4462 | 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] | 4463 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4464 | |
| 4465 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4466 | /* do not check "when" semantics in a grouping */ |
| 4467 | ly_set_add(&ctx->unres, cs, 0); |
| 4468 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4469 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4470 | 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] | 4471 | } else { |
| 4472 | LOGINT(ctx->ctx); |
| 4473 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4474 | } |
| 4475 | cs->module = ctx->mod; |
| 4476 | cs->prev = (struct lysc_node*)cs; |
| 4477 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4478 | 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] | 4479 | cs->child = child; |
| 4480 | |
| 4481 | return cs; |
| 4482 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4483 | if (cs) { |
| 4484 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4485 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4486 | return NULL; |
| 4487 | |
| 4488 | #undef UNIQUE_CHECK |
| 4489 | } |
| 4490 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4491 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4492 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4493 | * |
| 4494 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4495 | * @param[in] node Target node where the config is supposed to be changed. |
| 4496 | * @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] | 4497 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4498 | * 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] | 4499 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4500 | * @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] | 4501 | * @return LY_ERR value. |
| 4502 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4503 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4504 | 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] | 4505 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4506 | { |
| 4507 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4508 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4509 | |
| 4510 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4511 | /* nothing to do */ |
| 4512 | return LY_SUCCESS; |
| 4513 | } |
| 4514 | |
| 4515 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4516 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4517 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4518 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4519 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4520 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4521 | return LY_EVALID; |
| 4522 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4523 | node->flags |= LYS_SET_CONFIG; |
| 4524 | } else { |
| 4525 | if (node->flags & LYS_SET_CONFIG) { |
| 4526 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4527 | /* setting config flags, but have node with explicit config true */ |
| 4528 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4529 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4530 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4531 | return LY_EVALID; |
| 4532 | } |
| 4533 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4534 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4535 | return LY_SUCCESS; |
| 4536 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4537 | } |
| 4538 | node->flags &= ~LYS_CONFIG_MASK; |
| 4539 | node->flags |= config; |
| 4540 | |
| 4541 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4542 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4543 | 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] | 4544 | } |
| 4545 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4546 | return LY_SUCCESS; |
| 4547 | } |
| 4548 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4549 | /** |
| 4550 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4551 | * |
| 4552 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4553 | * the flag to such parents from a mandatory children. |
| 4554 | * |
| 4555 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4556 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4557 | * (mandatory children was removed). |
| 4558 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4559 | void |
| 4560 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4561 | { |
| 4562 | struct lysc_node *iter; |
| 4563 | |
| 4564 | if (add) { /* set flag */ |
| 4565 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4566 | parent = parent->parent) { |
| 4567 | parent->flags |= LYS_MAND_TRUE; |
| 4568 | } |
| 4569 | } else { /* unset flag */ |
| 4570 | 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] | 4571 | 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] | 4572 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4573 | /* there is another mandatory node */ |
| 4574 | return; |
| 4575 | } |
| 4576 | } |
| 4577 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4578 | parent->flags &= ~LYS_MAND_TRUE; |
| 4579 | } |
| 4580 | } |
| 4581 | } |
| 4582 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4583 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4584 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4585 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4586 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4587 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4588 | */ |
| 4589 | static void |
| 4590 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4591 | { |
| 4592 | unsigned int v; |
| 4593 | size_t len; |
| 4594 | |
| 4595 | len = strlen(aug_p->nodeid); |
| 4596 | LY_ARRAY_FOR(result, v) { |
| 4597 | if (strlen(result[v]->nodeid) <= len) { |
| 4598 | continue; |
| 4599 | } |
| 4600 | if (v < LY_ARRAY_SIZE(result)) { |
| 4601 | /* move the rest of array */ |
| 4602 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4603 | break; |
| 4604 | } |
| 4605 | } |
| 4606 | result[v] = aug_p; |
| 4607 | LY_ARRAY_INCREMENT(result); |
| 4608 | } |
| 4609 | |
| 4610 | /** |
| 4611 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4612 | * |
| 4613 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4614 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4615 | * |
| 4616 | * @param[in] ctx Compile context. |
| 4617 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4618 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4619 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4620 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4621 | * @return LY_ERR value. |
| 4622 | */ |
| 4623 | LY_ERR |
| 4624 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4625 | { |
| 4626 | struct lysp_augment **result = NULL; |
| 4627 | unsigned int u, v; |
| 4628 | size_t count = 0; |
| 4629 | |
| 4630 | assert(augments); |
| 4631 | |
| 4632 | /* get count of the augments in module and all its submodules */ |
| 4633 | if (aug_p) { |
| 4634 | count += LY_ARRAY_SIZE(aug_p); |
| 4635 | } |
| 4636 | LY_ARRAY_FOR(inc_p, u) { |
| 4637 | if (inc_p[u].submodule->augments) { |
| 4638 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | if (!count) { |
| 4643 | *augments = NULL; |
| 4644 | return LY_SUCCESS; |
| 4645 | } |
| 4646 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4647 | |
| 4648 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4649 | * together, so there can be even /z/y betwwen them. */ |
| 4650 | LY_ARRAY_FOR(aug_p, u) { |
| 4651 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4652 | } |
| 4653 | LY_ARRAY_FOR(inc_p, u) { |
| 4654 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4655 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4656 | } |
| 4657 | } |
| 4658 | |
| 4659 | *augments = result; |
| 4660 | return LY_SUCCESS; |
| 4661 | } |
| 4662 | |
| 4663 | /** |
| 4664 | * @brief Compile the parsed augment connecting it into its target. |
| 4665 | * |
| 4666 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4667 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4668 | * are already implemented and compiled. |
| 4669 | * |
| 4670 | * @param[in] ctx Compile context. |
| 4671 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4672 | * @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 |
| 4673 | * children in case of the augmenting uses data. |
| 4674 | * @return LY_SUCCESS on success. |
| 4675 | * @return LY_EVALID on failure. |
| 4676 | */ |
| 4677 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4678 | 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] | 4679 | { |
| 4680 | LY_ERR ret = LY_SUCCESS; |
| 4681 | struct lysp_node *node_p, *case_node_p; |
| 4682 | struct lysc_node *target; /* target target of the augment */ |
| 4683 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4684 | struct lysc_when **when, *when_shared; |
| 4685 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4686 | uint16_t flags = 0; |
| 4687 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4688 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4689 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4690 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4691 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4692 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4693 | 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] | 4694 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4695 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4696 | if (ret != LY_SUCCESS) { |
| 4697 | if (ret == LY_EDENIED) { |
| 4698 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4699 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4700 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4701 | } |
| 4702 | return LY_EVALID; |
| 4703 | } |
| 4704 | |
| 4705 | /* check for mandatory nodes |
| 4706 | * - new cases augmenting some choice can have mandatory nodes |
| 4707 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4708 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4709 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4710 | allow_mandatory = 1; |
| 4711 | } |
| 4712 | |
| 4713 | when_shared = NULL; |
| 4714 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4715 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4716 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4717 | && !((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] | 4718 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4719 | && !(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] | 4720 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4721 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4722 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4723 | return LY_EVALID; |
| 4724 | } |
| 4725 | |
| 4726 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4727 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4728 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4729 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4730 | } else { |
| 4731 | 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] | 4732 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4733 | } |
| 4734 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4735 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4736 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4737 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4738 | * 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] | 4739 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4740 | /* 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] | 4741 | 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] | 4742 | } else if (target->nodetype == LYS_CHOICE) { |
| 4743 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4744 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4745 | } else { |
| 4746 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4747 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4748 | } |
| 4749 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4750 | 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] | 4751 | node->flags &= ~LYS_MAND_TRUE; |
| 4752 | lys_compile_mandatory_parents(target, 0); |
| 4753 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4754 | "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] | 4755 | return LY_EVALID; |
| 4756 | } |
| 4757 | |
| 4758 | /* pass augment's when to all the children */ |
| 4759 | if (aug_p->when) { |
| 4760 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4761 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4762 | 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] | 4763 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4764 | |
| 4765 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4766 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4767 | ly_set_add(&ctx->unres, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4768 | } |
| 4769 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4770 | when_shared = *when; |
| 4771 | } else { |
| 4772 | ++when_shared->refcount; |
| 4773 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4774 | |
| 4775 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4776 | /* in this case check "when" again for all children because of dummy node check */ |
| 4777 | ly_set_add(&ctx->unres, node, 0); |
| 4778 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4779 | } |
| 4780 | } |
| 4781 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4782 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4783 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4784 | switch (target->nodetype) { |
| 4785 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4786 | 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] | 4787 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4788 | 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] | 4789 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4790 | break; |
| 4791 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4792 | 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] | 4793 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4794 | 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] | 4795 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4796 | break; |
| 4797 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4798 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4799 | if (aug_p->actions) { |
| 4800 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4801 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4802 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4803 | return LY_EVALID; |
| 4804 | } |
| 4805 | if (aug_p->notifs) { |
| 4806 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4807 | "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".", |
| 4808 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4809 | return LY_EVALID; |
| 4810 | } |
| 4811 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4812 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4813 | lysc_update_path(ctx, NULL, NULL); |
| 4814 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4815 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4816 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4817 | return ret; |
| 4818 | } |
| 4819 | |
| 4820 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4821 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4822 | * |
| 4823 | * @param[in] ctx Compile context. |
| 4824 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4825 | * @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] | 4826 | * @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] | 4827 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4828 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4829 | * 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] | 4830 | * @return LY_ERR value. |
| 4831 | */ |
| 4832 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4833 | 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] | 4834 | { |
| 4835 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4836 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4837 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4838 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4839 | return LY_EVALID; |
| 4840 | } |
| 4841 | |
| 4842 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4843 | /* check if node has default value */ |
| 4844 | if (node->nodetype & LYS_LEAF) { |
| 4845 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4846 | if (refine_flag) { |
| 4847 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4848 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4849 | return LY_EVALID; |
| 4850 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4851 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4852 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4853 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4854 | |
| 4855 | /* update the list of incomplete default values if needed */ |
| 4856 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4857 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4858 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4859 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4860 | free(leaf->dflt); |
| 4861 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4862 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4863 | } |
| 4864 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->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 - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4868 | return LY_EVALID; |
| 4869 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4870 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4871 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4872 | 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] | 4873 | return LY_EVALID; |
| 4874 | } |
| 4875 | |
| 4876 | node->flags &= ~LYS_MAND_FALSE; |
| 4877 | node->flags |= LYS_MAND_TRUE; |
| 4878 | lys_compile_mandatory_parents(node->parent, 1); |
| 4879 | } else { |
| 4880 | /* make mandatory false */ |
| 4881 | node->flags &= ~LYS_MAND_TRUE; |
| 4882 | node->flags |= LYS_MAND_FALSE; |
| 4883 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4884 | 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] | 4885 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4886 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4887 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4888 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4889 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4890 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4891 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4892 | } |
| 4893 | } |
| 4894 | return LY_SUCCESS; |
| 4895 | } |
| 4896 | |
| 4897 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4898 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4899 | * If present, also apply uses's modificators. |
| 4900 | * |
| 4901 | * @param[in] ctx Compile context |
| 4902 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4903 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4904 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4905 | * the compile context. |
| 4906 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4907 | */ |
| 4908 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4909 | 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] | 4910 | { |
| 4911 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4912 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4913 | /* 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] | 4914 | struct lysc_node_container context_node_fake = |
| 4915 | {.nodetype = LYS_CONTAINER, |
| 4916 | .module = ctx->mod, |
| 4917 | .flags = parent ? parent->flags : 0, |
| 4918 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4919 | .prev = (struct lysc_node*)&context_node_fake, |
| 4920 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4921 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4922 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4923 | int found; |
| 4924 | const char *id, *name, *prefix; |
| 4925 | size_t prefix_len, name_len; |
| 4926 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4927 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4928 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4929 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4930 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4931 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4932 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4933 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4934 | unsigned int actions_index, notifs_index; |
| 4935 | struct lysc_notif **notifs = NULL; |
| 4936 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4937 | |
| 4938 | /* search for the grouping definition */ |
| 4939 | found = 0; |
| 4940 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4941 | 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] | 4942 | if (prefix) { |
| 4943 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4944 | if (!mod) { |
| 4945 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4946 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4947 | return LY_EVALID; |
| 4948 | } |
| 4949 | } else { |
| 4950 | mod = ctx->mod_def; |
| 4951 | } |
| 4952 | if (mod == ctx->mod_def) { |
| 4953 | 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] | 4954 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4955 | LY_ARRAY_FOR(grp, u) { |
| 4956 | if (!strcmp(grp[u].name, name)) { |
| 4957 | grp = &grp[u]; |
| 4958 | found = 1; |
| 4959 | break; |
| 4960 | } |
| 4961 | } |
| 4962 | } |
| 4963 | } |
| 4964 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4965 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4966 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4967 | if (grp) { |
| 4968 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4969 | if (!strcmp(grp[u].name, name)) { |
| 4970 | grp = &grp[u]; |
| 4971 | found = 1; |
| 4972 | } |
| 4973 | } |
| 4974 | } |
| 4975 | if (!found && mod->parsed->includes) { |
| 4976 | /* ... and all the submodules */ |
| 4977 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4978 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4979 | if (grp) { |
| 4980 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4981 | if (!strcmp(grp[v].name, name)) { |
| 4982 | grp = &grp[v]; |
| 4983 | found = 1; |
| 4984 | } |
| 4985 | } |
| 4986 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4987 | } |
| 4988 | } |
| 4989 | } |
| 4990 | if (!found) { |
| 4991 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4992 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4993 | return LY_EVALID; |
| 4994 | } |
| 4995 | |
| 4996 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4997 | grp_stack_count = ctx->groupings.count; |
| 4998 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4999 | if (grp_stack_count == ctx->groupings.count) { |
| 5000 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 5001 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5002 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 5003 | return LY_EVALID; |
| 5004 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5005 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5006 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 5007 | grp->flags |= LYS_USED_GRP; |
| 5008 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5009 | |
| 5010 | /* switch context's mod_def */ |
| 5011 | mod_old = ctx->mod_def; |
| 5012 | ctx->mod_def = mod; |
| 5013 | |
| 5014 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5015 | 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] | 5016 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5017 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5018 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5019 | /* 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] | 5020 | 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] | 5021 | |
| 5022 | /* some preparation for applying refines */ |
| 5023 | if (grp->data == node_p) { |
| 5024 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 5025 | if (parent) { |
| 5026 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 5027 | } else if (ctx->mod->compiled->data) { |
| 5028 | child = ctx->mod->compiled->data; |
| 5029 | } else { |
| 5030 | child = NULL; |
| 5031 | } |
| 5032 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5033 | } |
| 5034 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5035 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5036 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5037 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5038 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5039 | /* 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] | 5040 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5041 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5042 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5043 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 5044 | |
| 5045 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5046 | /* do not check "when" semantics in a grouping */ |
| 5047 | ly_set_add(&ctx->unres, child, 0); |
| 5048 | } |
| 5049 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5050 | when_shared = *when; |
| 5051 | } else { |
| 5052 | ++when_shared->refcount; |
| 5053 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5054 | |
| 5055 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5056 | /* in this case check "when" again for all children because of dummy node check */ |
| 5057 | ly_set_add(&ctx->unres, child, 0); |
| 5058 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5059 | } |
| 5060 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5061 | } |
| 5062 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5063 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5064 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5065 | /* 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] | 5066 | 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] | 5067 | } |
| 5068 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5069 | /* compile actions */ |
| 5070 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 5071 | if (actions) { |
| 5072 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5073 | 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] | 5074 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 5075 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 5076 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 5077 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 5078 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | /* compile notifications */ |
| 5083 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 5084 | if (notifs) { |
| 5085 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5086 | 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] | 5087 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 5088 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 5089 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 5090 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 5091 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5092 | } |
| 5093 | } |
| 5094 | |
| 5095 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5096 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5097 | 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] | 5098 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5099 | 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] | 5100 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5101 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5102 | /* reload previous context's mod_def */ |
| 5103 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5104 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5105 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5106 | /* apply refine */ |
| 5107 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5108 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 5109 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5110 | 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] | 5111 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5112 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5113 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5114 | |
| 5115 | /* default value */ |
| 5116 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5117 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5118 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5119 | "Invalid refine of default - %s cannot hold %d default values.", |
| 5120 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5121 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5122 | } |
| 5123 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 5124 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5125 | "Invalid refine of default - %s cannot hold default value(s).", |
| 5126 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5127 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5128 | } |
| 5129 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5130 | struct ly_err_item *err = NULL; |
| 5131 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 5132 | if (leaf->dflt) { |
| 5133 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5134 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5135 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5136 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5137 | } else { |
| 5138 | /* prepare a new one */ |
| 5139 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 5140 | leaf->dflt->realtype = leaf->type; |
| 5141 | } |
| 5142 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5143 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5144 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 5145 | 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] | 5146 | 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] | 5147 | leaf->dflt->realtype->refcount++; |
| 5148 | if (err) { |
| 5149 | ly_err_print(err); |
| 5150 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5151 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 5152 | ly_err_free(err); |
| 5153 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5154 | if (rc == LY_EINCOMPLETE) { |
| 5155 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5156 | 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] | 5157 | |
| 5158 | /* but in general result is so far ok */ |
| 5159 | rc = LY_SUCCESS; |
| 5160 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5161 | LY_CHECK_GOTO(rc, cleanup); |
| 5162 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5163 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5164 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 5165 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5166 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5167 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5168 | "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] | 5169 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5170 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5171 | |
| 5172 | /* remove previous set of default values */ |
| 5173 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5174 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5175 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 5176 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 5177 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5178 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5179 | LY_ARRAY_FREE(llist->dflts); |
| 5180 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5181 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5182 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5183 | |
| 5184 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5185 | 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] | 5186 | 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] | 5187 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5188 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5189 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5190 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5191 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5192 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 5193 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5194 | 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] | 5195 | 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] | 5196 | 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] | 5197 | llist->dflts[u]->realtype->refcount++; |
| 5198 | if (err) { |
| 5199 | ly_err_print(err); |
| 5200 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5201 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 5202 | ly_err_free(err); |
| 5203 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5204 | if (rc == LY_EINCOMPLETE) { |
| 5205 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5206 | 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] | 5207 | |
| 5208 | /* but in general result is so far ok */ |
| 5209 | rc = LY_SUCCESS; |
| 5210 | } |
| 5211 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5212 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5213 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5214 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5215 | if (((struct lysc_node_choice*)node)->dflt) { |
| 5216 | /* unset LYS_SET_DFLT from the current default case */ |
| 5217 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 5218 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5219 | 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] | 5220 | } |
| 5221 | } |
| 5222 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5223 | /* description */ |
| 5224 | if (rfn->dsc) { |
| 5225 | FREE_STRING(ctx->ctx, node->dsc); |
| 5226 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 5227 | } |
| 5228 | |
| 5229 | /* reference */ |
| 5230 | if (rfn->ref) { |
| 5231 | FREE_STRING(ctx->ctx, node->ref); |
| 5232 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 5233 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5234 | |
| 5235 | /* config */ |
| 5236 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5237 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5238 | 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] | 5239 | } else { |
| 5240 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 5241 | flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path); |
| 5242 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | /* mandatory */ |
| 5246 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5247 | 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] | 5248 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5249 | |
| 5250 | /* presence */ |
| 5251 | if (rfn->presence) { |
| 5252 | if (node->nodetype != LYS_CONTAINER) { |
| 5253 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5254 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5255 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5256 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5257 | } |
| 5258 | node->flags |= LYS_PRESENCE; |
| 5259 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5260 | |
| 5261 | /* must */ |
| 5262 | if (rfn->musts) { |
| 5263 | switch (node->nodetype) { |
| 5264 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5265 | 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] | 5266 | break; |
| 5267 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5268 | 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] | 5269 | break; |
| 5270 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5271 | 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] | 5272 | break; |
| 5273 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5274 | 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] | 5275 | break; |
| 5276 | case LYS_ANYXML: |
| 5277 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5278 | 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] | 5279 | break; |
| 5280 | default: |
| 5281 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5282 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5283 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5284 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5285 | } |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 5286 | ly_set_add(&ctx->unres, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5287 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5288 | |
| 5289 | /* min/max-elements */ |
| 5290 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5291 | switch (node->nodetype) { |
| 5292 | case LYS_LEAFLIST: |
| 5293 | if (rfn->flags & LYS_SET_MAX) { |
| 5294 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5295 | } |
| 5296 | if (rfn->flags & LYS_SET_MIN) { |
| 5297 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5298 | if (rfn->min) { |
| 5299 | node->flags |= LYS_MAND_TRUE; |
| 5300 | lys_compile_mandatory_parents(node->parent, 1); |
| 5301 | } else { |
| 5302 | node->flags &= ~LYS_MAND_TRUE; |
| 5303 | lys_compile_mandatory_parents(node->parent, 0); |
| 5304 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5305 | } |
| 5306 | break; |
| 5307 | case LYS_LIST: |
| 5308 | if (rfn->flags & LYS_SET_MAX) { |
| 5309 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5310 | } |
| 5311 | if (rfn->flags & LYS_SET_MIN) { |
| 5312 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5313 | if (rfn->min) { |
| 5314 | node->flags |= LYS_MAND_TRUE; |
| 5315 | lys_compile_mandatory_parents(node->parent, 1); |
| 5316 | } else { |
| 5317 | node->flags &= ~LYS_MAND_TRUE; |
| 5318 | lys_compile_mandatory_parents(node->parent, 0); |
| 5319 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5320 | } |
| 5321 | break; |
| 5322 | default: |
| 5323 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5324 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5325 | (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] | 5326 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5327 | } |
| 5328 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5329 | |
| 5330 | /* if-feature */ |
| 5331 | if (rfn->iffeatures) { |
| 5332 | /* 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] | 5333 | 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] | 5334 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5335 | |
| 5336 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5337 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5338 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5339 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 5340 | for (u = 0; u < refined.count; ++u) { |
| 5341 | node = (struct lysc_node*)refined.objs[u]; |
| 5342 | rfn = &uses_p->refines[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5343 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5344 | |
| 5345 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5346 | if ((node->flags & LYS_MAND_TRUE) && |
| 5347 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5348 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5349 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5350 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5351 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5352 | } |
| 5353 | |
| 5354 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5355 | if (node->nodetype == LYS_LIST) { |
| 5356 | min = ((struct lysc_node_list*)node)->min; |
| 5357 | max = ((struct lysc_node_list*)node)->max; |
| 5358 | } else { |
| 5359 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5360 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5361 | } |
| 5362 | if (min > max) { |
| 5363 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5364 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5365 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5366 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5367 | } |
| 5368 | } |
| 5369 | } |
| 5370 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5371 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5372 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5373 | |
| 5374 | cleanup: |
| 5375 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5376 | if (context_node_fake.child) { |
| 5377 | context_node_fake.child->prev = child; |
| 5378 | } |
| 5379 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5380 | child->parent = parent; |
| 5381 | } |
| 5382 | |
| 5383 | if (uses_p->augments || uses_p->refines) { |
| 5384 | /* 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] | 5385 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5386 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5387 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5388 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5389 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5390 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5391 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5392 | } |
| 5393 | } |
| 5394 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5395 | /* reload previous context's mod_def */ |
| 5396 | ctx->mod_def = mod_old; |
| 5397 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5398 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5399 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5400 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5401 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5402 | |
| 5403 | return ret; |
| 5404 | } |
| 5405 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5406 | static int |
| 5407 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5408 | { |
| 5409 | struct lysp_node *iter; |
| 5410 | int len = 0; |
| 5411 | |
| 5412 | *path = NULL; |
| 5413 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5414 | char *s = *path; |
| 5415 | char *id; |
| 5416 | |
| 5417 | switch (iter->nodetype) { |
| 5418 | case LYS_USES: |
| 5419 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5420 | break; |
| 5421 | case LYS_GROUPING: |
| 5422 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5423 | break; |
| 5424 | case LYS_AUGMENT: |
| 5425 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5426 | break; |
| 5427 | default: |
| 5428 | id = strdup(iter->name); |
| 5429 | break; |
| 5430 | } |
| 5431 | |
| 5432 | if (!iter->parent) { |
| 5433 | /* print prefix */ |
| 5434 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5435 | } else { |
| 5436 | /* prefix is the same as in parent */ |
| 5437 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5438 | } |
| 5439 | free(s); |
| 5440 | free(id); |
| 5441 | } |
| 5442 | |
| 5443 | if (len < 0) { |
| 5444 | free(*path); |
| 5445 | *path = NULL; |
| 5446 | } else if (len == 0) { |
| 5447 | *path = strdup("/"); |
| 5448 | len = 1; |
| 5449 | } |
| 5450 | return len; |
| 5451 | } |
| 5452 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5453 | /** |
| 5454 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5455 | * |
| 5456 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5457 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5458 | */ |
| 5459 | static LY_ERR |
| 5460 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5461 | { |
| 5462 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5463 | char *path; |
| 5464 | int len; |
| 5465 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5466 | struct lysp_node_uses fake_uses = { |
| 5467 | .parent = node_p, |
| 5468 | .nodetype = LYS_USES, |
| 5469 | .flags = 0, .next = NULL, |
| 5470 | .name = grp->name, |
| 5471 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5472 | .refines = NULL, .augments = NULL |
| 5473 | }; |
| 5474 | struct lysc_node_container fake_container = { |
| 5475 | .nodetype = LYS_CONTAINER, |
| 5476 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5477 | .module = ctx->mod, |
| 5478 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5479 | .prev = (struct lysc_node*)&fake_container, |
| 5480 | .name = "fake", |
| 5481 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5482 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5483 | }; |
| 5484 | |
| 5485 | if (grp->parent) { |
| 5486 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5487 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5488 | |
| 5489 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5490 | if (len < 0) { |
| 5491 | LOGMEM(ctx->ctx); |
| 5492 | return LY_EMEM; |
| 5493 | } |
| 5494 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5495 | ctx->path_len = (uint16_t)len; |
| 5496 | free(path); |
| 5497 | |
| 5498 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5499 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5500 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5501 | lysc_update_path(ctx, NULL, NULL); |
| 5502 | lysc_update_path(ctx, NULL, NULL); |
| 5503 | |
| 5504 | ctx->path_len = 1; |
| 5505 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5506 | |
| 5507 | /* cleanup */ |
| 5508 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5509 | |
| 5510 | return ret; |
| 5511 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5512 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5513 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5514 | * @brief Compile parsed schema node information. |
| 5515 | * @param[in] ctx Compile context |
| 5516 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5517 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5518 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5519 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5520 | * @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). |
| 5521 | * 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] | 5522 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5523 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5524 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5525 | 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] | 5526 | { |
| 5527 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5528 | struct lysc_node *node; |
| 5529 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5530 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5531 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5532 | 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] | 5533 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5534 | if (node_p->nodetype != LYS_USES) { |
| 5535 | lysc_update_path(ctx, parent, node_p->name); |
| 5536 | } else { |
| 5537 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5538 | lysc_update_path(ctx, NULL, node_p->name); |
| 5539 | } |
| 5540 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5541 | switch (node_p->nodetype) { |
| 5542 | case LYS_CONTAINER: |
| 5543 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5544 | node_compile_spec = lys_compile_node_container; |
| 5545 | break; |
| 5546 | case LYS_LEAF: |
| 5547 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5548 | node_compile_spec = lys_compile_node_leaf; |
| 5549 | break; |
| 5550 | case LYS_LIST: |
| 5551 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5552 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5553 | break; |
| 5554 | case LYS_LEAFLIST: |
| 5555 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5556 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5557 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5558 | case LYS_CHOICE: |
| 5559 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5560 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5561 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5562 | case LYS_ANYXML: |
| 5563 | case LYS_ANYDATA: |
| 5564 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5565 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5566 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5567 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5568 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5569 | lysc_update_path(ctx, NULL, NULL); |
| 5570 | lysc_update_path(ctx, NULL, NULL); |
| 5571 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5572 | default: |
| 5573 | LOGINT(ctx->ctx); |
| 5574 | return LY_EINT; |
| 5575 | } |
| 5576 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5577 | node->nodetype = node_p->nodetype; |
| 5578 | node->module = ctx->mod; |
| 5579 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5580 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5581 | |
| 5582 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5583 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5584 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5585 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5586 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5587 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5588 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5589 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5590 | node->flags |= LYS_CONFIG_R; |
| 5591 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5592 | /* config not explicitely set, inherit it from parent */ |
| 5593 | if (parent) { |
| 5594 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5595 | } else { |
| 5596 | /* default is config true */ |
| 5597 | node->flags |= LYS_CONFIG_W; |
| 5598 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5599 | } else { |
| 5600 | /* config set explicitely */ |
| 5601 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5602 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5603 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5604 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5605 | "Configuration node cannot be child of any state data node."); |
| 5606 | goto error; |
| 5607 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5608 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5609 | /* *list ordering */ |
| 5610 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5611 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5612 | 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] | 5613 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5614 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5615 | node->flags &= ~LYS_ORDBY_MASK; |
| 5616 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5617 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5618 | /* default ordering is system */ |
| 5619 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5620 | } |
| 5621 | } |
| 5622 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5623 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5624 | * 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] | 5625 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5626 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5627 | * 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] | 5628 | 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] | 5629 | } |
| 5630 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5631 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5632 | node->sp = node_p; |
| 5633 | } |
| 5634 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5635 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5636 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5637 | if (node_p->when) { |
| 5638 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5639 | 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] | 5640 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5641 | |
| 5642 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5643 | /* do not check "when" semantics in a grouping */ |
| 5644 | ly_set_add(&ctx->unres, node, 0); |
| 5645 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5646 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5647 | 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] | 5648 | |
| 5649 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5650 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5651 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5652 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5653 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5654 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5655 | if (node->flags & LYS_MAND_TRUE) { |
| 5656 | lys_compile_mandatory_parents(parent, 1); |
| 5657 | } |
| 5658 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5659 | lysc_update_path(ctx, NULL, NULL); |
| 5660 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5661 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5662 | if (parent) { |
| 5663 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5664 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5665 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5666 | } else { |
| 5667 | lysc_update_path(ctx, parent, node->name); |
| 5668 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5669 | 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] | 5670 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5671 | if (uses_status) { |
| 5672 | |
| 5673 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5674 | /* 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] | 5675 | * it directly gets the same status flags as the choice; |
| 5676 | * 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] | 5677 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5678 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5679 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5680 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5681 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5682 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5683 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5684 | 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] | 5685 | |
| 5686 | if (parent->nodetype == LYS_CHOICE) { |
| 5687 | lysc_update_path(ctx, NULL, NULL); |
| 5688 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5689 | } else { |
| 5690 | /* top-level element */ |
| 5691 | if (!ctx->mod->compiled->data) { |
| 5692 | ctx->mod->compiled->data = node; |
| 5693 | } else { |
| 5694 | /* insert at the end of the module's top-level nodes list */ |
| 5695 | ctx->mod->compiled->data->prev->next = node; |
| 5696 | node->prev = ctx->mod->compiled->data->prev; |
| 5697 | ctx->mod->compiled->data->prev = node; |
| 5698 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5699 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5700 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5701 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5702 | return LY_EVALID; |
| 5703 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5704 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5705 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5706 | |
| 5707 | return LY_SUCCESS; |
| 5708 | |
| 5709 | error: |
| 5710 | lysc_node_free(ctx->ctx, node); |
| 5711 | return ret; |
| 5712 | } |
| 5713 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5714 | static void |
| 5715 | lysc_disconnect(struct lysc_node *node) |
| 5716 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5717 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5718 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5719 | int remove_cs = 0; |
| 5720 | |
| 5721 | parent = node->parent; |
| 5722 | |
| 5723 | /* parent's first child */ |
| 5724 | if (!parent) { |
| 5725 | return; |
| 5726 | } |
| 5727 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5728 | cs = (struct lysc_node_case*)node; |
| 5729 | } else if (parent->nodetype == LYS_CASE) { |
| 5730 | /* disconnecting some node in a case */ |
| 5731 | cs = (struct lysc_node_case*)parent; |
| 5732 | parent = cs->parent; |
| 5733 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5734 | if (child == node) { |
| 5735 | if (cs->child == child) { |
| 5736 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5737 | /* case with a single child -> remove also the case */ |
| 5738 | child->parent = NULL; |
| 5739 | remove_cs = 1; |
| 5740 | } else { |
| 5741 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5742 | } |
| 5743 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5744 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5745 | } |
| 5746 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5747 | if (!remove_cs) { |
| 5748 | cs = NULL; |
| 5749 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5750 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5751 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5752 | } |
| 5753 | |
| 5754 | if (cs) { |
| 5755 | if (remove_cs) { |
| 5756 | /* cs has only one child which is being also removed */ |
| 5757 | lysc_disconnect((struct lysc_node*)cs); |
| 5758 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5759 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5760 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5761 | /* default case removed */ |
| 5762 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5763 | } |
| 5764 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5765 | /* first case removed */ |
| 5766 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5767 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5768 | if (cs->child) { |
| 5769 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5770 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5771 | prev = cs->child->prev; |
| 5772 | } /* else all the children are under a single case */ |
| 5773 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5774 | if (child->parent != (struct lysc_node*)cs) { |
| 5775 | break; |
| 5776 | } |
| 5777 | lysc_node_free(node->module->ctx, child); |
| 5778 | } |
| 5779 | if (prev) { |
| 5780 | if (prev->next) { |
| 5781 | prev->next = child; |
| 5782 | } |
| 5783 | if (child) { |
| 5784 | child->prev = prev; |
| 5785 | } else { |
| 5786 | /* link from the first child under the cases */ |
| 5787 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5788 | } |
| 5789 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5790 | } |
| 5791 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5792 | } |
| 5793 | |
| 5794 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5795 | if (node->prev->next) { |
| 5796 | node->prev->next = node->next; |
| 5797 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5798 | if (node->next) { |
| 5799 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5800 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5801 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5802 | if (child) { |
| 5803 | child->prev = node->prev; |
| 5804 | } |
| 5805 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5806 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5807 | } |
| 5808 | } |
| 5809 | |
| 5810 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5811 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5812 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5813 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5814 | struct ly_set devs_p = {0}; |
| 5815 | struct ly_set targets = {0}; |
| 5816 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5817 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5818 | struct lysc_action *rpcs; |
| 5819 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5820 | struct lysp_deviation *dev; |
| 5821 | struct lysp_deviate *d, **dp_new; |
| 5822 | struct lysp_deviate_add *d_add; |
| 5823 | struct lysp_deviate_del *d_del; |
| 5824 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5825 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5826 | struct lysc_deviation { |
| 5827 | const char *nodeid; |
| 5828 | struct lysc_node *target; /* target node of the deviation */ |
| 5829 | 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] | 5830 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5831 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5832 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5833 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5834 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5835 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5836 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5837 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5838 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5839 | |
| 5840 | /* get all deviations from the module and all its submodules ... */ |
| 5841 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5842 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5843 | } |
| 5844 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5845 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5846 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5847 | } |
| 5848 | } |
| 5849 | if (!devs_p.count) { |
| 5850 | /* nothing to do */ |
| 5851 | return LY_SUCCESS; |
| 5852 | } |
| 5853 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5854 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5855 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5856 | /* ... and group them by the target node */ |
| 5857 | devs = calloc(devs_p.count, sizeof *devs); |
| 5858 | for (u = 0; u < devs_p.count; ++u) { |
| 5859 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5860 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5861 | |
| 5862 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5863 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5864 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5865 | if (target->nodetype == LYS_ACTION) { |
| 5866 | /* move the target pointer to input/output to make them different from the action and |
| 5867 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5868 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5869 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5870 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5871 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5872 | flags |= LYSC_OPT_INTERNAL; |
| 5873 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5874 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5875 | flags |= LYSC_OPT_INTERNAL; |
| 5876 | } |
| 5877 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5878 | /* insert into the set of targets with duplicity detection */ |
| 5879 | i = ly_set_add(&targets, target, 0); |
| 5880 | if (!devs[i]) { |
| 5881 | /* new record */ |
| 5882 | devs[i] = calloc(1, sizeof **devs); |
| 5883 | devs[i]->target = target; |
| 5884 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5885 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5886 | } |
| 5887 | /* add deviates into the deviation's list of deviates */ |
| 5888 | for (d = dev->deviates; d; d = d->next) { |
| 5889 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5890 | *dp_new = d; |
| 5891 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5892 | devs[i]->not_supported = 1; |
| 5893 | } |
| 5894 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5895 | |
| 5896 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5897 | } |
| 5898 | |
| 5899 | /* MACROS for deviates checking */ |
| 5900 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5901 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5902 | 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] | 5903 | goto cleanup; \ |
| 5904 | } |
| 5905 | |
| 5906 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5907 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5908 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \ |
| 5909 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5910 | goto cleanup; \ |
| 5911 | } |
| 5912 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5913 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5914 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5915 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5916 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5917 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5918 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5919 | goto cleanup; \ |
| 5920 | } |
| 5921 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5922 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5923 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5924 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5925 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5926 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5927 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5928 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5929 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5930 | goto cleanup; \ |
| 5931 | } |
| 5932 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5933 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5934 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5935 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5936 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5937 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5938 | goto cleanup; \ |
| 5939 | } |
| 5940 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5941 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5942 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5943 | 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] | 5944 | goto cleanup; \ |
| 5945 | } |
| 5946 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5947 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5948 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5949 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5950 | "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] | 5951 | goto cleanup; \ |
| 5952 | } |
| 5953 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5954 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5955 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5956 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5957 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5958 | 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] | 5959 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5960 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5961 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5962 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5963 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5964 | goto cleanup; \ |
| 5965 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5966 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5967 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5968 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5969 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5970 | (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] | 5971 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5972 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5973 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5974 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5975 | } |
| 5976 | |
| 5977 | /* apply deviations */ |
| 5978 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5979 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5980 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5981 | struct ly_err_item *err = NULL; |
| 5982 | |
| 5983 | dflt = NULL; |
| 5984 | changed_type = 0; |
| 5985 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5986 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5987 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5988 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5989 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5990 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5991 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5992 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5993 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5994 | } |
| 5995 | } |
| 5996 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5997 | /* not-supported */ |
| 5998 | if (devs[u]->not_supported) { |
| 5999 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 6000 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 6001 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 6002 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6003 | |
| 6004 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6005 | if (devs[u]->target->parent) { \ |
| 6006 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 6007 | } else { \ |
| 6008 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 6009 | } \ |
| 6010 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6011 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 6012 | } \ |
| 6013 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 6014 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6015 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6016 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6017 | } |
| 6018 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6019 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 6020 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6021 | /* remove RPC's/action's input */ |
| 6022 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 6023 | 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] | 6024 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 6025 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6026 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6027 | /* remove RPC's/action's output */ |
| 6028 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 6029 | 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] | 6030 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 6031 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6032 | } else { |
| 6033 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6034 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6035 | } |
| 6036 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6037 | /* remove Notification */ |
| 6038 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6039 | } else { |
| 6040 | /* remove the target node */ |
| 6041 | lysc_disconnect(devs[u]->target); |
| 6042 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 6043 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6044 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6045 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6046 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6047 | continue; |
| 6048 | } |
| 6049 | |
| 6050 | /* list of deviates (not-supported is not present in the list) */ |
| 6051 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 6052 | d = devs[u]->deviates[v]; |
| 6053 | |
| 6054 | switch (d->mod) { |
| 6055 | case LYS_DEV_ADD: |
| 6056 | d_add = (struct lysp_deviate_add*)d; |
| 6057 | /* [units-stmt] */ |
| 6058 | if (d_add->units) { |
| 6059 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 6060 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 6061 | |
| 6062 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6063 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6064 | } |
| 6065 | |
| 6066 | /* *must-stmt */ |
| 6067 | if (d_add->musts) { |
| 6068 | switch (devs[u]->target->nodetype) { |
| 6069 | case LYS_CONTAINER: |
| 6070 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6071 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 6072 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6073 | break; |
| 6074 | case LYS_LEAF: |
| 6075 | case LYS_LEAFLIST: |
| 6076 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6077 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 6078 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6079 | break; |
| 6080 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6081 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 6082 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6083 | break; |
| 6084 | case LYS_ACTION: |
| 6085 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6086 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 6087 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6088 | break; |
| 6089 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 6090 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 6091 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6092 | break; |
| 6093 | } |
| 6094 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6095 | default: |
| 6096 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6097 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6098 | goto cleanup; |
| 6099 | } |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6100 | ly_set_add(&ctx->unres, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6101 | } |
| 6102 | |
| 6103 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6104 | if (d_add->uniques) { |
| 6105 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 6106 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 6107 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6108 | |
| 6109 | /* *default-stmt */ |
| 6110 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6111 | switch (devs[u]->target->nodetype) { |
| 6112 | case LYS_LEAF: |
| 6113 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6114 | 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] | 6115 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6116 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6117 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6118 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6119 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6120 | } else { |
| 6121 | /* prepare new default value storage */ |
| 6122 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6123 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6124 | dflt = d_add->dflts[0]; |
| 6125 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 6126 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6127 | /* mark the new default values as leaf's own */ |
| 6128 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6129 | break; |
| 6130 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6131 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6132 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6133 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6134 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6135 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6136 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6137 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6138 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6139 | LY_ARRAY_FREE(llist->dflts); |
| 6140 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6141 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6142 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6143 | } |
| 6144 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6145 | 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] | 6146 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 6147 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6148 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6149 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 6150 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6151 | LY_ARRAY_INCREMENT(llist->dflts); |
| 6152 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 6153 | llist->dflts[x]->realtype = llist->type; |
| 6154 | 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] | 6155 | 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] | 6156 | (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] | 6157 | llist->dflts[x]->realtype->refcount++; |
| 6158 | if (err) { |
| 6159 | ly_err_print(err); |
| 6160 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6161 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 6162 | d_add->dflts[x - y], err->msg); |
| 6163 | ly_err_free(err); |
| 6164 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6165 | if (rc == LY_EINCOMPLETE) { |
| 6166 | /* postpone default compilation when the tree is complete */ |
| 6167 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6168 | |
| 6169 | /* but in general result is so far ok */ |
| 6170 | rc = LY_SUCCESS; |
| 6171 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6172 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6173 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6174 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6175 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 6176 | break; |
| 6177 | case LYS_CHOICE: |
| 6178 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 6179 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 6180 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 6181 | * 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] | 6182 | 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] | 6183 | goto cleanup; |
| 6184 | } |
| 6185 | break; |
| 6186 | default: |
| 6187 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6188 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6189 | goto cleanup; |
| 6190 | } |
| 6191 | } |
| 6192 | |
| 6193 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6194 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 6195 | 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] | 6196 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6197 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 6198 | goto cleanup; |
| 6199 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6200 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6201 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 6202 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6203 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6204 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 6205 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6206 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 6207 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6208 | goto cleanup; |
| 6209 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6210 | 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] | 6211 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6212 | |
| 6213 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6214 | if (d_add->flags & LYS_MAND_MASK) { |
| 6215 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 6216 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6217 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 6218 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6219 | goto cleanup; |
| 6220 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6221 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6222 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6223 | |
| 6224 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6225 | if (d_add->flags & LYS_SET_MIN) { |
| 6226 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6227 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6228 | /* change value */ |
| 6229 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 6230 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6231 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6232 | /* change value */ |
| 6233 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 6234 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6235 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6236 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 6237 | goto cleanup; |
| 6238 | } |
| 6239 | if (d_add->min) { |
| 6240 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6241 | } |
| 6242 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6243 | |
| 6244 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6245 | if (d_add->flags & LYS_SET_MAX) { |
| 6246 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6247 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6248 | /* change value */ |
| 6249 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6250 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6251 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6252 | /* change value */ |
| 6253 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6254 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6255 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6256 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6257 | goto cleanup; |
| 6258 | } |
| 6259 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6260 | |
| 6261 | break; |
| 6262 | case LYS_DEV_DELETE: |
| 6263 | d_del = (struct lysp_deviate_del*)d; |
| 6264 | |
| 6265 | /* [units-stmt] */ |
| 6266 | if (d_del->units) { |
| 6267 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6268 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6269 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6270 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6271 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6272 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6273 | goto cleanup; |
| 6274 | } |
| 6275 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6276 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6277 | } |
| 6278 | |
| 6279 | /* *must-stmt */ |
| 6280 | if (d_del->musts) { |
| 6281 | switch (devs[u]->target->nodetype) { |
| 6282 | case LYS_CONTAINER: |
| 6283 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6284 | 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] | 6285 | break; |
| 6286 | case LYS_LEAF: |
| 6287 | case LYS_LEAFLIST: |
| 6288 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6289 | 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] | 6290 | break; |
| 6291 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6292 | 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] | 6293 | break; |
| 6294 | case LYS_ACTION: |
| 6295 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6296 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6297 | break; |
| 6298 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6299 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6300 | break; |
| 6301 | } |
| 6302 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6303 | default: |
| 6304 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6305 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6306 | goto cleanup; |
| 6307 | } |
| 6308 | } |
| 6309 | |
| 6310 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6311 | if (d_del->uniques) { |
| 6312 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6313 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6314 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6315 | LY_ARRAY_FOR(list->uniques, z) { |
| 6316 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6317 | nodeid = strpbrk(name, " \t\n"); |
| 6318 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 6319 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6320 | break; |
| 6321 | } |
| 6322 | while (isspace(*nodeid)) { |
| 6323 | ++nodeid; |
| 6324 | } |
| 6325 | } else { |
| 6326 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6327 | break; |
| 6328 | } |
| 6329 | } |
| 6330 | } |
| 6331 | if (!name) { |
| 6332 | /* complete match - remove the unique */ |
| 6333 | LY_ARRAY_DECREMENT(list->uniques); |
| 6334 | LY_ARRAY_FREE(list->uniques[z]); |
| 6335 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6336 | --z; |
| 6337 | break; |
| 6338 | } |
| 6339 | } |
| 6340 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6341 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6342 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6343 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6344 | goto cleanup; |
| 6345 | } |
| 6346 | } |
| 6347 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6348 | LY_ARRAY_FREE(list->uniques); |
| 6349 | list->uniques = NULL; |
| 6350 | } |
| 6351 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6352 | |
| 6353 | /* *default-stmt */ |
| 6354 | if (d_del->dflts) { |
| 6355 | switch (devs[u]->target->nodetype) { |
| 6356 | case LYS_LEAF: |
| 6357 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6358 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6359 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6360 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6361 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6362 | 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] | 6363 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6364 | if (i) { |
| 6365 | free((char*)dflt); |
| 6366 | } |
| 6367 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6368 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6369 | d_del->dflts[0], dflt); |
| 6370 | goto cleanup; |
| 6371 | } |
| 6372 | if (i) { |
| 6373 | free((char*)dflt); |
| 6374 | } |
| 6375 | dflt = NULL; |
| 6376 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6377 | /* update the list of incomplete default values if needed */ |
| 6378 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6379 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6380 | /* remove the default specification */ |
| 6381 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6382 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6383 | free(leaf->dflt); |
| 6384 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6385 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6386 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6387 | break; |
| 6388 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6389 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6390 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6391 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6392 | 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] | 6393 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6394 | if (i) { |
| 6395 | free((char*)dflt); |
| 6396 | } |
| 6397 | dflt = NULL; |
| 6398 | break; |
| 6399 | } |
| 6400 | if (i) { |
| 6401 | free((char*)dflt); |
| 6402 | } |
| 6403 | dflt = NULL; |
| 6404 | } |
| 6405 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6406 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6407 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6408 | goto cleanup; |
| 6409 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6410 | |
| 6411 | /* update the list of incomplete default values if needed */ |
| 6412 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6413 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6414 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6415 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6416 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6417 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6418 | free(llist->dflts[y]); |
| 6419 | 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] | 6420 | 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] | 6421 | } |
| 6422 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6423 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6424 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6425 | LY_ARRAY_FREE(llist->dflts); |
| 6426 | llist->dflts = NULL; |
| 6427 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6428 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6429 | break; |
| 6430 | case LYS_CHOICE: |
| 6431 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6432 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6433 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6434 | 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] | 6435 | if (prefix) { |
| 6436 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6437 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6438 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6439 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6440 | "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] | 6441 | goto cleanup; |
| 6442 | } |
| 6443 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6444 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6445 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6446 | "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] | 6447 | goto cleanup; |
| 6448 | } |
| 6449 | } |
| 6450 | /* else { |
| 6451 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6452 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6453 | * the module of the default case, just matching its name */ |
| 6454 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6455 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6456 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6457 | 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] | 6458 | goto cleanup; |
| 6459 | } |
| 6460 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6461 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6462 | break; |
| 6463 | default: |
| 6464 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6465 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6466 | goto cleanup; |
| 6467 | } |
| 6468 | } |
| 6469 | |
| 6470 | break; |
| 6471 | case LYS_DEV_REPLACE: |
| 6472 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6473 | |
| 6474 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6475 | if (d_rpl->type) { |
| 6476 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6477 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6478 | 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] | 6479 | |
| 6480 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6481 | /* the target has default from the previous type - remove it */ |
| 6482 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6483 | /* update the list of incomplete default values if needed */ |
| 6484 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6485 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6486 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6487 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6488 | free(leaf->dflt); |
| 6489 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6490 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6491 | } else { /* LYS_LEAFLIST */ |
| 6492 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6493 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6494 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6495 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6496 | free(llist->dflts[x]); |
| 6497 | } |
| 6498 | LY_ARRAY_FREE(llist->dflts); |
| 6499 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6500 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6501 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6502 | } |
| 6503 | } |
| 6504 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6505 | /* there is no default value, do not set changed_type after type compilation |
| 6506 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6507 | changed_type = -1; |
| 6508 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6509 | 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] | 6510 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6511 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6512 | |
| 6513 | /* [units-stmt] */ |
| 6514 | if (d_rpl->units) { |
| 6515 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6516 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6517 | units, "replacing", "units", d_rpl->units); |
| 6518 | |
| 6519 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6520 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6521 | } |
| 6522 | |
| 6523 | /* [default-stmt] */ |
| 6524 | if (d_rpl->dflt) { |
| 6525 | switch (devs[u]->target->nodetype) { |
| 6526 | case LYS_LEAF: |
| 6527 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6528 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6529 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6530 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6531 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6532 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6533 | dflt = d_rpl->dflt; |
| 6534 | /* 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] | 6535 | break; |
| 6536 | case LYS_CHOICE: |
| 6537 | 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] | 6538 | 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] | 6539 | goto cleanup; |
| 6540 | } |
| 6541 | break; |
| 6542 | default: |
| 6543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6544 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6545 | goto cleanup; |
| 6546 | } |
| 6547 | } |
| 6548 | |
| 6549 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6550 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 6551 | 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] | 6552 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6553 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6554 | goto cleanup; |
| 6555 | } |
| 6556 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6557 | 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] | 6558 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6559 | goto cleanup; |
| 6560 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6561 | 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] | 6562 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6563 | |
| 6564 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6565 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6566 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6567 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6568 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6569 | goto cleanup; |
| 6570 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6571 | 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] | 6572 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6573 | |
| 6574 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6575 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6576 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6577 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6578 | /* change value */ |
| 6579 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6580 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6581 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6582 | /* change value */ |
| 6583 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6584 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6585 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6586 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6587 | goto cleanup; |
| 6588 | } |
| 6589 | if (d_rpl->min) { |
| 6590 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6591 | } |
| 6592 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6593 | |
| 6594 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6595 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6596 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6597 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6598 | /* change value */ |
| 6599 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6600 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6601 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6602 | /* change value */ |
| 6603 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6604 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6605 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6606 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6607 | goto cleanup; |
| 6608 | } |
| 6609 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6610 | |
| 6611 | break; |
| 6612 | default: |
| 6613 | LOGINT(ctx->ctx); |
| 6614 | goto cleanup; |
| 6615 | } |
| 6616 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6617 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6618 | /* final check when all deviations of a single target node are applied */ |
| 6619 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6620 | /* check min-max compatibility */ |
| 6621 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6622 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6623 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6624 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6625 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6626 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6627 | } else { |
| 6628 | min = max = 0; |
| 6629 | } |
| 6630 | if (min > max) { |
| 6631 | 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] | 6632 | "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] | 6633 | goto cleanup; |
| 6634 | } |
| 6635 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6636 | if (dflt) { |
| 6637 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6638 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6639 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6640 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6641 | 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] | 6642 | 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] | 6643 | leaf->dflt->realtype->refcount++; |
| 6644 | if (err) { |
| 6645 | ly_err_print(err); |
| 6646 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6647 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6648 | ly_err_free(err); |
| 6649 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6650 | if (rc == LY_EINCOMPLETE) { |
| 6651 | /* postpone default compilation when the tree is complete */ |
| 6652 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6653 | |
| 6654 | /* but in general result is so far ok */ |
| 6655 | rc = LY_SUCCESS; |
| 6656 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6657 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6658 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6659 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6660 | int dynamic; |
| 6661 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6662 | 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] | 6663 | |
| 6664 | /* update the list of incomplete default values if needed */ |
| 6665 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6666 | |
| 6667 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6668 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6669 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6670 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6671 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6672 | 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] | 6673 | 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] | 6674 | leaf->dflt->realtype->refcount++; |
| 6675 | if (err) { |
| 6676 | ly_err_print(err); |
| 6677 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6678 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6679 | ly_err_free(err); |
| 6680 | } |
| 6681 | if (dynamic) { |
| 6682 | free((void*)dflt); |
| 6683 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6684 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6685 | if (rc == LY_EINCOMPLETE) { |
| 6686 | /* postpone default compilation when the tree is complete */ |
| 6687 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6688 | |
| 6689 | /* but in general result is so far ok */ |
| 6690 | rc = LY_SUCCESS; |
| 6691 | } |
| 6692 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6693 | } else { /* LYS_LEAFLIST */ |
| 6694 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6695 | 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] | 6696 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6697 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6698 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6699 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6700 | 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] | 6701 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6702 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6703 | llist->dflts[x]->realtype->refcount++; |
| 6704 | if (err) { |
| 6705 | ly_err_print(err); |
| 6706 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6707 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6708 | dflt, err->msg); |
| 6709 | ly_err_free(err); |
| 6710 | } |
| 6711 | if (dynamic) { |
| 6712 | free((void*)dflt); |
| 6713 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6714 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6715 | if (rc == LY_EINCOMPLETE) { |
| 6716 | /* postpone default compilation when the tree is complete */ |
| 6717 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6718 | |
| 6719 | /* but in general result is so far ok */ |
| 6720 | rc = LY_SUCCESS; |
| 6721 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6722 | LY_CHECK_GOTO(rc, cleanup); |
| 6723 | } |
| 6724 | } |
| 6725 | } |
| 6726 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6727 | /* check mandatory - default compatibility */ |
| 6728 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6729 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6730 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6731 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6732 | "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] | 6733 | goto cleanup; |
| 6734 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6735 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6736 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6737 | 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] | 6738 | goto cleanup; |
| 6739 | } |
| 6740 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6741 | /* mandatory node under a default case */ |
| 6742 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6743 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6744 | 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] | 6745 | goto cleanup; |
| 6746 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6747 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6748 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6749 | } |
| 6750 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6751 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6752 | ret = LY_SUCCESS; |
| 6753 | |
| 6754 | cleanup: |
| 6755 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6756 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6757 | free(devs[u]); |
| 6758 | } |
| 6759 | free(devs); |
| 6760 | ly_set_erase(&targets, NULL); |
| 6761 | ly_set_erase(&devs_p, NULL); |
| 6762 | |
| 6763 | return ret; |
| 6764 | } |
| 6765 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6766 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6767 | * @brief Compile the given YANG submodule into the main module. |
| 6768 | * @param[in] ctx Compile context |
| 6769 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6770 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6771 | */ |
| 6772 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6773 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6774 | { |
| 6775 | unsigned int u; |
| 6776 | LY_ERR ret = LY_SUCCESS; |
| 6777 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6778 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6779 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6780 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6781 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6782 | if (!mainmod->mod->off_features) { |
| 6783 | /* features are compiled directly into the compiled module structure, |
| 6784 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6785 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6786 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6787 | LY_CHECK_GOTO(ret, error); |
| 6788 | } |
| 6789 | if (!mainmod->mod->off_extensions) { |
| 6790 | /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */ |
| 6791 | ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6792 | LY_CHECK_GOTO(ret, error); |
| 6793 | } |
| 6794 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6795 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6796 | 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] | 6797 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6798 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6799 | /* data nodes */ |
| 6800 | LY_LIST_FOR(submod->data, node_p) { |
| 6801 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6802 | LY_CHECK_GOTO(ret, error); |
| 6803 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6804 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6805 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6806 | 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] | 6807 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6808 | error: |
| 6809 | return ret; |
| 6810 | } |
| 6811 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6812 | static void * |
| 6813 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6814 | { |
| 6815 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6816 | if (substmts[u].stmt == stmt) { |
| 6817 | return substmts[u].storage; |
| 6818 | } |
| 6819 | } |
| 6820 | return NULL; |
| 6821 | } |
| 6822 | |
| 6823 | LY_ERR |
| 6824 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6825 | { |
| 6826 | LY_ERR ret = LY_EVALID, r; |
| 6827 | unsigned int u; |
| 6828 | struct lysp_stmt *stmt; |
| 6829 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6830 | |
| 6831 | /* check for invalid substatements */ |
| 6832 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6833 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6834 | continue; |
| 6835 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6836 | for (u = 0; substmts[u].stmt; ++u) { |
| 6837 | if (substmts[u].stmt == stmt->kw) { |
| 6838 | break; |
| 6839 | } |
| 6840 | } |
| 6841 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6842 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6843 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6844 | goto cleanup; |
| 6845 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6846 | } |
| 6847 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6848 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6849 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6850 | /* keep order of the processing the same as the order in the defined substmts, |
| 6851 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6852 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6853 | int stmt_present = 0; |
| 6854 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6855 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6856 | if (substmts[u].stmt != stmt->kw) { |
| 6857 | continue; |
| 6858 | } |
| 6859 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6860 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6861 | if (substmts[u].storage) { |
| 6862 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6863 | case LY_STMT_STATUS: |
| 6864 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6865 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6866 | break; |
| 6867 | case LY_STMT_UNITS: { |
| 6868 | const char **units; |
| 6869 | |
| 6870 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6871 | /* single item */ |
| 6872 | if (*((const char **)substmts[u].storage)) { |
| 6873 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6874 | goto cleanup; |
| 6875 | } |
| 6876 | units = (const char **)substmts[u].storage; |
| 6877 | } else { |
| 6878 | /* sized array */ |
| 6879 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6880 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6881 | } |
| 6882 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6883 | break; |
| 6884 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6885 | case LY_STMT_TYPE: { |
| 6886 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6887 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6888 | |
| 6889 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6890 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6891 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6892 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6893 | goto cleanup; |
| 6894 | } |
| 6895 | compiled = substmts[u].storage; |
| 6896 | } else { |
| 6897 | /* sized array */ |
| 6898 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6899 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6900 | compiled = (void*)type; |
| 6901 | } |
| 6902 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6903 | 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] | 6904 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6905 | 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] | 6906 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6907 | lysp_type_free(ctx->ctx, parsed); |
| 6908 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6909 | break; |
| 6910 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6911 | case LY_STMT_IF_FEATURE: { |
| 6912 | struct lysc_iffeature *iff = NULL; |
| 6913 | |
| 6914 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6915 | /* single item */ |
| 6916 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6917 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6918 | goto cleanup; |
| 6919 | } |
| 6920 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6921 | } else { |
| 6922 | /* sized array */ |
| 6923 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6924 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6925 | } |
| 6926 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6927 | break; |
| 6928 | } |
| 6929 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6930 | * 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] | 6931 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6932 | 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.", |
| 6933 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6934 | goto cleanup; |
| 6935 | } |
| 6936 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6937 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6938 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6939 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6940 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6941 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6942 | goto cleanup; |
| 6943 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6944 | } |
| 6945 | |
| 6946 | ret = LY_SUCCESS; |
| 6947 | |
| 6948 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6949 | return ret; |
| 6950 | } |
| 6951 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6952 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6953 | * @brief Check when for cyclic dependencies. |
| 6954 | * @param[in] set Set with all the referenced nodes. |
| 6955 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6956 | * @return LY_ERR value |
| 6957 | */ |
| 6958 | static LY_ERR |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6959 | 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] | 6960 | { |
| 6961 | struct lyxp_set tmp_set; |
| 6962 | struct lyxp_set_scnode *xp_scnode; |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6963 | uint32_t i, j, k; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6964 | int idx; |
| 6965 | struct lysc_when *when; |
| 6966 | LY_ERR ret = LY_SUCCESS; |
| 6967 | |
| 6968 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6969 | |
| 6970 | /* prepare in_ctx of the set */ |
| 6971 | for (i = 0; i < set->used; ++i) { |
| 6972 | xp_scnode = &set->val.scnodes[i]; |
| 6973 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6974 | if (xp_scnode->in_ctx != -1) { |
| 6975 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6976 | xp_scnode->in_ctx = 1; |
| 6977 | } |
| 6978 | } |
| 6979 | |
| 6980 | for (i = 0; i < set->used; ++i) { |
| 6981 | xp_scnode = &set->val.scnodes[i]; |
| 6982 | if (xp_scnode->in_ctx != 1) { |
| 6983 | /* already checked */ |
| 6984 | continue; |
| 6985 | } |
| 6986 | |
Michal Vasko | 2ff7efe | 2019-12-10 14:50:59 +0100 | [diff] [blame] | 6987 | if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_ACTION | LYS_NOTIF)) |
| 6988 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6989 | /* no when to check */ |
| 6990 | xp_scnode->in_ctx = 0; |
| 6991 | continue; |
| 6992 | } |
| 6993 | |
| 6994 | node = xp_scnode->scnode; |
| 6995 | do { |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6996 | LY_ARRAY_FOR(node->when, j) { |
| 6997 | when = node->when[j]; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6998 | ret = lyxp_atomize(when->cond, LYD_UNKNOWN, when->module, when->context, |
| 6999 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 7000 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7001 | 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] | 7002 | goto cleanup; |
| 7003 | } |
| 7004 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7005 | for (k = 0; k < tmp_set.used; ++k) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7006 | /* skip roots'n'stuff */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7007 | if (tmp_set.val.scnodes[k].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7008 | /* try to find this node in our set */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7009 | 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] | 7010 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7011 | 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] | 7012 | ret = LY_EVALID; |
| 7013 | goto cleanup; |
| 7014 | } |
| 7015 | |
| 7016 | /* needs to be checked, if in both sets, will be ignored */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7017 | tmp_set.val.scnodes[k].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7018 | } else { |
| 7019 | /* no when, nothing to check */ |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7020 | tmp_set.val.scnodes[k].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7021 | } |
| 7022 | } |
| 7023 | |
| 7024 | /* merge this set into the global when set */ |
| 7025 | lyxp_set_scnode_merge(set, &tmp_set); |
| 7026 | } |
| 7027 | |
| 7028 | /* check when of non-data parents as well */ |
| 7029 | node = node->parent; |
| 7030 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 7031 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 7032 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 7033 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | cleanup: |
| 7037 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 7038 | return ret; |
| 7039 | } |
| 7040 | |
| 7041 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7042 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 7043 | * @param[in] ctx Compile context. |
| 7044 | * @param[in] node Node to check. |
| 7045 | * @return LY_ERR value |
| 7046 | */ |
| 7047 | static LY_ERR |
| 7048 | lys_compile_check_xpath(struct lysc_ctx *ctx, const struct lysc_node *node) |
| 7049 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7050 | struct lyxp_set tmp_set; |
| 7051 | uint32_t i, j; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7052 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7053 | struct lysc_when **when = NULL; |
| 7054 | struct lysc_must *musts = NULL; |
| 7055 | LY_ERR ret = LY_SUCCESS; |
| 7056 | |
| 7057 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7058 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7059 | |
| 7060 | switch (node->nodetype) { |
| 7061 | case LYS_CONTAINER: |
| 7062 | when = ((struct lysc_node_container *)node)->when; |
| 7063 | musts = ((struct lysc_node_container *)node)->musts; |
| 7064 | break; |
| 7065 | case LYS_CHOICE: |
| 7066 | when = ((struct lysc_node_choice *)node)->when; |
| 7067 | break; |
| 7068 | case LYS_LEAF: |
| 7069 | when = ((struct lysc_node_leaf *)node)->when; |
| 7070 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 7071 | break; |
| 7072 | case LYS_LEAFLIST: |
| 7073 | when = ((struct lysc_node_leaflist *)node)->when; |
| 7074 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 7075 | break; |
| 7076 | case LYS_LIST: |
| 7077 | when = ((struct lysc_node_list *)node)->when; |
| 7078 | musts = ((struct lysc_node_list *)node)->musts; |
| 7079 | break; |
| 7080 | case LYS_ANYXML: |
| 7081 | case LYS_ANYDATA: |
| 7082 | when = ((struct lysc_node_anydata *)node)->when; |
| 7083 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 7084 | break; |
| 7085 | case LYS_CASE: |
| 7086 | when = ((struct lysc_node_case *)node)->when; |
| 7087 | break; |
| 7088 | case LYS_NOTIF: |
| 7089 | musts = ((struct lysc_notif *)node)->musts; |
| 7090 | break; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7091 | case LYS_ACTION: |
| 7092 | /* first process input musts */ |
| 7093 | musts = ((struct lysc_action *)node)->input.musts; |
| 7094 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7095 | default: |
| 7096 | /* nothing to check */ |
| 7097 | break; |
| 7098 | } |
| 7099 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7100 | /* check "when" */ |
| 7101 | LY_ARRAY_FOR(when, i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7102 | ret = lyxp_atomize(when[i]->cond, LYD_UNKNOWN, when[i]->module, when[i]->context, |
| 7103 | when[i]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts); |
| 7104 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7105 | 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] | 7106 | goto cleanup; |
| 7107 | } |
| 7108 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7109 | ctx->path[0] = '\0'; |
| 7110 | 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] | 7111 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7112 | /* skip roots'n'stuff */ |
| 7113 | 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] | 7114 | struct lysc_node *schema = tmp_set.val.scnodes[j].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7115 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7116 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 7117 | ret = lysc_check_status(ctx, when[i]->flags, when[i]->module, node->name, schema->flags, schema->module, |
| 7118 | schema->name); |
| 7119 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7120 | |
| 7121 | /* check dummy node accessing */ |
| 7122 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7123 | 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] | 7124 | ret = LY_EVALID; |
| 7125 | goto cleanup; |
| 7126 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7127 | } |
| 7128 | } |
| 7129 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7130 | /* check cyclic dependencies */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7131 | ret = lys_compile_check_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7132 | LY_CHECK_GOTO(ret, cleanup); |
| 7133 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7134 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 7135 | } |
| 7136 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7137 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7138 | /* check "must" */ |
| 7139 | LY_ARRAY_FOR(musts, i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7140 | ret = lyxp_atomize(musts[i].cond, LYD_UNKNOWN, musts[i].module, node, LYXP_NODE_ELEM, &tmp_set, opts); |
| 7141 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 7142 | 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] | 7143 | goto cleanup; |
| 7144 | } |
| 7145 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7146 | ctx->path[0] = '\0'; |
| 7147 | 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] | 7148 | for (j = 0; j < tmp_set.used; ++j) { |
| 7149 | /* skip roots'n'stuff */ |
| 7150 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
| 7151 | /* 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] | 7152 | 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] | 7153 | tmp_set.val.scnodes[j].scnode->module, tmp_set.val.scnodes[j].scnode->name); |
| 7154 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7155 | } |
| 7156 | } |
| 7157 | |
| 7158 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 7159 | } |
| 7160 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7161 | if ((node->nodetype == LYS_ACTION) && !input_done) { |
| 7162 | /* now check output musts */ |
| 7163 | input_done = 1; |
| 7164 | musts = ((struct lysc_action *)node)->output.musts; |
| 7165 | opts = LYXP_SCNODE_OUTPUT; |
| 7166 | goto check_musts; |
| 7167 | } |
| 7168 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7169 | cleanup: |
| 7170 | lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY); |
| 7171 | return ret; |
| 7172 | } |
| 7173 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7174 | LY_ERR |
| 7175 | lys_compile(struct lys_module *mod, int options) |
| 7176 | { |
| 7177 | struct lysc_ctx ctx = {0}; |
| 7178 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7179 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7180 | struct lysp_module *sp; |
| 7181 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7182 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7183 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7184 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7185 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7186 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7187 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7188 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7189 | |
| 7190 | if (!mod->implemented) { |
| 7191 | /* just imported modules are not compiled */ |
| 7192 | return LY_SUCCESS; |
| 7193 | } |
| 7194 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7195 | sp = mod->parsed; |
| 7196 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7197 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7198 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7199 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7200 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7201 | ctx.path_len = 1; |
| 7202 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7203 | |
| 7204 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7205 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 7206 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7207 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7208 | 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] | 7209 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7210 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7211 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7212 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7213 | |
| 7214 | /* features */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7215 | if (mod->off_features) { |
| 7216 | /* there is already precompiled array of features */ |
| 7217 | mod_c->features = mod->off_features; |
| 7218 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7219 | } else { |
| 7220 | /* features are compiled directly into the compiled module structure, |
| 7221 | * 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] | 7222 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7223 | LY_CHECK_GOTO(ret, error); |
| 7224 | } |
| 7225 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7226 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7227 | * are present. */ |
| 7228 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7229 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7230 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7231 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7232 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7233 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7234 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7235 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7236 | 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] | 7237 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7238 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7239 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7240 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7241 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7242 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7243 | /* extensions */ |
| 7244 | /* 2-steps: a) prepare compiled structures and ... */ |
| 7245 | if (mod->off_extensions) { |
| 7246 | /* there is already precompiled array of extension definitions */ |
| 7247 | mod_c->extensions = mod->off_extensions; |
| 7248 | mod->off_extensions = NULL; |
| 7249 | } else { |
| 7250 | /* extension definitions are compiled directly into the compiled module structure */ |
| 7251 | ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions); |
Radek Krejci | bfb2e14 | 2019-09-09 14:47:44 +0200 | [diff] [blame] | 7252 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7253 | } |
| 7254 | /* ... b) connect the extension definitions with the appropriate extension plugins */ |
| 7255 | lys_compile_extension_plugins(mod_c->extensions); |
| 7256 | |
| 7257 | /* identities */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7258 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7259 | 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] | 7260 | if (sp->identities) { |
| 7261 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 7262 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7263 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7264 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7265 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7266 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7267 | 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] | 7268 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7269 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7270 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7271 | 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] | 7272 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7273 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7274 | 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] | 7275 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7276 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7277 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7278 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7279 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7280 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7281 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7282 | /* extension instances TODO cover extension instances from submodules */ |
| 7283 | 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] | 7284 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7285 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7286 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7287 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7288 | * 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] | 7289 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 7290 | 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] | 7291 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 7292 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7293 | /* validate the path */ |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 7294 | 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] | 7295 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7296 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7297 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7298 | /* validate the path */ |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7299 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 7300 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v], NULL); |
| 7301 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7302 | } |
| 7303 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7304 | } |
| 7305 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7306 | |
| 7307 | /* check xpath */ |
| 7308 | 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] | 7309 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7310 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 7311 | 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] | 7312 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 7313 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7314 | /* store pointer to the real type */ |
| 7315 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7316 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7317 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7318 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 7319 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7320 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7321 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7322 | /* store pointer to the real type */ |
| 7323 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7324 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7325 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7326 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7327 | } |
| 7328 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 7329 | } |
| 7330 | } |
| 7331 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7332 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7333 | /* finish incomplete default values compilation */ |
| 7334 | for (u = 0; u < ctx.dflts.count; ++u) { |
| 7335 | struct ly_err_item *err = NULL; |
| 7336 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[u]; |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 7337 | 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] | 7338 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7339 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7340 | if (err) { |
| 7341 | ly_err_print(err); |
| 7342 | ctx.path[0] = '\0'; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7343 | 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] | 7344 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 7345 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7346 | ly_err_free(err); |
| 7347 | } |
| 7348 | LY_CHECK_GOTO(ret, error); |
| 7349 | } |
| 7350 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7351 | /* validate non-instantiated groupings from the parsed schema, |
| 7352 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7353 | ctx.options |= LYSC_OPT_GROUPING; |
| 7354 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7355 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7356 | 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] | 7357 | } |
| 7358 | } |
| 7359 | LY_LIST_FOR(sp->data, node_p) { |
| 7360 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7361 | LY_ARRAY_FOR(grps, u) { |
| 7362 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7363 | 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] | 7364 | } |
| 7365 | } |
| 7366 | } |
| 7367 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7368 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7369 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7370 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7371 | } |
| 7372 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7373 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7374 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7375 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7376 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7377 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7378 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7379 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7380 | lysp_module_free(mod->parsed); |
| 7381 | ((struct lys_module*)mod)->parsed = NULL; |
| 7382 | } |
| 7383 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7384 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7385 | /* remove flag of the modules implemented by dependency */ |
| 7386 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 7387 | m = ctx.ctx->list.objs[u]; |
| 7388 | if (m->implemented == 2) { |
| 7389 | m->implemented = 1; |
| 7390 | } |
| 7391 | } |
| 7392 | } |
| 7393 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7394 | ((struct lys_module*)mod)->compiled = mod_c; |
| 7395 | return LY_SUCCESS; |
| 7396 | |
| 7397 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7398 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7399 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7400 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7401 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7402 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7403 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7404 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7405 | mod->compiled = NULL; |
| 7406 | |
| 7407 | /* revert compilation of modules implemented by dependency */ |
| 7408 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 7409 | m = ctx.ctx->list.objs[u]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7410 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7411 | /* revert features list to the precompiled state */ |
| 7412 | lys_feature_precompile_revert(&ctx, m); |
| 7413 | /* mark module as imported-only / not-implemented */ |
| 7414 | m->implemented = 0; |
| 7415 | /* free the compiled version of the module */ |
| 7416 | lysc_module_free(m->compiled, NULL); |
| 7417 | m->compiled = NULL; |
| 7418 | } |
| 7419 | } |
| 7420 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7421 | return ret; |
| 7422 | } |