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 | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 419 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 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 | { |
| 430 | const char *name; |
| 431 | unsigned int u; |
| 432 | const struct lys_module *mod; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 433 | struct lysc_ext *elist = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 434 | |
| 435 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 436 | ext->insubstmt = ext_p->insubstmt; |
| 437 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 438 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 439 | ext->parent = parent; |
| 440 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 441 | |
| 442 | /* get module where the extension definition should be placed */ |
| 443 | for (u = 0; ext_p->name[u] != ':'; ++u); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 444 | mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 445 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 446 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 447 | LY_EVALID); |
| 448 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 449 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 450 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 451 | ext_p->name, mod->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 452 | LY_EVALID); |
| 453 | name = &ext_p->name[u + 1]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 454 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 455 | /* find the extension definition there */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 456 | if (mod->off_extensions) { |
| 457 | elist = mod->off_extensions; |
| 458 | } else { |
| 459 | elist = mod->compiled->extensions; |
| 460 | } |
| 461 | LY_ARRAY_FOR(elist, u) { |
| 462 | if (!strcmp(name, elist[u].name)) { |
| 463 | ext->def = &elist[u]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 464 | break; |
| 465 | } |
| 466 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 467 | LY_CHECK_ERR_RET(!ext->def, |
| 468 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 469 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 470 | LY_EVALID); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 471 | |
| 472 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 473 | lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}"); |
| 474 | lysc_update_path(ctx, NULL, ext_p->name ); |
| 475 | if (ext->argument) { |
| 476 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 477 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 478 | LY_CHECK_RET(ext->def->plugin->compile(ctx, ext_p, ext),LY_EVALID); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 479 | if (ext->argument) { |
| 480 | lysc_update_path(ctx, NULL, NULL); |
| 481 | } |
| 482 | lysc_update_path(ctx, NULL, NULL); |
| 483 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | return LY_SUCCESS; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 491 | */ |
| 492 | static LY_ERR |
| 493 | lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext) |
| 494 | { |
| 495 | LY_ERR ret = LY_SUCCESS; |
| 496 | |
| 497 | DUP_STRING(ctx->ctx, ext_p->name, ext->name); |
| 498 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 499 | ext->module = ctx->mod_def; |
| 500 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done); |
| 501 | |
| 502 | done: |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * @brief Link the extensions definitions with the available extension plugins. |
| 508 | * |
| 509 | * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules |
| 510 | * are not connected with even available extension plugins. |
| 511 | * |
| 512 | * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)). |
| 513 | */ |
| 514 | static void |
| 515 | lys_compile_extension_plugins(struct lysc_ext *extensions) |
| 516 | { |
| 517 | unsigned int u; |
| 518 | |
| 519 | LY_ARRAY_FOR(extensions, u) { |
| 520 | extensions[u].plugin = lyext_get_plugin(&extensions[u]); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | LY_ERR |
| 525 | lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 526 | struct lysp_ext *extensions_p, struct lysc_ext **extensions) |
| 527 | { |
| 528 | unsigned int offset = 0, u; |
| 529 | struct lysc_ctx context = {0}; |
| 530 | |
| 531 | assert(ctx_sc || ctx); |
| 532 | |
| 533 | if (!ctx_sc) { |
| 534 | context.ctx = ctx; |
| 535 | context.mod = module; |
| 536 | context.path_len = 1; |
| 537 | context.path[0] = '/'; |
| 538 | ctx_sc = &context; |
| 539 | } |
| 540 | |
| 541 | if (!extensions_p) { |
| 542 | return LY_SUCCESS; |
| 543 | } |
| 544 | if (*extensions) { |
| 545 | offset = LY_ARRAY_SIZE(*extensions); |
| 546 | } |
| 547 | |
| 548 | lysc_update_path(ctx_sc, NULL, "{extension}"); |
| 549 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM); |
| 550 | LY_ARRAY_FOR(extensions_p, u) { |
| 551 | lysc_update_path(ctx_sc, NULL, extensions_p[u].name); |
| 552 | LY_ARRAY_INCREMENT(*extensions); |
| 553 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name); |
| 554 | LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u])); |
| 555 | lysc_update_path(ctx_sc, NULL, NULL); |
| 556 | } |
| 557 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 558 | |
| 559 | return LY_SUCCESS; |
| 560 | } |
| 561 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 562 | /** |
| 563 | * @brief Compile information from the if-feature statement |
| 564 | * @param[in] ctx Compile context. |
| 565 | * @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] | 566 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 567 | * @return LY_ERR value. |
| 568 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 569 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 570 | 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] | 571 | { |
| 572 | const char *c = *value; |
| 573 | int r, rc = EXIT_FAILURE; |
| 574 | int i, j, last_not, checkversion = 0; |
| 575 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 576 | uint8_t op; |
| 577 | struct iff_stack stack = {0, 0, NULL}; |
| 578 | struct lysc_feature *f; |
| 579 | |
| 580 | assert(c); |
| 581 | |
| 582 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 583 | for (i = j = last_not = 0; c[i]; i++) { |
| 584 | if (c[i] == '(') { |
| 585 | j++; |
| 586 | checkversion = 1; |
| 587 | continue; |
| 588 | } else if (c[i] == ')') { |
| 589 | j--; |
| 590 | continue; |
| 591 | } else if (isspace(c[i])) { |
| 592 | checkversion = 1; |
| 593 | continue; |
| 594 | } |
| 595 | |
| 596 | 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] | 597 | int sp; |
| 598 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 599 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 600 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 601 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 602 | return LY_EVALID; |
| 603 | } else if (!isspace(c[i + r])) { |
| 604 | /* feature name starting with the not/and/or */ |
| 605 | last_not = 0; |
| 606 | f_size++; |
| 607 | } else if (c[i] == 'n') { /* not operation */ |
| 608 | if (last_not) { |
| 609 | /* double not */ |
| 610 | expr_size = expr_size - 2; |
| 611 | last_not = 0; |
| 612 | } else { |
| 613 | last_not = 1; |
| 614 | } |
| 615 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 616 | if (f_exp != f_size) { |
| 617 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 618 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 619 | return LY_EVALID; |
| 620 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 621 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 622 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 623 | /* not a not operation */ |
| 624 | last_not = 0; |
| 625 | } |
| 626 | i += r; |
| 627 | } else { |
| 628 | f_size++; |
| 629 | last_not = 0; |
| 630 | } |
| 631 | expr_size++; |
| 632 | |
| 633 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 634 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 635 | i--; |
| 636 | break; |
| 637 | } |
| 638 | i++; |
| 639 | } |
| 640 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 641 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 642 | /* not matching count of ( and ) */ |
| 643 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 644 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 645 | return LY_EVALID; |
| 646 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 647 | if (f_exp != f_size) { |
| 648 | /* features do not match the needed arguments for the logical operations */ |
| 649 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 650 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 651 | "the required number of operands for the operations.", *value); |
| 652 | return LY_EVALID; |
| 653 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 654 | |
| 655 | if (checkversion || expr_size > 1) { |
| 656 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 657 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 658 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 659 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 660 | return LY_EVALID; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /* allocate the memory */ |
| 665 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 666 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 667 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 668 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 669 | |
| 670 | stack.size = expr_size; |
| 671 | f_size--; expr_size--; /* used as indexes from now */ |
| 672 | |
| 673 | for (i--; i >= 0; i--) { |
| 674 | if (c[i] == ')') { |
| 675 | /* push it on stack */ |
| 676 | iff_stack_push(&stack, LYS_IFF_RP); |
| 677 | continue; |
| 678 | } else if (c[i] == '(') { |
| 679 | /* pop from the stack into result all operators until ) */ |
| 680 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 681 | iff_setop(iff->expr, op, expr_size--); |
| 682 | } |
| 683 | continue; |
| 684 | } else if (isspace(c[i])) { |
| 685 | continue; |
| 686 | } |
| 687 | |
| 688 | /* end of operator or operand -> find beginning and get what is it */ |
| 689 | j = i + 1; |
| 690 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 691 | i--; |
| 692 | } |
| 693 | i++; /* go back by one step */ |
| 694 | |
| 695 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 696 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 697 | /* double not */ |
| 698 | iff_stack_pop(&stack); |
| 699 | } else { |
| 700 | /* not has the highest priority, so do not pop from the stack |
| 701 | * as in case of AND and OR */ |
| 702 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 703 | } |
| 704 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 705 | /* as for OR - pop from the stack all operators with the same or higher |
| 706 | * priority and store them to the result, then push the AND to the stack */ |
| 707 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 708 | op = iff_stack_pop(&stack); |
| 709 | iff_setop(iff->expr, op, expr_size--); |
| 710 | } |
| 711 | iff_stack_push(&stack, LYS_IFF_AND); |
| 712 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 713 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 714 | op = iff_stack_pop(&stack); |
| 715 | iff_setop(iff->expr, op, expr_size--); |
| 716 | } |
| 717 | iff_stack_push(&stack, LYS_IFF_OR); |
| 718 | } else { |
| 719 | /* feature name, length is j - i */ |
| 720 | |
| 721 | /* add it to the expression */ |
| 722 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 723 | |
| 724 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 725 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 726 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 727 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 728 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 729 | iff->features[f_size] = f; |
| 730 | LY_ARRAY_INCREMENT(iff->features); |
| 731 | f_size--; |
| 732 | } |
| 733 | } |
| 734 | while (stack.index) { |
| 735 | op = iff_stack_pop(&stack); |
| 736 | iff_setop(iff->expr, op, expr_size--); |
| 737 | } |
| 738 | |
| 739 | if (++expr_size || ++f_size) { |
| 740 | /* not all expected operators and operands found */ |
| 741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 742 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 743 | rc = LY_EINT; |
| 744 | } else { |
| 745 | rc = LY_SUCCESS; |
| 746 | } |
| 747 | |
| 748 | error: |
| 749 | /* cleanup */ |
| 750 | iff_stack_clean(&stack); |
| 751 | |
| 752 | return rc; |
| 753 | } |
| 754 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 755 | /** |
| 756 | * @brief Compile information from the when statement |
| 757 | * @param[in] ctx Compile context. |
| 758 | * @param[in] when_p The parsed when statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 759 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 760 | * @return LY_ERR value. |
| 761 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 762 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 763 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 764 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 765 | LY_ERR ret = LY_SUCCESS; |
| 766 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 767 | *when = calloc(1, sizeof **when); |
| 768 | (*when)->refcount = 1; |
| 769 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
| 770 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 771 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 772 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 773 | COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 774 | |
| 775 | done: |
| 776 | return ret; |
| 777 | } |
| 778 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 779 | /** |
| 780 | * @brief Compile information from the must statement |
| 781 | * @param[in] ctx Compile context. |
| 782 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 783 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 784 | * @return LY_ERR value. |
| 785 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 786 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 787 | 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] | 788 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 789 | LY_ERR ret = LY_SUCCESS; |
| 790 | |
| 791 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 792 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 793 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 794 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 795 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 796 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 797 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 798 | 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] | 799 | |
| 800 | done: |
| 801 | return ret; |
| 802 | } |
| 803 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 804 | /** |
| 805 | * @brief Compile information from the import statement |
| 806 | * @param[in] ctx Compile context. |
| 807 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 808 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 809 | * @return LY_ERR value. |
| 810 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 811 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 812 | 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] | 813 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 814 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 815 | LY_ERR ret = LY_SUCCESS; |
| 816 | |
| 817 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 818 | 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] | 819 | imp->module = imp_p->module; |
| 820 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 821 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 822 | * 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] | 823 | * 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] | 824 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 825 | /* try to use filepath if present */ |
| 826 | if (imp->module->filepath) { |
| 827 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 828 | !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] | 829 | if (mod != imp->module) { |
| 830 | 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] | 831 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 832 | mod = NULL; |
| 833 | } |
| 834 | } |
| 835 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 836 | 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] | 837 | 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] | 838 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 839 | return LY_ENOTFOUND; |
| 840 | } |
| 841 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | done: |
| 845 | return ret; |
| 846 | } |
| 847 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 848 | /** |
| 849 | * @brief Compile information from the identity statement |
| 850 | * |
| 851 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 852 | * |
| 853 | * @param[in] ctx Compile context. |
| 854 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 855 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 856 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 857 | * @return LY_ERR value. |
| 858 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 859 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 860 | 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] | 861 | { |
| 862 | unsigned int u; |
| 863 | LY_ERR ret = LY_SUCCESS; |
| 864 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 865 | lysc_update_path(ctx, NULL, ident_p->name); |
| 866 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 867 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 868 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 869 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 870 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 871 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 872 | 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] | 873 | /* 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] | 874 | 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] | 875 | ident->flags = ident_p->flags; |
| 876 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 877 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 878 | done: |
| 879 | return ret; |
| 880 | } |
| 881 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 882 | /** |
| 883 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 884 | * |
| 885 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 886 | * |
| 887 | * @param[in] ctx Compile context for logging. |
| 888 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 889 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 890 | * @return LY_SUCCESS if everything is ok. |
| 891 | * @return LY_EVALID if the identity is derived from itself. |
| 892 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 893 | static LY_ERR |
| 894 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 895 | { |
| 896 | LY_ERR ret = LY_EVALID; |
| 897 | unsigned int u, v; |
| 898 | struct ly_set recursion = {0}; |
| 899 | struct lysc_ident *drv; |
| 900 | |
| 901 | if (!derived) { |
| 902 | return LY_SUCCESS; |
| 903 | } |
| 904 | |
| 905 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 906 | if (ident == derived[u]) { |
| 907 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 908 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 909 | goto cleanup; |
| 910 | } |
| 911 | ly_set_add(&recursion, derived[u], 0); |
| 912 | } |
| 913 | |
| 914 | for (v = 0; v < recursion.count; ++v) { |
| 915 | drv = recursion.objs[v]; |
| 916 | if (!drv->derived) { |
| 917 | continue; |
| 918 | } |
| 919 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 920 | if (ident == drv->derived[u]) { |
| 921 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 922 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 923 | goto cleanup; |
| 924 | } |
| 925 | ly_set_add(&recursion, drv->derived[u], 0); |
| 926 | } |
| 927 | } |
| 928 | ret = LY_SUCCESS; |
| 929 | |
| 930 | cleanup: |
| 931 | ly_set_erase(&recursion, NULL); |
| 932 | return ret; |
| 933 | } |
| 934 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 935 | /** |
| 936 | * @brief Find and process the referenced base identities from another identity or identityref |
| 937 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 938 | * 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] | 939 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 940 | * to distinguish these two use cases. |
| 941 | * |
| 942 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 943 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 944 | * @param[in] ident Referencing identity to work with. |
| 945 | * @param[in] bases Array of bases of identityref to fill in. |
| 946 | * @return LY_ERR value. |
| 947 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 948 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 949 | 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] | 950 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 951 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 952 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 953 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 954 | struct lysc_ident **idref; |
| 955 | |
| 956 | assert(ident || bases); |
| 957 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 958 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 959 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 960 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 961 | return LY_EVALID; |
| 962 | } |
| 963 | |
| 964 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 965 | s = strchr(bases_p[u], ':'); |
| 966 | if (s) { |
| 967 | /* prefixed identity */ |
| 968 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 969 | 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] | 970 | } else { |
| 971 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 972 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 973 | } |
| 974 | if (!mod) { |
| 975 | if (ident) { |
| 976 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 977 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 978 | } else { |
| 979 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 980 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 981 | } |
| 982 | return LY_EVALID; |
| 983 | } |
| 984 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 985 | if (mod->compiled && mod->compiled->identities) { |
| 986 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 987 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 988 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 989 | if (ident == &mod->compiled->identities[v]) { |
| 990 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 991 | "Identity \"%s\" is derived from itself.", ident->name); |
| 992 | return LY_EVALID; |
| 993 | } |
| 994 | 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] | 995 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 996 | 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] | 997 | *idref = ident; |
| 998 | } else { |
| 999 | /* we have match! store the found identity */ |
| 1000 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1001 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1002 | } |
| 1003 | break; |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | if (!idref || !(*idref)) { |
| 1008 | if (ident) { |
| 1009 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1010 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1011 | } else { |
| 1012 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1013 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1014 | } |
| 1015 | return LY_EVALID; |
| 1016 | } |
| 1017 | } |
| 1018 | return LY_SUCCESS; |
| 1019 | } |
| 1020 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1021 | /** |
| 1022 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1023 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1024 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1025 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1026 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1027 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1028 | static LY_ERR |
| 1029 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1030 | { |
| 1031 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1032 | |
| 1033 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 1034 | if (!idents_p[i].bases) { |
| 1035 | continue; |
| 1036 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1037 | lysc_update_path(ctx, NULL, idents[i].name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1038 | 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] | 1039 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1040 | } |
| 1041 | return LY_SUCCESS; |
| 1042 | } |
| 1043 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1044 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1045 | 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] | 1046 | { |
| 1047 | unsigned int offset = 0, u; |
| 1048 | struct lysc_ctx context = {0}; |
| 1049 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1050 | assert(ctx_sc || ctx); |
| 1051 | |
| 1052 | if (!ctx_sc) { |
| 1053 | context.ctx = ctx; |
| 1054 | context.mod = module; |
| 1055 | context.path_len = 1; |
| 1056 | context.path[0] = '/'; |
| 1057 | ctx_sc = &context; |
| 1058 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1059 | |
| 1060 | if (!features_p) { |
| 1061 | return LY_SUCCESS; |
| 1062 | } |
| 1063 | if (*features) { |
| 1064 | offset = LY_ARRAY_SIZE(*features); |
| 1065 | } |
| 1066 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1067 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 1068 | 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] | 1069 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1070 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1071 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1072 | LY_ARRAY_INCREMENT(*features); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1073 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 1074 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1075 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1076 | 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] | 1077 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1078 | (*features)[offset + u].module = ctx_sc->mod; |
| 1079 | |
| 1080 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1081 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1082 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1083 | |
| 1084 | return LY_SUCCESS; |
| 1085 | } |
| 1086 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1087 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1088 | * @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] | 1089 | * |
| 1090 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1091 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1092 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1093 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1094 | * being currently processed). |
| 1095 | * @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] | 1096 | * @return LY_SUCCESS if everything is ok. |
| 1097 | * @return LY_EVALID if the feature references indirectly itself. |
| 1098 | */ |
| 1099 | static LY_ERR |
| 1100 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1101 | { |
| 1102 | LY_ERR ret = LY_EVALID; |
| 1103 | unsigned int u, v; |
| 1104 | struct ly_set recursion = {0}; |
| 1105 | struct lysc_feature *drv; |
| 1106 | |
| 1107 | if (!depfeatures) { |
| 1108 | return LY_SUCCESS; |
| 1109 | } |
| 1110 | |
| 1111 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1112 | if (feature == depfeatures[u]) { |
| 1113 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1114 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1115 | goto cleanup; |
| 1116 | } |
| 1117 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1118 | } |
| 1119 | |
| 1120 | for (v = 0; v < recursion.count; ++v) { |
| 1121 | drv = recursion.objs[v]; |
| 1122 | if (!drv->depfeatures) { |
| 1123 | continue; |
| 1124 | } |
| 1125 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1126 | if (feature == drv->depfeatures[u]) { |
| 1127 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1128 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1129 | goto cleanup; |
| 1130 | } |
| 1131 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1132 | } |
| 1133 | } |
| 1134 | ret = LY_SUCCESS; |
| 1135 | |
| 1136 | cleanup: |
| 1137 | ly_set_erase(&recursion, NULL); |
| 1138 | return ret; |
| 1139 | } |
| 1140 | |
| 1141 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1142 | * @brief Create pre-compiled features array. |
| 1143 | * |
| 1144 | * See lys_feature_precompile() for more details. |
| 1145 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1146 | * @param[in] ctx Compile context. |
| 1147 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1148 | * @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] | 1149 | * @return LY_ERR value. |
| 1150 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1151 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1152 | 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] | 1153 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1154 | unsigned int u, v, x; |
| 1155 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1156 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1157 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1158 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1159 | /* find the preprecompiled feature */ |
| 1160 | LY_ARRAY_FOR(features, x) { |
| 1161 | if (strcmp(features[x].name, feature_p->name)) { |
| 1162 | continue; |
| 1163 | } |
| 1164 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1165 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1166 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1167 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1168 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1169 | 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] | 1170 | 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] | 1171 | if (feature->iffeatures) { |
| 1172 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1173 | if (feature->iffeatures[u].features) { |
| 1174 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1175 | /* check for circular dependency - direct reference first,... */ |
| 1176 | if (feature == feature->iffeatures[u].features[v]) { |
| 1177 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1178 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1179 | return LY_EVALID; |
| 1180 | } |
| 1181 | /* ... and indirect circular reference */ |
| 1182 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1183 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1184 | /* add itself into the dependants list */ |
| 1185 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1186 | *df = feature; |
| 1187 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1188 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1189 | } |
| 1190 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1191 | lysc_update_path(ctx, NULL, NULL); |
| 1192 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1193 | done: |
| 1194 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1195 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1196 | |
| 1197 | LOGINT(ctx->ctx); |
| 1198 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1199 | } |
| 1200 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1201 | /** |
| 1202 | * @brief Revert compiled list of features back to the precompiled state. |
| 1203 | * |
| 1204 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1205 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1206 | * |
| 1207 | * @param[in] ctx Compilation context. |
| 1208 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1209 | * and supposed to hold the off_features list. |
| 1210 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1211 | static void |
| 1212 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1213 | { |
| 1214 | unsigned int u, v; |
| 1215 | |
| 1216 | /* keep the off_features list until the complete lys_module is freed */ |
| 1217 | mod->off_features = mod->compiled->features; |
| 1218 | mod->compiled->features = NULL; |
| 1219 | |
| 1220 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1221 | * which may points into the data being freed here */ |
| 1222 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1223 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1224 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1225 | } |
| 1226 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1227 | mod->off_features[u].iffeatures = NULL; |
| 1228 | |
| 1229 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1230 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1231 | } |
| 1232 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1233 | mod->off_features[u].exts = NULL; |
| 1234 | } |
| 1235 | } |
| 1236 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1237 | /** |
| 1238 | * @brief Validate and normalize numeric value from a range definition. |
| 1239 | * @param[in] ctx Compile context. |
| 1240 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1241 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1242 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1243 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1244 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1245 | * @param[in] value String value of the range boundary. |
| 1246 | * @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. |
| 1247 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1248 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1249 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1250 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1251 | 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] | 1252 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1253 | size_t fraction = 0, size; |
| 1254 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1255 | *len = 0; |
| 1256 | |
| 1257 | assert(value); |
| 1258 | /* parse value */ |
| 1259 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1260 | return LY_EVALID; |
| 1261 | } |
| 1262 | |
| 1263 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1264 | ++(*len); |
| 1265 | } |
| 1266 | |
| 1267 | while (isdigit(value[*len])) { |
| 1268 | ++(*len); |
| 1269 | } |
| 1270 | |
| 1271 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1272 | if (basetype == LY_TYPE_DEC64) { |
| 1273 | goto decimal; |
| 1274 | } else { |
| 1275 | *valcopy = strndup(value, *len); |
| 1276 | return LY_SUCCESS; |
| 1277 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1278 | } |
| 1279 | fraction = *len; |
| 1280 | |
| 1281 | ++(*len); |
| 1282 | while (isdigit(value[*len])) { |
| 1283 | ++(*len); |
| 1284 | } |
| 1285 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1286 | if (basetype == LY_TYPE_DEC64) { |
| 1287 | decimal: |
| 1288 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1289 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1290 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1291 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1292 | *len, value, frdigits); |
| 1293 | return LY_EINVAL; |
| 1294 | } |
| 1295 | if (fraction) { |
| 1296 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1297 | } else { |
| 1298 | size = (*len) + frdigits + 1; |
| 1299 | } |
| 1300 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1301 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1302 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1303 | (*valcopy)[size - 1] = '\0'; |
| 1304 | if (fraction) { |
| 1305 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1306 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1307 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1308 | } else { |
| 1309 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1310 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1311 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1312 | } |
| 1313 | return LY_SUCCESS; |
| 1314 | } |
| 1315 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1316 | /** |
| 1317 | * @brief Check that values in range are in ascendant order. |
| 1318 | * @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] | 1319 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1320 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1321 | * @param[in] value Current value to check. |
| 1322 | * @param[in] prev_value The last seen value. |
| 1323 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1324 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1325 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1326 | 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] | 1327 | { |
| 1328 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1329 | 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] | 1330 | return LY_EEXIST; |
| 1331 | } |
| 1332 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1333 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1334 | return LY_EEXIST; |
| 1335 | } |
| 1336 | } |
| 1337 | return LY_SUCCESS; |
| 1338 | } |
| 1339 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1340 | /** |
| 1341 | * @brief Set min/max value of the range part. |
| 1342 | * @param[in] ctx Compile context. |
| 1343 | * @param[in] part Range part structure to fill. |
| 1344 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1345 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1346 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1347 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1348 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1349 | * @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] | 1350 | * @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] | 1351 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1352 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1353 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1354 | * frdigits value), LY_EMEM. |
| 1355 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1356 | static LY_ERR |
| 1357 | 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] | 1358 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1359 | { |
| 1360 | LY_ERR ret = LY_SUCCESS; |
| 1361 | char *valcopy = NULL; |
| 1362 | size_t len; |
| 1363 | |
| 1364 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1365 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1366 | LY_CHECK_GOTO(ret, finalize); |
| 1367 | } |
| 1368 | if (!valcopy && base_range) { |
| 1369 | if (max) { |
| 1370 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1371 | } else { |
| 1372 | part->min_64 = base_range->parts[0].min_64; |
| 1373 | } |
| 1374 | if (!first) { |
| 1375 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1376 | } |
| 1377 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1381 | case LY_TYPE_INT8: /* range */ |
| 1382 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1383 | 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] | 1384 | } else if (max) { |
| 1385 | part->max_64 = INT64_C(127); |
| 1386 | } else { |
| 1387 | part->min_64 = INT64_C(-128); |
| 1388 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1389 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1390 | 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] | 1391 | } |
| 1392 | break; |
| 1393 | case LY_TYPE_INT16: /* range */ |
| 1394 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1395 | 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] | 1396 | } else if (max) { |
| 1397 | part->max_64 = INT64_C(32767); |
| 1398 | } else { |
| 1399 | part->min_64 = INT64_C(-32768); |
| 1400 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1401 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1402 | 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] | 1403 | } |
| 1404 | break; |
| 1405 | case LY_TYPE_INT32: /* range */ |
| 1406 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1407 | 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] | 1408 | } else if (max) { |
| 1409 | part->max_64 = INT64_C(2147483647); |
| 1410 | } else { |
| 1411 | part->min_64 = INT64_C(-2147483648); |
| 1412 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1413 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1414 | 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] | 1415 | } |
| 1416 | break; |
| 1417 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1418 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1419 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1420 | 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] | 1421 | max ? &part->max_64 : &part->min_64); |
| 1422 | } else if (max) { |
| 1423 | part->max_64 = INT64_C(9223372036854775807); |
| 1424 | } else { |
| 1425 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1426 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1427 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1428 | 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] | 1429 | } |
| 1430 | break; |
| 1431 | case LY_TYPE_UINT8: /* range */ |
| 1432 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1433 | 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] | 1434 | } else if (max) { |
| 1435 | part->max_u64 = UINT64_C(255); |
| 1436 | } else { |
| 1437 | part->min_u64 = UINT64_C(0); |
| 1438 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1439 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1440 | 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] | 1441 | } |
| 1442 | break; |
| 1443 | case LY_TYPE_UINT16: /* range */ |
| 1444 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1445 | 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] | 1446 | } else if (max) { |
| 1447 | part->max_u64 = UINT64_C(65535); |
| 1448 | } else { |
| 1449 | part->min_u64 = UINT64_C(0); |
| 1450 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1451 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1452 | 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] | 1453 | } |
| 1454 | break; |
| 1455 | case LY_TYPE_UINT32: /* range */ |
| 1456 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1457 | 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] | 1458 | } else if (max) { |
| 1459 | part->max_u64 = UINT64_C(4294967295); |
| 1460 | } else { |
| 1461 | part->min_u64 = UINT64_C(0); |
| 1462 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1463 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1464 | 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] | 1465 | } |
| 1466 | break; |
| 1467 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1468 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1469 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1470 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1471 | 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] | 1472 | } else if (max) { |
| 1473 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1474 | } else { |
| 1475 | part->min_u64 = UINT64_C(0); |
| 1476 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1477 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1478 | 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] | 1479 | } |
| 1480 | break; |
| 1481 | default: |
| 1482 | LOGINT(ctx->ctx); |
| 1483 | ret = LY_EINT; |
| 1484 | } |
| 1485 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1486 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1487 | if (ret == LY_EDENIED) { |
| 1488 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1489 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1490 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1491 | } else if (ret == LY_EVALID) { |
| 1492 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1493 | "Invalid %s restriction - invalid value \"%s\".", |
| 1494 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1495 | } else if (ret == LY_EEXIST) { |
| 1496 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1497 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1498 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1499 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1500 | } else if (!ret && value) { |
| 1501 | *value = *value + len; |
| 1502 | } |
| 1503 | free(valcopy); |
| 1504 | return ret; |
| 1505 | } |
| 1506 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1507 | /** |
| 1508 | * @brief Compile the parsed range restriction. |
| 1509 | * @param[in] ctx Compile context. |
| 1510 | * @param[in] range_p Parsed range structure to compile. |
| 1511 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1512 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1513 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1514 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1515 | * range restriction must be more restrictive than the base_range. |
| 1516 | * @param[in,out] range Pointer to the created current range structure. |
| 1517 | * @return LY_ERR value. |
| 1518 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1519 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1520 | 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] | 1521 | struct lysc_range *base_range, struct lysc_range **range) |
| 1522 | { |
| 1523 | LY_ERR ret = LY_EVALID; |
| 1524 | const char *expr; |
| 1525 | struct lysc_range_part *parts = NULL, *part; |
| 1526 | int range_expected = 0, uns; |
| 1527 | unsigned int parts_done = 0, u, v; |
| 1528 | |
| 1529 | assert(range); |
| 1530 | assert(range_p); |
| 1531 | |
| 1532 | expr = range_p->arg; |
| 1533 | while(1) { |
| 1534 | if (isspace(*expr)) { |
| 1535 | ++expr; |
| 1536 | } else if (*expr == '\0') { |
| 1537 | if (range_expected) { |
| 1538 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1539 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1540 | length_restr ? "length" : "range", range_p->arg); |
| 1541 | goto cleanup; |
| 1542 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1544 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1545 | length_restr ? "length" : "range", range_p->arg); |
| 1546 | goto cleanup; |
| 1547 | } |
| 1548 | parts_done++; |
| 1549 | break; |
| 1550 | } else if (!strncmp(expr, "min", 3)) { |
| 1551 | if (parts) { |
| 1552 | /* min cannot be used elsewhere than in the first part */ |
| 1553 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1554 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1555 | expr - range_p->arg, range_p->arg); |
| 1556 | goto cleanup; |
| 1557 | } |
| 1558 | expr += 3; |
| 1559 | |
| 1560 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1561 | 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] | 1562 | part->max_64 = part->min_64; |
| 1563 | } else if (*expr == '|') { |
| 1564 | if (!parts || range_expected) { |
| 1565 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1566 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1567 | goto cleanup; |
| 1568 | } |
| 1569 | expr++; |
| 1570 | parts_done++; |
| 1571 | /* process next part of the expression */ |
| 1572 | } else if (!strncmp(expr, "..", 2)) { |
| 1573 | expr += 2; |
| 1574 | while (isspace(*expr)) { |
| 1575 | expr++; |
| 1576 | } |
| 1577 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1578 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1579 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1580 | goto cleanup; |
| 1581 | } |
| 1582 | /* continue expecting the upper boundary */ |
| 1583 | range_expected = 1; |
| 1584 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1585 | /* number */ |
| 1586 | if (range_expected) { |
| 1587 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1588 | 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] | 1589 | range_expected = 0; |
| 1590 | } else { |
| 1591 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1592 | 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] | 1593 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1594 | part->max_64 = part->min_64; |
| 1595 | } |
| 1596 | |
| 1597 | /* continue with possible another expression part */ |
| 1598 | } else if (!strncmp(expr, "max", 3)) { |
| 1599 | expr += 3; |
| 1600 | while (isspace(*expr)) { |
| 1601 | expr++; |
| 1602 | } |
| 1603 | if (*expr != '\0') { |
| 1604 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1605 | length_restr ? "length" : "range", expr); |
| 1606 | goto cleanup; |
| 1607 | } |
| 1608 | if (range_expected) { |
| 1609 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1610 | 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] | 1611 | range_expected = 0; |
| 1612 | } else { |
| 1613 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1614 | 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] | 1615 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1616 | part->min_64 = part->max_64; |
| 1617 | } |
| 1618 | } else { |
| 1619 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1620 | length_restr ? "length" : "range", expr); |
| 1621 | goto cleanup; |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | /* check with the previous range/length restriction */ |
| 1626 | if (base_range) { |
| 1627 | switch (basetype) { |
| 1628 | case LY_TYPE_BINARY: |
| 1629 | case LY_TYPE_UINT8: |
| 1630 | case LY_TYPE_UINT16: |
| 1631 | case LY_TYPE_UINT32: |
| 1632 | case LY_TYPE_UINT64: |
| 1633 | case LY_TYPE_STRING: |
| 1634 | uns = 1; |
| 1635 | break; |
| 1636 | case LY_TYPE_DEC64: |
| 1637 | case LY_TYPE_INT8: |
| 1638 | case LY_TYPE_INT16: |
| 1639 | case LY_TYPE_INT32: |
| 1640 | case LY_TYPE_INT64: |
| 1641 | uns = 0; |
| 1642 | break; |
| 1643 | default: |
| 1644 | LOGINT(ctx->ctx); |
| 1645 | ret = LY_EINT; |
| 1646 | goto cleanup; |
| 1647 | } |
| 1648 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1649 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1650 | goto baseerror; |
| 1651 | } |
| 1652 | /* current lower bound is not lower than the base */ |
| 1653 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1654 | /* base has single value */ |
| 1655 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1656 | /* both lower bounds are the same */ |
| 1657 | if (parts[u].min_64 != parts[u].max_64) { |
| 1658 | /* current continues with a range */ |
| 1659 | goto baseerror; |
| 1660 | } else { |
| 1661 | /* equal single values, move both forward */ |
| 1662 | ++v; |
| 1663 | continue; |
| 1664 | } |
| 1665 | } else { |
| 1666 | /* base is single value lower than current range, so the |
| 1667 | * value from base range is removed in the current, |
| 1668 | * move only base and repeat checking */ |
| 1669 | ++v; |
| 1670 | --u; |
| 1671 | continue; |
| 1672 | } |
| 1673 | } else { |
| 1674 | /* base is the range */ |
| 1675 | if (parts[u].min_64 == parts[u].max_64) { |
| 1676 | /* current is a single value */ |
| 1677 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1678 | /* current is behind the base range, so base range is omitted, |
| 1679 | * move the base and keep the current for further check */ |
| 1680 | ++v; |
| 1681 | --u; |
| 1682 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1683 | continue; |
| 1684 | } else { |
| 1685 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1686 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1687 | /* higher bound is higher than the current higher bound */ |
| 1688 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1689 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1690 | * continue with the same current, but move the base */ |
| 1691 | --u; |
| 1692 | ++v; |
| 1693 | continue; |
| 1694 | } |
| 1695 | /* current range starts within the base range but end behind it */ |
| 1696 | goto baseerror; |
| 1697 | } else { |
| 1698 | /* current range is smaller than the base, |
| 1699 | * move current, but stay with the base */ |
| 1700 | continue; |
| 1701 | } |
| 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | if (u != parts_done) { |
| 1706 | baseerror: |
| 1707 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1708 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1709 | length_restr ? "length" : "range", range_p->arg); |
| 1710 | goto cleanup; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | if (!(*range)) { |
| 1715 | *range = calloc(1, sizeof **range); |
| 1716 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1717 | } |
| 1718 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1719 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1720 | if (range_p->eapptag) { |
| 1721 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1722 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1723 | } |
| 1724 | if (range_p->emsg) { |
| 1725 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1726 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1727 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1728 | if (range_p->dsc) { |
| 1729 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1730 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1731 | } |
| 1732 | if (range_p->ref) { |
| 1733 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1734 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1735 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1736 | /* extensions are taken only from the last range by the caller */ |
| 1737 | |
| 1738 | (*range)->parts = parts; |
| 1739 | parts = NULL; |
| 1740 | ret = LY_SUCCESS; |
| 1741 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1742 | LY_ARRAY_FREE(parts); |
| 1743 | |
| 1744 | return ret; |
| 1745 | } |
| 1746 | |
| 1747 | /** |
| 1748 | * @brief Checks pattern syntax. |
| 1749 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1750 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1751 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1752 | * @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] | 1753 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1754 | */ |
| 1755 | static LY_ERR |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1756 | lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1757 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1758 | int idx, idx2, start, end, count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1759 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1760 | int err_code; |
| 1761 | const char *orig_ptr; |
| 1762 | PCRE2_SIZE err_offset; |
| 1763 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1764 | #define URANGE_LEN 19 |
| 1765 | char *ublock2urange[][2] = { |
| 1766 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1767 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1768 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1769 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1770 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1771 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1772 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1773 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1774 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1775 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1776 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1777 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1778 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1779 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1780 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1781 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1782 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1783 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1784 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1785 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1786 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1787 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1788 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1789 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1790 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1791 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1792 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1793 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1794 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1795 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1796 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1797 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1798 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1799 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1800 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1801 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1802 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1803 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1804 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1805 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1806 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1807 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1808 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1809 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1810 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1811 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1812 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1813 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1814 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1815 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1816 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1817 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1818 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1819 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1820 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1821 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1822 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1823 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1824 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1825 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1826 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1827 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1828 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1829 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1830 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1831 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1832 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1833 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1834 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1835 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1836 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1837 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1838 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1839 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1840 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1841 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1842 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1843 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1844 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1845 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1846 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1847 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1848 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1849 | {NULL, NULL} |
| 1850 | }; |
| 1851 | |
| 1852 | /* adjust the expression to a Perl equivalent |
| 1853 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1854 | |
| 1855 | /* we need to replace all "$" with "\$", count them now */ |
| 1856 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1857 | |
| 1858 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1859 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1860 | perl_regex[0] = '\0'; |
| 1861 | |
| 1862 | ptr = perl_regex; |
| 1863 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1864 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1865 | if (orig_ptr[0] == '$') { |
| 1866 | ptr += sprintf(ptr, "\\$"); |
| 1867 | } else if (orig_ptr[0] == '^') { |
| 1868 | ptr += sprintf(ptr, "\\^"); |
| 1869 | } else { |
| 1870 | ptr[0] = orig_ptr[0]; |
| 1871 | ++ptr; |
| 1872 | } |
| 1873 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1874 | ptr[0] = '\0'; |
| 1875 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1876 | |
| 1877 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1878 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1879 | start = ptr - perl_regex; |
| 1880 | |
| 1881 | ptr = strchr(ptr, '}'); |
| 1882 | if (!ptr) { |
| 1883 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1884 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1885 | free(perl_regex); |
| 1886 | return LY_EVALID; |
| 1887 | } |
| 1888 | end = (ptr - perl_regex) + 1; |
| 1889 | |
| 1890 | /* need more space */ |
| 1891 | if (end - start < URANGE_LEN) { |
| 1892 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1893 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1894 | } |
| 1895 | |
| 1896 | /* find our range */ |
| 1897 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1898 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1899 | break; |
| 1900 | } |
| 1901 | } |
| 1902 | if (!ublock2urange[idx][0]) { |
| 1903 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1904 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1905 | free(perl_regex); |
| 1906 | return LY_EVALID; |
| 1907 | } |
| 1908 | |
| 1909 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1910 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1911 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1912 | ++count; |
| 1913 | } |
| 1914 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1915 | --count; |
| 1916 | } |
| 1917 | } |
| 1918 | if (count) { |
| 1919 | /* skip brackets */ |
| 1920 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1921 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1922 | } else { |
| 1923 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1924 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1929 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 1930 | 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] | 1931 | &err_code, &err_offset, NULL); |
| 1932 | if (!code_local) { |
| 1933 | PCRE2_UCHAR err_msg[256] = {0}; |
| 1934 | pcre2_get_error_message(err_code, err_msg, 256); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1935 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1936 | free(perl_regex); |
| 1937 | return LY_EVALID; |
| 1938 | } |
| 1939 | free(perl_regex); |
| 1940 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1941 | if (code) { |
| 1942 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1943 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1944 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | return LY_SUCCESS; |
| 1948 | |
| 1949 | #undef URANGE_LEN |
| 1950 | } |
| 1951 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1952 | /** |
| 1953 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1954 | * @param[in] ctx Compile context. |
| 1955 | * @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] | 1956 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1957 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1958 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1959 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1960 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1961 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1962 | 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] | 1963 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1964 | { |
| 1965 | struct lysc_pattern **pattern; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1966 | unsigned int u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1967 | LY_ERR ret = LY_SUCCESS; |
| 1968 | |
| 1969 | /* first, copy the patterns from the base type */ |
| 1970 | if (base_patterns) { |
| 1971 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1972 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1973 | } |
| 1974 | |
| 1975 | LY_ARRAY_FOR(patterns_p, u) { |
| 1976 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1977 | *pattern = calloc(1, sizeof **pattern); |
| 1978 | ++(*pattern)->refcount; |
| 1979 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1980 | ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1981 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1982 | |
| 1983 | if (patterns_p[u].arg[0] == 0x15) { |
| 1984 | (*pattern)->inverted = 1; |
| 1985 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1986 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1987 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1988 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1989 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 1990 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1991 | 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] | 1992 | } |
| 1993 | done: |
| 1994 | return ret; |
| 1995 | } |
| 1996 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1997 | /** |
| 1998 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1999 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2000 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2001 | 0 /* LY_TYPE_UNKNOWN */, |
| 2002 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2003 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2004 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2005 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2006 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2007 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2008 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2009 | 0 /* LY_TYPE_BOOL */, |
| 2010 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2011 | 0 /* LY_TYPE_EMPTY */, |
| 2012 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2013 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2014 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2015 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2016 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2017 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2018 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2019 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2020 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2021 | }; |
| 2022 | |
| 2023 | /** |
| 2024 | * @brief stringification of the YANG built-in data types |
| 2025 | */ |
| 2026 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2027 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2028 | "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] | 2029 | }; |
| 2030 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2031 | /** |
| 2032 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2033 | * @param[in] ctx Compile context. |
| 2034 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2035 | * @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] | 2036 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2037 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2038 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2039 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2040 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2041 | 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] | 2042 | 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] | 2043 | { |
| 2044 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 2045 | unsigned int u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2046 | int32_t value = 0; |
| 2047 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2048 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2049 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2050 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2051 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2052 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2053 | return LY_EVALID; |
| 2054 | } |
| 2055 | |
| 2056 | LY_ARRAY_FOR(enums_p, u) { |
| 2057 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2058 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2059 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2060 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2061 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2062 | if (base_enums) { |
| 2063 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2064 | LY_ARRAY_FOR(base_enums, v) { |
| 2065 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2066 | break; |
| 2067 | } |
| 2068 | } |
| 2069 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 2070 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2071 | "Invalid %s - derived type adds new item \"%s\".", |
| 2072 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2073 | return LY_EVALID; |
| 2074 | } |
| 2075 | match = v; |
| 2076 | } |
| 2077 | |
| 2078 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2079 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2080 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2081 | e->value = (int32_t)enums_p[u].value; |
| 2082 | if (!u || e->value >= value) { |
| 2083 | value = e->value + 1; |
| 2084 | } |
| 2085 | /* check collision with other values */ |
| 2086 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2087 | if (e->value == (*enums)[v].value) { |
| 2088 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2089 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2090 | e->value, e->name, (*enums)[v].name); |
| 2091 | return LY_EVALID; |
| 2092 | } |
| 2093 | } |
| 2094 | } else if (base_enums) { |
| 2095 | /* inherit the assigned value */ |
| 2096 | e->value = base_enums[match].value; |
| 2097 | if (!u || e->value >= value) { |
| 2098 | value = e->value + 1; |
| 2099 | } |
| 2100 | } else { |
| 2101 | /* assign value automatically */ |
| 2102 | if (u && value == INT32_MIN) { |
| 2103 | /* counter overflow */ |
| 2104 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2105 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2106 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2107 | return LY_EVALID; |
| 2108 | } |
| 2109 | e->value = value++; |
| 2110 | } |
| 2111 | } else { /* LY_TYPE_BITS */ |
| 2112 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2113 | e->value = (int32_t)enums_p[u].value; |
| 2114 | if (!u || (uint32_t)e->value >= position) { |
| 2115 | position = (uint32_t)e->value + 1; |
| 2116 | } |
| 2117 | /* check collision with other values */ |
| 2118 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2119 | if (e->value == (*enums)[v].value) { |
| 2120 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2121 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2122 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2123 | return LY_EVALID; |
| 2124 | } |
| 2125 | } |
| 2126 | } else if (base_enums) { |
| 2127 | /* inherit the assigned value */ |
| 2128 | e->value = base_enums[match].value; |
| 2129 | if (!u || (uint32_t)e->value >= position) { |
| 2130 | position = (uint32_t)e->value + 1; |
| 2131 | } |
| 2132 | } else { |
| 2133 | /* assign value automatically */ |
| 2134 | if (u && position == 0) { |
| 2135 | /* counter overflow */ |
| 2136 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2137 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2138 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2139 | return LY_EVALID; |
| 2140 | } |
| 2141 | e->value = position++; |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | if (base_enums) { |
| 2146 | /* the assigned values must not change from the derived type */ |
| 2147 | if (e->value != base_enums[match].value) { |
| 2148 | if (basetype == LY_TYPE_ENUM) { |
| 2149 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2150 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2151 | e->name, base_enums[match].value, e->value); |
| 2152 | } else { |
| 2153 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2154 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2155 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2156 | } |
| 2157 | return LY_EVALID; |
| 2158 | } |
| 2159 | } |
| 2160 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2161 | 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] | 2162 | 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] | 2163 | |
| 2164 | if (basetype == LY_TYPE_BITS) { |
| 2165 | /* keep bits ordered by position */ |
| 2166 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2167 | if (v != u) { |
| 2168 | memcpy(&storage, e, sizeof *e); |
| 2169 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2170 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2171 | } |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | done: |
| 2176 | return ret; |
| 2177 | } |
| 2178 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2179 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2180 | for ((NODE) = (NODE)->parent; \ |
| 2181 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 2182 | (NODE) = (NODE)->parent); \ |
| 2183 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2184 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2185 | TERM; \ |
| 2186 | } |
| 2187 | |
| 2188 | /** |
| 2189 | * @brief Validate the predicate(s) from the leafref path. |
| 2190 | * @param[in] ctx Compile context |
| 2191 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2192 | * 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] | 2193 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2194 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2195 | * @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] | 2196 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2197 | */ |
| 2198 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2199 | 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] | 2200 | 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] | 2201 | { |
| 2202 | LY_ERR ret = LY_EVALID; |
| 2203 | const struct lys_module *mod; |
| 2204 | const struct lysc_node *src_node, *dst_node; |
| 2205 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2206 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2207 | unsigned int dest_parent_times, c; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2208 | const char *start, *end, *pke_end; |
| 2209 | struct ly_set keys = {0}; |
| 2210 | int i; |
| 2211 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2212 | assert(path_context); |
| 2213 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2214 | while (**predicate == '[') { |
| 2215 | start = (*predicate)++; |
| 2216 | |
| 2217 | while (isspace(**predicate)) { |
| 2218 | ++(*predicate); |
| 2219 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2220 | 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] | 2221 | while (isspace(**predicate)) { |
| 2222 | ++(*predicate); |
| 2223 | } |
| 2224 | if (**predicate != '=') { |
| 2225 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2226 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2227 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2228 | goto cleanup; |
| 2229 | } |
| 2230 | ++(*predicate); |
| 2231 | while (isspace(**predicate)) { |
| 2232 | ++(*predicate); |
| 2233 | } |
| 2234 | |
| 2235 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2236 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2237 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2238 | goto cleanup; |
| 2239 | } |
| 2240 | --pke_end; |
| 2241 | while (isspace(*pke_end)) { |
| 2242 | --pke_end; |
| 2243 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2244 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2245 | /* localize path-key-expr */ |
| 2246 | pke_start = path_key_expr = *predicate; |
| 2247 | /* move after the current predicate */ |
| 2248 | *predicate = end + 1; |
| 2249 | |
| 2250 | /* source (must be leaf or leaf-list) */ |
| 2251 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2252 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2253 | if (!mod) { |
| 2254 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2255 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2256 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2257 | goto cleanup; |
| 2258 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2259 | if (!mod->implemented) { |
| 2260 | /* make the module implemented */ |
| 2261 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2262 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2263 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2264 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2265 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2266 | src_node = NULL; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2267 | if (!(context_node->flags & LYS_KEYLESS)) { |
| 2268 | struct lysc_node *key; |
| 2269 | for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) { |
| 2270 | if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') { |
| 2271 | src_node = key; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2272 | break; |
| 2273 | } |
| 2274 | } |
| 2275 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2276 | if (!src_node) { |
| 2277 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2278 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2279 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2280 | goto cleanup; |
| 2281 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2282 | |
| 2283 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2284 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2285 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2286 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2287 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2288 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2289 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2290 | *predicate - start, start, src_node->name); |
| 2291 | goto cleanup; |
| 2292 | } |
| 2293 | |
| 2294 | /* destination */ |
| 2295 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2296 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2297 | |
| 2298 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2299 | if (strncmp(path_key_expr, "current", 7)) { |
| 2300 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2301 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2302 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2303 | *predicate - start, start); |
| 2304 | goto cleanup; |
| 2305 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2306 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2307 | if (*path_key_expr != '(') { |
| 2308 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2309 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2310 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2311 | if (*path_key_expr != ')') { |
| 2312 | goto error_current_function_invocation; |
| 2313 | } |
| 2314 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2315 | |
| 2316 | if (*path_key_expr != '/') { |
| 2317 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2318 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2319 | *predicate - start, start); |
| 2320 | goto cleanup; |
| 2321 | } |
| 2322 | ++path_key_expr; |
| 2323 | while (isspace(*path_key_expr)) { |
| 2324 | ++path_key_expr; |
| 2325 | } |
| 2326 | |
| 2327 | /* rel-path-keyexpr: |
| 2328 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2329 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2330 | ++dest_parent_times; |
| 2331 | path_key_expr += 2; |
| 2332 | while (isspace(*path_key_expr)) { |
| 2333 | ++path_key_expr; |
| 2334 | } |
| 2335 | if (*path_key_expr != '/') { |
| 2336 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2337 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2338 | *predicate - start, start); |
| 2339 | goto cleanup; |
| 2340 | } |
| 2341 | ++path_key_expr; |
| 2342 | while (isspace(*path_key_expr)) { |
| 2343 | ++path_key_expr; |
| 2344 | } |
| 2345 | |
| 2346 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2347 | * all schema nodes that cannot be instantiated in data tree */ |
| 2348 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2349 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2350 | *predicate - start, start); |
| 2351 | } |
| 2352 | if (!dest_parent_times) { |
| 2353 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2354 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2355 | *predicate - start, start); |
| 2356 | goto cleanup; |
| 2357 | } |
| 2358 | if (path_key_expr == pke_end) { |
| 2359 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2360 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2361 | *predicate - start, start); |
| 2362 | goto cleanup; |
| 2363 | } |
| 2364 | |
| 2365 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2366 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2367 | 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] | 2368 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2369 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2370 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2371 | goto cleanup; |
| 2372 | } |
| 2373 | |
| 2374 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2375 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2376 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2377 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2378 | } |
| 2379 | if (!mod) { |
| 2380 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2381 | "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] | 2382 | *predicate - start, start, dst_len, dst); |
| 2383 | goto cleanup; |
| 2384 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2385 | if (!mod->implemented) { |
| 2386 | /* make the module implemented */ |
| 2387 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2388 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2389 | |
| 2390 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2391 | if (!dst_node) { |
| 2392 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2393 | "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] | 2394 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2395 | goto cleanup; |
| 2396 | } |
| 2397 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2398 | 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] | 2399 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2400 | "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] | 2401 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2402 | goto cleanup; |
| 2403 | } |
| 2404 | } |
| 2405 | |
| 2406 | ret = LY_SUCCESS; |
| 2407 | cleanup: |
| 2408 | ly_set_erase(&keys, NULL); |
| 2409 | return ret; |
| 2410 | } |
| 2411 | |
| 2412 | /** |
| 2413 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2414 | * |
| 2415 | * path-arg = absolute-path / relative-path |
| 2416 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2417 | * relative-path = 1*(".." "/") descendant-path |
| 2418 | * |
| 2419 | * @param[in,out] path Path to parse. |
| 2420 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2421 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2422 | * @param[out] name Name of the token. |
| 2423 | * @param[out] nam_len Length of the name. |
| 2424 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2425 | * must not be changed between consecutive calls. -1 if the |
| 2426 | * path is absolute. |
| 2427 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2428 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2429 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2430 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2431 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2432 | int *parent_times, int *has_predicate) |
| 2433 | { |
| 2434 | int par_times = 0; |
| 2435 | |
| 2436 | assert(path && *path); |
| 2437 | assert(parent_times); |
| 2438 | assert(prefix); |
| 2439 | assert(prefix_len); |
| 2440 | assert(name); |
| 2441 | assert(name_len); |
| 2442 | assert(has_predicate); |
| 2443 | |
| 2444 | *prefix = NULL; |
| 2445 | *prefix_len = 0; |
| 2446 | *name = NULL; |
| 2447 | *name_len = 0; |
| 2448 | *has_predicate = 0; |
| 2449 | |
| 2450 | if (!*parent_times) { |
| 2451 | if (!strncmp(*path, "..", 2)) { |
| 2452 | *path += 2; |
| 2453 | ++par_times; |
| 2454 | while (!strncmp(*path, "/..", 3)) { |
| 2455 | *path += 3; |
| 2456 | ++par_times; |
| 2457 | } |
| 2458 | } |
| 2459 | if (par_times) { |
| 2460 | *parent_times = par_times; |
| 2461 | } else { |
| 2462 | *parent_times = -1; |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | if (**path != '/') { |
| 2467 | return LY_EINVAL; |
| 2468 | } |
| 2469 | /* skip '/' */ |
| 2470 | ++(*path); |
| 2471 | |
| 2472 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2473 | 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] | 2474 | |
| 2475 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2476 | /* path continues by another token or this is the last token */ |
| 2477 | return LY_SUCCESS; |
| 2478 | } else if ((*path)[0] != '[') { |
| 2479 | /* unexpected character */ |
| 2480 | return LY_EINVAL; |
| 2481 | } else { |
| 2482 | /* predicate starting with [ */ |
| 2483 | *has_predicate = 1; |
| 2484 | return LY_SUCCESS; |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2489 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2490 | * |
| 2491 | * The set of features used for target must be a subset of features used for the leafref. |
| 2492 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2493 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2494 | * |
| 2495 | * @param[in] refnode The leafref node. |
| 2496 | * @param[in] target Tha target node of the leafref. |
| 2497 | * @return LY_SUCCESS or LY_EVALID; |
| 2498 | */ |
| 2499 | static LY_ERR |
| 2500 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2501 | { |
| 2502 | LY_ERR ret = LY_EVALID; |
| 2503 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2504 | unsigned int u, v, count; |
| 2505 | struct ly_set features = {0}; |
| 2506 | |
| 2507 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2508 | if (iter->iffeatures) { |
| 2509 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2510 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2511 | 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] | 2512 | } |
| 2513 | } |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2518 | * So when adding features applicable to the target into the features set, we should not be |
| 2519 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2520 | * to the leafref itself. */ |
| 2521 | count = features.count; |
| 2522 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2523 | if (iter->iffeatures) { |
| 2524 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2525 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2526 | 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] | 2527 | /* new feature was added (or LY_EMEM) */ |
| 2528 | goto cleanup; |
| 2529 | } |
| 2530 | } |
| 2531 | } |
| 2532 | } |
| 2533 | } |
| 2534 | ret = LY_SUCCESS; |
| 2535 | |
| 2536 | cleanup: |
| 2537 | ly_set_erase(&features, NULL); |
| 2538 | return ret; |
| 2539 | } |
| 2540 | |
| 2541 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2542 | * @brief Validate the leafref path. |
| 2543 | * @param[in] ctx Compile context |
| 2544 | * @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] | 2545 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2546 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2547 | */ |
| 2548 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2549 | lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2550 | { |
| 2551 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2552 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2553 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2554 | const char *id, *prefix, *name; |
| 2555 | size_t prefix_len, name_len; |
| 2556 | int parent_times = 0, has_predicate; |
| 2557 | unsigned int iter, u; |
| 2558 | LY_ERR ret = LY_SUCCESS; |
| 2559 | |
| 2560 | assert(ctx); |
| 2561 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2562 | assert(leafref); |
| 2563 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2564 | ctx->path[0] = '\0'; |
| 2565 | lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 2566 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2567 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2568 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2569 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2570 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2571 | if (!iter) { /* first iteration */ |
| 2572 | /* precess ".." in relative paths */ |
| 2573 | if (parent_times > 0) { |
| 2574 | /* move from the context node */ |
| 2575 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2576 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2577 | * all schema nodes that cannot be instantiated in data tree */ |
| 2578 | 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] | 2579 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2580 | } |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2585 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2586 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2587 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2588 | } |
| 2589 | if (!mod) { |
| 2590 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2591 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2592 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2593 | return LY_EVALID; |
| 2594 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2595 | if (!mod->implemented) { |
| 2596 | /* make the module implemented */ |
| 2597 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2598 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2599 | |
| 2600 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2601 | if (!node) { |
| 2602 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2603 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2604 | return LY_EVALID; |
| 2605 | } |
| 2606 | parent = node; |
| 2607 | |
| 2608 | if (has_predicate) { |
| 2609 | /* we have predicate, so the current result must be list */ |
| 2610 | if (node->nodetype != LYS_LIST) { |
| 2611 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2612 | "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] | 2613 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2614 | return LY_EVALID; |
| 2615 | } |
| 2616 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2617 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2618 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | ++iter; |
| 2622 | } |
| 2623 | if (ret) { |
| 2624 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2625 | "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] | 2626 | return LY_EVALID; |
| 2627 | } |
| 2628 | |
| 2629 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2630 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2631 | "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] | 2632 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2633 | return LY_EVALID; |
| 2634 | } |
| 2635 | |
| 2636 | /* check status */ |
| 2637 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2638 | return LY_EVALID; |
| 2639 | } |
| 2640 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2641 | /* check config */ |
| 2642 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2643 | if (node->flags & LYS_CONFIG_R) { |
| 2644 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2645 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2646 | leafref->path); |
| 2647 | return LY_EVALID; |
| 2648 | } |
| 2649 | } |
| 2650 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2651 | /* store the target's type and check for circular chain of leafrefs */ |
| 2652 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2653 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2654 | if (type == (struct lysc_type*)leafref) { |
| 2655 | /* circular chain detected */ |
| 2656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2657 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2658 | return LY_EVALID; |
| 2659 | } |
| 2660 | } |
| 2661 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2662 | /* check if leafref and its target are under common if-features */ |
| 2663 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2664 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2665 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2666 | leafref->path); |
| 2667 | return LY_EVALID; |
| 2668 | } |
| 2669 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2670 | ctx->path_len = 1; |
| 2671 | ctx->path[1] = '\0'; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2672 | return LY_SUCCESS; |
| 2673 | } |
| 2674 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2675 | 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] | 2676 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2677 | /** |
| 2678 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2679 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2680 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2681 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2682 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2683 | * @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] | 2684 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2685 | * @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] | 2686 | * @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] | 2687 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2688 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2689 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2690 | * @return LY_ERR value. |
| 2691 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2692 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2693 | 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] | 2694 | 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] | 2695 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2696 | { |
| 2697 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2698 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2699 | struct lysc_type_bin *bin; |
| 2700 | struct lysc_type_num *num; |
| 2701 | struct lysc_type_str *str; |
| 2702 | struct lysc_type_bits *bits; |
| 2703 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2704 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2705 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2706 | struct lysc_type_union *un, *un_aux; |
| 2707 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2708 | |
| 2709 | switch (basetype) { |
| 2710 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2711 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2712 | |
| 2713 | /* 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] | 2714 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2715 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2716 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2717 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2718 | 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] | 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | if (tpdfname) { |
| 2723 | type_p->compiled = *type; |
| 2724 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2725 | } |
| 2726 | break; |
| 2727 | case LY_TYPE_BITS: |
| 2728 | /* RFC 7950 9.7 - bits */ |
| 2729 | bits = (struct lysc_type_bits*)(*type); |
| 2730 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2731 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2732 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2733 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2734 | } |
| 2735 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2736 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2737 | /* 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] | 2738 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2739 | 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] | 2740 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2741 | 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] | 2742 | free(*type); |
| 2743 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2744 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2745 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | if (tpdfname) { |
| 2749 | type_p->compiled = *type; |
| 2750 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2751 | } |
| 2752 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2753 | case LY_TYPE_DEC64: |
| 2754 | dec = (struct lysc_type_dec*)(*type); |
| 2755 | |
| 2756 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2757 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2758 | if (!type_p->fraction_digits) { |
| 2759 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2760 | 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] | 2761 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2762 | 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] | 2763 | free(*type); |
| 2764 | *type = NULL; |
| 2765 | } |
| 2766 | return LY_EVALID; |
| 2767 | } |
| 2768 | } else if (type_p->fraction_digits) { |
| 2769 | /* 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] | 2770 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2771 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2772 | "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] | 2773 | tpdfname); |
| 2774 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2775 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2776 | "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] | 2777 | free(*type); |
| 2778 | *type = NULL; |
| 2779 | } |
| 2780 | return LY_EVALID; |
| 2781 | } |
| 2782 | dec->fraction_digits = type_p->fraction_digits; |
| 2783 | |
| 2784 | /* RFC 7950 9.2.4 - range */ |
| 2785 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2786 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2787 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2788 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2789 | 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] | 2790 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | if (tpdfname) { |
| 2794 | type_p->compiled = *type; |
| 2795 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2796 | } |
| 2797 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2798 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2799 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2800 | |
| 2801 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2802 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2803 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2804 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2805 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2806 | 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] | 2807 | } |
| 2808 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2809 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2810 | } |
| 2811 | |
| 2812 | /* RFC 7950 9.4.5 - pattern */ |
| 2813 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2814 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2815 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2816 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2817 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2818 | } |
| 2819 | |
| 2820 | if (tpdfname) { |
| 2821 | type_p->compiled = *type; |
| 2822 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2823 | } |
| 2824 | break; |
| 2825 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2826 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2827 | |
| 2828 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2829 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2830 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2831 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2832 | } |
| 2833 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2834 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2835 | /* 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] | 2836 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2837 | 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] | 2838 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2839 | 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] | 2840 | free(*type); |
| 2841 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2842 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2843 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | if (tpdfname) { |
| 2847 | type_p->compiled = *type; |
| 2848 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2849 | } |
| 2850 | break; |
| 2851 | case LY_TYPE_INT8: |
| 2852 | case LY_TYPE_UINT8: |
| 2853 | case LY_TYPE_INT16: |
| 2854 | case LY_TYPE_UINT16: |
| 2855 | case LY_TYPE_INT32: |
| 2856 | case LY_TYPE_UINT32: |
| 2857 | case LY_TYPE_INT64: |
| 2858 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2859 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2860 | |
| 2861 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2862 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2863 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2864 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2865 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2866 | 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] | 2867 | } |
| 2868 | } |
| 2869 | |
| 2870 | if (tpdfname) { |
| 2871 | type_p->compiled = *type; |
| 2872 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2873 | } |
| 2874 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2875 | case LY_TYPE_IDENT: |
| 2876 | idref = (struct lysc_type_identityref*)(*type); |
| 2877 | |
| 2878 | /* RFC 7950 9.10.2 - base */ |
| 2879 | if (type_p->bases) { |
| 2880 | if (base) { |
| 2881 | /* only the directly derived identityrefs can contain base specification */ |
| 2882 | if (tpdfname) { |
| 2883 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2884 | "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] | 2885 | tpdfname); |
| 2886 | } else { |
| 2887 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2888 | "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] | 2889 | free(*type); |
| 2890 | *type = NULL; |
| 2891 | } |
| 2892 | return LY_EVALID; |
| 2893 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2894 | 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] | 2895 | } |
| 2896 | |
| 2897 | if (!base && !type_p->flags) { |
| 2898 | /* type derived from identityref built-in type must contain at least one base */ |
| 2899 | if (tpdfname) { |
| 2900 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2901 | } else { |
| 2902 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2903 | free(*type); |
| 2904 | *type = NULL; |
| 2905 | } |
| 2906 | return LY_EVALID; |
| 2907 | } |
| 2908 | |
| 2909 | if (tpdfname) { |
| 2910 | type_p->compiled = *type; |
| 2911 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2912 | } |
| 2913 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2914 | case LY_TYPE_LEAFREF: |
| 2915 | /* RFC 7950 9.9.3 - require-instance */ |
| 2916 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2917 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2918 | if (tpdfname) { |
| 2919 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2920 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2921 | } else { |
| 2922 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2923 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2924 | free(*type); |
| 2925 | *type = NULL; |
| 2926 | } |
| 2927 | return LY_EVALID; |
| 2928 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2929 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2930 | } else if (base) { |
| 2931 | /* inherit */ |
| 2932 | ((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] | 2933 | } else { |
| 2934 | /* default is true */ |
| 2935 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2936 | } |
| 2937 | if (type_p->path) { |
| 2938 | 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] | 2939 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2940 | } else if (base) { |
| 2941 | 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] | 2942 | ((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] | 2943 | } else if (tpdfname) { |
| 2944 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2945 | return LY_EVALID; |
| 2946 | } else { |
| 2947 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2948 | free(*type); |
| 2949 | *type = NULL; |
| 2950 | return LY_EVALID; |
| 2951 | } |
| 2952 | if (tpdfname) { |
| 2953 | type_p->compiled = *type; |
| 2954 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2955 | } |
| 2956 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2957 | case LY_TYPE_INST: |
| 2958 | /* RFC 7950 9.9.3 - require-instance */ |
| 2959 | if (type_p->flags & LYS_SET_REQINST) { |
| 2960 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2961 | } else { |
| 2962 | /* default is true */ |
| 2963 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2964 | } |
| 2965 | |
| 2966 | if (tpdfname) { |
| 2967 | type_p->compiled = *type; |
| 2968 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2969 | } |
| 2970 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2971 | case LY_TYPE_UNION: |
| 2972 | un = (struct lysc_type_union*)(*type); |
| 2973 | |
| 2974 | /* RFC 7950 7.4 - type */ |
| 2975 | if (type_p->types) { |
| 2976 | if (base) { |
| 2977 | /* only the directly derived union can contain types specification */ |
| 2978 | if (tpdfname) { |
| 2979 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2980 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2981 | tpdfname); |
| 2982 | } else { |
| 2983 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2984 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2985 | free(*type); |
| 2986 | *type = NULL; |
| 2987 | } |
| 2988 | return LY_EVALID; |
| 2989 | } |
| 2990 | /* compile the type */ |
| 2991 | additional = 0; |
| 2992 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 2993 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2994 | 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] | 2995 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2996 | /* add space for additional types from the union subtype */ |
| 2997 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 2998 | 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))); |
| 2999 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3000 | un->types = (void*)((uint32_t*)(p) + 1); |
| 3001 | |
| 3002 | /* copy subtypes of the subtype union */ |
| 3003 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 3004 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 3005 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 3006 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3007 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3008 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 3009 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 3010 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 3011 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 3012 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 3013 | /* TODO extensions */ |
| 3014 | |
| 3015 | } else { |
| 3016 | un->types[u + additional] = un_aux->types[v]; |
| 3017 | ++un_aux->types[v]->refcount; |
| 3018 | } |
| 3019 | ++additional; |
| 3020 | LY_ARRAY_INCREMENT(un->types); |
| 3021 | } |
| 3022 | /* compensate u increment in main loop */ |
| 3023 | --additional; |
| 3024 | |
| 3025 | /* free the replaced union subtype */ |
| 3026 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 3027 | } else { |
| 3028 | LY_ARRAY_INCREMENT(un->types); |
| 3029 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | if (!base && !type_p->flags) { |
| 3034 | /* type derived from union built-in type must contain at least one type */ |
| 3035 | if (tpdfname) { |
| 3036 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 3037 | } else { |
| 3038 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 3039 | free(*type); |
| 3040 | *type = NULL; |
| 3041 | } |
| 3042 | return LY_EVALID; |
| 3043 | } |
| 3044 | |
| 3045 | if (tpdfname) { |
| 3046 | type_p->compiled = *type; |
| 3047 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3048 | } |
| 3049 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3050 | case LY_TYPE_BOOL: |
| 3051 | case LY_TYPE_EMPTY: |
| 3052 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 3053 | break; |
| 3054 | } |
| 3055 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 3056 | done: |
| 3057 | return ret; |
| 3058 | } |
| 3059 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3060 | /** |
| 3061 | * @brief Compile information about the leaf/leaf-list's type. |
| 3062 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3063 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 3064 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3065 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3066 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 3067 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3068 | * @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] | 3069 | * @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] | 3070 | * @return LY_ERR value. |
| 3071 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3072 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3073 | 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] | 3074 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3075 | { |
| 3076 | LY_ERR ret = LY_SUCCESS; |
| 3077 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3078 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3079 | struct type_context { |
| 3080 | const struct lysp_tpdf *tpdf; |
| 3081 | struct lysp_node *node; |
| 3082 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3083 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3084 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3085 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3086 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3087 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3088 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3089 | |
| 3090 | (*type) = NULL; |
| 3091 | |
| 3092 | tctx = calloc(1, sizeof *tctx); |
| 3093 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3094 | 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] | 3095 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 3096 | ret == LY_SUCCESS; |
| 3097 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 3098 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 3099 | if (basetype) { |
| 3100 | break; |
| 3101 | } |
| 3102 | |
| 3103 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3104 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3105 | 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] | 3106 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3107 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3108 | if (units && !*units) { |
| 3109 | /* inherit units */ |
| 3110 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3111 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3112 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3113 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3114 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3115 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3116 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3117 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3118 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3119 | break; |
| 3120 | } |
| 3121 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3122 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3123 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3124 | * 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] | 3125 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3126 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3127 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3128 | dummyloops = 1; |
| 3129 | goto preparenext; |
| 3130 | } else { |
| 3131 | tctx = NULL; |
| 3132 | break; |
| 3133 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3134 | } |
| 3135 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3136 | /* circular typedef reference detection */ |
| 3137 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3138 | /* local part */ |
| 3139 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3140 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3141 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3142 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3143 | free(tctx); |
| 3144 | ret = LY_EVALID; |
| 3145 | goto cleanup; |
| 3146 | } |
| 3147 | } |
| 3148 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3149 | /* global part for unions corner case */ |
| 3150 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3151 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3152 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3153 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3154 | free(tctx); |
| 3155 | ret = LY_EVALID; |
| 3156 | goto cleanup; |
| 3157 | } |
| 3158 | } |
| 3159 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3160 | /* store information for the following processing */ |
| 3161 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3162 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3163 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3164 | /* prepare next loop */ |
| 3165 | tctx_prev = tctx; |
| 3166 | tctx = calloc(1, sizeof *tctx); |
| 3167 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3168 | } |
| 3169 | free(tctx); |
| 3170 | |
| 3171 | /* allocate type according to the basetype */ |
| 3172 | switch (basetype) { |
| 3173 | case LY_TYPE_BINARY: |
| 3174 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3175 | break; |
| 3176 | case LY_TYPE_BITS: |
| 3177 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3178 | break; |
| 3179 | case LY_TYPE_BOOL: |
| 3180 | case LY_TYPE_EMPTY: |
| 3181 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3182 | break; |
| 3183 | case LY_TYPE_DEC64: |
| 3184 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3185 | break; |
| 3186 | case LY_TYPE_ENUM: |
| 3187 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3188 | break; |
| 3189 | case LY_TYPE_IDENT: |
| 3190 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3191 | break; |
| 3192 | case LY_TYPE_INST: |
| 3193 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3194 | break; |
| 3195 | case LY_TYPE_LEAFREF: |
| 3196 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3197 | break; |
| 3198 | case LY_TYPE_STRING: |
| 3199 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3200 | break; |
| 3201 | case LY_TYPE_UNION: |
| 3202 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3203 | break; |
| 3204 | case LY_TYPE_INT8: |
| 3205 | case LY_TYPE_UINT8: |
| 3206 | case LY_TYPE_INT16: |
| 3207 | case LY_TYPE_UINT16: |
| 3208 | case LY_TYPE_INT32: |
| 3209 | case LY_TYPE_UINT32: |
| 3210 | case LY_TYPE_INT64: |
| 3211 | case LY_TYPE_UINT64: |
| 3212 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3213 | break; |
| 3214 | case LY_TYPE_UNKNOWN: |
| 3215 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3216 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3217 | ret = LY_EVALID; |
| 3218 | goto cleanup; |
| 3219 | } |
| 3220 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3221 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3222 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3223 | ly_data_type2str[basetype]); |
| 3224 | free(*type); |
| 3225 | (*type) = NULL; |
| 3226 | ret = LY_EVALID; |
| 3227 | goto cleanup; |
| 3228 | } |
| 3229 | |
| 3230 | /* get restrictions from the referred typedefs */ |
| 3231 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3232 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3233 | |
| 3234 | /* remember the typedef context for circular check */ |
| 3235 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3236 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3237 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3238 | base = tctx->tpdf->type.compiled; |
| 3239 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3240 | } 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] | 3241 | /* no change, just use the type information from the base */ |
| 3242 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3243 | ++base->refcount; |
| 3244 | continue; |
| 3245 | } |
| 3246 | |
| 3247 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3248 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3249 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3250 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3251 | ret = LY_EVALID; |
| 3252 | goto cleanup; |
| 3253 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3254 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3255 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3256 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3257 | ret = LY_EVALID; |
| 3258 | goto cleanup; |
| 3259 | } |
| 3260 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3261 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3262 | /* TODO user type plugins */ |
| 3263 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3264 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3265 | 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] | 3266 | 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] | 3267 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3268 | LY_CHECK_GOTO(ret, cleanup); |
| 3269 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3270 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3271 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3272 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3273 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3274 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3275 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3276 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3277 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3278 | /* TODO user type plugins */ |
| 3279 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3280 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3281 | 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] | 3282 | LY_CHECK_GOTO(ret, cleanup); |
| 3283 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3284 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3285 | free(*type); |
| 3286 | (*type) = base; |
| 3287 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3288 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3289 | if (dflt && !(*type)->dflt) { |
| 3290 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3291 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3292 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3293 | (*type)->dflt->realtype = (*type); |
| 3294 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3295 | 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] | 3296 | 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] | 3297 | if (err) { |
| 3298 | ly_err_print(err); |
| 3299 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3300 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3301 | ly_err_free(err); |
| 3302 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3303 | if (ret == LY_EINCOMPLETE) { |
| 3304 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3305 | 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] | 3306 | |
| 3307 | /* but in general result is so far ok */ |
| 3308 | ret = LY_SUCCESS; |
| 3309 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3310 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3311 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3312 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3313 | 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] | 3314 | |
| 3315 | cleanup: |
| 3316 | ly_set_erase(&tpdf_chain, free); |
| 3317 | return ret; |
| 3318 | } |
| 3319 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3320 | /** |
| 3321 | * @brief Compile status information of the given node. |
| 3322 | * |
| 3323 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3324 | * has the status correctly set during the compilation. |
| 3325 | * |
| 3326 | * @param[in] ctx Compile context |
| 3327 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3328 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3329 | * the compatibility with the parent's status value. |
| 3330 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3331 | * @return LY_ERR value. |
| 3332 | */ |
| 3333 | static LY_ERR |
| 3334 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3335 | { |
| 3336 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3337 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3338 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3339 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3340 | if ((parent_flags & 0x3) != 0x3) { |
| 3341 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3342 | * combination of bits (0x3) which marks the uses_status value */ |
| 3343 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3344 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3345 | } |
| 3346 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3347 | } else { |
| 3348 | (*node_flags) |= LYS_STATUS_CURR; |
| 3349 | } |
| 3350 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3351 | /* check status compatibility with the parent */ |
| 3352 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3353 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3354 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3355 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3356 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3357 | } else { /* LYS_STATUS_DEPRC */ |
| 3358 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3359 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3360 | } |
| 3361 | return LY_EVALID; |
| 3362 | } |
| 3363 | } |
| 3364 | return LY_SUCCESS; |
| 3365 | } |
| 3366 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3367 | /** |
| 3368 | * @brief Check uniqness of the node/action/notification name. |
| 3369 | * |
| 3370 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3371 | * structures, but they share the namespace so we need to check their name collisions. |
| 3372 | * |
| 3373 | * @param[in] ctx Compile context. |
| 3374 | * @param[in] children List (linked list) of data nodes to go through. |
| 3375 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3376 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3377 | * @param[in] name Name of the item to find in the given lists. |
| 3378 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3379 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3380 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3381 | */ |
| 3382 | static LY_ERR |
| 3383 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3384 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3385 | const char *name, void *exclude) |
| 3386 | { |
| 3387 | const struct lysc_node *iter; |
| 3388 | unsigned int u; |
| 3389 | |
| 3390 | LY_LIST_FOR(children, iter) { |
| 3391 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3392 | goto error; |
| 3393 | } |
| 3394 | } |
| 3395 | LY_ARRAY_FOR(actions, u) { |
| 3396 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3397 | goto error; |
| 3398 | } |
| 3399 | } |
| 3400 | LY_ARRAY_FOR(notifs, u) { |
| 3401 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3402 | goto error; |
| 3403 | } |
| 3404 | } |
| 3405 | return LY_SUCCESS; |
| 3406 | error: |
| 3407 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification"); |
| 3408 | return LY_EEXIST; |
| 3409 | } |
| 3410 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3411 | 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] | 3412 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3413 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3414 | * @brief Compile parsed RPC/action schema node information. |
| 3415 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3416 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3417 | * @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] | 3418 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3419 | * @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). |
| 3420 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3421 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3422 | */ |
| 3423 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3424 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3425 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3426 | { |
| 3427 | LY_ERR ret = LY_SUCCESS; |
| 3428 | struct lysp_node *child_p; |
| 3429 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3430 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3431 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3432 | lysc_update_path(ctx, parent, action_p->name); |
| 3433 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3434 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3435 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3436 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3437 | action_p->name, action)) { |
| 3438 | return LY_EVALID; |
| 3439 | } |
| 3440 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3441 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3442 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3443 | "Action \"%s\" is placed inside %s.", action_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3444 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3445 | return LY_EVALID; |
| 3446 | } |
| 3447 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3448 | action->nodetype = LYS_ACTION; |
| 3449 | action->module = ctx->mod; |
| 3450 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3451 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3452 | action->sp = action_p; |
| 3453 | } |
| 3454 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3455 | |
| 3456 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3457 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3458 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3459 | |
| 3460 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3461 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3462 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3463 | 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] | 3464 | 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] | 3465 | |
| 3466 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3467 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3468 | 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] | 3469 | 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] | 3470 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3471 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3472 | 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] | 3473 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3474 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3475 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3476 | |
| 3477 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3478 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3479 | 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] | 3480 | 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] | 3481 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3482 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3483 | 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] | 3484 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3485 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3486 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3487 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3488 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3489 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3490 | return ret; |
| 3491 | } |
| 3492 | |
| 3493 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3494 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3495 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3496 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3497 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3498 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3499 | * @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] | 3500 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3501 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3502 | */ |
| 3503 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3504 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3505 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3506 | { |
| 3507 | LY_ERR ret = LY_SUCCESS; |
| 3508 | struct lysp_node *child_p; |
| 3509 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3510 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3511 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3512 | lysc_update_path(ctx, parent, notif_p->name); |
| 3513 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3514 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3515 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3516 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3517 | notif_p->name, notif)) { |
| 3518 | return LY_EVALID; |
| 3519 | } |
| 3520 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3521 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3522 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3523 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3524 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3525 | return LY_EVALID; |
| 3526 | } |
| 3527 | |
| 3528 | notif->nodetype = LYS_NOTIF; |
| 3529 | notif->module = ctx->mod; |
| 3530 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3531 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3532 | notif->sp = notif_p; |
| 3533 | } |
| 3534 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3535 | |
| 3536 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3537 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3538 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3539 | |
| 3540 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3541 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3542 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3543 | COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3544 | COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3545 | 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] | 3546 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3547 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3548 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3549 | 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] | 3550 | } |
| 3551 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3552 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3553 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3554 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3555 | return ret; |
| 3556 | } |
| 3557 | |
| 3558 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3559 | * @brief Compile parsed container node information. |
| 3560 | * @param[in] ctx Compile context |
| 3561 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3562 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3563 | * is enriched with the container-specific information. |
| 3564 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3565 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3566 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3567 | 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] | 3568 | { |
| 3569 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3570 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3571 | struct lysp_node *child_p; |
| 3572 | unsigned int u; |
| 3573 | LY_ERR ret = LY_SUCCESS; |
| 3574 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3575 | if (cont_p->presence) { |
| 3576 | cont->flags |= LYS_PRESENCE; |
| 3577 | } |
| 3578 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3579 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3580 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3581 | } |
| 3582 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3583 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done); |
| 3584 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3585 | 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] | 3586 | |
| 3587 | done: |
| 3588 | return ret; |
| 3589 | } |
| 3590 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3591 | /* |
| 3592 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3593 | * @param[in] ctx Compile context. |
| 3594 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3595 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3596 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3597 | * @return LY_ERR value. |
| 3598 | */ |
| 3599 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3600 | 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] | 3601 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3602 | unsigned int u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3603 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3604 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3605 | 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] | 3606 | leaf->units ? NULL : &leaf->units)); |
| 3607 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3608 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3609 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3610 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3611 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3612 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3613 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3614 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3615 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3616 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3617 | } |
| 3618 | } else { |
| 3619 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3620 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3621 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3622 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3623 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3624 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3625 | } |
| 3626 | } |
| 3627 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3628 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3629 | ly_set_add(&ctx->unres, leaf, 0); |
| 3630 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3631 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3632 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3633 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3634 | ly_set_add(&ctx->unres, leaf, 0); |
| 3635 | } |
| 3636 | } |
| 3637 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3638 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3639 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3640 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3641 | return LY_EVALID; |
| 3642 | } |
| 3643 | } |
| 3644 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3645 | return LY_SUCCESS; |
| 3646 | } |
| 3647 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3648 | /** |
| 3649 | * @brief Compile parsed leaf node information. |
| 3650 | * @param[in] ctx Compile context |
| 3651 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3652 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3653 | * is enriched with the leaf-specific information. |
| 3654 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3655 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3656 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3657 | 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] | 3658 | { |
| 3659 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3660 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3661 | unsigned int u; |
| 3662 | LY_ERR ret = LY_SUCCESS; |
| 3663 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3664 | COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3665 | if (leaf_p->units) { |
| 3666 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3667 | leaf->flags |= LYS_SET_UNITS; |
| 3668 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3669 | |
| 3670 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3671 | leaf->dflt = (void*)leaf_p->dflt; |
| 3672 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame^] | 3673 | leaf->dflt = NULL; |
| 3674 | LY_CHECK_RET(ret); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3675 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3676 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3677 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3678 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3679 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3680 | leaf->dflt->realtype = leaf->type; |
| 3681 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3682 | 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] | 3683 | 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] | 3684 | leaf->dflt->realtype->refcount++; |
| 3685 | if (err) { |
| 3686 | ly_err_print(err); |
| 3687 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3688 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3689 | ly_err_free(err); |
| 3690 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3691 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3692 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame^] | 3693 | 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] | 3694 | |
| 3695 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3696 | ret = LY_SUCCESS; |
| 3697 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame^] | 3698 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3699 | leaf->flags |= LYS_SET_DFLT; |
| 3700 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3701 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3702 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3703 | done: |
| 3704 | return ret; |
| 3705 | } |
| 3706 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3707 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3708 | * @brief Compile parsed leaf-list node information. |
| 3709 | * @param[in] ctx Compile context |
| 3710 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3711 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3712 | * is enriched with the leaf-list-specific information. |
| 3713 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3714 | */ |
| 3715 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3716 | 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] | 3717 | { |
| 3718 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3719 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3720 | unsigned int u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3721 | LY_ERR ret = LY_SUCCESS; |
| 3722 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3723 | COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3724 | if (llist_p->units) { |
| 3725 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3726 | llist->flags |= LYS_SET_UNITS; |
| 3727 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3728 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3729 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3730 | llist->dflts = (void*)llist_p->dflts; |
| 3731 | 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] | 3732 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3733 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3734 | 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] | 3735 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3736 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3737 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3738 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3739 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3740 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3741 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3742 | llist->dflts[u]->realtype = llist->type; |
| 3743 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3744 | 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] | 3745 | 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] | 3746 | llist->dflts[u]->realtype->refcount++; |
| 3747 | if (err) { |
| 3748 | ly_err_print(err); |
| 3749 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3750 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3751 | ly_err_free(err); |
| 3752 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3753 | if (ret == LY_EINCOMPLETE) { |
| 3754 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3755 | 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] | 3756 | |
| 3757 | /* but in general result is so far ok */ |
| 3758 | ret = LY_SUCCESS; |
| 3759 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3760 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3761 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3762 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3763 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3764 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3765 | /* configuration data values must be unique - so check the default values */ |
| 3766 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3767 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3768 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3769 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3770 | 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] | 3771 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3772 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3773 | if (dynamic) { |
| 3774 | free((char*)val); |
| 3775 | } |
| 3776 | return LY_EVALID; |
| 3777 | } |
| 3778 | } |
| 3779 | } |
| 3780 | } |
| 3781 | |
| 3782 | /* 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] | 3783 | |
| 3784 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3785 | if (llist->min) { |
| 3786 | llist->flags |= LYS_MAND_TRUE; |
| 3787 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3788 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3789 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3790 | done: |
| 3791 | return ret; |
| 3792 | } |
| 3793 | |
| 3794 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3795 | * @brief Compile information about list's uniques. |
| 3796 | * @param[in] ctx Compile context. |
| 3797 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3798 | * @param[in] uniques Sized array list of unique statements. |
| 3799 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3800 | * @return LY_ERR value. |
| 3801 | */ |
| 3802 | static LY_ERR |
| 3803 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3804 | { |
| 3805 | LY_ERR ret = LY_SUCCESS; |
| 3806 | struct lysc_node_leaf **key, ***unique; |
| 3807 | const char *keystr, *delim; |
| 3808 | size_t len; |
| 3809 | unsigned int v; |
| 3810 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3811 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3812 | |
| 3813 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3814 | config = -1; |
| 3815 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3816 | keystr = uniques[v]; |
| 3817 | while (keystr) { |
| 3818 | delim = strpbrk(keystr, " \t\n"); |
| 3819 | if (delim) { |
| 3820 | len = delim - keystr; |
| 3821 | while (isspace(*delim)) { |
| 3822 | ++delim; |
| 3823 | } |
| 3824 | } else { |
| 3825 | len = strlen(keystr); |
| 3826 | } |
| 3827 | |
| 3828 | /* unique node must be present */ |
| 3829 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3830 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3831 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3832 | if (ret != LY_SUCCESS) { |
| 3833 | if (ret == LY_EDENIED) { |
| 3834 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3835 | "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] | 3836 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3837 | } |
| 3838 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3839 | } else if (flags) { |
| 3840 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3841 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3842 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
| 3843 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3844 | } |
| 3845 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3846 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3847 | /* all referenced leafs must be of the same config type */ |
| 3848 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3849 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3850 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3851 | return LY_EVALID; |
| 3852 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3853 | config = 1; |
| 3854 | } else { /* LYS_CONFIG_R */ |
| 3855 | config = 0; |
| 3856 | } |
| 3857 | |
| 3858 | /* check status */ |
| 3859 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3860 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3861 | |
| 3862 | /* mark leaf as unique */ |
| 3863 | (*key)->flags |= LYS_UNIQUE; |
| 3864 | |
| 3865 | /* next unique value in line */ |
| 3866 | keystr = delim; |
| 3867 | } |
| 3868 | /* next unique definition */ |
| 3869 | } |
| 3870 | |
| 3871 | return LY_SUCCESS; |
| 3872 | } |
| 3873 | |
| 3874 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3875 | * @brief Compile parsed list node information. |
| 3876 | * @param[in] ctx Compile context |
| 3877 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3878 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3879 | * is enriched with the list-specific information. |
| 3880 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3881 | */ |
| 3882 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3883 | 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] | 3884 | { |
| 3885 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3886 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3887 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3888 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3889 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3890 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3891 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3892 | LY_ERR ret = LY_SUCCESS; |
| 3893 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3894 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3895 | if (list->min) { |
| 3896 | list->flags |= LYS_MAND_TRUE; |
| 3897 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3898 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3899 | |
| 3900 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3901 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3902 | } |
| 3903 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3904 | COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3905 | |
| 3906 | /* keys */ |
| 3907 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3908 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3909 | return LY_EVALID; |
| 3910 | } |
| 3911 | |
| 3912 | /* find all the keys (must be direct children) */ |
| 3913 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3914 | if (!keystr) { |
| 3915 | /* keyless list */ |
| 3916 | list->flags |= LYS_KEYLESS; |
| 3917 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3918 | while (keystr) { |
| 3919 | delim = strpbrk(keystr, " \t\n"); |
| 3920 | if (delim) { |
| 3921 | len = delim - keystr; |
| 3922 | while (isspace(*delim)) { |
| 3923 | ++delim; |
| 3924 | } |
| 3925 | } else { |
| 3926 | len = strlen(keystr); |
| 3927 | } |
| 3928 | |
| 3929 | /* key node must be present */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3930 | key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 3931 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3932 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3933 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3934 | return LY_EVALID; |
| 3935 | } |
| 3936 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3937 | if (key->flags & LYS_KEY) { |
| 3938 | /* the node was already marked as a key */ |
| 3939 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3940 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3941 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3942 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3943 | |
| 3944 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3945 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3946 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3947 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3948 | return LY_EVALID; |
| 3949 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3950 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3951 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3952 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3953 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3954 | "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] | 3955 | return LY_EVALID; |
| 3956 | } |
| 3957 | } else { |
| 3958 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3959 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3960 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3961 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3962 | return LY_EVALID; |
| 3963 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3964 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3965 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3966 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3967 | return LY_EVALID; |
| 3968 | } |
| 3969 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3970 | |
| 3971 | /* check status */ |
| 3972 | 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] | 3973 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3974 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3975 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3976 | if (key->dflt) { |
| 3977 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3978 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3979 | free(key->dflt); |
| 3980 | key->dflt = NULL; |
| 3981 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3982 | } |
| 3983 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3984 | key->flags |= LYS_KEY; |
| 3985 | |
| 3986 | /* move it to the correct position */ |
| 3987 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3988 | /* fix links in closest previous siblings of the key */ |
| 3989 | if (key->next) { |
| 3990 | key->next->prev = key->prev; |
| 3991 | } else { |
| 3992 | /* last child */ |
| 3993 | list->child->prev = key->prev; |
| 3994 | } |
| 3995 | if (key->prev->next) { |
| 3996 | key->prev->next = key->next; |
| 3997 | } |
| 3998 | /* fix links in the key */ |
| 3999 | if (prev_key) { |
| 4000 | key->prev = (struct lysc_node*)prev_key; |
| 4001 | key->next = prev_key->next; |
| 4002 | } else { |
| 4003 | key->prev = list->child->prev; |
| 4004 | key->next = list->child; |
| 4005 | } |
| 4006 | /* fix links in closes future siblings of the key */ |
| 4007 | if (prev_key) { |
| 4008 | if (prev_key->next) { |
| 4009 | prev_key->next->prev = (struct lysc_node*)key; |
| 4010 | } else { |
| 4011 | list->child->prev = (struct lysc_node*)key; |
| 4012 | } |
| 4013 | prev_key->next = (struct lysc_node*)key; |
| 4014 | } else { |
| 4015 | list->child->prev = (struct lysc_node*)key; |
| 4016 | } |
| 4017 | /* fix links in parent */ |
| 4018 | if (!key->prev->next) { |
| 4019 | list->child = (struct lysc_node*)key; |
| 4020 | } |
| 4021 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4022 | |
| 4023 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4024 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4025 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4026 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | /* uniques */ |
| 4030 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4031 | 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] | 4032 | } |
| 4033 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4034 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 4035 | 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] | 4036 | |
| 4037 | done: |
| 4038 | return ret; |
| 4039 | } |
| 4040 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4041 | /** |
| 4042 | * @brief Do some checks and set the default choice's case. |
| 4043 | * |
| 4044 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 4045 | * |
| 4046 | * @param[in] ctx Compile context. |
| 4047 | * @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, |
| 4048 | * not the reference to the imported module. |
| 4049 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 4050 | * @return LY_ERR value. |
| 4051 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4052 | static LY_ERR |
| 4053 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 4054 | { |
| 4055 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 4056 | const char *prefix = NULL, *name; |
| 4057 | size_t prefix_len = 0; |
| 4058 | |
| 4059 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4060 | name = strchr(dflt, ':'); |
| 4061 | if (name) { |
| 4062 | prefix = dflt; |
| 4063 | prefix_len = name - prefix; |
| 4064 | ++name; |
| 4065 | } else { |
| 4066 | name = dflt; |
| 4067 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4068 | if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4069 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 4070 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4071 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 4072 | prefix_len, prefix); |
| 4073 | return LY_EVALID; |
| 4074 | } |
| 4075 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 4076 | if (!ch->dflt) { |
| 4077 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4078 | "Default case \"%s\" not found.", dflt); |
| 4079 | return LY_EVALID; |
| 4080 | } |
| 4081 | /* no mandatory nodes directly under the default case */ |
| 4082 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4083 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 4084 | break; |
| 4085 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4086 | if (iter->flags & LYS_MAND_TRUE) { |
| 4087 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4088 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 4089 | return LY_EVALID; |
| 4090 | } |
| 4091 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4092 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4093 | return LY_SUCCESS; |
| 4094 | } |
| 4095 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4096 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4097 | 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] | 4098 | { |
| 4099 | struct lys_module *mod; |
| 4100 | const char *prefix = NULL, *name; |
| 4101 | size_t prefix_len = 0; |
| 4102 | struct lysc_node_case *cs; |
| 4103 | struct lysc_node *node; |
| 4104 | |
| 4105 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4106 | name = strchr(dflt, ':'); |
| 4107 | if (name) { |
| 4108 | prefix = dflt; |
| 4109 | prefix_len = name - prefix; |
| 4110 | ++name; |
| 4111 | } else { |
| 4112 | name = dflt; |
| 4113 | } |
| 4114 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 4115 | if (prefix) { |
| 4116 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 4117 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4118 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 4119 | "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] | 4120 | return LY_EVALID; |
| 4121 | } |
| 4122 | } else { |
| 4123 | mod = ctx->mod; |
| 4124 | } |
| 4125 | /* get the default case */ |
| 4126 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 4127 | if (!cs) { |
| 4128 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4129 | "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] | 4130 | return LY_EVALID; |
| 4131 | } |
| 4132 | |
| 4133 | /* check that there is no mandatory node */ |
| 4134 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4135 | if (node->parent != (struct lysc_node*)cs) { |
| 4136 | break; |
| 4137 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4138 | if (node->flags & LYS_MAND_TRUE) { |
| 4139 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4140 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4141 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4142 | return LY_EVALID; |
| 4143 | } |
| 4144 | } |
| 4145 | |
| 4146 | /* set the default case in choice */ |
| 4147 | ch->dflt = cs; |
| 4148 | cs->flags |= LYS_SET_DFLT; |
| 4149 | |
| 4150 | return LY_SUCCESS; |
| 4151 | } |
| 4152 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4153 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4154 | * @brief Compile parsed choice node information. |
| 4155 | * @param[in] ctx Compile context |
| 4156 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4157 | * @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] | 4158 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4159 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4160 | */ |
| 4161 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4162 | 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] | 4163 | { |
| 4164 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4165 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4166 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4167 | LY_ERR ret = LY_SUCCESS; |
| 4168 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4169 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4170 | if (child_p->nodetype == LYS_CASE) { |
| 4171 | 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] | 4172 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4173 | } |
| 4174 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4175 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4180 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4181 | 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] | 4182 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4183 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4184 | return ret; |
| 4185 | } |
| 4186 | |
| 4187 | /** |
| 4188 | * @brief Compile parsed anydata or anyxml node information. |
| 4189 | * @param[in] ctx Compile context |
| 4190 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4191 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4192 | * is enriched with the any-specific information. |
| 4193 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4194 | */ |
| 4195 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4196 | 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] | 4197 | { |
| 4198 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4199 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4200 | unsigned int u; |
| 4201 | LY_ERR ret = LY_SUCCESS; |
| 4202 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4203 | COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4204 | |
| 4205 | if (any->flags & LYS_CONFIG_W) { |
| 4206 | 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] | 4207 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4208 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4209 | done: |
| 4210 | return ret; |
| 4211 | } |
| 4212 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4213 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4214 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4215 | * |
| 4216 | * @param[in] ctx Compile context |
| 4217 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4218 | * the choice itself is expected instead of a specific case node. |
| 4219 | * @param[in] node Schema node to connect into the list. |
| 4220 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 4221 | */ |
| 4222 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4223 | 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] | 4224 | { |
| 4225 | struct lysc_node **children; |
| 4226 | |
| 4227 | if (node->nodetype == LYS_CASE) { |
| 4228 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4229 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4230 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4231 | } |
| 4232 | if (children) { |
| 4233 | if (!(*children)) { |
| 4234 | /* first child */ |
| 4235 | *children = node; |
| 4236 | } else if (*children != node) { |
| 4237 | /* by the condition in previous branch we cover the choice/case children |
| 4238 | * - the children list is shared by the choice and the the first case, in addition |
| 4239 | * the first child of each case must be referenced from the case node. So the node is |
| 4240 | * actually always already inserted in case it is the first children - so here such |
| 4241 | * a situation actually corresponds to the first branch */ |
| 4242 | /* insert at the end of the parent's children list */ |
| 4243 | (*children)->prev->next = node; |
| 4244 | node->prev = (*children)->prev; |
| 4245 | (*children)->prev = node; |
| 4246 | |
| 4247 | /* check the name uniqueness */ |
| 4248 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4249 | lysc_node_notifs(parent), node->name, node)) { |
| 4250 | return LY_EEXIST; |
| 4251 | } |
| 4252 | } |
| 4253 | } |
| 4254 | return LY_SUCCESS; |
| 4255 | } |
| 4256 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4257 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4258 | * @brief Get the XPath context node for the given schema node. |
| 4259 | * @param[in] start The schema node where the XPath expression appears. |
| 4260 | * @return The context node to evaluate XPath expression in given schema node. |
| 4261 | * @return NULL in case the context node is the root node. |
| 4262 | */ |
| 4263 | static struct lysc_node * |
| 4264 | lysc_xpath_context(struct lysc_node *start) |
| 4265 | { |
| 4266 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 4267 | start = start->parent); |
| 4268 | return start; |
| 4269 | } |
| 4270 | |
| 4271 | /** |
| 4272 | * @brief Prepare the case structure in choice node for the new data node. |
| 4273 | * |
| 4274 | * 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 |
| 4275 | * created in the choice when the first child was processed. |
| 4276 | * |
| 4277 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4278 | * @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, |
| 4279 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4280 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4281 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4282 | * @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, |
| 4283 | * 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] | 4284 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4285 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4286 | 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] | 4287 | { |
| 4288 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4289 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4290 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4291 | unsigned int u; |
| 4292 | LY_ERR ret; |
| 4293 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4294 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4295 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4296 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4297 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4298 | return NULL; \ |
| 4299 | } \ |
| 4300 | } |
| 4301 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4302 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4303 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4304 | |
| 4305 | /* we have to add an implicit case node into the parent choice */ |
| 4306 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4307 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 4308 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4309 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4310 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4311 | /* the case is already present since the child is not its first children */ |
| 4312 | return (struct lysc_node_case*)ch->cases->prev; |
| 4313 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4314 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4315 | |
| 4316 | /* explicit parent case is not present (this is its first child) */ |
| 4317 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4318 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 4319 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4320 | cs->sp = node_p; |
| 4321 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4322 | /* 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] | 4323 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4324 | |
| 4325 | if (node_p->when) { |
| 4326 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4327 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4328 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4329 | (*when)->context = lysc_xpath_context(ch->parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4330 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4331 | 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] | 4332 | } else { |
| 4333 | LOGINT(ctx->ctx); |
| 4334 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4335 | } |
| 4336 | cs->module = ctx->mod; |
| 4337 | cs->prev = (struct lysc_node*)cs; |
| 4338 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4339 | 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] | 4340 | cs->parent = (struct lysc_node*)ch; |
| 4341 | cs->child = child; |
| 4342 | |
| 4343 | return cs; |
| 4344 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4345 | if (cs) { |
| 4346 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4347 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4348 | return NULL; |
| 4349 | |
| 4350 | #undef UNIQUE_CHECK |
| 4351 | } |
| 4352 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4353 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4354 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4355 | * |
| 4356 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4357 | * @param[in] node Target node where the config is supposed to be changed. |
| 4358 | * @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] | 4359 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4360 | * 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] | 4361 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4362 | * @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] | 4363 | * @return LY_ERR value. |
| 4364 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4365 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4366 | 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] | 4367 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4368 | { |
| 4369 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4370 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4371 | |
| 4372 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4373 | /* nothing to do */ |
| 4374 | return LY_SUCCESS; |
| 4375 | } |
| 4376 | |
| 4377 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4378 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4379 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4380 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4381 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4382 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4383 | return LY_EVALID; |
| 4384 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4385 | node->flags |= LYS_SET_CONFIG; |
| 4386 | } else { |
| 4387 | if (node->flags & LYS_SET_CONFIG) { |
| 4388 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4389 | /* setting config flags, but have node with explicit config true */ |
| 4390 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4391 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4392 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4393 | return LY_EVALID; |
| 4394 | } |
| 4395 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4396 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4397 | return LY_SUCCESS; |
| 4398 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4399 | } |
| 4400 | node->flags &= ~LYS_CONFIG_MASK; |
| 4401 | node->flags |= config; |
| 4402 | |
| 4403 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4404 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4405 | 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] | 4406 | } |
| 4407 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4408 | return LY_SUCCESS; |
| 4409 | } |
| 4410 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4411 | /** |
| 4412 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4413 | * |
| 4414 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4415 | * the flag to such parents from a mandatory children. |
| 4416 | * |
| 4417 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4418 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4419 | * (mandatory children was removed). |
| 4420 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4421 | void |
| 4422 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4423 | { |
| 4424 | struct lysc_node *iter; |
| 4425 | |
| 4426 | if (add) { /* set flag */ |
| 4427 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4428 | parent = parent->parent) { |
| 4429 | parent->flags |= LYS_MAND_TRUE; |
| 4430 | } |
| 4431 | } else { /* unset flag */ |
| 4432 | 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] | 4433 | 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] | 4434 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4435 | /* there is another mandatory node */ |
| 4436 | return; |
| 4437 | } |
| 4438 | } |
| 4439 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4440 | parent->flags &= ~LYS_MAND_TRUE; |
| 4441 | } |
| 4442 | } |
| 4443 | } |
| 4444 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4445 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4446 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4447 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4448 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4449 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4450 | */ |
| 4451 | static void |
| 4452 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4453 | { |
| 4454 | unsigned int v; |
| 4455 | size_t len; |
| 4456 | |
| 4457 | len = strlen(aug_p->nodeid); |
| 4458 | LY_ARRAY_FOR(result, v) { |
| 4459 | if (strlen(result[v]->nodeid) <= len) { |
| 4460 | continue; |
| 4461 | } |
| 4462 | if (v < LY_ARRAY_SIZE(result)) { |
| 4463 | /* move the rest of array */ |
| 4464 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4465 | break; |
| 4466 | } |
| 4467 | } |
| 4468 | result[v] = aug_p; |
| 4469 | LY_ARRAY_INCREMENT(result); |
| 4470 | } |
| 4471 | |
| 4472 | /** |
| 4473 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4474 | * |
| 4475 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4476 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4477 | * |
| 4478 | * @param[in] ctx Compile context. |
| 4479 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4480 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4481 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4482 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4483 | * @return LY_ERR value. |
| 4484 | */ |
| 4485 | LY_ERR |
| 4486 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4487 | { |
| 4488 | struct lysp_augment **result = NULL; |
| 4489 | unsigned int u, v; |
| 4490 | size_t count = 0; |
| 4491 | |
| 4492 | assert(augments); |
| 4493 | |
| 4494 | /* get count of the augments in module and all its submodules */ |
| 4495 | if (aug_p) { |
| 4496 | count += LY_ARRAY_SIZE(aug_p); |
| 4497 | } |
| 4498 | LY_ARRAY_FOR(inc_p, u) { |
| 4499 | if (inc_p[u].submodule->augments) { |
| 4500 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4501 | } |
| 4502 | } |
| 4503 | |
| 4504 | if (!count) { |
| 4505 | *augments = NULL; |
| 4506 | return LY_SUCCESS; |
| 4507 | } |
| 4508 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4509 | |
| 4510 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4511 | * together, so there can be even /z/y betwwen them. */ |
| 4512 | LY_ARRAY_FOR(aug_p, u) { |
| 4513 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4514 | } |
| 4515 | LY_ARRAY_FOR(inc_p, u) { |
| 4516 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4517 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4518 | } |
| 4519 | } |
| 4520 | |
| 4521 | *augments = result; |
| 4522 | return LY_SUCCESS; |
| 4523 | } |
| 4524 | |
| 4525 | /** |
| 4526 | * @brief Compile the parsed augment connecting it into its target. |
| 4527 | * |
| 4528 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4529 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4530 | * are already implemented and compiled. |
| 4531 | * |
| 4532 | * @param[in] ctx Compile context. |
| 4533 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4534 | * @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 |
| 4535 | * children in case of the augmenting uses data. |
| 4536 | * @return LY_SUCCESS on success. |
| 4537 | * @return LY_EVALID on failure. |
| 4538 | */ |
| 4539 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4540 | 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] | 4541 | { |
| 4542 | LY_ERR ret = LY_SUCCESS; |
| 4543 | struct lysp_node *node_p, *case_node_p; |
| 4544 | struct lysc_node *target; /* target target of the augment */ |
| 4545 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4546 | struct lysc_when **when, *when_shared; |
| 4547 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4548 | uint16_t flags = 0; |
| 4549 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4550 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4551 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4552 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4553 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4554 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4555 | 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] | 4556 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4557 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4558 | if (ret != LY_SUCCESS) { |
| 4559 | if (ret == LY_EDENIED) { |
| 4560 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4561 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4562 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4563 | } |
| 4564 | return LY_EVALID; |
| 4565 | } |
| 4566 | |
| 4567 | /* check for mandatory nodes |
| 4568 | * - new cases augmenting some choice can have mandatory nodes |
| 4569 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4570 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4571 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4572 | allow_mandatory = 1; |
| 4573 | } |
| 4574 | |
| 4575 | when_shared = NULL; |
| 4576 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4577 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4578 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4579 | && !((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] | 4580 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4581 | && !(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] | 4582 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4583 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4584 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4585 | return LY_EVALID; |
| 4586 | } |
| 4587 | |
| 4588 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4589 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4590 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4591 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4592 | } else { |
| 4593 | 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] | 4594 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4595 | } |
| 4596 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4597 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4598 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4599 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4600 | * 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] | 4601 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4602 | /* 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] | 4603 | 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] | 4604 | } else if (target->nodetype == LYS_CHOICE) { |
| 4605 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4606 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4607 | } else { |
| 4608 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4609 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4610 | } |
| 4611 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4612 | 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] | 4613 | node->flags &= ~LYS_MAND_TRUE; |
| 4614 | lys_compile_mandatory_parents(target, 0); |
| 4615 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4616 | "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] | 4617 | return LY_EVALID; |
| 4618 | } |
| 4619 | |
| 4620 | /* pass augment's when to all the children */ |
| 4621 | if (aug_p->when) { |
| 4622 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4623 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4624 | ret = lys_compile_when(ctx, aug_p->when, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4625 | LY_CHECK_GOTO(ret, error); |
| 4626 | (*when)->context = lysc_xpath_context(target); |
| 4627 | when_shared = *when; |
| 4628 | } else { |
| 4629 | ++when_shared->refcount; |
| 4630 | (*when) = when_shared; |
| 4631 | } |
| 4632 | } |
| 4633 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4634 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4635 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4636 | switch (target->nodetype) { |
| 4637 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4638 | 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] | 4639 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4640 | 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] | 4641 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4642 | break; |
| 4643 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4644 | 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] | 4645 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4646 | 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] | 4647 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4648 | break; |
| 4649 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4650 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4651 | if (aug_p->actions) { |
| 4652 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4653 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4654 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4655 | return LY_EVALID; |
| 4656 | } |
| 4657 | if (aug_p->notifs) { |
| 4658 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4659 | "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".", |
| 4660 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4661 | return LY_EVALID; |
| 4662 | } |
| 4663 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4664 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4665 | lysc_update_path(ctx, NULL, NULL); |
| 4666 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4667 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4668 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4669 | return ret; |
| 4670 | } |
| 4671 | |
| 4672 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4673 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4674 | * |
| 4675 | * @param[in] ctx Compile context. |
| 4676 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4677 | * @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] | 4678 | * @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] | 4679 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4680 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4681 | * 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] | 4682 | * @return LY_ERR value. |
| 4683 | */ |
| 4684 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4685 | 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] | 4686 | { |
| 4687 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4688 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4689 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4690 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4691 | return LY_EVALID; |
| 4692 | } |
| 4693 | |
| 4694 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4695 | /* check if node has default value */ |
| 4696 | if (node->nodetype & LYS_LEAF) { |
| 4697 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4698 | if (refine_flag) { |
| 4699 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4700 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4701 | return LY_EVALID; |
| 4702 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4703 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4704 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4705 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4706 | |
| 4707 | /* update the list of incomplete default values if needed */ |
| 4708 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4709 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4710 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4711 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4712 | free(leaf->dflt); |
| 4713 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4714 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4715 | } |
| 4716 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4717 | if (refine_flag) { |
| 4718 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4719 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4720 | return LY_EVALID; |
| 4721 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4722 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4723 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4724 | 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] | 4725 | return LY_EVALID; |
| 4726 | } |
| 4727 | |
| 4728 | node->flags &= ~LYS_MAND_FALSE; |
| 4729 | node->flags |= LYS_MAND_TRUE; |
| 4730 | lys_compile_mandatory_parents(node->parent, 1); |
| 4731 | } else { |
| 4732 | /* make mandatory false */ |
| 4733 | node->flags &= ~LYS_MAND_TRUE; |
| 4734 | node->flags |= LYS_MAND_FALSE; |
| 4735 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4736 | 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] | 4737 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4738 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4739 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4740 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4741 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4742 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4743 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4744 | } |
| 4745 | } |
| 4746 | return LY_SUCCESS; |
| 4747 | } |
| 4748 | |
| 4749 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4750 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4751 | * If present, also apply uses's modificators. |
| 4752 | * |
| 4753 | * @param[in] ctx Compile context |
| 4754 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4755 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4756 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4757 | * the compile context. |
| 4758 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4759 | */ |
| 4760 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4761 | 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] | 4762 | { |
| 4763 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4764 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4765 | /* 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] | 4766 | struct lysc_node_container context_node_fake = |
| 4767 | {.nodetype = LYS_CONTAINER, |
| 4768 | .module = ctx->mod, |
| 4769 | .flags = parent ? parent->flags : 0, |
| 4770 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4771 | .prev = (struct lysc_node*)&context_node_fake, |
| 4772 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4773 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4774 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4775 | int found; |
| 4776 | const char *id, *name, *prefix; |
| 4777 | size_t prefix_len, name_len; |
| 4778 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4779 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4780 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4781 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4782 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4783 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4784 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4785 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4786 | unsigned int actions_index, notifs_index; |
| 4787 | struct lysc_notif **notifs = NULL; |
| 4788 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4789 | |
| 4790 | /* search for the grouping definition */ |
| 4791 | found = 0; |
| 4792 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4793 | 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] | 4794 | if (prefix) { |
| 4795 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4796 | if (!mod) { |
| 4797 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4798 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4799 | return LY_EVALID; |
| 4800 | } |
| 4801 | } else { |
| 4802 | mod = ctx->mod_def; |
| 4803 | } |
| 4804 | if (mod == ctx->mod_def) { |
| 4805 | 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] | 4806 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4807 | LY_ARRAY_FOR(grp, u) { |
| 4808 | if (!strcmp(grp[u].name, name)) { |
| 4809 | grp = &grp[u]; |
| 4810 | found = 1; |
| 4811 | break; |
| 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | } |
| 4816 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4817 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4818 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4819 | if (grp) { |
| 4820 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4821 | if (!strcmp(grp[u].name, name)) { |
| 4822 | grp = &grp[u]; |
| 4823 | found = 1; |
| 4824 | } |
| 4825 | } |
| 4826 | } |
| 4827 | if (!found && mod->parsed->includes) { |
| 4828 | /* ... and all the submodules */ |
| 4829 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4830 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4831 | if (grp) { |
| 4832 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4833 | if (!strcmp(grp[v].name, name)) { |
| 4834 | grp = &grp[v]; |
| 4835 | found = 1; |
| 4836 | } |
| 4837 | } |
| 4838 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4839 | } |
| 4840 | } |
| 4841 | } |
| 4842 | if (!found) { |
| 4843 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4844 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4845 | return LY_EVALID; |
| 4846 | } |
| 4847 | |
| 4848 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4849 | grp_stack_count = ctx->groupings.count; |
| 4850 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4851 | if (grp_stack_count == ctx->groupings.count) { |
| 4852 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4853 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4854 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4855 | return LY_EVALID; |
| 4856 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4857 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4858 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4859 | grp->flags |= LYS_USED_GRP; |
| 4860 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4861 | |
| 4862 | /* switch context's mod_def */ |
| 4863 | mod_old = ctx->mod_def; |
| 4864 | ctx->mod_def = mod; |
| 4865 | |
| 4866 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4867 | 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] | 4868 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4869 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4870 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4871 | /* 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] | 4872 | 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] | 4873 | |
| 4874 | /* some preparation for applying refines */ |
| 4875 | if (grp->data == node_p) { |
| 4876 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4877 | if (parent) { |
| 4878 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4879 | } else if (ctx->mod->compiled->data) { |
| 4880 | child = ctx->mod->compiled->data; |
| 4881 | } else { |
| 4882 | child = NULL; |
| 4883 | } |
| 4884 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4885 | } |
| 4886 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4887 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4888 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4889 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4890 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4891 | /* 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] | 4892 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4893 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4894 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4895 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4896 | (*when)->context = lysc_xpath_context(parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4897 | when_shared = *when; |
| 4898 | } else { |
| 4899 | ++when_shared->refcount; |
| 4900 | (*when) = when_shared; |
| 4901 | } |
| 4902 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4903 | } |
| 4904 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4905 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4906 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4907 | /* 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] | 4908 | 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] | 4909 | } |
| 4910 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4911 | /* compile actions */ |
| 4912 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4913 | if (actions) { |
| 4914 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4915 | 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] | 4916 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4917 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4918 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4919 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4920 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4921 | } |
| 4922 | } |
| 4923 | |
| 4924 | /* compile notifications */ |
| 4925 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4926 | if (notifs) { |
| 4927 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4928 | 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] | 4929 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4930 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4931 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4932 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4933 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4934 | } |
| 4935 | } |
| 4936 | |
| 4937 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4938 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4939 | 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] | 4940 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4941 | 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] | 4942 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4943 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4944 | /* reload previous context's mod_def */ |
| 4945 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4946 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4947 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4948 | /* apply refine */ |
| 4949 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4950 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4951 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4952 | 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] | 4953 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4954 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4955 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4956 | |
| 4957 | /* default value */ |
| 4958 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4959 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4960 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4961 | "Invalid refine of default - %s cannot hold %d default values.", |
| 4962 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4963 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4964 | } |
| 4965 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4966 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4967 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4968 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4969 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4970 | } |
| 4971 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4972 | struct ly_err_item *err = NULL; |
| 4973 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4974 | if (leaf->dflt) { |
| 4975 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4976 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4977 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4978 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4979 | } else { |
| 4980 | /* prepare a new one */ |
| 4981 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4982 | leaf->dflt->realtype = leaf->type; |
| 4983 | } |
| 4984 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4985 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4986 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4987 | 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] | 4988 | 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] | 4989 | leaf->dflt->realtype->refcount++; |
| 4990 | if (err) { |
| 4991 | ly_err_print(err); |
| 4992 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4993 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4994 | ly_err_free(err); |
| 4995 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4996 | if (rc == LY_EINCOMPLETE) { |
| 4997 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4998 | 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] | 4999 | |
| 5000 | /* but in general result is so far ok */ |
| 5001 | rc = LY_SUCCESS; |
| 5002 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5003 | LY_CHECK_GOTO(rc, cleanup); |
| 5004 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5005 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5006 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 5007 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5008 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5009 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5010 | "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] | 5011 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5012 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5013 | |
| 5014 | /* remove previous set of default values */ |
| 5015 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5016 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5017 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 5018 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 5019 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5020 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5021 | LY_ARRAY_FREE(llist->dflts); |
| 5022 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5023 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5024 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5025 | |
| 5026 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5027 | 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] | 5028 | 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] | 5029 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5030 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5031 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5032 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5033 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5034 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 5035 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5036 | 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] | 5037 | 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] | 5038 | 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] | 5039 | llist->dflts[u]->realtype->refcount++; |
| 5040 | if (err) { |
| 5041 | ly_err_print(err); |
| 5042 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5043 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 5044 | ly_err_free(err); |
| 5045 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5046 | if (rc == LY_EINCOMPLETE) { |
| 5047 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5048 | 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] | 5049 | |
| 5050 | /* but in general result is so far ok */ |
| 5051 | rc = LY_SUCCESS; |
| 5052 | } |
| 5053 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5054 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5055 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5056 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5057 | if (((struct lysc_node_choice*)node)->dflt) { |
| 5058 | /* unset LYS_SET_DFLT from the current default case */ |
| 5059 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 5060 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5061 | 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] | 5062 | } |
| 5063 | } |
| 5064 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5065 | /* description */ |
| 5066 | if (rfn->dsc) { |
| 5067 | FREE_STRING(ctx->ctx, node->dsc); |
| 5068 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 5069 | } |
| 5070 | |
| 5071 | /* reference */ |
| 5072 | if (rfn->ref) { |
| 5073 | FREE_STRING(ctx->ctx, node->ref); |
| 5074 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 5075 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5076 | |
| 5077 | /* config */ |
| 5078 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5079 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5080 | 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] | 5081 | } else { |
| 5082 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 5083 | flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path); |
| 5084 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | /* mandatory */ |
| 5088 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5089 | 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] | 5090 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5091 | |
| 5092 | /* presence */ |
| 5093 | if (rfn->presence) { |
| 5094 | if (node->nodetype != LYS_CONTAINER) { |
| 5095 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5096 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5097 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5098 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5099 | } |
| 5100 | node->flags |= LYS_PRESENCE; |
| 5101 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5102 | |
| 5103 | /* must */ |
| 5104 | if (rfn->musts) { |
| 5105 | switch (node->nodetype) { |
| 5106 | case LYS_LEAF: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5107 | 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] | 5108 | break; |
| 5109 | case LYS_LEAFLIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5110 | 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] | 5111 | break; |
| 5112 | case LYS_LIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5113 | 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] | 5114 | break; |
| 5115 | case LYS_CONTAINER: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5116 | 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] | 5117 | break; |
| 5118 | case LYS_ANYXML: |
| 5119 | case LYS_ANYDATA: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5120 | 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] | 5121 | break; |
| 5122 | default: |
| 5123 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5124 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5125 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5126 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5127 | } |
| 5128 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5129 | |
| 5130 | /* min/max-elements */ |
| 5131 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5132 | switch (node->nodetype) { |
| 5133 | case LYS_LEAFLIST: |
| 5134 | if (rfn->flags & LYS_SET_MAX) { |
| 5135 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5136 | } |
| 5137 | if (rfn->flags & LYS_SET_MIN) { |
| 5138 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5139 | if (rfn->min) { |
| 5140 | node->flags |= LYS_MAND_TRUE; |
| 5141 | lys_compile_mandatory_parents(node->parent, 1); |
| 5142 | } else { |
| 5143 | node->flags &= ~LYS_MAND_TRUE; |
| 5144 | lys_compile_mandatory_parents(node->parent, 0); |
| 5145 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5146 | } |
| 5147 | break; |
| 5148 | case LYS_LIST: |
| 5149 | if (rfn->flags & LYS_SET_MAX) { |
| 5150 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5151 | } |
| 5152 | if (rfn->flags & LYS_SET_MIN) { |
| 5153 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5154 | if (rfn->min) { |
| 5155 | node->flags |= LYS_MAND_TRUE; |
| 5156 | lys_compile_mandatory_parents(node->parent, 1); |
| 5157 | } else { |
| 5158 | node->flags &= ~LYS_MAND_TRUE; |
| 5159 | lys_compile_mandatory_parents(node->parent, 0); |
| 5160 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5161 | } |
| 5162 | break; |
| 5163 | default: |
| 5164 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5165 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5166 | (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] | 5167 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5168 | } |
| 5169 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5170 | |
| 5171 | /* if-feature */ |
| 5172 | if (rfn->iffeatures) { |
| 5173 | /* 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] | 5174 | 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] | 5175 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5176 | |
| 5177 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5178 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5179 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5180 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 5181 | for (u = 0; u < refined.count; ++u) { |
| 5182 | node = (struct lysc_node*)refined.objs[u]; |
| 5183 | rfn = &uses_p->refines[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5184 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5185 | |
| 5186 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5187 | if ((node->flags & LYS_MAND_TRUE) && |
| 5188 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5189 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5190 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5191 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5192 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5196 | if (node->nodetype == LYS_LIST) { |
| 5197 | min = ((struct lysc_node_list*)node)->min; |
| 5198 | max = ((struct lysc_node_list*)node)->max; |
| 5199 | } else { |
| 5200 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5201 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5202 | } |
| 5203 | if (min > max) { |
| 5204 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5205 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5206 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5207 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5208 | } |
| 5209 | } |
| 5210 | } |
| 5211 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5212 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5213 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5214 | |
| 5215 | cleanup: |
| 5216 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5217 | if (context_node_fake.child) { |
| 5218 | context_node_fake.child->prev = child; |
| 5219 | } |
| 5220 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5221 | child->parent = parent; |
| 5222 | } |
| 5223 | |
| 5224 | if (uses_p->augments || uses_p->refines) { |
| 5225 | /* 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] | 5226 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5227 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5228 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5229 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5230 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5231 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5232 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5233 | } |
| 5234 | } |
| 5235 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5236 | /* reload previous context's mod_def */ |
| 5237 | ctx->mod_def = mod_old; |
| 5238 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5239 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5240 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5241 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5242 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5243 | |
| 5244 | return ret; |
| 5245 | } |
| 5246 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5247 | static int |
| 5248 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5249 | { |
| 5250 | struct lysp_node *iter; |
| 5251 | int len = 0; |
| 5252 | |
| 5253 | *path = NULL; |
| 5254 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5255 | char *s = *path; |
| 5256 | char *id; |
| 5257 | |
| 5258 | switch (iter->nodetype) { |
| 5259 | case LYS_USES: |
| 5260 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5261 | break; |
| 5262 | case LYS_GROUPING: |
| 5263 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5264 | break; |
| 5265 | case LYS_AUGMENT: |
| 5266 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5267 | break; |
| 5268 | default: |
| 5269 | id = strdup(iter->name); |
| 5270 | break; |
| 5271 | } |
| 5272 | |
| 5273 | if (!iter->parent) { |
| 5274 | /* print prefix */ |
| 5275 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5276 | } else { |
| 5277 | /* prefix is the same as in parent */ |
| 5278 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5279 | } |
| 5280 | free(s); |
| 5281 | free(id); |
| 5282 | } |
| 5283 | |
| 5284 | if (len < 0) { |
| 5285 | free(*path); |
| 5286 | *path = NULL; |
| 5287 | } else if (len == 0) { |
| 5288 | *path = strdup("/"); |
| 5289 | len = 1; |
| 5290 | } |
| 5291 | return len; |
| 5292 | } |
| 5293 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5294 | /** |
| 5295 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5296 | * |
| 5297 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5298 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5299 | */ |
| 5300 | static LY_ERR |
| 5301 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5302 | { |
| 5303 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5304 | char *path; |
| 5305 | int len; |
| 5306 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5307 | struct lysp_node_uses fake_uses = { |
| 5308 | .parent = node_p, |
| 5309 | .nodetype = LYS_USES, |
| 5310 | .flags = 0, .next = NULL, |
| 5311 | .name = grp->name, |
| 5312 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5313 | .refines = NULL, .augments = NULL |
| 5314 | }; |
| 5315 | struct lysc_node_container fake_container = { |
| 5316 | .nodetype = LYS_CONTAINER, |
| 5317 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5318 | .module = ctx->mod, |
| 5319 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5320 | .prev = (struct lysc_node*)&fake_container, |
| 5321 | .name = "fake", |
| 5322 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5323 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5324 | }; |
| 5325 | |
| 5326 | if (grp->parent) { |
| 5327 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5328 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5329 | |
| 5330 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5331 | if (len < 0) { |
| 5332 | LOGMEM(ctx->ctx); |
| 5333 | return LY_EMEM; |
| 5334 | } |
| 5335 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5336 | ctx->path_len = (uint16_t)len; |
| 5337 | free(path); |
| 5338 | |
| 5339 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5340 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5341 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5342 | lysc_update_path(ctx, NULL, NULL); |
| 5343 | lysc_update_path(ctx, NULL, NULL); |
| 5344 | |
| 5345 | ctx->path_len = 1; |
| 5346 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5347 | |
| 5348 | /* cleanup */ |
| 5349 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5350 | |
| 5351 | return ret; |
| 5352 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5353 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5354 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5355 | * @brief Compile parsed schema node information. |
| 5356 | * @param[in] ctx Compile context |
| 5357 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5358 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5359 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5360 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5361 | * @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). |
| 5362 | * 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] | 5363 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5364 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5365 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5366 | 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] | 5367 | { |
| 5368 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5369 | struct lysc_node *node; |
| 5370 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5371 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5372 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5373 | 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] | 5374 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5375 | if (node_p->nodetype != LYS_USES) { |
| 5376 | lysc_update_path(ctx, parent, node_p->name); |
| 5377 | } else { |
| 5378 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5379 | lysc_update_path(ctx, NULL, node_p->name); |
| 5380 | } |
| 5381 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5382 | switch (node_p->nodetype) { |
| 5383 | case LYS_CONTAINER: |
| 5384 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5385 | node_compile_spec = lys_compile_node_container; |
| 5386 | break; |
| 5387 | case LYS_LEAF: |
| 5388 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5389 | node_compile_spec = lys_compile_node_leaf; |
| 5390 | break; |
| 5391 | case LYS_LIST: |
| 5392 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5393 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5394 | break; |
| 5395 | case LYS_LEAFLIST: |
| 5396 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5397 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5398 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5399 | case LYS_CHOICE: |
| 5400 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5401 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5402 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5403 | case LYS_ANYXML: |
| 5404 | case LYS_ANYDATA: |
| 5405 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5406 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5407 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5408 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5409 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5410 | lysc_update_path(ctx, NULL, NULL); |
| 5411 | lysc_update_path(ctx, NULL, NULL); |
| 5412 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5413 | default: |
| 5414 | LOGINT(ctx->ctx); |
| 5415 | return LY_EINT; |
| 5416 | } |
| 5417 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5418 | node->nodetype = node_p->nodetype; |
| 5419 | node->module = ctx->mod; |
| 5420 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5421 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5422 | |
| 5423 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5424 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5425 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5426 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5427 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5428 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5429 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5430 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5431 | node->flags |= LYS_CONFIG_R; |
| 5432 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5433 | /* config not explicitely set, inherit it from parent */ |
| 5434 | if (parent) { |
| 5435 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5436 | } else { |
| 5437 | /* default is config true */ |
| 5438 | node->flags |= LYS_CONFIG_W; |
| 5439 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5440 | } else { |
| 5441 | /* config set explicitely */ |
| 5442 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5443 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5444 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5445 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5446 | "Configuration node cannot be child of any state data node."); |
| 5447 | goto error; |
| 5448 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5449 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5450 | /* *list ordering */ |
| 5451 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5452 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5453 | 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] | 5454 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5455 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5456 | node->flags &= ~LYS_ORDBY_MASK; |
| 5457 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5458 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5459 | /* default ordering is system */ |
| 5460 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5461 | } |
| 5462 | } |
| 5463 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5464 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5465 | * 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] | 5466 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5467 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5468 | * 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] | 5469 | 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] | 5470 | } |
| 5471 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5472 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5473 | node->sp = node_p; |
| 5474 | } |
| 5475 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5476 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5477 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5478 | if (node_p->when) { |
| 5479 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5480 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5481 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 5482 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5483 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5484 | 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] | 5485 | |
| 5486 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5487 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5488 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5489 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5490 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5491 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5492 | if (node->flags & LYS_MAND_TRUE) { |
| 5493 | lys_compile_mandatory_parents(parent, 1); |
| 5494 | } |
| 5495 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5496 | lysc_update_path(ctx, NULL, NULL); |
| 5497 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5498 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5499 | if (parent) { |
| 5500 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5501 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5502 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5503 | } else { |
| 5504 | lysc_update_path(ctx, parent, node->name); |
| 5505 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5506 | 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] | 5507 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5508 | if (uses_status) { |
| 5509 | |
| 5510 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5511 | /* 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] | 5512 | * it directly gets the same status flags as the choice; |
| 5513 | * 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] | 5514 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5515 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5516 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5517 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5518 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5519 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5520 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5521 | 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] | 5522 | |
| 5523 | if (parent->nodetype == LYS_CHOICE) { |
| 5524 | lysc_update_path(ctx, NULL, NULL); |
| 5525 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5526 | } else { |
| 5527 | /* top-level element */ |
| 5528 | if (!ctx->mod->compiled->data) { |
| 5529 | ctx->mod->compiled->data = node; |
| 5530 | } else { |
| 5531 | /* insert at the end of the module's top-level nodes list */ |
| 5532 | ctx->mod->compiled->data->prev->next = node; |
| 5533 | node->prev = ctx->mod->compiled->data->prev; |
| 5534 | ctx->mod->compiled->data->prev = node; |
| 5535 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5536 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5537 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5538 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5539 | return LY_EVALID; |
| 5540 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5541 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5542 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5543 | |
| 5544 | return LY_SUCCESS; |
| 5545 | |
| 5546 | error: |
| 5547 | lysc_node_free(ctx->ctx, node); |
| 5548 | return ret; |
| 5549 | } |
| 5550 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5551 | static void |
| 5552 | lysc_disconnect(struct lysc_node *node) |
| 5553 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5554 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5555 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5556 | int remove_cs = 0; |
| 5557 | |
| 5558 | parent = node->parent; |
| 5559 | |
| 5560 | /* parent's first child */ |
| 5561 | if (!parent) { |
| 5562 | return; |
| 5563 | } |
| 5564 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5565 | cs = (struct lysc_node_case*)node; |
| 5566 | } else if (parent->nodetype == LYS_CASE) { |
| 5567 | /* disconnecting some node in a case */ |
| 5568 | cs = (struct lysc_node_case*)parent; |
| 5569 | parent = cs->parent; |
| 5570 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5571 | if (child == node) { |
| 5572 | if (cs->child == child) { |
| 5573 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5574 | /* case with a single child -> remove also the case */ |
| 5575 | child->parent = NULL; |
| 5576 | remove_cs = 1; |
| 5577 | } else { |
| 5578 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5579 | } |
| 5580 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5581 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5582 | } |
| 5583 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5584 | if (!remove_cs) { |
| 5585 | cs = NULL; |
| 5586 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5587 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5588 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | if (cs) { |
| 5592 | if (remove_cs) { |
| 5593 | /* cs has only one child which is being also removed */ |
| 5594 | lysc_disconnect((struct lysc_node*)cs); |
| 5595 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5596 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5597 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5598 | /* default case removed */ |
| 5599 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5600 | } |
| 5601 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5602 | /* first case removed */ |
| 5603 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5604 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5605 | if (cs->child) { |
| 5606 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5607 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5608 | prev = cs->child->prev; |
| 5609 | } /* else all the children are under a single case */ |
| 5610 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5611 | if (child->parent != (struct lysc_node*)cs) { |
| 5612 | break; |
| 5613 | } |
| 5614 | lysc_node_free(node->module->ctx, child); |
| 5615 | } |
| 5616 | if (prev) { |
| 5617 | if (prev->next) { |
| 5618 | prev->next = child; |
| 5619 | } |
| 5620 | if (child) { |
| 5621 | child->prev = prev; |
| 5622 | } else { |
| 5623 | /* link from the first child under the cases */ |
| 5624 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5625 | } |
| 5626 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5627 | } |
| 5628 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5629 | } |
| 5630 | |
| 5631 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5632 | if (node->prev->next) { |
| 5633 | node->prev->next = node->next; |
| 5634 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5635 | if (node->next) { |
| 5636 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5637 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5638 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5639 | if (child) { |
| 5640 | child->prev = node->prev; |
| 5641 | } |
| 5642 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5643 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5644 | } |
| 5645 | } |
| 5646 | |
| 5647 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5648 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5649 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5650 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5651 | struct ly_set devs_p = {0}; |
| 5652 | struct ly_set targets = {0}; |
| 5653 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5654 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5655 | struct lysc_action *rpcs; |
| 5656 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5657 | struct lysp_deviation *dev; |
| 5658 | struct lysp_deviate *d, **dp_new; |
| 5659 | struct lysp_deviate_add *d_add; |
| 5660 | struct lysp_deviate_del *d_del; |
| 5661 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5662 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5663 | struct lysc_deviation { |
| 5664 | const char *nodeid; |
| 5665 | struct lysc_node *target; /* target node of the deviation */ |
| 5666 | 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] | 5667 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5668 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5669 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5670 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5671 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5672 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5673 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5674 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5675 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5676 | |
| 5677 | /* get all deviations from the module and all its submodules ... */ |
| 5678 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5679 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5680 | } |
| 5681 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5682 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5683 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5684 | } |
| 5685 | } |
| 5686 | if (!devs_p.count) { |
| 5687 | /* nothing to do */ |
| 5688 | return LY_SUCCESS; |
| 5689 | } |
| 5690 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5691 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5692 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5693 | /* ... and group them by the target node */ |
| 5694 | devs = calloc(devs_p.count, sizeof *devs); |
| 5695 | for (u = 0; u < devs_p.count; ++u) { |
| 5696 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5697 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5698 | |
| 5699 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5700 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5701 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5702 | if (target->nodetype == LYS_ACTION) { |
| 5703 | /* move the target pointer to input/output to make them different from the action and |
| 5704 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5705 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5706 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5707 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5708 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5709 | flags |= LYSC_OPT_INTERNAL; |
| 5710 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5711 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5712 | flags |= LYSC_OPT_INTERNAL; |
| 5713 | } |
| 5714 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5715 | /* insert into the set of targets with duplicity detection */ |
| 5716 | i = ly_set_add(&targets, target, 0); |
| 5717 | if (!devs[i]) { |
| 5718 | /* new record */ |
| 5719 | devs[i] = calloc(1, sizeof **devs); |
| 5720 | devs[i]->target = target; |
| 5721 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5722 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5723 | } |
| 5724 | /* add deviates into the deviation's list of deviates */ |
| 5725 | for (d = dev->deviates; d; d = d->next) { |
| 5726 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5727 | *dp_new = d; |
| 5728 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5729 | devs[i]->not_supported = 1; |
| 5730 | } |
| 5731 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5732 | |
| 5733 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5734 | } |
| 5735 | |
| 5736 | /* MACROS for deviates checking */ |
| 5737 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5738 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5739 | 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] | 5740 | goto cleanup; \ |
| 5741 | } |
| 5742 | |
| 5743 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5744 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5745 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \ |
| 5746 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5747 | goto cleanup; \ |
| 5748 | } |
| 5749 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5750 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5751 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5752 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5753 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5754 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5755 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5756 | goto cleanup; \ |
| 5757 | } |
| 5758 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5759 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5760 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5761 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5762 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5763 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5764 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5765 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5766 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5767 | goto cleanup; \ |
| 5768 | } |
| 5769 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5770 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5771 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5772 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5773 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5774 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5775 | goto cleanup; \ |
| 5776 | } |
| 5777 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5778 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5779 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5780 | 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] | 5781 | goto cleanup; \ |
| 5782 | } |
| 5783 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5784 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5785 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5786 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5787 | "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] | 5788 | goto cleanup; \ |
| 5789 | } |
| 5790 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5791 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5792 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5793 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5794 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5795 | 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] | 5796 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5797 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5798 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5799 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5800 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5801 | goto cleanup; \ |
| 5802 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5803 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5804 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5805 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5806 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5807 | (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] | 5808 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5809 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5810 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5811 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5812 | } |
| 5813 | |
| 5814 | /* apply deviations */ |
| 5815 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5816 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5817 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5818 | struct ly_err_item *err = NULL; |
| 5819 | |
| 5820 | dflt = NULL; |
| 5821 | changed_type = 0; |
| 5822 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5823 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5824 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5825 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5826 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5827 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5828 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5829 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5830 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5831 | } |
| 5832 | } |
| 5833 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5834 | /* not-supported */ |
| 5835 | if (devs[u]->not_supported) { |
| 5836 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 5837 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 5838 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5839 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5840 | |
| 5841 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5842 | if (devs[u]->target->parent) { \ |
| 5843 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5844 | } else { \ |
| 5845 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5846 | } \ |
| 5847 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5848 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5849 | } \ |
| 5850 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5851 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5852 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5853 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5854 | } |
| 5855 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5856 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 5857 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5858 | /* remove RPC's/action's input */ |
| 5859 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5860 | 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] | 5861 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5862 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5863 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5864 | /* remove RPC's/action's output */ |
| 5865 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5866 | 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] | 5867 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5868 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5869 | } else { |
| 5870 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5871 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5872 | } |
| 5873 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5874 | /* remove Notification */ |
| 5875 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5876 | } else { |
| 5877 | /* remove the target node */ |
| 5878 | lysc_disconnect(devs[u]->target); |
| 5879 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5880 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5881 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5882 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5883 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5884 | continue; |
| 5885 | } |
| 5886 | |
| 5887 | /* list of deviates (not-supported is not present in the list) */ |
| 5888 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5889 | d = devs[u]->deviates[v]; |
| 5890 | |
| 5891 | switch (d->mod) { |
| 5892 | case LYS_DEV_ADD: |
| 5893 | d_add = (struct lysp_deviate_add*)d; |
| 5894 | /* [units-stmt] */ |
| 5895 | if (d_add->units) { |
| 5896 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5897 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5898 | |
| 5899 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5900 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5901 | } |
| 5902 | |
| 5903 | /* *must-stmt */ |
| 5904 | if (d_add->musts) { |
| 5905 | switch (devs[u]->target->nodetype) { |
| 5906 | case LYS_CONTAINER: |
| 5907 | case LYS_LIST: |
| 5908 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5909 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5910 | break; |
| 5911 | case LYS_LEAF: |
| 5912 | case LYS_LEAFLIST: |
| 5913 | case LYS_ANYDATA: |
| 5914 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5915 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5916 | break; |
| 5917 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5918 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5919 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5920 | break; |
| 5921 | case LYS_ACTION: |
| 5922 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5923 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5924 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5925 | break; |
| 5926 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5927 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5928 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5929 | break; |
| 5930 | } |
| 5931 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5932 | default: |
| 5933 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5934 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5935 | goto cleanup; |
| 5936 | } |
| 5937 | } |
| 5938 | |
| 5939 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5940 | if (d_add->uniques) { |
| 5941 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5942 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5943 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5944 | |
| 5945 | /* *default-stmt */ |
| 5946 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5947 | switch (devs[u]->target->nodetype) { |
| 5948 | case LYS_LEAF: |
| 5949 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5950 | 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] | 5951 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5952 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5953 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5954 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5955 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5956 | } else { |
| 5957 | /* prepare new default value storage */ |
| 5958 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5959 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5960 | dflt = d_add->dflts[0]; |
| 5961 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5962 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5963 | /* mark the new default values as leaf's own */ |
| 5964 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5965 | break; |
| 5966 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5967 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5968 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5969 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5970 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5971 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5972 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5973 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5974 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5975 | LY_ARRAY_FREE(llist->dflts); |
| 5976 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5977 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5978 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5979 | } |
| 5980 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5981 | 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] | 5982 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 5983 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5984 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5985 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5986 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5987 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5988 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5989 | llist->dflts[x]->realtype = llist->type; |
| 5990 | 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] | 5991 | 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] | 5992 | (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] | 5993 | llist->dflts[x]->realtype->refcount++; |
| 5994 | if (err) { |
| 5995 | ly_err_print(err); |
| 5996 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5997 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5998 | d_add->dflts[x - y], err->msg); |
| 5999 | ly_err_free(err); |
| 6000 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6001 | if (rc == LY_EINCOMPLETE) { |
| 6002 | /* postpone default compilation when the tree is complete */ |
| 6003 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6004 | |
| 6005 | /* but in general result is so far ok */ |
| 6006 | rc = LY_SUCCESS; |
| 6007 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6008 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6009 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6010 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6011 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 6012 | break; |
| 6013 | case LYS_CHOICE: |
| 6014 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 6015 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 6016 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 6017 | * 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] | 6018 | 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] | 6019 | goto cleanup; |
| 6020 | } |
| 6021 | break; |
| 6022 | default: |
| 6023 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6024 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6025 | goto cleanup; |
| 6026 | } |
| 6027 | } |
| 6028 | |
| 6029 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6030 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 6031 | 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] | 6032 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6033 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 6034 | goto cleanup; |
| 6035 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6036 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6037 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 6038 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6039 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6040 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 6041 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6042 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 6043 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6044 | goto cleanup; |
| 6045 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6046 | 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] | 6047 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6048 | |
| 6049 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6050 | if (d_add->flags & LYS_MAND_MASK) { |
| 6051 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 6052 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6053 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 6054 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6055 | goto cleanup; |
| 6056 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6057 | 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] | 6058 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6059 | |
| 6060 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6061 | if (d_add->flags & LYS_SET_MIN) { |
| 6062 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6063 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6064 | /* change value */ |
| 6065 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 6066 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6067 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6068 | /* change value */ |
| 6069 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 6070 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6071 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6072 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 6073 | goto cleanup; |
| 6074 | } |
| 6075 | if (d_add->min) { |
| 6076 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6077 | } |
| 6078 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6079 | |
| 6080 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6081 | if (d_add->flags & LYS_SET_MAX) { |
| 6082 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6083 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6084 | /* change value */ |
| 6085 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6086 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6087 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6088 | /* change value */ |
| 6089 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6090 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6091 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6092 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6093 | goto cleanup; |
| 6094 | } |
| 6095 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6096 | |
| 6097 | break; |
| 6098 | case LYS_DEV_DELETE: |
| 6099 | d_del = (struct lysp_deviate_del*)d; |
| 6100 | |
| 6101 | /* [units-stmt] */ |
| 6102 | if (d_del->units) { |
| 6103 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6104 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6105 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6106 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6107 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6108 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6109 | goto cleanup; |
| 6110 | } |
| 6111 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6112 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6113 | } |
| 6114 | |
| 6115 | /* *must-stmt */ |
| 6116 | if (d_del->musts) { |
| 6117 | switch (devs[u]->target->nodetype) { |
| 6118 | case LYS_CONTAINER: |
| 6119 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6120 | 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] | 6121 | break; |
| 6122 | case LYS_LEAF: |
| 6123 | case LYS_LEAFLIST: |
| 6124 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6125 | 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] | 6126 | break; |
| 6127 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6128 | 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] | 6129 | break; |
| 6130 | case LYS_ACTION: |
| 6131 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6132 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6133 | break; |
| 6134 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6135 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6136 | break; |
| 6137 | } |
| 6138 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6139 | default: |
| 6140 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6141 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6142 | goto cleanup; |
| 6143 | } |
| 6144 | } |
| 6145 | |
| 6146 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6147 | if (d_del->uniques) { |
| 6148 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6149 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6150 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6151 | LY_ARRAY_FOR(list->uniques, z) { |
| 6152 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6153 | nodeid = strpbrk(name, " \t\n"); |
| 6154 | if (nodeid) { |
| 6155 | if (strncmp(name, list->uniques[z][y]->name, nodeid - name) |
| 6156 | || list->uniques[z][y]->name[nodeid - name] != '\0') { |
| 6157 | break; |
| 6158 | } |
| 6159 | while (isspace(*nodeid)) { |
| 6160 | ++nodeid; |
| 6161 | } |
| 6162 | } else { |
| 6163 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6164 | break; |
| 6165 | } |
| 6166 | } |
| 6167 | } |
| 6168 | if (!name) { |
| 6169 | /* complete match - remove the unique */ |
| 6170 | LY_ARRAY_DECREMENT(list->uniques); |
| 6171 | LY_ARRAY_FREE(list->uniques[z]); |
| 6172 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6173 | --z; |
| 6174 | break; |
| 6175 | } |
| 6176 | } |
| 6177 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6178 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6179 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6180 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6181 | goto cleanup; |
| 6182 | } |
| 6183 | } |
| 6184 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6185 | LY_ARRAY_FREE(list->uniques); |
| 6186 | list->uniques = NULL; |
| 6187 | } |
| 6188 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6189 | |
| 6190 | /* *default-stmt */ |
| 6191 | if (d_del->dflts) { |
| 6192 | switch (devs[u]->target->nodetype) { |
| 6193 | case LYS_LEAF: |
| 6194 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6195 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6196 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6197 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6198 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6199 | 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] | 6200 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6201 | if (i) { |
| 6202 | free((char*)dflt); |
| 6203 | } |
| 6204 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6205 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6206 | d_del->dflts[0], dflt); |
| 6207 | goto cleanup; |
| 6208 | } |
| 6209 | if (i) { |
| 6210 | free((char*)dflt); |
| 6211 | } |
| 6212 | dflt = NULL; |
| 6213 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6214 | /* update the list of incomplete default values if needed */ |
| 6215 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6216 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6217 | /* remove the default specification */ |
| 6218 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6219 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6220 | free(leaf->dflt); |
| 6221 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6222 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6223 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6224 | break; |
| 6225 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6226 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6227 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6228 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6229 | 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] | 6230 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6231 | if (i) { |
| 6232 | free((char*)dflt); |
| 6233 | } |
| 6234 | dflt = NULL; |
| 6235 | break; |
| 6236 | } |
| 6237 | if (i) { |
| 6238 | free((char*)dflt); |
| 6239 | } |
| 6240 | dflt = NULL; |
| 6241 | } |
| 6242 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6244 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6245 | goto cleanup; |
| 6246 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6247 | |
| 6248 | /* update the list of incomplete default values if needed */ |
| 6249 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6250 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6251 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6252 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6253 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6254 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6255 | free(llist->dflts[y]); |
| 6256 | 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] | 6257 | 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] | 6258 | } |
| 6259 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6260 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6261 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6262 | LY_ARRAY_FREE(llist->dflts); |
| 6263 | llist->dflts = NULL; |
| 6264 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6265 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6266 | break; |
| 6267 | case LYS_CHOICE: |
| 6268 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6269 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6270 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6271 | 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] | 6272 | if (prefix) { |
| 6273 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6274 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6275 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6276 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6277 | "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] | 6278 | goto cleanup; |
| 6279 | } |
| 6280 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6281 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6282 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6283 | "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] | 6284 | goto cleanup; |
| 6285 | } |
| 6286 | } |
| 6287 | /* else { |
| 6288 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6289 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6290 | * the module of the default case, just matching its name */ |
| 6291 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6292 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6293 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6294 | 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] | 6295 | goto cleanup; |
| 6296 | } |
| 6297 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6298 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6299 | break; |
| 6300 | default: |
| 6301 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6302 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6303 | goto cleanup; |
| 6304 | } |
| 6305 | } |
| 6306 | |
| 6307 | break; |
| 6308 | case LYS_DEV_REPLACE: |
| 6309 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6310 | |
| 6311 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6312 | if (d_rpl->type) { |
| 6313 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6314 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6315 | 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] | 6316 | |
| 6317 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6318 | /* the target has default from the previous type - remove it */ |
| 6319 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6320 | /* update the list of incomplete default values if needed */ |
| 6321 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6322 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6323 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6324 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6325 | free(leaf->dflt); |
| 6326 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6327 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6328 | } else { /* LYS_LEAFLIST */ |
| 6329 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6330 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6331 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6332 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6333 | free(llist->dflts[x]); |
| 6334 | } |
| 6335 | LY_ARRAY_FREE(llist->dflts); |
| 6336 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6337 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6338 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6339 | } |
| 6340 | } |
| 6341 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6342 | /* there is no default value, do not set changed_type after type compilation |
| 6343 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6344 | changed_type = -1; |
| 6345 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6346 | 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] | 6347 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6348 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6349 | |
| 6350 | /* [units-stmt] */ |
| 6351 | if (d_rpl->units) { |
| 6352 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6353 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6354 | units, "replacing", "units", d_rpl->units); |
| 6355 | |
| 6356 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6357 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6358 | } |
| 6359 | |
| 6360 | /* [default-stmt] */ |
| 6361 | if (d_rpl->dflt) { |
| 6362 | switch (devs[u]->target->nodetype) { |
| 6363 | case LYS_LEAF: |
| 6364 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6365 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6366 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6367 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6368 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6369 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6370 | dflt = d_rpl->dflt; |
| 6371 | /* 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] | 6372 | break; |
| 6373 | case LYS_CHOICE: |
| 6374 | 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] | 6375 | 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] | 6376 | goto cleanup; |
| 6377 | } |
| 6378 | break; |
| 6379 | default: |
| 6380 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6381 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6382 | goto cleanup; |
| 6383 | } |
| 6384 | } |
| 6385 | |
| 6386 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6387 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 6388 | 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] | 6389 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6390 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6391 | goto cleanup; |
| 6392 | } |
| 6393 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6394 | 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] | 6395 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6396 | goto cleanup; |
| 6397 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6398 | 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] | 6399 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6400 | |
| 6401 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6402 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6403 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6404 | 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] | 6405 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6406 | goto cleanup; |
| 6407 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6408 | 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] | 6409 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6410 | |
| 6411 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6412 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6413 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6414 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6415 | /* change value */ |
| 6416 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6417 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6418 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6419 | /* change value */ |
| 6420 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6421 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6422 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6423 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6424 | goto cleanup; |
| 6425 | } |
| 6426 | if (d_rpl->min) { |
| 6427 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6428 | } |
| 6429 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6430 | |
| 6431 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6432 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6433 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6434 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6435 | /* change value */ |
| 6436 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6437 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6438 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6439 | /* change value */ |
| 6440 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6441 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6442 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6443 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6444 | goto cleanup; |
| 6445 | } |
| 6446 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6447 | |
| 6448 | break; |
| 6449 | default: |
| 6450 | LOGINT(ctx->ctx); |
| 6451 | goto cleanup; |
| 6452 | } |
| 6453 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6454 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6455 | /* final check when all deviations of a single target node are applied */ |
| 6456 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6457 | /* check min-max compatibility */ |
| 6458 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6459 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6460 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6461 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6462 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6463 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6464 | } else { |
| 6465 | min = max = 0; |
| 6466 | } |
| 6467 | if (min > max) { |
| 6468 | 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] | 6469 | "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] | 6470 | goto cleanup; |
| 6471 | } |
| 6472 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6473 | if (dflt) { |
| 6474 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6475 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6476 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6477 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6478 | 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] | 6479 | 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] | 6480 | leaf->dflt->realtype->refcount++; |
| 6481 | if (err) { |
| 6482 | ly_err_print(err); |
| 6483 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6484 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6485 | ly_err_free(err); |
| 6486 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6487 | if (rc == LY_EINCOMPLETE) { |
| 6488 | /* postpone default compilation when the tree is complete */ |
| 6489 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6490 | |
| 6491 | /* but in general result is so far ok */ |
| 6492 | rc = LY_SUCCESS; |
| 6493 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6494 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6495 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6496 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6497 | int dynamic; |
| 6498 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6499 | 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] | 6500 | |
| 6501 | /* update the list of incomplete default values if needed */ |
| 6502 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6503 | |
| 6504 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6505 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6506 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6507 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6508 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6509 | 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] | 6510 | 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] | 6511 | leaf->dflt->realtype->refcount++; |
| 6512 | if (err) { |
| 6513 | ly_err_print(err); |
| 6514 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6515 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6516 | ly_err_free(err); |
| 6517 | } |
| 6518 | if (dynamic) { |
| 6519 | free((void*)dflt); |
| 6520 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6521 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6522 | if (rc == LY_EINCOMPLETE) { |
| 6523 | /* postpone default compilation when the tree is complete */ |
| 6524 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6525 | |
| 6526 | /* but in general result is so far ok */ |
| 6527 | rc = LY_SUCCESS; |
| 6528 | } |
| 6529 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6530 | } else { /* LYS_LEAFLIST */ |
| 6531 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6532 | 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] | 6533 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6534 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6535 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6536 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6537 | 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] | 6538 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6539 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6540 | llist->dflts[x]->realtype->refcount++; |
| 6541 | if (err) { |
| 6542 | ly_err_print(err); |
| 6543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6544 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6545 | dflt, err->msg); |
| 6546 | ly_err_free(err); |
| 6547 | } |
| 6548 | if (dynamic) { |
| 6549 | free((void*)dflt); |
| 6550 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6551 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6552 | if (rc == LY_EINCOMPLETE) { |
| 6553 | /* postpone default compilation when the tree is complete */ |
| 6554 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6555 | |
| 6556 | /* but in general result is so far ok */ |
| 6557 | rc = LY_SUCCESS; |
| 6558 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6559 | LY_CHECK_GOTO(rc, cleanup); |
| 6560 | } |
| 6561 | } |
| 6562 | } |
| 6563 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6564 | /* check mandatory - default compatibility */ |
| 6565 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6566 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6567 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6568 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6569 | "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] | 6570 | goto cleanup; |
| 6571 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6572 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6573 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6574 | 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] | 6575 | goto cleanup; |
| 6576 | } |
| 6577 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6578 | /* mandatory node under a default case */ |
| 6579 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6580 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6581 | 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] | 6582 | goto cleanup; |
| 6583 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6584 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6585 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6586 | } |
| 6587 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6588 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6589 | ret = LY_SUCCESS; |
| 6590 | |
| 6591 | cleanup: |
| 6592 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6593 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6594 | free(devs[u]); |
| 6595 | } |
| 6596 | free(devs); |
| 6597 | ly_set_erase(&targets, NULL); |
| 6598 | ly_set_erase(&devs_p, NULL); |
| 6599 | |
| 6600 | return ret; |
| 6601 | } |
| 6602 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6603 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6604 | * @brief Compile the given YANG submodule into the main module. |
| 6605 | * @param[in] ctx Compile context |
| 6606 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6607 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6608 | */ |
| 6609 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6610 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6611 | { |
| 6612 | unsigned int u; |
| 6613 | LY_ERR ret = LY_SUCCESS; |
| 6614 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6615 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6616 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6617 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6618 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6619 | if (!mainmod->mod->off_features) { |
| 6620 | /* features are compiled directly into the compiled module structure, |
| 6621 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6622 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6623 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6624 | LY_CHECK_GOTO(ret, error); |
| 6625 | } |
| 6626 | if (!mainmod->mod->off_extensions) { |
| 6627 | /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */ |
| 6628 | ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6629 | LY_CHECK_GOTO(ret, error); |
| 6630 | } |
| 6631 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6632 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6633 | 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] | 6634 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6635 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6636 | /* data nodes */ |
| 6637 | LY_LIST_FOR(submod->data, node_p) { |
| 6638 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6639 | LY_CHECK_GOTO(ret, error); |
| 6640 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6641 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6642 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6643 | 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] | 6644 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6645 | error: |
| 6646 | return ret; |
| 6647 | } |
| 6648 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6649 | static void * |
| 6650 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6651 | { |
| 6652 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6653 | if (substmts[u].stmt == stmt) { |
| 6654 | return substmts[u].storage; |
| 6655 | } |
| 6656 | } |
| 6657 | return NULL; |
| 6658 | } |
| 6659 | |
| 6660 | LY_ERR |
| 6661 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6662 | { |
| 6663 | LY_ERR ret = LY_EVALID, r; |
| 6664 | unsigned int u; |
| 6665 | struct lysp_stmt *stmt; |
| 6666 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6667 | |
| 6668 | /* check for invalid substatements */ |
| 6669 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6670 | for (u = 0; substmts[u].stmt; ++u) { |
| 6671 | if (substmts[u].stmt == stmt->kw) { |
| 6672 | break; |
| 6673 | } |
| 6674 | } |
| 6675 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6676 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6677 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6678 | goto cleanup; |
| 6679 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6680 | } |
| 6681 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6682 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6683 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6684 | /* keep order of the processing the same as the order in the defined substmts, |
| 6685 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6686 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6687 | int stmt_present = 0; |
| 6688 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6689 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6690 | if (substmts[u].stmt != stmt->kw) { |
| 6691 | continue; |
| 6692 | } |
| 6693 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6694 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6695 | if (substmts[u].storage) { |
| 6696 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6697 | case LY_STMT_STATUS: |
| 6698 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6699 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6700 | break; |
| 6701 | case LY_STMT_UNITS: { |
| 6702 | const char **units; |
| 6703 | |
| 6704 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6705 | /* single item */ |
| 6706 | if (*((const char **)substmts[u].storage)) { |
| 6707 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6708 | goto cleanup; |
| 6709 | } |
| 6710 | units = (const char **)substmts[u].storage; |
| 6711 | } else { |
| 6712 | /* sized array */ |
| 6713 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6714 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6715 | } |
| 6716 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6717 | break; |
| 6718 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6719 | case LY_STMT_TYPE: { |
| 6720 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6721 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6722 | |
| 6723 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6724 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6725 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6726 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6727 | goto cleanup; |
| 6728 | } |
| 6729 | compiled = substmts[u].storage; |
| 6730 | } else { |
| 6731 | /* sized array */ |
| 6732 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6733 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6734 | compiled = (void*)type; |
| 6735 | } |
| 6736 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6737 | 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] | 6738 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6739 | 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] | 6740 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6741 | lysp_type_free(ctx->ctx, parsed); |
| 6742 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6743 | break; |
| 6744 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6745 | case LY_STMT_IF_FEATURE: { |
| 6746 | struct lysc_iffeature *iff = NULL; |
| 6747 | |
| 6748 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6749 | /* single item */ |
| 6750 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6751 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6752 | goto cleanup; |
| 6753 | } |
| 6754 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6755 | } else { |
| 6756 | /* sized array */ |
| 6757 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6758 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6759 | } |
| 6760 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6761 | break; |
| 6762 | } |
| 6763 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6764 | * 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] | 6765 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6766 | 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.", |
| 6767 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6768 | goto cleanup; |
| 6769 | } |
| 6770 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6771 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6772 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6773 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6774 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6775 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6776 | goto cleanup; |
| 6777 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6778 | } |
| 6779 | |
| 6780 | ret = LY_SUCCESS; |
| 6781 | |
| 6782 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6783 | return ret; |
| 6784 | } |
| 6785 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6786 | LY_ERR |
| 6787 | lys_compile(struct lys_module *mod, int options) |
| 6788 | { |
| 6789 | struct lysc_ctx ctx = {0}; |
| 6790 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6791 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6792 | struct lysp_module *sp; |
| 6793 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6794 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6795 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6796 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6797 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6798 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6799 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6800 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 6801 | |
| 6802 | if (!mod->implemented) { |
| 6803 | /* just imported modules are not compiled */ |
| 6804 | return LY_SUCCESS; |
| 6805 | } |
| 6806 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6807 | sp = mod->parsed; |
| 6808 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6809 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6810 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6811 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6812 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6813 | ctx.path_len = 1; |
| 6814 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6815 | |
| 6816 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6817 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 6818 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6819 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6820 | 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] | 6821 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6822 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6823 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6824 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6825 | |
| 6826 | /* features */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6827 | if (mod->off_features) { |
| 6828 | /* there is already precompiled array of features */ |
| 6829 | mod_c->features = mod->off_features; |
| 6830 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6831 | } else { |
| 6832 | /* features are compiled directly into the compiled module structure, |
| 6833 | * 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] | 6834 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6835 | LY_CHECK_GOTO(ret, error); |
| 6836 | } |
| 6837 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 6838 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 6839 | * are present. */ |
| 6840 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6841 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6842 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6843 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6844 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6845 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6846 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6847 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6848 | 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] | 6849 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6850 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6851 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6852 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6853 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6854 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6855 | /* extensions */ |
| 6856 | /* 2-steps: a) prepare compiled structures and ... */ |
| 6857 | if (mod->off_extensions) { |
| 6858 | /* there is already precompiled array of extension definitions */ |
| 6859 | mod_c->extensions = mod->off_extensions; |
| 6860 | mod->off_extensions = NULL; |
| 6861 | } else { |
| 6862 | /* extension definitions are compiled directly into the compiled module structure */ |
| 6863 | ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions); |
| 6864 | } |
| 6865 | /* ... b) connect the extension definitions with the appropriate extension plugins */ |
| 6866 | lys_compile_extension_plugins(mod_c->extensions); |
| 6867 | |
| 6868 | /* identities */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6869 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6870 | 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] | 6871 | if (sp->identities) { |
| 6872 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 6873 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6874 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6875 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6876 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6877 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6878 | ret = lys_compile_node(&ctx, node_p, NULL, 0); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6879 | LY_CHECK_GOTO(ret, error); |
| 6880 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6881 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6882 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6883 | 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] | 6884 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6885 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 6886 | ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6887 | LY_CHECK_GOTO(ret, error); |
| 6888 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6889 | ret = lys_compile_augment(&ctx, augments[u], NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6890 | LY_CHECK_GOTO(ret, error); |
| 6891 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6892 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6893 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6894 | ret = lys_compile_deviations(&ctx, sp); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6895 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6896 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6897 | /* extension instances TODO cover extension instances from submodules */ |
| 6898 | 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] | 6899 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6900 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6901 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 6902 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 6903 | * 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] | 6904 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6905 | 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] | 6906 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6907 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6908 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6909 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6910 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6911 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6912 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6913 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6914 | /* validate the path */ |
| 6915 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 6916 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 6917 | LY_CHECK_GOTO(ret, error); |
| 6918 | } |
| 6919 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6920 | } |
| 6921 | } |
| 6922 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6923 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6924 | 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] | 6925 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6926 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6927 | /* store pointer to the real type */ |
| 6928 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 6929 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6930 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6931 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6932 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6933 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6934 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6935 | /* store pointer to the real type */ |
| 6936 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 6937 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6938 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6939 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 6940 | } |
| 6941 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6942 | } |
| 6943 | } |
| 6944 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6945 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6946 | /* finish incomplete default values compilation */ |
| 6947 | for (u = 0; u < ctx.dflts.count; ++u) { |
| 6948 | struct ly_err_item *err = NULL; |
| 6949 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[u]; |
| 6950 | ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized), |
| 6951 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 6952 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 6953 | if (err) { |
| 6954 | ly_err_print(err); |
| 6955 | ctx.path[0] = '\0'; |
| 6956 | lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE); |
| 6957 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 6958 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6959 | ly_err_free(err); |
| 6960 | } |
| 6961 | LY_CHECK_GOTO(ret, error); |
| 6962 | } |
| 6963 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6964 | /* validate non-instantiated groupings from the parsed schema, |
| 6965 | * without it we would accept even the schemas with invalid grouping specification */ |
| 6966 | ctx.options |= LYSC_OPT_GROUPING; |
| 6967 | LY_ARRAY_FOR(sp->groupings, u) { |
| 6968 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
| 6969 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error); |
| 6970 | } |
| 6971 | } |
| 6972 | LY_LIST_FOR(sp->data, node_p) { |
| 6973 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 6974 | LY_ARRAY_FOR(grps, u) { |
| 6975 | if (!(grps[u].flags & LYS_USED_GRP)) { |
| 6976 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error); |
| 6977 | } |
| 6978 | } |
| 6979 | } |
| 6980 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6981 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 6982 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 6983 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 6984 | } |
| 6985 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6986 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6987 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6988 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6989 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6990 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6991 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6992 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6993 | lysp_module_free(mod->parsed); |
| 6994 | ((struct lys_module*)mod)->parsed = NULL; |
| 6995 | } |
| 6996 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6997 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6998 | /* remove flag of the modules implemented by dependency */ |
| 6999 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 7000 | m = ctx.ctx->list.objs[u]; |
| 7001 | if (m->implemented == 2) { |
| 7002 | m->implemented = 1; |
| 7003 | } |
| 7004 | } |
| 7005 | } |
| 7006 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7007 | ((struct lys_module*)mod)->compiled = mod_c; |
| 7008 | return LY_SUCCESS; |
| 7009 | |
| 7010 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7011 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7012 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7013 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7014 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7015 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7016 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7017 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7018 | mod->compiled = NULL; |
| 7019 | |
| 7020 | /* revert compilation of modules implemented by dependency */ |
| 7021 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 7022 | m = ctx.ctx->list.objs[u]; |
| 7023 | if (m->implemented == 2) { |
| 7024 | /* revert features list to the precompiled state */ |
| 7025 | lys_feature_precompile_revert(&ctx, m); |
| 7026 | /* mark module as imported-only / not-implemented */ |
| 7027 | m->implemented = 0; |
| 7028 | /* free the compiled version of the module */ |
| 7029 | lysc_module_free(m->compiled, NULL); |
| 7030 | m->compiled = NULL; |
| 7031 | } |
| 7032 | } |
| 7033 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7034 | return ret; |
| 7035 | } |