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" |
| 27 | #include "set.h" |
| 28 | #include "plugins_types.h" |
| 29 | #include "tree.h" |
| 30 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 31 | #include "tree_schema_internal.h" |
| 32 | #include "xpath.h" |
| 33 | |
| 34 | /** |
| 35 | * @brief Duplicate string into dictionary |
| 36 | * @param[in] CTX libyang context of the dictionary. |
| 37 | * @param[in] ORIG String to duplicate. |
| 38 | * @param[out] DUP Where to store the result. |
| 39 | */ |
| 40 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 41 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 42 | #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] | 43 | if (ARRAY_P) { \ |
| 44 | 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] | 45 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 46 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 47 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 48 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 49 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 50 | } \ |
| 51 | } |
| 52 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 53 | #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] | 54 | if (ARRAY_P) { \ |
| 55 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 56 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 57 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 58 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 59 | 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] | 60 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 61 | } \ |
| 62 | } |
| 63 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 64 | #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] | 65 | if (ARRAY_P) { \ |
| 66 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 67 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 68 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 69 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 70 | 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] | 71 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 72 | } \ |
| 73 | } |
| 74 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 75 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 76 | if (MEMBER_P) { \ |
| 77 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 78 | 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] | 79 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 80 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 81 | } |
| 82 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 83 | #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] | 84 | if (MEMBER_P) { \ |
| 85 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
| 86 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 87 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 88 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 89 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 90 | } |
| 91 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 92 | #define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 93 | if (ARRAY) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 94 | for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
| 95 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 96 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 97 | return LY_EVALID; \ |
| 98 | } \ |
| 99 | } \ |
| 100 | } |
| 101 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 102 | static struct lysc_ext_instance * |
| 103 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 104 | { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 105 | /* TODO - extensions */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 106 | (void) ctx; |
| 107 | (void) orig; |
| 108 | return NULL; |
| 109 | } |
| 110 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 111 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 112 | * @brief Add record into the compile context's list of incomplete default values. |
| 113 | * @param[in] ctx Compile context with the incomplete default values list. |
| 114 | * @param[in] context_node Context schema node to store in the record. |
| 115 | * @param[in] dflt Incomplete default value to store in the record. |
| 116 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 117 | * @return LY_EMEM in case of memory allocation failure. |
| 118 | * @return LY_SUCCESS |
| 119 | */ |
| 120 | static LY_ERR |
| 121 | lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod) |
| 122 | { |
| 123 | struct lysc_incomplete_dflt *r; |
| 124 | r = malloc(sizeof *r); |
| 125 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 126 | r->context_node = context_node; |
| 127 | r->dflt = dflt; |
| 128 | r->dflt_mod = dflt_mod; |
| 129 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 130 | |
| 131 | return LY_SUCCESS; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 136 | * @param[in] ctx Compile context with the incomplete default values list. |
| 137 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 138 | */ |
| 139 | static void |
| 140 | lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt) |
| 141 | { |
| 142 | unsigned int u; |
| 143 | struct lysc_incomplete_dflt *r; |
| 144 | |
| 145 | for (u = 0; u < ctx->dflts.count; ++u) { |
| 146 | r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u]; |
| 147 | if (r->dflt == dflt) { |
| 148 | free(ctx->dflts.objs[u]); |
| 149 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 150 | --ctx->dflts.count; |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 157 | * @brief Update path in the compile context. |
| 158 | * |
| 159 | * @param[in] ctx Compile context with the path. |
| 160 | * @param[in] parent Parent of the current node to check difference of the node's module. The current module is taken from lysc_ctx::mod. |
| 161 | * @param[in] name Name of the node to update path with. If NULL, the last segment is removed. If the format is `{keyword}`, the following |
| 162 | * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used). |
| 163 | */ |
| 164 | static void |
| 165 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 166 | { |
| 167 | int len; |
| 168 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 169 | |
| 170 | if (!name) { |
| 171 | /* removing last path segment */ |
| 172 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 173 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 174 | if (ctx->path[ctx->path_len] == '=') { |
| 175 | ctx->path[ctx->path_len++] = '}'; |
| 176 | } else { |
| 177 | /* not a top-level special tag, remove also preceiding '/' */ |
| 178 | goto remove_nodelevel; |
| 179 | } |
| 180 | } else { |
| 181 | remove_nodelevel: |
| 182 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 183 | if (ctx->path_len == 0) { |
| 184 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 185 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | /* set new terminating NULL-byte */ |
| 189 | ctx->path[ctx->path_len] = '\0'; |
| 190 | } else { |
| 191 | if (ctx->path_len > 1) { |
| 192 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 193 | /* extension of the special tag */ |
| 194 | nextlevel = 2; |
| 195 | --ctx->path_len; |
| 196 | } else { |
| 197 | /* there is already some path, so add next level */ |
| 198 | nextlevel = 1; |
| 199 | } |
| 200 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 201 | |
| 202 | if (nextlevel != 2) { |
| 203 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 204 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 205 | 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] | 206 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 207 | 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] | 208 | } |
| 209 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 210 | 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] | 211 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 212 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 213 | /* output truncated */ |
| 214 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 215 | } else { |
| 216 | ctx->path_len += len; |
| 217 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 222 | * @brief Duplicate the compiled pattern structure. |
| 223 | * |
| 224 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 225 | * |
| 226 | * @param[in] orig The pattern structure to duplicate. |
| 227 | * @return The duplicated structure to use. |
| 228 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 229 | static struct lysc_pattern* |
| 230 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 231 | { |
| 232 | ++orig->refcount; |
| 233 | return orig; |
| 234 | } |
| 235 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 236 | /** |
| 237 | * @brief Duplicate the array of compiled patterns. |
| 238 | * |
| 239 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 240 | * |
| 241 | * @param[in] ctx Libyang context for logging. |
| 242 | * @param[in] orig The patterns sized array to duplicate. |
| 243 | * @return New sized array as a copy of @p orig. |
| 244 | * @return NULL in case of memory allocation error. |
| 245 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 246 | static struct lysc_pattern** |
| 247 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 248 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 249 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 250 | unsigned int u; |
| 251 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 252 | assert(orig); |
| 253 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 254 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 255 | LY_ARRAY_FOR(orig, u) { |
| 256 | dup[u] = lysc_pattern_dup(orig[u]); |
| 257 | LY_ARRAY_INCREMENT(dup); |
| 258 | } |
| 259 | return dup; |
| 260 | } |
| 261 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 262 | /** |
| 263 | * @brief Duplicate compiled range structure. |
| 264 | * |
| 265 | * @param[in] ctx Libyang context for logging. |
| 266 | * @param[in] orig The range structure to be duplicated. |
| 267 | * @return New compiled range structure as a copy of @p orig. |
| 268 | * @return NULL in case of memory allocation error. |
| 269 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 270 | struct lysc_range* |
| 271 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 272 | { |
| 273 | struct lysc_range *dup; |
| 274 | LY_ERR ret; |
| 275 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 276 | assert(orig); |
| 277 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 278 | dup = calloc(1, sizeof *dup); |
| 279 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 280 | if (orig->parts) { |
| 281 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 282 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 283 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 284 | } |
| 285 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 286 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 287 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 288 | |
| 289 | return dup; |
| 290 | cleanup: |
| 291 | free(dup); |
| 292 | (void) ret; /* set but not used due to the return type */ |
| 293 | return NULL; |
| 294 | } |
| 295 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 296 | /** |
| 297 | * @brief Stack for processing if-feature expressions. |
| 298 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 299 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 300 | int size; /**< number of items in the stack */ |
| 301 | int index; /**< first empty item */ |
| 302 | 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] | 303 | }; |
| 304 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 305 | /** |
| 306 | * @brief Add @ref ifftokens into the stack. |
| 307 | * @param[in] stack The if-feature stack to use. |
| 308 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 309 | * @return LY_EMEM in case of memory allocation error |
| 310 | * @return LY_ESUCCESS if the value successfully stored. |
| 311 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 312 | static LY_ERR |
| 313 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 314 | { |
| 315 | if (stack->index == stack->size) { |
| 316 | stack->size += 4; |
| 317 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 318 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 319 | } |
| 320 | stack->stack[stack->index++] = value; |
| 321 | return LY_SUCCESS; |
| 322 | } |
| 323 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 324 | /** |
| 325 | * @brief Get (and remove) the last item form the stack. |
| 326 | * @param[in] stack The if-feature stack to use. |
| 327 | * @return The value from the top of the stack. |
| 328 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 329 | static uint8_t |
| 330 | iff_stack_pop(struct iff_stack *stack) |
| 331 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 332 | assert(stack && stack->index); |
| 333 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 334 | stack->index--; |
| 335 | return stack->stack[stack->index]; |
| 336 | } |
| 337 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 338 | /** |
| 339 | * @brief Clean up the stack. |
| 340 | * @param[in] stack The if-feature stack to use. |
| 341 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 342 | static void |
| 343 | iff_stack_clean(struct iff_stack *stack) |
| 344 | { |
| 345 | stack->size = 0; |
| 346 | free(stack->stack); |
| 347 | } |
| 348 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 349 | /** |
| 350 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 351 | * (libyang format of the if-feature expression). |
| 352 | * @param[in,out] list The 2bits array to modify. |
| 353 | * @param[in] op The operand (@ref ifftokens) to store. |
| 354 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 355 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 356 | static void |
| 357 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 358 | { |
| 359 | uint8_t *item; |
| 360 | uint8_t mask = 3; |
| 361 | |
| 362 | assert(pos >= 0); |
| 363 | assert(op <= 3); /* max 2 bits */ |
| 364 | |
| 365 | item = &list[pos / 4]; |
| 366 | mask = mask << 2 * (pos % 4); |
| 367 | *item = (*item) & ~mask; |
| 368 | *item = (*item) | (op << 2 * (pos % 4)); |
| 369 | } |
| 370 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 371 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 372 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 373 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 374 | /** |
| 375 | * @brief Find a feature of the given name and referenced in the given module. |
| 376 | * |
| 377 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 378 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 379 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 380 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 381 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 382 | * assigned till that time will be still valid. |
| 383 | * |
| 384 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 385 | * @param[in] name Name of the feature including possible prefix. |
| 386 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 387 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 388 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 389 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 390 | 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] | 391 | { |
| 392 | size_t i; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 393 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 394 | |
| 395 | for (i = 0; i < len; ++i) { |
| 396 | if (name[i] == ':') { |
| 397 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 398 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 399 | LY_CHECK_RET(!mod, NULL); |
| 400 | |
| 401 | name = &name[i + 1]; |
| 402 | len = len - i - 1; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 407 | if (mod->implemented) { |
| 408 | /* module is implemented so there is already the compiled schema */ |
| 409 | flist = mod->compiled->features; |
| 410 | } else { |
| 411 | flist = mod->off_features; |
| 412 | } |
| 413 | LY_ARRAY_FOR(flist, i) { |
| 414 | f = &flist[i]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 415 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 416 | return f; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 424 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 425 | { |
| 426 | const char *name; |
| 427 | unsigned int u; |
| 428 | const struct lys_module *mod; |
| 429 | struct lysp_ext *edef = NULL; |
| 430 | |
| 431 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 432 | ext->insubstmt = ext_p->insubstmt; |
| 433 | ext->insubstmt_index = ext_p->insubstmt_index; |
| 434 | |
| 435 | /* get module where the extension definition should be placed */ |
| 436 | for (u = 0; ext_p->name[u] != ':'; ++u); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 437 | mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 438 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 439 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 440 | LY_EVALID); |
| 441 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 442 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 443 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 444 | ext_p->name, mod->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 445 | LY_EVALID); |
| 446 | name = &ext_p->name[u + 1]; |
| 447 | /* find the extension definition there */ |
| 448 | for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) { |
| 449 | if (!strcmp(name, mod->parsed->extensions[u].name)) { |
| 450 | edef = &mod->parsed->extensions[u]; |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 455 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
| 456 | LY_EVALID); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 457 | /* TODO extension plugins */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 458 | |
| 459 | return LY_SUCCESS; |
| 460 | } |
| 461 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 462 | /** |
| 463 | * @brief Compile information from the if-feature statement |
| 464 | * @param[in] ctx Compile context. |
| 465 | * @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] | 466 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 467 | * @return LY_ERR value. |
| 468 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 469 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 470 | 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] | 471 | { |
| 472 | const char *c = *value; |
| 473 | int r, rc = EXIT_FAILURE; |
| 474 | int i, j, last_not, checkversion = 0; |
| 475 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 476 | uint8_t op; |
| 477 | struct iff_stack stack = {0, 0, NULL}; |
| 478 | struct lysc_feature *f; |
| 479 | |
| 480 | assert(c); |
| 481 | |
| 482 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 483 | for (i = j = last_not = 0; c[i]; i++) { |
| 484 | if (c[i] == '(') { |
| 485 | j++; |
| 486 | checkversion = 1; |
| 487 | continue; |
| 488 | } else if (c[i] == ')') { |
| 489 | j--; |
| 490 | continue; |
| 491 | } else if (isspace(c[i])) { |
| 492 | checkversion = 1; |
| 493 | continue; |
| 494 | } |
| 495 | |
| 496 | 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] | 497 | int sp; |
| 498 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 499 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 500 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 501 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 502 | return LY_EVALID; |
| 503 | } else if (!isspace(c[i + r])) { |
| 504 | /* feature name starting with the not/and/or */ |
| 505 | last_not = 0; |
| 506 | f_size++; |
| 507 | } else if (c[i] == 'n') { /* not operation */ |
| 508 | if (last_not) { |
| 509 | /* double not */ |
| 510 | expr_size = expr_size - 2; |
| 511 | last_not = 0; |
| 512 | } else { |
| 513 | last_not = 1; |
| 514 | } |
| 515 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 516 | if (f_exp != f_size) { |
| 517 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 518 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 519 | return LY_EVALID; |
| 520 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 521 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 522 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 523 | /* not a not operation */ |
| 524 | last_not = 0; |
| 525 | } |
| 526 | i += r; |
| 527 | } else { |
| 528 | f_size++; |
| 529 | last_not = 0; |
| 530 | } |
| 531 | expr_size++; |
| 532 | |
| 533 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 534 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 535 | i--; |
| 536 | break; |
| 537 | } |
| 538 | i++; |
| 539 | } |
| 540 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 541 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 542 | /* not matching count of ( and ) */ |
| 543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 544 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 545 | return LY_EVALID; |
| 546 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 547 | if (f_exp != f_size) { |
| 548 | /* features do not match the needed arguments for the logical operations */ |
| 549 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 550 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 551 | "the required number of operands for the operations.", *value); |
| 552 | return LY_EVALID; |
| 553 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 554 | |
| 555 | if (checkversion || expr_size > 1) { |
| 556 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 557 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 558 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 559 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 560 | return LY_EVALID; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | /* allocate the memory */ |
| 565 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 566 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 567 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 568 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 569 | |
| 570 | stack.size = expr_size; |
| 571 | f_size--; expr_size--; /* used as indexes from now */ |
| 572 | |
| 573 | for (i--; i >= 0; i--) { |
| 574 | if (c[i] == ')') { |
| 575 | /* push it on stack */ |
| 576 | iff_stack_push(&stack, LYS_IFF_RP); |
| 577 | continue; |
| 578 | } else if (c[i] == '(') { |
| 579 | /* pop from the stack into result all operators until ) */ |
| 580 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 581 | iff_setop(iff->expr, op, expr_size--); |
| 582 | } |
| 583 | continue; |
| 584 | } else if (isspace(c[i])) { |
| 585 | continue; |
| 586 | } |
| 587 | |
| 588 | /* end of operator or operand -> find beginning and get what is it */ |
| 589 | j = i + 1; |
| 590 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 591 | i--; |
| 592 | } |
| 593 | i++; /* go back by one step */ |
| 594 | |
| 595 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 596 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 597 | /* double not */ |
| 598 | iff_stack_pop(&stack); |
| 599 | } else { |
| 600 | /* not has the highest priority, so do not pop from the stack |
| 601 | * as in case of AND and OR */ |
| 602 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 603 | } |
| 604 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 605 | /* as for OR - pop from the stack all operators with the same or higher |
| 606 | * priority and store them to the result, then push the AND to the stack */ |
| 607 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 608 | op = iff_stack_pop(&stack); |
| 609 | iff_setop(iff->expr, op, expr_size--); |
| 610 | } |
| 611 | iff_stack_push(&stack, LYS_IFF_AND); |
| 612 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 613 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 614 | op = iff_stack_pop(&stack); |
| 615 | iff_setop(iff->expr, op, expr_size--); |
| 616 | } |
| 617 | iff_stack_push(&stack, LYS_IFF_OR); |
| 618 | } else { |
| 619 | /* feature name, length is j - i */ |
| 620 | |
| 621 | /* add it to the expression */ |
| 622 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 623 | |
| 624 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 625 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 626 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 627 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 628 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 629 | iff->features[f_size] = f; |
| 630 | LY_ARRAY_INCREMENT(iff->features); |
| 631 | f_size--; |
| 632 | } |
| 633 | } |
| 634 | while (stack.index) { |
| 635 | op = iff_stack_pop(&stack); |
| 636 | iff_setop(iff->expr, op, expr_size--); |
| 637 | } |
| 638 | |
| 639 | if (++expr_size || ++f_size) { |
| 640 | /* not all expected operators and operands found */ |
| 641 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 642 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 643 | rc = LY_EINT; |
| 644 | } else { |
| 645 | rc = LY_SUCCESS; |
| 646 | } |
| 647 | |
| 648 | error: |
| 649 | /* cleanup */ |
| 650 | iff_stack_clean(&stack); |
| 651 | |
| 652 | return rc; |
| 653 | } |
| 654 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 655 | /** |
| 656 | * @brief Compile information from the when statement |
| 657 | * @param[in] ctx Compile context. |
| 658 | * @param[in] when_p The parsed when statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 659 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 660 | * @return LY_ERR value. |
| 661 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 662 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 663 | 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] | 664 | { |
| 665 | unsigned int u; |
| 666 | LY_ERR ret = LY_SUCCESS; |
| 667 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 668 | *when = calloc(1, sizeof **when); |
| 669 | (*when)->refcount = 1; |
| 670 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
| 671 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 672 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 673 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 674 | COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 675 | |
| 676 | done: |
| 677 | return ret; |
| 678 | } |
| 679 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 680 | /** |
| 681 | * @brief Compile information from the must statement |
| 682 | * @param[in] ctx Compile context. |
| 683 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 684 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 685 | * @return LY_ERR value. |
| 686 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 687 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 688 | 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] | 689 | { |
| 690 | unsigned int u; |
| 691 | LY_ERR ret = LY_SUCCESS; |
| 692 | |
| 693 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 694 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 695 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 696 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 697 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 698 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 699 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 700 | COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 701 | |
| 702 | done: |
| 703 | return ret; |
| 704 | } |
| 705 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 706 | /** |
| 707 | * @brief Compile information from the import statement |
| 708 | * @param[in] ctx Compile context. |
| 709 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 710 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 711 | * @return LY_ERR value. |
| 712 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 713 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 714 | 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] | 715 | { |
| 716 | unsigned int u; |
| 717 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 718 | LY_ERR ret = LY_SUCCESS; |
| 719 | |
| 720 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 721 | COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 722 | imp->module = imp_p->module; |
| 723 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 724 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 725 | * 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] | 726 | * 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] | 727 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 728 | /* try to use filepath if present */ |
| 729 | if (imp->module->filepath) { |
| 730 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 731 | !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] | 732 | if (mod != imp->module) { |
| 733 | 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] | 734 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 735 | mod = NULL; |
| 736 | } |
| 737 | } |
| 738 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 739 | 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] | 740 | 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] | 741 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 742 | return LY_ENOTFOUND; |
| 743 | } |
| 744 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | done: |
| 748 | return ret; |
| 749 | } |
| 750 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 751 | /** |
| 752 | * @brief Compile information from the identity statement |
| 753 | * |
| 754 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 755 | * |
| 756 | * @param[in] ctx Compile context. |
| 757 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 758 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 759 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 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_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] | 764 | { |
| 765 | unsigned int u; |
| 766 | LY_ERR ret = LY_SUCCESS; |
| 767 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 768 | lysc_update_path(ctx, NULL, ident_p->name); |
| 769 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 770 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 771 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 772 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 773 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 774 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 775 | 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] | 776 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 777 | COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 778 | ident->flags = ident_p->flags; |
| 779 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 780 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 781 | done: |
| 782 | return ret; |
| 783 | } |
| 784 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 785 | /** |
| 786 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 787 | * |
| 788 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 789 | * |
| 790 | * @param[in] ctx Compile context for logging. |
| 791 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 792 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 793 | * @return LY_SUCCESS if everything is ok. |
| 794 | * @return LY_EVALID if the identity is derived from itself. |
| 795 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 796 | static LY_ERR |
| 797 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 798 | { |
| 799 | LY_ERR ret = LY_EVALID; |
| 800 | unsigned int u, v; |
| 801 | struct ly_set recursion = {0}; |
| 802 | struct lysc_ident *drv; |
| 803 | |
| 804 | if (!derived) { |
| 805 | return LY_SUCCESS; |
| 806 | } |
| 807 | |
| 808 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 809 | if (ident == derived[u]) { |
| 810 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 811 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 812 | goto cleanup; |
| 813 | } |
| 814 | ly_set_add(&recursion, derived[u], 0); |
| 815 | } |
| 816 | |
| 817 | for (v = 0; v < recursion.count; ++v) { |
| 818 | drv = recursion.objs[v]; |
| 819 | if (!drv->derived) { |
| 820 | continue; |
| 821 | } |
| 822 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 823 | if (ident == drv->derived[u]) { |
| 824 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 825 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 826 | goto cleanup; |
| 827 | } |
| 828 | ly_set_add(&recursion, drv->derived[u], 0); |
| 829 | } |
| 830 | } |
| 831 | ret = LY_SUCCESS; |
| 832 | |
| 833 | cleanup: |
| 834 | ly_set_erase(&recursion, NULL); |
| 835 | return ret; |
| 836 | } |
| 837 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 838 | /** |
| 839 | * @brief Find and process the referenced base identities from another identity or identityref |
| 840 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 841 | * 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] | 842 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 843 | * to distinguish these two use cases. |
| 844 | * |
| 845 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 846 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 847 | * @param[in] ident Referencing identity to work with. |
| 848 | * @param[in] bases Array of bases of identityref to fill in. |
| 849 | * @return LY_ERR value. |
| 850 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 851 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 852 | 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] | 853 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 854 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 855 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 856 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 857 | struct lysc_ident **idref; |
| 858 | |
| 859 | assert(ident || bases); |
| 860 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 861 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 862 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 863 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 864 | return LY_EVALID; |
| 865 | } |
| 866 | |
| 867 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 868 | s = strchr(bases_p[u], ':'); |
| 869 | if (s) { |
| 870 | /* prefixed identity */ |
| 871 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 872 | 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] | 873 | } else { |
| 874 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 875 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 876 | } |
| 877 | if (!mod) { |
| 878 | if (ident) { |
| 879 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 880 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 881 | } else { |
| 882 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 883 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 884 | } |
| 885 | return LY_EVALID; |
| 886 | } |
| 887 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 888 | if (mod->compiled && mod->compiled->identities) { |
| 889 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 890 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 891 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 892 | if (ident == &mod->compiled->identities[v]) { |
| 893 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 894 | "Identity \"%s\" is derived from itself.", ident->name); |
| 895 | return LY_EVALID; |
| 896 | } |
| 897 | 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] | 898 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 899 | 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] | 900 | *idref = ident; |
| 901 | } else { |
| 902 | /* we have match! store the found identity */ |
| 903 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 904 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 905 | } |
| 906 | break; |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | if (!idref || !(*idref)) { |
| 911 | if (ident) { |
| 912 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 913 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 914 | } else { |
| 915 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 916 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 917 | } |
| 918 | return LY_EVALID; |
| 919 | } |
| 920 | } |
| 921 | return LY_SUCCESS; |
| 922 | } |
| 923 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 924 | /** |
| 925 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 926 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 927 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 928 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 929 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 930 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 931 | static LY_ERR |
| 932 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 933 | { |
| 934 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 935 | |
| 936 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 937 | if (!idents_p[i].bases) { |
| 938 | continue; |
| 939 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 940 | lysc_update_path(ctx, NULL, idents[i].name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 941 | 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] | 942 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 943 | } |
| 944 | return LY_SUCCESS; |
| 945 | } |
| 946 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 947 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 948 | 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] | 949 | { |
| 950 | unsigned int offset = 0, u; |
| 951 | struct lysc_ctx context = {0}; |
| 952 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 953 | assert(ctx_sc || ctx); |
| 954 | |
| 955 | if (!ctx_sc) { |
| 956 | context.ctx = ctx; |
| 957 | context.mod = module; |
| 958 | context.path_len = 1; |
| 959 | context.path[0] = '/'; |
| 960 | ctx_sc = &context; |
| 961 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 962 | |
| 963 | if (!features_p) { |
| 964 | return LY_SUCCESS; |
| 965 | } |
| 966 | if (*features) { |
| 967 | offset = LY_ARRAY_SIZE(*features); |
| 968 | } |
| 969 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 970 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 971 | 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] | 972 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 973 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 974 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 975 | LY_ARRAY_INCREMENT(*features); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 976 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 977 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 978 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 979 | 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] | 980 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 981 | (*features)[offset + u].module = ctx_sc->mod; |
| 982 | |
| 983 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 984 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 985 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 986 | |
| 987 | return LY_SUCCESS; |
| 988 | } |
| 989 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 990 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 991 | * @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] | 992 | * |
| 993 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 994 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 995 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 996 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 997 | * being currently processed). |
| 998 | * @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] | 999 | * @return LY_SUCCESS if everything is ok. |
| 1000 | * @return LY_EVALID if the feature references indirectly itself. |
| 1001 | */ |
| 1002 | static LY_ERR |
| 1003 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1004 | { |
| 1005 | LY_ERR ret = LY_EVALID; |
| 1006 | unsigned int u, v; |
| 1007 | struct ly_set recursion = {0}; |
| 1008 | struct lysc_feature *drv; |
| 1009 | |
| 1010 | if (!depfeatures) { |
| 1011 | return LY_SUCCESS; |
| 1012 | } |
| 1013 | |
| 1014 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1015 | if (feature == depfeatures[u]) { |
| 1016 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1017 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1018 | goto cleanup; |
| 1019 | } |
| 1020 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1021 | } |
| 1022 | |
| 1023 | for (v = 0; v < recursion.count; ++v) { |
| 1024 | drv = recursion.objs[v]; |
| 1025 | if (!drv->depfeatures) { |
| 1026 | continue; |
| 1027 | } |
| 1028 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1029 | if (feature == drv->depfeatures[u]) { |
| 1030 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1031 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1032 | goto cleanup; |
| 1033 | } |
| 1034 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1035 | } |
| 1036 | } |
| 1037 | ret = LY_SUCCESS; |
| 1038 | |
| 1039 | cleanup: |
| 1040 | ly_set_erase(&recursion, NULL); |
| 1041 | return ret; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1045 | * @brief Create pre-compiled features array. |
| 1046 | * |
| 1047 | * See lys_feature_precompile() for more details. |
| 1048 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1049 | * @param[in] ctx Compile context. |
| 1050 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1051 | * @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] | 1052 | * @return LY_ERR value. |
| 1053 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1054 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1055 | 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] | 1056 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1057 | unsigned int u, v, x; |
| 1058 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1059 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1060 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1061 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1062 | /* find the preprecompiled feature */ |
| 1063 | LY_ARRAY_FOR(features, x) { |
| 1064 | if (strcmp(features[x].name, feature_p->name)) { |
| 1065 | continue; |
| 1066 | } |
| 1067 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1068 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1069 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1070 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1071 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1072 | COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, u, lys_compile_ext, ret, done); |
| 1073 | 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] | 1074 | if (feature->iffeatures) { |
| 1075 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1076 | if (feature->iffeatures[u].features) { |
| 1077 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1078 | /* check for circular dependency - direct reference first,... */ |
| 1079 | if (feature == feature->iffeatures[u].features[v]) { |
| 1080 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1081 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1082 | return LY_EVALID; |
| 1083 | } |
| 1084 | /* ... and indirect circular reference */ |
| 1085 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1086 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1087 | /* add itself into the dependants list */ |
| 1088 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1089 | *df = feature; |
| 1090 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1091 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1092 | } |
| 1093 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1094 | lysc_update_path(ctx, NULL, NULL); |
| 1095 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1096 | done: |
| 1097 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1098 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1099 | |
| 1100 | LOGINT(ctx->ctx); |
| 1101 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1102 | } |
| 1103 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1104 | /** |
| 1105 | * @brief Revert compiled list of features back to the precompiled state. |
| 1106 | * |
| 1107 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1108 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1109 | * |
| 1110 | * @param[in] ctx Compilation context. |
| 1111 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1112 | * and supposed to hold the off_features list. |
| 1113 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1114 | static void |
| 1115 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1116 | { |
| 1117 | unsigned int u, v; |
| 1118 | |
| 1119 | /* keep the off_features list until the complete lys_module is freed */ |
| 1120 | mod->off_features = mod->compiled->features; |
| 1121 | mod->compiled->features = NULL; |
| 1122 | |
| 1123 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1124 | * which may points into the data being freed here */ |
| 1125 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1126 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1127 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1128 | } |
| 1129 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1130 | mod->off_features[u].iffeatures = NULL; |
| 1131 | |
| 1132 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1133 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1134 | } |
| 1135 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1136 | mod->off_features[u].exts = NULL; |
| 1137 | } |
| 1138 | } |
| 1139 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1140 | /** |
| 1141 | * @brief Validate and normalize numeric value from a range definition. |
| 1142 | * @param[in] ctx Compile context. |
| 1143 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1144 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1145 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1146 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1147 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1148 | * @param[in] value String value of the range boundary. |
| 1149 | * @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. |
| 1150 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1151 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1152 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1153 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1154 | 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] | 1155 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1156 | size_t fraction = 0, size; |
| 1157 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1158 | *len = 0; |
| 1159 | |
| 1160 | assert(value); |
| 1161 | /* parse value */ |
| 1162 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1163 | return LY_EVALID; |
| 1164 | } |
| 1165 | |
| 1166 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1167 | ++(*len); |
| 1168 | } |
| 1169 | |
| 1170 | while (isdigit(value[*len])) { |
| 1171 | ++(*len); |
| 1172 | } |
| 1173 | |
| 1174 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1175 | if (basetype == LY_TYPE_DEC64) { |
| 1176 | goto decimal; |
| 1177 | } else { |
| 1178 | *valcopy = strndup(value, *len); |
| 1179 | return LY_SUCCESS; |
| 1180 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1181 | } |
| 1182 | fraction = *len; |
| 1183 | |
| 1184 | ++(*len); |
| 1185 | while (isdigit(value[*len])) { |
| 1186 | ++(*len); |
| 1187 | } |
| 1188 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1189 | if (basetype == LY_TYPE_DEC64) { |
| 1190 | decimal: |
| 1191 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1192 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1193 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1194 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1195 | *len, value, frdigits); |
| 1196 | return LY_EINVAL; |
| 1197 | } |
| 1198 | if (fraction) { |
| 1199 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1200 | } else { |
| 1201 | size = (*len) + frdigits + 1; |
| 1202 | } |
| 1203 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1204 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1205 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1206 | (*valcopy)[size - 1] = '\0'; |
| 1207 | if (fraction) { |
| 1208 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1209 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1210 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1211 | } else { |
| 1212 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1213 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1214 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1215 | } |
| 1216 | return LY_SUCCESS; |
| 1217 | } |
| 1218 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1219 | /** |
| 1220 | * @brief Check that values in range are in ascendant order. |
| 1221 | * @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] | 1222 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1223 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1224 | * @param[in] value Current value to check. |
| 1225 | * @param[in] prev_value The last seen value. |
| 1226 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1227 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1228 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1229 | 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] | 1230 | { |
| 1231 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1232 | 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] | 1233 | return LY_EEXIST; |
| 1234 | } |
| 1235 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1236 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1237 | return LY_EEXIST; |
| 1238 | } |
| 1239 | } |
| 1240 | return LY_SUCCESS; |
| 1241 | } |
| 1242 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1243 | /** |
| 1244 | * @brief Set min/max value of the range part. |
| 1245 | * @param[in] ctx Compile context. |
| 1246 | * @param[in] part Range part structure to fill. |
| 1247 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1248 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1249 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1250 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1251 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1252 | * @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] | 1253 | * @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] | 1254 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1255 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1256 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1257 | * frdigits value), LY_EMEM. |
| 1258 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1259 | static LY_ERR |
| 1260 | 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] | 1261 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1262 | { |
| 1263 | LY_ERR ret = LY_SUCCESS; |
| 1264 | char *valcopy = NULL; |
| 1265 | size_t len; |
| 1266 | |
| 1267 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1268 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1269 | LY_CHECK_GOTO(ret, finalize); |
| 1270 | } |
| 1271 | if (!valcopy && base_range) { |
| 1272 | if (max) { |
| 1273 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1274 | } else { |
| 1275 | part->min_64 = base_range->parts[0].min_64; |
| 1276 | } |
| 1277 | if (!first) { |
| 1278 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1279 | } |
| 1280 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1284 | case LY_TYPE_INT8: /* range */ |
| 1285 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1286 | 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] | 1287 | } else if (max) { |
| 1288 | part->max_64 = INT64_C(127); |
| 1289 | } else { |
| 1290 | part->min_64 = INT64_C(-128); |
| 1291 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1292 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1293 | 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] | 1294 | } |
| 1295 | break; |
| 1296 | case LY_TYPE_INT16: /* range */ |
| 1297 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1298 | 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] | 1299 | } else if (max) { |
| 1300 | part->max_64 = INT64_C(32767); |
| 1301 | } else { |
| 1302 | part->min_64 = INT64_C(-32768); |
| 1303 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1304 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1305 | 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] | 1306 | } |
| 1307 | break; |
| 1308 | case LY_TYPE_INT32: /* range */ |
| 1309 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1310 | 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] | 1311 | } else if (max) { |
| 1312 | part->max_64 = INT64_C(2147483647); |
| 1313 | } else { |
| 1314 | part->min_64 = INT64_C(-2147483648); |
| 1315 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1316 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1317 | 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] | 1318 | } |
| 1319 | break; |
| 1320 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1321 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1322 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1323 | 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] | 1324 | max ? &part->max_64 : &part->min_64); |
| 1325 | } else if (max) { |
| 1326 | part->max_64 = INT64_C(9223372036854775807); |
| 1327 | } else { |
| 1328 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1329 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1330 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1331 | 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] | 1332 | } |
| 1333 | break; |
| 1334 | case LY_TYPE_UINT8: /* range */ |
| 1335 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1336 | 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] | 1337 | } else if (max) { |
| 1338 | part->max_u64 = UINT64_C(255); |
| 1339 | } else { |
| 1340 | part->min_u64 = UINT64_C(0); |
| 1341 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1342 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1343 | 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] | 1344 | } |
| 1345 | break; |
| 1346 | case LY_TYPE_UINT16: /* range */ |
| 1347 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1348 | 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] | 1349 | } else if (max) { |
| 1350 | part->max_u64 = UINT64_C(65535); |
| 1351 | } else { |
| 1352 | part->min_u64 = UINT64_C(0); |
| 1353 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1354 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1355 | 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] | 1356 | } |
| 1357 | break; |
| 1358 | case LY_TYPE_UINT32: /* range */ |
| 1359 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1360 | 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] | 1361 | } else if (max) { |
| 1362 | part->max_u64 = UINT64_C(4294967295); |
| 1363 | } else { |
| 1364 | part->min_u64 = UINT64_C(0); |
| 1365 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1366 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1367 | 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] | 1368 | } |
| 1369 | break; |
| 1370 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1371 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1372 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1373 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1374 | 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] | 1375 | } else if (max) { |
| 1376 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1377 | } else { |
| 1378 | part->min_u64 = UINT64_C(0); |
| 1379 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1380 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1381 | 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] | 1382 | } |
| 1383 | break; |
| 1384 | default: |
| 1385 | LOGINT(ctx->ctx); |
| 1386 | ret = LY_EINT; |
| 1387 | } |
| 1388 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1389 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1390 | if (ret == LY_EDENIED) { |
| 1391 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1392 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1393 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1394 | } else if (ret == LY_EVALID) { |
| 1395 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1396 | "Invalid %s restriction - invalid value \"%s\".", |
| 1397 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1398 | } else if (ret == LY_EEXIST) { |
| 1399 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1400 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1401 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1402 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1403 | } else if (!ret && value) { |
| 1404 | *value = *value + len; |
| 1405 | } |
| 1406 | free(valcopy); |
| 1407 | return ret; |
| 1408 | } |
| 1409 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1410 | /** |
| 1411 | * @brief Compile the parsed range restriction. |
| 1412 | * @param[in] ctx Compile context. |
| 1413 | * @param[in] range_p Parsed range structure to compile. |
| 1414 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1415 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1416 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1417 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1418 | * range restriction must be more restrictive than the base_range. |
| 1419 | * @param[in,out] range Pointer to the created current range structure. |
| 1420 | * @return LY_ERR value. |
| 1421 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1422 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1423 | 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] | 1424 | struct lysc_range *base_range, struct lysc_range **range) |
| 1425 | { |
| 1426 | LY_ERR ret = LY_EVALID; |
| 1427 | const char *expr; |
| 1428 | struct lysc_range_part *parts = NULL, *part; |
| 1429 | int range_expected = 0, uns; |
| 1430 | unsigned int parts_done = 0, u, v; |
| 1431 | |
| 1432 | assert(range); |
| 1433 | assert(range_p); |
| 1434 | |
| 1435 | expr = range_p->arg; |
| 1436 | while(1) { |
| 1437 | if (isspace(*expr)) { |
| 1438 | ++expr; |
| 1439 | } else if (*expr == '\0') { |
| 1440 | if (range_expected) { |
| 1441 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1442 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1443 | length_restr ? "length" : "range", range_p->arg); |
| 1444 | goto cleanup; |
| 1445 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1446 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1447 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1448 | length_restr ? "length" : "range", range_p->arg); |
| 1449 | goto cleanup; |
| 1450 | } |
| 1451 | parts_done++; |
| 1452 | break; |
| 1453 | } else if (!strncmp(expr, "min", 3)) { |
| 1454 | if (parts) { |
| 1455 | /* min cannot be used elsewhere than in the first part */ |
| 1456 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1457 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1458 | expr - range_p->arg, range_p->arg); |
| 1459 | goto cleanup; |
| 1460 | } |
| 1461 | expr += 3; |
| 1462 | |
| 1463 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1464 | 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] | 1465 | part->max_64 = part->min_64; |
| 1466 | } else if (*expr == '|') { |
| 1467 | if (!parts || range_expected) { |
| 1468 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1469 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1470 | goto cleanup; |
| 1471 | } |
| 1472 | expr++; |
| 1473 | parts_done++; |
| 1474 | /* process next part of the expression */ |
| 1475 | } else if (!strncmp(expr, "..", 2)) { |
| 1476 | expr += 2; |
| 1477 | while (isspace(*expr)) { |
| 1478 | expr++; |
| 1479 | } |
| 1480 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1481 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1482 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1483 | goto cleanup; |
| 1484 | } |
| 1485 | /* continue expecting the upper boundary */ |
| 1486 | range_expected = 1; |
| 1487 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1488 | /* number */ |
| 1489 | if (range_expected) { |
| 1490 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1491 | 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] | 1492 | range_expected = 0; |
| 1493 | } else { |
| 1494 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1495 | 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] | 1496 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1497 | part->max_64 = part->min_64; |
| 1498 | } |
| 1499 | |
| 1500 | /* continue with possible another expression part */ |
| 1501 | } else if (!strncmp(expr, "max", 3)) { |
| 1502 | expr += 3; |
| 1503 | while (isspace(*expr)) { |
| 1504 | expr++; |
| 1505 | } |
| 1506 | if (*expr != '\0') { |
| 1507 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1508 | length_restr ? "length" : "range", expr); |
| 1509 | goto cleanup; |
| 1510 | } |
| 1511 | if (range_expected) { |
| 1512 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1513 | 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] | 1514 | range_expected = 0; |
| 1515 | } else { |
| 1516 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1517 | 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] | 1518 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1519 | part->min_64 = part->max_64; |
| 1520 | } |
| 1521 | } else { |
| 1522 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1523 | length_restr ? "length" : "range", expr); |
| 1524 | goto cleanup; |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | /* check with the previous range/length restriction */ |
| 1529 | if (base_range) { |
| 1530 | switch (basetype) { |
| 1531 | case LY_TYPE_BINARY: |
| 1532 | case LY_TYPE_UINT8: |
| 1533 | case LY_TYPE_UINT16: |
| 1534 | case LY_TYPE_UINT32: |
| 1535 | case LY_TYPE_UINT64: |
| 1536 | case LY_TYPE_STRING: |
| 1537 | uns = 1; |
| 1538 | break; |
| 1539 | case LY_TYPE_DEC64: |
| 1540 | case LY_TYPE_INT8: |
| 1541 | case LY_TYPE_INT16: |
| 1542 | case LY_TYPE_INT32: |
| 1543 | case LY_TYPE_INT64: |
| 1544 | uns = 0; |
| 1545 | break; |
| 1546 | default: |
| 1547 | LOGINT(ctx->ctx); |
| 1548 | ret = LY_EINT; |
| 1549 | goto cleanup; |
| 1550 | } |
| 1551 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1552 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1553 | goto baseerror; |
| 1554 | } |
| 1555 | /* current lower bound is not lower than the base */ |
| 1556 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1557 | /* base has single value */ |
| 1558 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1559 | /* both lower bounds are the same */ |
| 1560 | if (parts[u].min_64 != parts[u].max_64) { |
| 1561 | /* current continues with a range */ |
| 1562 | goto baseerror; |
| 1563 | } else { |
| 1564 | /* equal single values, move both forward */ |
| 1565 | ++v; |
| 1566 | continue; |
| 1567 | } |
| 1568 | } else { |
| 1569 | /* base is single value lower than current range, so the |
| 1570 | * value from base range is removed in the current, |
| 1571 | * move only base and repeat checking */ |
| 1572 | ++v; |
| 1573 | --u; |
| 1574 | continue; |
| 1575 | } |
| 1576 | } else { |
| 1577 | /* base is the range */ |
| 1578 | if (parts[u].min_64 == parts[u].max_64) { |
| 1579 | /* current is a single value */ |
| 1580 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1581 | /* current is behind the base range, so base range is omitted, |
| 1582 | * move the base and keep the current for further check */ |
| 1583 | ++v; |
| 1584 | --u; |
| 1585 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1586 | continue; |
| 1587 | } else { |
| 1588 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1589 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1590 | /* higher bound is higher than the current higher bound */ |
| 1591 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1592 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1593 | * continue with the same current, but move the base */ |
| 1594 | --u; |
| 1595 | ++v; |
| 1596 | continue; |
| 1597 | } |
| 1598 | /* current range starts within the base range but end behind it */ |
| 1599 | goto baseerror; |
| 1600 | } else { |
| 1601 | /* current range is smaller than the base, |
| 1602 | * move current, but stay with the base */ |
| 1603 | continue; |
| 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | } |
| 1608 | if (u != parts_done) { |
| 1609 | baseerror: |
| 1610 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1611 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1612 | length_restr ? "length" : "range", range_p->arg); |
| 1613 | goto cleanup; |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | if (!(*range)) { |
| 1618 | *range = calloc(1, sizeof **range); |
| 1619 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1620 | } |
| 1621 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1622 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1623 | if (range_p->eapptag) { |
| 1624 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1625 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1626 | } |
| 1627 | if (range_p->emsg) { |
| 1628 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1629 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1630 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1631 | if (range_p->dsc) { |
| 1632 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1633 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1634 | } |
| 1635 | if (range_p->ref) { |
| 1636 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1637 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1638 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1639 | /* extensions are taken only from the last range by the caller */ |
| 1640 | |
| 1641 | (*range)->parts = parts; |
| 1642 | parts = NULL; |
| 1643 | ret = LY_SUCCESS; |
| 1644 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1645 | LY_ARRAY_FREE(parts); |
| 1646 | |
| 1647 | return ret; |
| 1648 | } |
| 1649 | |
| 1650 | /** |
| 1651 | * @brief Checks pattern syntax. |
| 1652 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1653 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1654 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1655 | * @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] | 1656 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1657 | */ |
| 1658 | static LY_ERR |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1659 | 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] | 1660 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1661 | int idx, idx2, start, end, count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1662 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1663 | int err_code; |
| 1664 | const char *orig_ptr; |
| 1665 | PCRE2_SIZE err_offset; |
| 1666 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1667 | #define URANGE_LEN 19 |
| 1668 | char *ublock2urange[][2] = { |
| 1669 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1670 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1671 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1672 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1673 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1674 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1675 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1676 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1677 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1678 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1679 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1680 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1681 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1682 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1683 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1684 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1685 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1686 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1687 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1688 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1689 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1690 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1691 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1692 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1693 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1694 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1695 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1696 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1697 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1698 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1699 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1700 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1701 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1702 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1703 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1704 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1705 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1706 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1707 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1708 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1709 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1710 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1711 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1712 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1713 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1714 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1715 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1716 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1717 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1718 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1719 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1720 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1721 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1722 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1723 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1724 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1725 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1726 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1727 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1728 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1729 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1730 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1731 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1732 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1733 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1734 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1735 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1736 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1737 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1738 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1739 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1740 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1741 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1742 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1743 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1744 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1745 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1746 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1747 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1748 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1749 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1750 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1751 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1752 | {NULL, NULL} |
| 1753 | }; |
| 1754 | |
| 1755 | /* adjust the expression to a Perl equivalent |
| 1756 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1757 | |
| 1758 | /* we need to replace all "$" with "\$", count them now */ |
| 1759 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1760 | |
| 1761 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1762 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1763 | perl_regex[0] = '\0'; |
| 1764 | |
| 1765 | ptr = perl_regex; |
| 1766 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1767 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1768 | if (orig_ptr[0] == '$') { |
| 1769 | ptr += sprintf(ptr, "\\$"); |
| 1770 | } else if (orig_ptr[0] == '^') { |
| 1771 | ptr += sprintf(ptr, "\\^"); |
| 1772 | } else { |
| 1773 | ptr[0] = orig_ptr[0]; |
| 1774 | ++ptr; |
| 1775 | } |
| 1776 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1777 | ptr[0] = '\0'; |
| 1778 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1779 | |
| 1780 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1781 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1782 | start = ptr - perl_regex; |
| 1783 | |
| 1784 | ptr = strchr(ptr, '}'); |
| 1785 | if (!ptr) { |
| 1786 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1787 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1788 | free(perl_regex); |
| 1789 | return LY_EVALID; |
| 1790 | } |
| 1791 | end = (ptr - perl_regex) + 1; |
| 1792 | |
| 1793 | /* need more space */ |
| 1794 | if (end - start < URANGE_LEN) { |
| 1795 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1796 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1797 | } |
| 1798 | |
| 1799 | /* find our range */ |
| 1800 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1801 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1802 | break; |
| 1803 | } |
| 1804 | } |
| 1805 | if (!ublock2urange[idx][0]) { |
| 1806 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1807 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1808 | free(perl_regex); |
| 1809 | return LY_EVALID; |
| 1810 | } |
| 1811 | |
| 1812 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1813 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1814 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1815 | ++count; |
| 1816 | } |
| 1817 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1818 | --count; |
| 1819 | } |
| 1820 | } |
| 1821 | if (count) { |
| 1822 | /* skip brackets */ |
| 1823 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1824 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1825 | } else { |
| 1826 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1827 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1832 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 1833 | 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] | 1834 | &err_code, &err_offset, NULL); |
| 1835 | if (!code_local) { |
| 1836 | PCRE2_UCHAR err_msg[256] = {0}; |
| 1837 | pcre2_get_error_message(err_code, err_msg, 256); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1838 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1839 | free(perl_regex); |
| 1840 | return LY_EVALID; |
| 1841 | } |
| 1842 | free(perl_regex); |
| 1843 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1844 | if (code) { |
| 1845 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1846 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1847 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | return LY_SUCCESS; |
| 1851 | |
| 1852 | #undef URANGE_LEN |
| 1853 | } |
| 1854 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1855 | /** |
| 1856 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1857 | * @param[in] ctx Compile context. |
| 1858 | * @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] | 1859 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1860 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1861 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1862 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1863 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1864 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1865 | 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] | 1866 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1867 | { |
| 1868 | struct lysc_pattern **pattern; |
| 1869 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1870 | LY_ERR ret = LY_SUCCESS; |
| 1871 | |
| 1872 | /* first, copy the patterns from the base type */ |
| 1873 | if (base_patterns) { |
| 1874 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1875 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1876 | } |
| 1877 | |
| 1878 | LY_ARRAY_FOR(patterns_p, u) { |
| 1879 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1880 | *pattern = calloc(1, sizeof **pattern); |
| 1881 | ++(*pattern)->refcount; |
| 1882 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1883 | 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] | 1884 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1885 | |
| 1886 | if (patterns_p[u].arg[0] == 0x15) { |
| 1887 | (*pattern)->inverted = 1; |
| 1888 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1889 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1890 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1891 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1892 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 1893 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1894 | COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, v, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1895 | } |
| 1896 | done: |
| 1897 | return ret; |
| 1898 | } |
| 1899 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1900 | /** |
| 1901 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1902 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1903 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1904 | 0 /* LY_TYPE_UNKNOWN */, |
| 1905 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1906 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1907 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1908 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1909 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 1910 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1911 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1912 | 0 /* LY_TYPE_BOOL */, |
| 1913 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 1914 | 0 /* LY_TYPE_EMPTY */, |
| 1915 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 1916 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 1917 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 1918 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1919 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 1920 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1921 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1922 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1923 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 1924 | }; |
| 1925 | |
| 1926 | /** |
| 1927 | * @brief stringification of the YANG built-in data types |
| 1928 | */ |
| 1929 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 1930 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 1931 | "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] | 1932 | }; |
| 1933 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1934 | /** |
| 1935 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 1936 | * @param[in] ctx Compile context. |
| 1937 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 1938 | * @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] | 1939 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 1940 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 1941 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1942 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1943 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1944 | 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] | 1945 | 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] | 1946 | { |
| 1947 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 1948 | unsigned int u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1949 | int32_t value = 0; |
| 1950 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1951 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1952 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1953 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1954 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 1955 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 1956 | return LY_EVALID; |
| 1957 | } |
| 1958 | |
| 1959 | LY_ARRAY_FOR(enums_p, u) { |
| 1960 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 1961 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1962 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 1963 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1964 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1965 | if (base_enums) { |
| 1966 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 1967 | LY_ARRAY_FOR(base_enums, v) { |
| 1968 | if (!strcmp(e->name, base_enums[v].name)) { |
| 1969 | break; |
| 1970 | } |
| 1971 | } |
| 1972 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 1973 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1974 | "Invalid %s - derived type adds new item \"%s\".", |
| 1975 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 1976 | return LY_EVALID; |
| 1977 | } |
| 1978 | match = v; |
| 1979 | } |
| 1980 | |
| 1981 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1982 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1983 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1984 | e->value = (int32_t)enums_p[u].value; |
| 1985 | if (!u || e->value >= value) { |
| 1986 | value = e->value + 1; |
| 1987 | } |
| 1988 | /* check collision with other values */ |
| 1989 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1990 | if (e->value == (*enums)[v].value) { |
| 1991 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1992 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 1993 | e->value, e->name, (*enums)[v].name); |
| 1994 | return LY_EVALID; |
| 1995 | } |
| 1996 | } |
| 1997 | } else if (base_enums) { |
| 1998 | /* inherit the assigned value */ |
| 1999 | e->value = base_enums[match].value; |
| 2000 | if (!u || e->value >= value) { |
| 2001 | value = e->value + 1; |
| 2002 | } |
| 2003 | } else { |
| 2004 | /* assign value automatically */ |
| 2005 | if (u && value == INT32_MIN) { |
| 2006 | /* counter overflow */ |
| 2007 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2008 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2009 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2010 | return LY_EVALID; |
| 2011 | } |
| 2012 | e->value = value++; |
| 2013 | } |
| 2014 | } else { /* LY_TYPE_BITS */ |
| 2015 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2016 | e->value = (int32_t)enums_p[u].value; |
| 2017 | if (!u || (uint32_t)e->value >= position) { |
| 2018 | position = (uint32_t)e->value + 1; |
| 2019 | } |
| 2020 | /* check collision with other values */ |
| 2021 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2022 | if (e->value == (*enums)[v].value) { |
| 2023 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2024 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2025 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2026 | return LY_EVALID; |
| 2027 | } |
| 2028 | } |
| 2029 | } else if (base_enums) { |
| 2030 | /* inherit the assigned value */ |
| 2031 | e->value = base_enums[match].value; |
| 2032 | if (!u || (uint32_t)e->value >= position) { |
| 2033 | position = (uint32_t)e->value + 1; |
| 2034 | } |
| 2035 | } else { |
| 2036 | /* assign value automatically */ |
| 2037 | if (u && position == 0) { |
| 2038 | /* counter overflow */ |
| 2039 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2040 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2041 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2042 | return LY_EVALID; |
| 2043 | } |
| 2044 | e->value = position++; |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | if (base_enums) { |
| 2049 | /* the assigned values must not change from the derived type */ |
| 2050 | if (e->value != base_enums[match].value) { |
| 2051 | if (basetype == LY_TYPE_ENUM) { |
| 2052 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2053 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2054 | e->name, base_enums[match].value, e->value); |
| 2055 | } else { |
| 2056 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2057 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2058 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2059 | } |
| 2060 | return LY_EVALID; |
| 2061 | } |
| 2062 | } |
| 2063 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2064 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done); |
| 2065 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, v, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2066 | |
| 2067 | if (basetype == LY_TYPE_BITS) { |
| 2068 | /* keep bits ordered by position */ |
| 2069 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2070 | if (v != u) { |
| 2071 | memcpy(&storage, e, sizeof *e); |
| 2072 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2073 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2074 | } |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | done: |
| 2079 | return ret; |
| 2080 | } |
| 2081 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2082 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2083 | for ((NODE) = (NODE)->parent; \ |
| 2084 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 2085 | (NODE) = (NODE)->parent); \ |
| 2086 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2087 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2088 | TERM; \ |
| 2089 | } |
| 2090 | |
| 2091 | /** |
| 2092 | * @brief Validate the predicate(s) from the leafref path. |
| 2093 | * @param[in] ctx Compile context |
| 2094 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2095 | * 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] | 2096 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2097 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2098 | * @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] | 2099 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2100 | */ |
| 2101 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2102 | 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] | 2103 | 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] | 2104 | { |
| 2105 | LY_ERR ret = LY_EVALID; |
| 2106 | const struct lys_module *mod; |
| 2107 | const struct lysc_node *src_node, *dst_node; |
| 2108 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2109 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2110 | unsigned int dest_parent_times, c; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2111 | const char *start, *end, *pke_end; |
| 2112 | struct ly_set keys = {0}; |
| 2113 | int i; |
| 2114 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2115 | assert(path_context); |
| 2116 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2117 | while (**predicate == '[') { |
| 2118 | start = (*predicate)++; |
| 2119 | |
| 2120 | while (isspace(**predicate)) { |
| 2121 | ++(*predicate); |
| 2122 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2123 | 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] | 2124 | while (isspace(**predicate)) { |
| 2125 | ++(*predicate); |
| 2126 | } |
| 2127 | if (**predicate != '=') { |
| 2128 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2129 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2130 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2131 | goto cleanup; |
| 2132 | } |
| 2133 | ++(*predicate); |
| 2134 | while (isspace(**predicate)) { |
| 2135 | ++(*predicate); |
| 2136 | } |
| 2137 | |
| 2138 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2139 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2140 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2141 | goto cleanup; |
| 2142 | } |
| 2143 | --pke_end; |
| 2144 | while (isspace(*pke_end)) { |
| 2145 | --pke_end; |
| 2146 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2147 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2148 | /* localize path-key-expr */ |
| 2149 | pke_start = path_key_expr = *predicate; |
| 2150 | /* move after the current predicate */ |
| 2151 | *predicate = end + 1; |
| 2152 | |
| 2153 | /* source (must be leaf or leaf-list) */ |
| 2154 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2155 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2156 | if (!mod) { |
| 2157 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2158 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2159 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2160 | goto cleanup; |
| 2161 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2162 | if (!mod->implemented) { |
| 2163 | /* make the module implemented */ |
| 2164 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2165 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2166 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2167 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2168 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2169 | src_node = NULL; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2170 | if (!(context_node->flags & LYS_KEYLESS)) { |
| 2171 | struct lysc_node *key; |
| 2172 | for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) { |
| 2173 | if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') { |
| 2174 | src_node = key; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2175 | break; |
| 2176 | } |
| 2177 | } |
| 2178 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2179 | if (!src_node) { |
| 2180 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2181 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2182 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2183 | goto cleanup; |
| 2184 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2185 | |
| 2186 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2187 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2188 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2189 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2190 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2191 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2192 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2193 | *predicate - start, start, src_node->name); |
| 2194 | goto cleanup; |
| 2195 | } |
| 2196 | |
| 2197 | /* destination */ |
| 2198 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2199 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2200 | |
| 2201 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2202 | if (strncmp(path_key_expr, "current", 7)) { |
| 2203 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2204 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2205 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2206 | *predicate - start, start); |
| 2207 | goto cleanup; |
| 2208 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2209 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2210 | if (*path_key_expr != '(') { |
| 2211 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2212 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2213 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2214 | if (*path_key_expr != ')') { |
| 2215 | goto error_current_function_invocation; |
| 2216 | } |
| 2217 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2218 | |
| 2219 | if (*path_key_expr != '/') { |
| 2220 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2221 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2222 | *predicate - start, start); |
| 2223 | goto cleanup; |
| 2224 | } |
| 2225 | ++path_key_expr; |
| 2226 | while (isspace(*path_key_expr)) { |
| 2227 | ++path_key_expr; |
| 2228 | } |
| 2229 | |
| 2230 | /* rel-path-keyexpr: |
| 2231 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2232 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2233 | ++dest_parent_times; |
| 2234 | path_key_expr += 2; |
| 2235 | while (isspace(*path_key_expr)) { |
| 2236 | ++path_key_expr; |
| 2237 | } |
| 2238 | if (*path_key_expr != '/') { |
| 2239 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2240 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2241 | *predicate - start, start); |
| 2242 | goto cleanup; |
| 2243 | } |
| 2244 | ++path_key_expr; |
| 2245 | while (isspace(*path_key_expr)) { |
| 2246 | ++path_key_expr; |
| 2247 | } |
| 2248 | |
| 2249 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2250 | * all schema nodes that cannot be instantiated in data tree */ |
| 2251 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2252 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2253 | *predicate - start, start); |
| 2254 | } |
| 2255 | if (!dest_parent_times) { |
| 2256 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2257 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2258 | *predicate - start, start); |
| 2259 | goto cleanup; |
| 2260 | } |
| 2261 | if (path_key_expr == pke_end) { |
| 2262 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2263 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2264 | *predicate - start, start); |
| 2265 | goto cleanup; |
| 2266 | } |
| 2267 | |
| 2268 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2269 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2270 | 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] | 2271 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2272 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2273 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2274 | goto cleanup; |
| 2275 | } |
| 2276 | |
| 2277 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2278 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2279 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2280 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2281 | } |
| 2282 | if (!mod) { |
| 2283 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2284 | "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] | 2285 | *predicate - start, start, dst_len, dst); |
| 2286 | goto cleanup; |
| 2287 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2288 | if (!mod->implemented) { |
| 2289 | /* make the module implemented */ |
| 2290 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2291 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2292 | |
| 2293 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2294 | if (!dst_node) { |
| 2295 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2296 | "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] | 2297 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2298 | goto cleanup; |
| 2299 | } |
| 2300 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2301 | 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] | 2302 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2303 | "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] | 2304 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2305 | goto cleanup; |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | ret = LY_SUCCESS; |
| 2310 | cleanup: |
| 2311 | ly_set_erase(&keys, NULL); |
| 2312 | return ret; |
| 2313 | } |
| 2314 | |
| 2315 | /** |
| 2316 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2317 | * |
| 2318 | * path-arg = absolute-path / relative-path |
| 2319 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2320 | * relative-path = 1*(".." "/") descendant-path |
| 2321 | * |
| 2322 | * @param[in,out] path Path to parse. |
| 2323 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2324 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2325 | * @param[out] name Name of the token. |
| 2326 | * @param[out] nam_len Length of the name. |
| 2327 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2328 | * must not be changed between consecutive calls. -1 if the |
| 2329 | * path is absolute. |
| 2330 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2331 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2332 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2333 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2334 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2335 | int *parent_times, int *has_predicate) |
| 2336 | { |
| 2337 | int par_times = 0; |
| 2338 | |
| 2339 | assert(path && *path); |
| 2340 | assert(parent_times); |
| 2341 | assert(prefix); |
| 2342 | assert(prefix_len); |
| 2343 | assert(name); |
| 2344 | assert(name_len); |
| 2345 | assert(has_predicate); |
| 2346 | |
| 2347 | *prefix = NULL; |
| 2348 | *prefix_len = 0; |
| 2349 | *name = NULL; |
| 2350 | *name_len = 0; |
| 2351 | *has_predicate = 0; |
| 2352 | |
| 2353 | if (!*parent_times) { |
| 2354 | if (!strncmp(*path, "..", 2)) { |
| 2355 | *path += 2; |
| 2356 | ++par_times; |
| 2357 | while (!strncmp(*path, "/..", 3)) { |
| 2358 | *path += 3; |
| 2359 | ++par_times; |
| 2360 | } |
| 2361 | } |
| 2362 | if (par_times) { |
| 2363 | *parent_times = par_times; |
| 2364 | } else { |
| 2365 | *parent_times = -1; |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | if (**path != '/') { |
| 2370 | return LY_EINVAL; |
| 2371 | } |
| 2372 | /* skip '/' */ |
| 2373 | ++(*path); |
| 2374 | |
| 2375 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2376 | 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] | 2377 | |
| 2378 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2379 | /* path continues by another token or this is the last token */ |
| 2380 | return LY_SUCCESS; |
| 2381 | } else if ((*path)[0] != '[') { |
| 2382 | /* unexpected character */ |
| 2383 | return LY_EINVAL; |
| 2384 | } else { |
| 2385 | /* predicate starting with [ */ |
| 2386 | *has_predicate = 1; |
| 2387 | return LY_SUCCESS; |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2392 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2393 | * |
| 2394 | * The set of features used for target must be a subset of features used for the leafref. |
| 2395 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2396 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2397 | * |
| 2398 | * @param[in] refnode The leafref node. |
| 2399 | * @param[in] target Tha target node of the leafref. |
| 2400 | * @return LY_SUCCESS or LY_EVALID; |
| 2401 | */ |
| 2402 | static LY_ERR |
| 2403 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2404 | { |
| 2405 | LY_ERR ret = LY_EVALID; |
| 2406 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2407 | unsigned int u, v, count; |
| 2408 | struct ly_set features = {0}; |
| 2409 | |
| 2410 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2411 | if (iter->iffeatures) { |
| 2412 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2413 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2414 | 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] | 2415 | } |
| 2416 | } |
| 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2421 | * So when adding features applicable to the target into the features set, we should not be |
| 2422 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2423 | * to the leafref itself. */ |
| 2424 | count = features.count; |
| 2425 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2426 | if (iter->iffeatures) { |
| 2427 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2428 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2429 | 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] | 2430 | /* new feature was added (or LY_EMEM) */ |
| 2431 | goto cleanup; |
| 2432 | } |
| 2433 | } |
| 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | ret = LY_SUCCESS; |
| 2438 | |
| 2439 | cleanup: |
| 2440 | ly_set_erase(&features, NULL); |
| 2441 | return ret; |
| 2442 | } |
| 2443 | |
| 2444 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2445 | * @brief Validate the leafref path. |
| 2446 | * @param[in] ctx Compile context |
| 2447 | * @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] | 2448 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2449 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2450 | */ |
| 2451 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2452 | 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] | 2453 | { |
| 2454 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2455 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2456 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2457 | const char *id, *prefix, *name; |
| 2458 | size_t prefix_len, name_len; |
| 2459 | int parent_times = 0, has_predicate; |
| 2460 | unsigned int iter, u; |
| 2461 | LY_ERR ret = LY_SUCCESS; |
| 2462 | |
| 2463 | assert(ctx); |
| 2464 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2465 | assert(leafref); |
| 2466 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2467 | ctx->path[0] = '\0'; |
| 2468 | lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 2469 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2470 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2471 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2472 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2473 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2474 | if (!iter) { /* first iteration */ |
| 2475 | /* precess ".." in relative paths */ |
| 2476 | if (parent_times > 0) { |
| 2477 | /* move from the context node */ |
| 2478 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2479 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2480 | * all schema nodes that cannot be instantiated in data tree */ |
| 2481 | 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] | 2482 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2483 | } |
| 2484 | } |
| 2485 | } |
| 2486 | |
| 2487 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2488 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2489 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2490 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2491 | } |
| 2492 | if (!mod) { |
| 2493 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2494 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2495 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2496 | return LY_EVALID; |
| 2497 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2498 | if (!mod->implemented) { |
| 2499 | /* make the module implemented */ |
| 2500 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2501 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2502 | |
| 2503 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2504 | if (!node) { |
| 2505 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2506 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2507 | return LY_EVALID; |
| 2508 | } |
| 2509 | parent = node; |
| 2510 | |
| 2511 | if (has_predicate) { |
| 2512 | /* we have predicate, so the current result must be list */ |
| 2513 | if (node->nodetype != LYS_LIST) { |
| 2514 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2515 | "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] | 2516 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2517 | return LY_EVALID; |
| 2518 | } |
| 2519 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2520 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2521 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | ++iter; |
| 2525 | } |
| 2526 | if (ret) { |
| 2527 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2528 | "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] | 2529 | return LY_EVALID; |
| 2530 | } |
| 2531 | |
| 2532 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2533 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2534 | "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] | 2535 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2536 | return LY_EVALID; |
| 2537 | } |
| 2538 | |
| 2539 | /* check status */ |
| 2540 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2541 | return LY_EVALID; |
| 2542 | } |
| 2543 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2544 | /* check config */ |
| 2545 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2546 | if (node->flags & LYS_CONFIG_R) { |
| 2547 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2548 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2549 | leafref->path); |
| 2550 | return LY_EVALID; |
| 2551 | } |
| 2552 | } |
| 2553 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2554 | /* store the target's type and check for circular chain of leafrefs */ |
| 2555 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2556 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2557 | if (type == (struct lysc_type*)leafref) { |
| 2558 | /* circular chain detected */ |
| 2559 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2560 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2561 | return LY_EVALID; |
| 2562 | } |
| 2563 | } |
| 2564 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2565 | /* check if leafref and its target are under common if-features */ |
| 2566 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2567 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2568 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2569 | leafref->path); |
| 2570 | return LY_EVALID; |
| 2571 | } |
| 2572 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2573 | ctx->path_len = 1; |
| 2574 | ctx->path[1] = '\0'; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2575 | return LY_SUCCESS; |
| 2576 | } |
| 2577 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2578 | 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] | 2579 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2580 | /** |
| 2581 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2582 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2583 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2584 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2585 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2586 | * @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] | 2587 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2588 | * @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] | 2589 | * @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] | 2590 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2591 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2592 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2593 | * @return LY_ERR value. |
| 2594 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2595 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2596 | 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] | 2597 | 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] | 2598 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2599 | { |
| 2600 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2601 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2602 | struct lysc_type_bin *bin; |
| 2603 | struct lysc_type_num *num; |
| 2604 | struct lysc_type_str *str; |
| 2605 | struct lysc_type_bits *bits; |
| 2606 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2607 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2608 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2609 | struct lysc_type_union *un, *un_aux; |
| 2610 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2611 | |
| 2612 | switch (basetype) { |
| 2613 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2614 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2615 | |
| 2616 | /* 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] | 2617 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2618 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2619 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2620 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2621 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | if (tpdfname) { |
| 2626 | type_p->compiled = *type; |
| 2627 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2628 | } |
| 2629 | break; |
| 2630 | case LY_TYPE_BITS: |
| 2631 | /* RFC 7950 9.7 - bits */ |
| 2632 | bits = (struct lysc_type_bits*)(*type); |
| 2633 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2634 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2635 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2636 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2637 | } |
| 2638 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2639 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2640 | /* 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] | 2641 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2642 | 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] | 2643 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2644 | 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] | 2645 | free(*type); |
| 2646 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2647 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2648 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | if (tpdfname) { |
| 2652 | type_p->compiled = *type; |
| 2653 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2654 | } |
| 2655 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2656 | case LY_TYPE_DEC64: |
| 2657 | dec = (struct lysc_type_dec*)(*type); |
| 2658 | |
| 2659 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2660 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2661 | if (!type_p->fraction_digits) { |
| 2662 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2663 | 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] | 2664 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2665 | 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] | 2666 | free(*type); |
| 2667 | *type = NULL; |
| 2668 | } |
| 2669 | return LY_EVALID; |
| 2670 | } |
| 2671 | } else if (type_p->fraction_digits) { |
| 2672 | /* 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] | 2673 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2674 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2675 | "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] | 2676 | tpdfname); |
| 2677 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2678 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2679 | "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] | 2680 | free(*type); |
| 2681 | *type = NULL; |
| 2682 | } |
| 2683 | return LY_EVALID; |
| 2684 | } |
| 2685 | dec->fraction_digits = type_p->fraction_digits; |
| 2686 | |
| 2687 | /* RFC 7950 9.2.4 - range */ |
| 2688 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2689 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2690 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2691 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2692 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2693 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | if (tpdfname) { |
| 2697 | type_p->compiled = *type; |
| 2698 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2699 | } |
| 2700 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2701 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2702 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2703 | |
| 2704 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2705 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2706 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2707 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2708 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2709 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2710 | } |
| 2711 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2712 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2713 | } |
| 2714 | |
| 2715 | /* RFC 7950 9.4.5 - pattern */ |
| 2716 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2717 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2718 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2719 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2720 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2721 | } |
| 2722 | |
| 2723 | if (tpdfname) { |
| 2724 | type_p->compiled = *type; |
| 2725 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2726 | } |
| 2727 | break; |
| 2728 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2729 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2730 | |
| 2731 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2732 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2733 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2734 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2735 | } |
| 2736 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2737 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2738 | /* 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] | 2739 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2740 | 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] | 2741 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2742 | 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] | 2743 | free(*type); |
| 2744 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2745 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2746 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | if (tpdfname) { |
| 2750 | type_p->compiled = *type; |
| 2751 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2752 | } |
| 2753 | break; |
| 2754 | case LY_TYPE_INT8: |
| 2755 | case LY_TYPE_UINT8: |
| 2756 | case LY_TYPE_INT16: |
| 2757 | case LY_TYPE_UINT16: |
| 2758 | case LY_TYPE_INT32: |
| 2759 | case LY_TYPE_UINT32: |
| 2760 | case LY_TYPE_INT64: |
| 2761 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2762 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2763 | |
| 2764 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2765 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2766 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2767 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2768 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2769 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | if (tpdfname) { |
| 2774 | type_p->compiled = *type; |
| 2775 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2776 | } |
| 2777 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2778 | case LY_TYPE_IDENT: |
| 2779 | idref = (struct lysc_type_identityref*)(*type); |
| 2780 | |
| 2781 | /* RFC 7950 9.10.2 - base */ |
| 2782 | if (type_p->bases) { |
| 2783 | if (base) { |
| 2784 | /* only the directly derived identityrefs can contain base specification */ |
| 2785 | if (tpdfname) { |
| 2786 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2787 | "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] | 2788 | tpdfname); |
| 2789 | } else { |
| 2790 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2791 | "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] | 2792 | free(*type); |
| 2793 | *type = NULL; |
| 2794 | } |
| 2795 | return LY_EVALID; |
| 2796 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2797 | 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] | 2798 | } |
| 2799 | |
| 2800 | if (!base && !type_p->flags) { |
| 2801 | /* type derived from identityref built-in type must contain at least one base */ |
| 2802 | if (tpdfname) { |
| 2803 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2804 | } else { |
| 2805 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2806 | free(*type); |
| 2807 | *type = NULL; |
| 2808 | } |
| 2809 | return LY_EVALID; |
| 2810 | } |
| 2811 | |
| 2812 | if (tpdfname) { |
| 2813 | type_p->compiled = *type; |
| 2814 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2815 | } |
| 2816 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2817 | case LY_TYPE_LEAFREF: |
| 2818 | /* RFC 7950 9.9.3 - require-instance */ |
| 2819 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2820 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2821 | if (tpdfname) { |
| 2822 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2823 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2824 | } else { |
| 2825 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2826 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2827 | free(*type); |
| 2828 | *type = NULL; |
| 2829 | } |
| 2830 | return LY_EVALID; |
| 2831 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2832 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2833 | } else if (base) { |
| 2834 | /* inherit */ |
| 2835 | ((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] | 2836 | } else { |
| 2837 | /* default is true */ |
| 2838 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2839 | } |
| 2840 | if (type_p->path) { |
| 2841 | 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] | 2842 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2843 | } else if (base) { |
| 2844 | 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] | 2845 | ((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] | 2846 | } else if (tpdfname) { |
| 2847 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2848 | return LY_EVALID; |
| 2849 | } else { |
| 2850 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2851 | free(*type); |
| 2852 | *type = NULL; |
| 2853 | return LY_EVALID; |
| 2854 | } |
| 2855 | if (tpdfname) { |
| 2856 | type_p->compiled = *type; |
| 2857 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2858 | } |
| 2859 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2860 | case LY_TYPE_INST: |
| 2861 | /* RFC 7950 9.9.3 - require-instance */ |
| 2862 | if (type_p->flags & LYS_SET_REQINST) { |
| 2863 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2864 | } else { |
| 2865 | /* default is true */ |
| 2866 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2867 | } |
| 2868 | |
| 2869 | if (tpdfname) { |
| 2870 | type_p->compiled = *type; |
| 2871 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2872 | } |
| 2873 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2874 | case LY_TYPE_UNION: |
| 2875 | un = (struct lysc_type_union*)(*type); |
| 2876 | |
| 2877 | /* RFC 7950 7.4 - type */ |
| 2878 | if (type_p->types) { |
| 2879 | if (base) { |
| 2880 | /* only the directly derived union can contain types specification */ |
| 2881 | if (tpdfname) { |
| 2882 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2883 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2884 | tpdfname); |
| 2885 | } else { |
| 2886 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2887 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2888 | free(*type); |
| 2889 | *type = NULL; |
| 2890 | } |
| 2891 | return LY_EVALID; |
| 2892 | } |
| 2893 | /* compile the type */ |
| 2894 | additional = 0; |
| 2895 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 2896 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2897 | 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] | 2898 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2899 | /* add space for additional types from the union subtype */ |
| 2900 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 2901 | 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))); |
| 2902 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2903 | un->types = (void*)((uint32_t*)(p) + 1); |
| 2904 | |
| 2905 | /* copy subtypes of the subtype union */ |
| 2906 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 2907 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2908 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2909 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2910 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2911 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 2912 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 2913 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 2914 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2915 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 2916 | /* TODO extensions */ |
| 2917 | |
| 2918 | } else { |
| 2919 | un->types[u + additional] = un_aux->types[v]; |
| 2920 | ++un_aux->types[v]->refcount; |
| 2921 | } |
| 2922 | ++additional; |
| 2923 | LY_ARRAY_INCREMENT(un->types); |
| 2924 | } |
| 2925 | /* compensate u increment in main loop */ |
| 2926 | --additional; |
| 2927 | |
| 2928 | /* free the replaced union subtype */ |
| 2929 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2930 | } else { |
| 2931 | LY_ARRAY_INCREMENT(un->types); |
| 2932 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | if (!base && !type_p->flags) { |
| 2937 | /* type derived from union built-in type must contain at least one type */ |
| 2938 | if (tpdfname) { |
| 2939 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2940 | } else { |
| 2941 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2942 | free(*type); |
| 2943 | *type = NULL; |
| 2944 | } |
| 2945 | return LY_EVALID; |
| 2946 | } |
| 2947 | |
| 2948 | if (tpdfname) { |
| 2949 | type_p->compiled = *type; |
| 2950 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2951 | } |
| 2952 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2953 | case LY_TYPE_BOOL: |
| 2954 | case LY_TYPE_EMPTY: |
| 2955 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2956 | break; |
| 2957 | } |
| 2958 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2959 | done: |
| 2960 | return ret; |
| 2961 | } |
| 2962 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2963 | /** |
| 2964 | * @brief Compile information about the leaf/leaf-list's type. |
| 2965 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2966 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2967 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2968 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2969 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2970 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2971 | * @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] | 2972 | * @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] | 2973 | * @return LY_ERR value. |
| 2974 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2975 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2976 | 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] | 2977 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2978 | { |
| 2979 | LY_ERR ret = LY_SUCCESS; |
| 2980 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2981 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2982 | struct type_context { |
| 2983 | const struct lysp_tpdf *tpdf; |
| 2984 | struct lysp_node *node; |
| 2985 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2986 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2987 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2988 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2989 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2990 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2991 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2992 | |
| 2993 | (*type) = NULL; |
| 2994 | |
| 2995 | tctx = calloc(1, sizeof *tctx); |
| 2996 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2997 | 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] | 2998 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2999 | ret == LY_SUCCESS; |
| 3000 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 3001 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 3002 | if (basetype) { |
| 3003 | break; |
| 3004 | } |
| 3005 | |
| 3006 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3007 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3008 | 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] | 3009 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3010 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3011 | if (units && !*units) { |
| 3012 | /* inherit units */ |
| 3013 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3014 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3015 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3016 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3017 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3018 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3019 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3020 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3021 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3022 | break; |
| 3023 | } |
| 3024 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3025 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3026 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3027 | * 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] | 3028 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3029 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3030 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3031 | dummyloops = 1; |
| 3032 | goto preparenext; |
| 3033 | } else { |
| 3034 | tctx = NULL; |
| 3035 | break; |
| 3036 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3037 | } |
| 3038 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3039 | /* circular typedef reference detection */ |
| 3040 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3041 | /* local part */ |
| 3042 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3043 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3044 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3045 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3046 | free(tctx); |
| 3047 | ret = LY_EVALID; |
| 3048 | goto cleanup; |
| 3049 | } |
| 3050 | } |
| 3051 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3052 | /* global part for unions corner case */ |
| 3053 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3054 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3055 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3056 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3057 | free(tctx); |
| 3058 | ret = LY_EVALID; |
| 3059 | goto cleanup; |
| 3060 | } |
| 3061 | } |
| 3062 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3063 | /* store information for the following processing */ |
| 3064 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3065 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3066 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3067 | /* prepare next loop */ |
| 3068 | tctx_prev = tctx; |
| 3069 | tctx = calloc(1, sizeof *tctx); |
| 3070 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3071 | } |
| 3072 | free(tctx); |
| 3073 | |
| 3074 | /* allocate type according to the basetype */ |
| 3075 | switch (basetype) { |
| 3076 | case LY_TYPE_BINARY: |
| 3077 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3078 | break; |
| 3079 | case LY_TYPE_BITS: |
| 3080 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3081 | break; |
| 3082 | case LY_TYPE_BOOL: |
| 3083 | case LY_TYPE_EMPTY: |
| 3084 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3085 | break; |
| 3086 | case LY_TYPE_DEC64: |
| 3087 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3088 | break; |
| 3089 | case LY_TYPE_ENUM: |
| 3090 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3091 | break; |
| 3092 | case LY_TYPE_IDENT: |
| 3093 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3094 | break; |
| 3095 | case LY_TYPE_INST: |
| 3096 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3097 | break; |
| 3098 | case LY_TYPE_LEAFREF: |
| 3099 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3100 | break; |
| 3101 | case LY_TYPE_STRING: |
| 3102 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3103 | break; |
| 3104 | case LY_TYPE_UNION: |
| 3105 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3106 | break; |
| 3107 | case LY_TYPE_INT8: |
| 3108 | case LY_TYPE_UINT8: |
| 3109 | case LY_TYPE_INT16: |
| 3110 | case LY_TYPE_UINT16: |
| 3111 | case LY_TYPE_INT32: |
| 3112 | case LY_TYPE_UINT32: |
| 3113 | case LY_TYPE_INT64: |
| 3114 | case LY_TYPE_UINT64: |
| 3115 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3116 | break; |
| 3117 | case LY_TYPE_UNKNOWN: |
| 3118 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3119 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3120 | ret = LY_EVALID; |
| 3121 | goto cleanup; |
| 3122 | } |
| 3123 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3124 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3125 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3126 | ly_data_type2str[basetype]); |
| 3127 | free(*type); |
| 3128 | (*type) = NULL; |
| 3129 | ret = LY_EVALID; |
| 3130 | goto cleanup; |
| 3131 | } |
| 3132 | |
| 3133 | /* get restrictions from the referred typedefs */ |
| 3134 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3135 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3136 | |
| 3137 | /* remember the typedef context for circular check */ |
| 3138 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3139 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3140 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3141 | base = tctx->tpdf->type.compiled; |
| 3142 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3143 | } 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] | 3144 | /* no change, just use the type information from the base */ |
| 3145 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3146 | ++base->refcount; |
| 3147 | continue; |
| 3148 | } |
| 3149 | |
| 3150 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3151 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3152 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3153 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3154 | ret = LY_EVALID; |
| 3155 | goto cleanup; |
| 3156 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3157 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3158 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3159 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3160 | ret = LY_EVALID; |
| 3161 | goto cleanup; |
| 3162 | } |
| 3163 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3164 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3165 | /* TODO user type plugins */ |
| 3166 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3167 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3168 | 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] | 3169 | 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] | 3170 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3171 | LY_CHECK_GOTO(ret, cleanup); |
| 3172 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3173 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3174 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3175 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3176 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3177 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3178 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3179 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3180 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3181 | /* TODO user type plugins */ |
| 3182 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3183 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3184 | 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] | 3185 | LY_CHECK_GOTO(ret, cleanup); |
| 3186 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3187 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3188 | free(*type); |
| 3189 | (*type) = base; |
| 3190 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3191 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3192 | if (dflt && !(*type)->dflt) { |
| 3193 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3194 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3195 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3196 | (*type)->dflt->realtype = (*type); |
| 3197 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3198 | 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] | 3199 | 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] | 3200 | if (err) { |
| 3201 | ly_err_print(err); |
| 3202 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3203 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3204 | ly_err_free(err); |
| 3205 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3206 | if (ret == LY_EINCOMPLETE) { |
| 3207 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3208 | 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] | 3209 | |
| 3210 | /* but in general result is so far ok */ |
| 3211 | ret = LY_SUCCESS; |
| 3212 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3213 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3214 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3215 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3216 | COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3217 | |
| 3218 | cleanup: |
| 3219 | ly_set_erase(&tpdf_chain, free); |
| 3220 | return ret; |
| 3221 | } |
| 3222 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3223 | /** |
| 3224 | * @brief Compile status information of the given node. |
| 3225 | * |
| 3226 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3227 | * has the status correctly set during the compilation. |
| 3228 | * |
| 3229 | * @param[in] ctx Compile context |
| 3230 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3231 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3232 | * the compatibility with the parent's status value. |
| 3233 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3234 | * @return LY_ERR value. |
| 3235 | */ |
| 3236 | static LY_ERR |
| 3237 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3238 | { |
| 3239 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3240 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3241 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3242 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3243 | if ((parent_flags & 0x3) != 0x3) { |
| 3244 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3245 | * combination of bits (0x3) which marks the uses_status value */ |
| 3246 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3247 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3248 | } |
| 3249 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3250 | } else { |
| 3251 | (*node_flags) |= LYS_STATUS_CURR; |
| 3252 | } |
| 3253 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3254 | /* check status compatibility with the parent */ |
| 3255 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3256 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3257 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3258 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3259 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3260 | } else { /* LYS_STATUS_DEPRC */ |
| 3261 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3262 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3263 | } |
| 3264 | return LY_EVALID; |
| 3265 | } |
| 3266 | } |
| 3267 | return LY_SUCCESS; |
| 3268 | } |
| 3269 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3270 | /** |
| 3271 | * @brief Check uniqness of the node/action/notification name. |
| 3272 | * |
| 3273 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3274 | * structures, but they share the namespace so we need to check their name collisions. |
| 3275 | * |
| 3276 | * @param[in] ctx Compile context. |
| 3277 | * @param[in] children List (linked list) of data nodes to go through. |
| 3278 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3279 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3280 | * @param[in] name Name of the item to find in the given lists. |
| 3281 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3282 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3283 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3284 | */ |
| 3285 | static LY_ERR |
| 3286 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3287 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3288 | const char *name, void *exclude) |
| 3289 | { |
| 3290 | const struct lysc_node *iter; |
| 3291 | unsigned int u; |
| 3292 | |
| 3293 | LY_LIST_FOR(children, iter) { |
| 3294 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3295 | goto error; |
| 3296 | } |
| 3297 | } |
| 3298 | LY_ARRAY_FOR(actions, u) { |
| 3299 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3300 | goto error; |
| 3301 | } |
| 3302 | } |
| 3303 | LY_ARRAY_FOR(notifs, u) { |
| 3304 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3305 | goto error; |
| 3306 | } |
| 3307 | } |
| 3308 | return LY_SUCCESS; |
| 3309 | error: |
| 3310 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification"); |
| 3311 | return LY_EEXIST; |
| 3312 | } |
| 3313 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3314 | 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] | 3315 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3316 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3317 | * @brief Compile parsed RPC/action schema node information. |
| 3318 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3319 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3320 | * @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] | 3321 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3322 | * @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). |
| 3323 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3324 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3325 | */ |
| 3326 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3327 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3328 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3329 | { |
| 3330 | LY_ERR ret = LY_SUCCESS; |
| 3331 | struct lysp_node *child_p; |
| 3332 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3333 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3334 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3335 | lysc_update_path(ctx, parent, action_p->name); |
| 3336 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3337 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3338 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3339 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3340 | action_p->name, action)) { |
| 3341 | return LY_EVALID; |
| 3342 | } |
| 3343 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3344 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3345 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3346 | "Action \"%s\" is placed inside %s.", action_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3347 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3348 | return LY_EVALID; |
| 3349 | } |
| 3350 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3351 | action->nodetype = LYS_ACTION; |
| 3352 | action->module = ctx->mod; |
| 3353 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3354 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3355 | action->sp = action_p; |
| 3356 | } |
| 3357 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3358 | |
| 3359 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3360 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3361 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3362 | |
| 3363 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3364 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3365 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3366 | COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
| 3367 | COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3368 | |
| 3369 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3370 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3371 | COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup); |
| 3372 | COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup); |
| 3373 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3374 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3375 | 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] | 3376 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3377 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3378 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3379 | |
| 3380 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3381 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3382 | COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup); |
| 3383 | COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup); |
| 3384 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3385 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3386 | 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] | 3387 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3388 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3389 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3390 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3391 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3392 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3393 | return ret; |
| 3394 | } |
| 3395 | |
| 3396 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3397 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3398 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3399 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3400 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3401 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3402 | * @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] | 3403 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3404 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3405 | */ |
| 3406 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3407 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3408 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3409 | { |
| 3410 | LY_ERR ret = LY_SUCCESS; |
| 3411 | struct lysp_node *child_p; |
| 3412 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3413 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3414 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3415 | lysc_update_path(ctx, parent, notif_p->name); |
| 3416 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3417 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3418 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3419 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3420 | notif_p->name, notif)) { |
| 3421 | return LY_EVALID; |
| 3422 | } |
| 3423 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3424 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3425 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3426 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3427 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3428 | return LY_EVALID; |
| 3429 | } |
| 3430 | |
| 3431 | notif->nodetype = LYS_NOTIF; |
| 3432 | notif->module = ctx->mod; |
| 3433 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3434 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3435 | notif->sp = notif_p; |
| 3436 | } |
| 3437 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3438 | |
| 3439 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3440 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3441 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3442 | |
| 3443 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3444 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3445 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3446 | COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
| 3447 | COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup); |
| 3448 | COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3449 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3450 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3451 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3452 | 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] | 3453 | } |
| 3454 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3455 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3456 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3457 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3458 | return ret; |
| 3459 | } |
| 3460 | |
| 3461 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3462 | * @brief Compile parsed container node information. |
| 3463 | * @param[in] ctx Compile context |
| 3464 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3465 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3466 | * is enriched with the container-specific information. |
| 3467 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3468 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3469 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3470 | 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] | 3471 | { |
| 3472 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3473 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3474 | struct lysp_node *child_p; |
| 3475 | unsigned int u; |
| 3476 | LY_ERR ret = LY_SUCCESS; |
| 3477 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3478 | if (cont_p->presence) { |
| 3479 | cont->flags |= LYS_PRESENCE; |
| 3480 | } |
| 3481 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3482 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3483 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3484 | } |
| 3485 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3486 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done); |
| 3487 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3488 | 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] | 3489 | |
| 3490 | done: |
| 3491 | return ret; |
| 3492 | } |
| 3493 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3494 | /* |
| 3495 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3496 | * @param[in] ctx Compile context. |
| 3497 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3498 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3499 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3500 | * @return LY_ERR value. |
| 3501 | */ |
| 3502 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3503 | 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] | 3504 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3505 | unsigned int u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3506 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3507 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3508 | 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] | 3509 | leaf->units ? NULL : &leaf->units)); |
| 3510 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3511 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3512 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3513 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3514 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3515 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3516 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3517 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3518 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3519 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3520 | } |
| 3521 | } else { |
| 3522 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3523 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3524 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3525 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3526 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3527 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3528 | } |
| 3529 | } |
| 3530 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3531 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3532 | ly_set_add(&ctx->unres, leaf, 0); |
| 3533 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3534 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3535 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3536 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3537 | ly_set_add(&ctx->unres, leaf, 0); |
| 3538 | } |
| 3539 | } |
| 3540 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3541 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3542 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3543 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3544 | return LY_EVALID; |
| 3545 | } |
| 3546 | } |
| 3547 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3548 | return LY_SUCCESS; |
| 3549 | } |
| 3550 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3551 | /** |
| 3552 | * @brief Compile parsed leaf node information. |
| 3553 | * @param[in] ctx Compile context |
| 3554 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3555 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3556 | * is enriched with the leaf-specific information. |
| 3557 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3558 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3559 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3560 | 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] | 3561 | { |
| 3562 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3563 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3564 | unsigned int u; |
| 3565 | LY_ERR ret = LY_SUCCESS; |
| 3566 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3567 | 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] | 3568 | if (leaf_p->units) { |
| 3569 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3570 | leaf->flags |= LYS_SET_UNITS; |
| 3571 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3572 | |
| 3573 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3574 | leaf->dflt = (void*)leaf_p->dflt; |
| 3575 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
| 3576 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3577 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3578 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3579 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3580 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3581 | leaf->dflt->realtype = leaf->type; |
| 3582 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3583 | 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] | 3584 | 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] | 3585 | leaf->dflt->realtype->refcount++; |
| 3586 | if (err) { |
| 3587 | ly_err_print(err); |
| 3588 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3589 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3590 | ly_err_free(err); |
| 3591 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3592 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3593 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3594 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), done); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3595 | |
| 3596 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3597 | ret = LY_SUCCESS; |
| 3598 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3599 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3600 | leaf->flags |= LYS_SET_DFLT; |
| 3601 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3602 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3603 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3604 | done: |
| 3605 | return ret; |
| 3606 | } |
| 3607 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3608 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3609 | * @brief Compile parsed leaf-list node information. |
| 3610 | * @param[in] ctx Compile context |
| 3611 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3612 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3613 | * is enriched with the leaf-list-specific information. |
| 3614 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3615 | */ |
| 3616 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3617 | 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] | 3618 | { |
| 3619 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3620 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3621 | unsigned int u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3622 | LY_ERR ret = LY_SUCCESS; |
| 3623 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3624 | 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] | 3625 | if (llist_p->units) { |
| 3626 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3627 | llist->flags |= LYS_SET_UNITS; |
| 3628 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3629 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3630 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3631 | llist->dflts = (void*)llist_p->dflts; |
| 3632 | 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] | 3633 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3634 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3635 | 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] | 3636 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3637 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3638 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3639 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3640 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3641 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3642 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3643 | llist->dflts[u]->realtype = llist->type; |
| 3644 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3645 | 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] | 3646 | 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] | 3647 | llist->dflts[u]->realtype->refcount++; |
| 3648 | if (err) { |
| 3649 | ly_err_print(err); |
| 3650 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3651 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3652 | ly_err_free(err); |
| 3653 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3654 | if (ret == LY_EINCOMPLETE) { |
| 3655 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3656 | 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] | 3657 | |
| 3658 | /* but in general result is so far ok */ |
| 3659 | ret = LY_SUCCESS; |
| 3660 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3661 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3662 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3663 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3664 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3665 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3666 | /* configuration data values must be unique - so check the default values */ |
| 3667 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3668 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3669 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3670 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3671 | 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] | 3672 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3673 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3674 | if (dynamic) { |
| 3675 | free((char*)val); |
| 3676 | } |
| 3677 | return LY_EVALID; |
| 3678 | } |
| 3679 | } |
| 3680 | } |
| 3681 | } |
| 3682 | |
| 3683 | /* 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] | 3684 | |
| 3685 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3686 | if (llist->min) { |
| 3687 | llist->flags |= LYS_MAND_TRUE; |
| 3688 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3689 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3690 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3691 | done: |
| 3692 | return ret; |
| 3693 | } |
| 3694 | |
| 3695 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3696 | * @brief Compile information about list's uniques. |
| 3697 | * @param[in] ctx Compile context. |
| 3698 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3699 | * @param[in] uniques Sized array list of unique statements. |
| 3700 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3701 | * @return LY_ERR value. |
| 3702 | */ |
| 3703 | static LY_ERR |
| 3704 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3705 | { |
| 3706 | LY_ERR ret = LY_SUCCESS; |
| 3707 | struct lysc_node_leaf **key, ***unique; |
| 3708 | const char *keystr, *delim; |
| 3709 | size_t len; |
| 3710 | unsigned int v; |
| 3711 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3712 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3713 | |
| 3714 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3715 | config = -1; |
| 3716 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3717 | keystr = uniques[v]; |
| 3718 | while (keystr) { |
| 3719 | delim = strpbrk(keystr, " \t\n"); |
| 3720 | if (delim) { |
| 3721 | len = delim - keystr; |
| 3722 | while (isspace(*delim)) { |
| 3723 | ++delim; |
| 3724 | } |
| 3725 | } else { |
| 3726 | len = strlen(keystr); |
| 3727 | } |
| 3728 | |
| 3729 | /* unique node must be present */ |
| 3730 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3731 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3732 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3733 | if (ret != LY_SUCCESS) { |
| 3734 | if (ret == LY_EDENIED) { |
| 3735 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3736 | "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] | 3737 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3738 | } |
| 3739 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3740 | } else if (flags) { |
| 3741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3742 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3743 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
| 3744 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3745 | } |
| 3746 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3747 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3748 | /* all referenced leafs must be of the same config type */ |
| 3749 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3750 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3751 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3752 | return LY_EVALID; |
| 3753 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3754 | config = 1; |
| 3755 | } else { /* LYS_CONFIG_R */ |
| 3756 | config = 0; |
| 3757 | } |
| 3758 | |
| 3759 | /* check status */ |
| 3760 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3761 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3762 | |
| 3763 | /* mark leaf as unique */ |
| 3764 | (*key)->flags |= LYS_UNIQUE; |
| 3765 | |
| 3766 | /* next unique value in line */ |
| 3767 | keystr = delim; |
| 3768 | } |
| 3769 | /* next unique definition */ |
| 3770 | } |
| 3771 | |
| 3772 | return LY_SUCCESS; |
| 3773 | } |
| 3774 | |
| 3775 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3776 | * @brief Compile parsed list node information. |
| 3777 | * @param[in] ctx Compile context |
| 3778 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3779 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3780 | * is enriched with the list-specific information. |
| 3781 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3782 | */ |
| 3783 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3784 | 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] | 3785 | { |
| 3786 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3787 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3788 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3789 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3790 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3791 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3792 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3793 | LY_ERR ret = LY_SUCCESS; |
| 3794 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3795 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3796 | if (list->min) { |
| 3797 | list->flags |= LYS_MAND_TRUE; |
| 3798 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3799 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3800 | |
| 3801 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3802 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3803 | } |
| 3804 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3805 | 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] | 3806 | |
| 3807 | /* keys */ |
| 3808 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3809 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3810 | return LY_EVALID; |
| 3811 | } |
| 3812 | |
| 3813 | /* find all the keys (must be direct children) */ |
| 3814 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3815 | if (!keystr) { |
| 3816 | /* keyless list */ |
| 3817 | list->flags |= LYS_KEYLESS; |
| 3818 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3819 | while (keystr) { |
| 3820 | delim = strpbrk(keystr, " \t\n"); |
| 3821 | if (delim) { |
| 3822 | len = delim - keystr; |
| 3823 | while (isspace(*delim)) { |
| 3824 | ++delim; |
| 3825 | } |
| 3826 | } else { |
| 3827 | len = strlen(keystr); |
| 3828 | } |
| 3829 | |
| 3830 | /* key node must be present */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3831 | key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 3832 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3833 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3834 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3835 | return LY_EVALID; |
| 3836 | } |
| 3837 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3838 | if (key->flags & LYS_KEY) { |
| 3839 | /* the node was already marked as a key */ |
| 3840 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3841 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3842 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3843 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3844 | |
| 3845 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3846 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3847 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3848 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3849 | return LY_EVALID; |
| 3850 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3851 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3852 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3853 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3854 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3855 | "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] | 3856 | return LY_EVALID; |
| 3857 | } |
| 3858 | } else { |
| 3859 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3860 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3861 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3862 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3863 | return LY_EVALID; |
| 3864 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3865 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3866 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3867 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3868 | return LY_EVALID; |
| 3869 | } |
| 3870 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3871 | |
| 3872 | /* check status */ |
| 3873 | 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] | 3874 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3875 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3876 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3877 | if (key->dflt) { |
| 3878 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3879 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3880 | free(key->dflt); |
| 3881 | key->dflt = NULL; |
| 3882 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3883 | } |
| 3884 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3885 | key->flags |= LYS_KEY; |
| 3886 | |
| 3887 | /* move it to the correct position */ |
| 3888 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3889 | /* fix links in closest previous siblings of the key */ |
| 3890 | if (key->next) { |
| 3891 | key->next->prev = key->prev; |
| 3892 | } else { |
| 3893 | /* last child */ |
| 3894 | list->child->prev = key->prev; |
| 3895 | } |
| 3896 | if (key->prev->next) { |
| 3897 | key->prev->next = key->next; |
| 3898 | } |
| 3899 | /* fix links in the key */ |
| 3900 | if (prev_key) { |
| 3901 | key->prev = (struct lysc_node*)prev_key; |
| 3902 | key->next = prev_key->next; |
| 3903 | } else { |
| 3904 | key->prev = list->child->prev; |
| 3905 | key->next = list->child; |
| 3906 | } |
| 3907 | /* fix links in closes future siblings of the key */ |
| 3908 | if (prev_key) { |
| 3909 | if (prev_key->next) { |
| 3910 | prev_key->next->prev = (struct lysc_node*)key; |
| 3911 | } else { |
| 3912 | list->child->prev = (struct lysc_node*)key; |
| 3913 | } |
| 3914 | prev_key->next = (struct lysc_node*)key; |
| 3915 | } else { |
| 3916 | list->child->prev = (struct lysc_node*)key; |
| 3917 | } |
| 3918 | /* fix links in parent */ |
| 3919 | if (!key->prev->next) { |
| 3920 | list->child = (struct lysc_node*)key; |
| 3921 | } |
| 3922 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3923 | |
| 3924 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3925 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3926 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3927 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3928 | } |
| 3929 | |
| 3930 | /* uniques */ |
| 3931 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3932 | 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] | 3933 | } |
| 3934 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3935 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3936 | 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] | 3937 | |
| 3938 | done: |
| 3939 | return ret; |
| 3940 | } |
| 3941 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3942 | /** |
| 3943 | * @brief Do some checks and set the default choice's case. |
| 3944 | * |
| 3945 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3946 | * |
| 3947 | * @param[in] ctx Compile context. |
| 3948 | * @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, |
| 3949 | * not the reference to the imported module. |
| 3950 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3951 | * @return LY_ERR value. |
| 3952 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3953 | static LY_ERR |
| 3954 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3955 | { |
| 3956 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3957 | const char *prefix = NULL, *name; |
| 3958 | size_t prefix_len = 0; |
| 3959 | |
| 3960 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3961 | name = strchr(dflt, ':'); |
| 3962 | if (name) { |
| 3963 | prefix = dflt; |
| 3964 | prefix_len = name - prefix; |
| 3965 | ++name; |
| 3966 | } else { |
| 3967 | name = dflt; |
| 3968 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3969 | 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] | 3970 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3971 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3972 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3973 | prefix_len, prefix); |
| 3974 | return LY_EVALID; |
| 3975 | } |
| 3976 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3977 | if (!ch->dflt) { |
| 3978 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3979 | "Default case \"%s\" not found.", dflt); |
| 3980 | return LY_EVALID; |
| 3981 | } |
| 3982 | /* no mandatory nodes directly under the default case */ |
| 3983 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3984 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3985 | break; |
| 3986 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3987 | if (iter->flags & LYS_MAND_TRUE) { |
| 3988 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3989 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3990 | return LY_EVALID; |
| 3991 | } |
| 3992 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3993 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3994 | return LY_SUCCESS; |
| 3995 | } |
| 3996 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3997 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3998 | 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] | 3999 | { |
| 4000 | struct lys_module *mod; |
| 4001 | const char *prefix = NULL, *name; |
| 4002 | size_t prefix_len = 0; |
| 4003 | struct lysc_node_case *cs; |
| 4004 | struct lysc_node *node; |
| 4005 | |
| 4006 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4007 | name = strchr(dflt, ':'); |
| 4008 | if (name) { |
| 4009 | prefix = dflt; |
| 4010 | prefix_len = name - prefix; |
| 4011 | ++name; |
| 4012 | } else { |
| 4013 | name = dflt; |
| 4014 | } |
| 4015 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 4016 | if (prefix) { |
| 4017 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 4018 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4019 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 4020 | "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] | 4021 | return LY_EVALID; |
| 4022 | } |
| 4023 | } else { |
| 4024 | mod = ctx->mod; |
| 4025 | } |
| 4026 | /* get the default case */ |
| 4027 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 4028 | if (!cs) { |
| 4029 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4030 | "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] | 4031 | return LY_EVALID; |
| 4032 | } |
| 4033 | |
| 4034 | /* check that there is no mandatory node */ |
| 4035 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4036 | if (node->parent != (struct lysc_node*)cs) { |
| 4037 | break; |
| 4038 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4039 | if (node->flags & LYS_MAND_TRUE) { |
| 4040 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4041 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4042 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4043 | return LY_EVALID; |
| 4044 | } |
| 4045 | } |
| 4046 | |
| 4047 | /* set the default case in choice */ |
| 4048 | ch->dflt = cs; |
| 4049 | cs->flags |= LYS_SET_DFLT; |
| 4050 | |
| 4051 | return LY_SUCCESS; |
| 4052 | } |
| 4053 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4054 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4055 | * @brief Compile parsed choice node information. |
| 4056 | * @param[in] ctx Compile context |
| 4057 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4058 | * @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] | 4059 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4060 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4061 | */ |
| 4062 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4063 | 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] | 4064 | { |
| 4065 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4066 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4067 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4068 | LY_ERR ret = LY_SUCCESS; |
| 4069 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4070 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4071 | if (child_p->nodetype == LYS_CASE) { |
| 4072 | 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] | 4073 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4074 | } |
| 4075 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4076 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4077 | } |
| 4078 | } |
| 4079 | |
| 4080 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4081 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4082 | 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] | 4083 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4084 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4085 | return ret; |
| 4086 | } |
| 4087 | |
| 4088 | /** |
| 4089 | * @brief Compile parsed anydata or anyxml node information. |
| 4090 | * @param[in] ctx Compile context |
| 4091 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4092 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4093 | * is enriched with the any-specific information. |
| 4094 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4095 | */ |
| 4096 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4097 | 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] | 4098 | { |
| 4099 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4100 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4101 | unsigned int u; |
| 4102 | LY_ERR ret = LY_SUCCESS; |
| 4103 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4104 | 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] | 4105 | |
| 4106 | if (any->flags & LYS_CONFIG_W) { |
| 4107 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
| 4108 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML)); |
| 4109 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4110 | done: |
| 4111 | return ret; |
| 4112 | } |
| 4113 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4114 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4115 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4116 | * |
| 4117 | * @param[in] ctx Compile context |
| 4118 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4119 | * the choice itself is expected instead of a specific case node. |
| 4120 | * @param[in] node Schema node to connect into the list. |
| 4121 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 4122 | */ |
| 4123 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4124 | 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] | 4125 | { |
| 4126 | struct lysc_node **children; |
| 4127 | |
| 4128 | if (node->nodetype == LYS_CASE) { |
| 4129 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4130 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4131 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4132 | } |
| 4133 | if (children) { |
| 4134 | if (!(*children)) { |
| 4135 | /* first child */ |
| 4136 | *children = node; |
| 4137 | } else if (*children != node) { |
| 4138 | /* by the condition in previous branch we cover the choice/case children |
| 4139 | * - the children list is shared by the choice and the the first case, in addition |
| 4140 | * the first child of each case must be referenced from the case node. So the node is |
| 4141 | * actually always already inserted in case it is the first children - so here such |
| 4142 | * a situation actually corresponds to the first branch */ |
| 4143 | /* insert at the end of the parent's children list */ |
| 4144 | (*children)->prev->next = node; |
| 4145 | node->prev = (*children)->prev; |
| 4146 | (*children)->prev = node; |
| 4147 | |
| 4148 | /* check the name uniqueness */ |
| 4149 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4150 | lysc_node_notifs(parent), node->name, node)) { |
| 4151 | return LY_EEXIST; |
| 4152 | } |
| 4153 | } |
| 4154 | } |
| 4155 | return LY_SUCCESS; |
| 4156 | } |
| 4157 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4158 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4159 | * @brief Get the XPath context node for the given schema node. |
| 4160 | * @param[in] start The schema node where the XPath expression appears. |
| 4161 | * @return The context node to evaluate XPath expression in given schema node. |
| 4162 | * @return NULL in case the context node is the root node. |
| 4163 | */ |
| 4164 | static struct lysc_node * |
| 4165 | lysc_xpath_context(struct lysc_node *start) |
| 4166 | { |
| 4167 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 4168 | start = start->parent); |
| 4169 | return start; |
| 4170 | } |
| 4171 | |
| 4172 | /** |
| 4173 | * @brief Prepare the case structure in choice node for the new data node. |
| 4174 | * |
| 4175 | * 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 |
| 4176 | * created in the choice when the first child was processed. |
| 4177 | * |
| 4178 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4179 | * @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, |
| 4180 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4181 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4182 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4183 | * @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, |
| 4184 | * 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] | 4185 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4186 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4187 | 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] | 4188 | { |
| 4189 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4190 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4191 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4192 | unsigned int u; |
| 4193 | LY_ERR ret; |
| 4194 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4195 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4196 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4197 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4198 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4199 | return NULL; \ |
| 4200 | } \ |
| 4201 | } |
| 4202 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4203 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4204 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4205 | |
| 4206 | /* we have to add an implicit case node into the parent choice */ |
| 4207 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4208 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 4209 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4210 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4211 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4212 | /* the case is already present since the child is not its first children */ |
| 4213 | return (struct lysc_node_case*)ch->cases->prev; |
| 4214 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4215 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4216 | |
| 4217 | /* explicit parent case is not present (this is its first child) */ |
| 4218 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4219 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 4220 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4221 | cs->sp = node_p; |
| 4222 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4223 | /* 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] | 4224 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4225 | |
| 4226 | if (node_p->when) { |
| 4227 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4228 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4229 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4230 | (*when)->context = lysc_xpath_context(ch->parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4231 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4232 | 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] | 4233 | } else { |
| 4234 | LOGINT(ctx->ctx); |
| 4235 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4236 | } |
| 4237 | cs->module = ctx->mod; |
| 4238 | cs->prev = (struct lysc_node*)cs; |
| 4239 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4240 | 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] | 4241 | cs->parent = (struct lysc_node*)ch; |
| 4242 | cs->child = child; |
| 4243 | |
| 4244 | return cs; |
| 4245 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4246 | if (cs) { |
| 4247 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4248 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4249 | return NULL; |
| 4250 | |
| 4251 | #undef UNIQUE_CHECK |
| 4252 | } |
| 4253 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4254 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4255 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4256 | * |
| 4257 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4258 | * @param[in] node Target node where the config is supposed to be changed. |
| 4259 | * @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] | 4260 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4261 | * 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] | 4262 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4263 | * @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] | 4264 | * @return LY_ERR value. |
| 4265 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4266 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4267 | 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] | 4268 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4269 | { |
| 4270 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4271 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4272 | |
| 4273 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4274 | /* nothing to do */ |
| 4275 | return LY_SUCCESS; |
| 4276 | } |
| 4277 | |
| 4278 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4279 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4280 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4281 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4282 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4283 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4284 | return LY_EVALID; |
| 4285 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4286 | node->flags |= LYS_SET_CONFIG; |
| 4287 | } else { |
| 4288 | if (node->flags & LYS_SET_CONFIG) { |
| 4289 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4290 | /* setting config flags, but have node with explicit config true */ |
| 4291 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4292 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4293 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4294 | return LY_EVALID; |
| 4295 | } |
| 4296 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4297 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4298 | return LY_SUCCESS; |
| 4299 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4300 | } |
| 4301 | node->flags &= ~LYS_CONFIG_MASK; |
| 4302 | node->flags |= config; |
| 4303 | |
| 4304 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4305 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4306 | 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] | 4307 | } |
| 4308 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4309 | return LY_SUCCESS; |
| 4310 | } |
| 4311 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4312 | /** |
| 4313 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4314 | * |
| 4315 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4316 | * the flag to such parents from a mandatory children. |
| 4317 | * |
| 4318 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4319 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4320 | * (mandatory children was removed). |
| 4321 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4322 | void |
| 4323 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4324 | { |
| 4325 | struct lysc_node *iter; |
| 4326 | |
| 4327 | if (add) { /* set flag */ |
| 4328 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4329 | parent = parent->parent) { |
| 4330 | parent->flags |= LYS_MAND_TRUE; |
| 4331 | } |
| 4332 | } else { /* unset flag */ |
| 4333 | 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] | 4334 | 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] | 4335 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4336 | /* there is another mandatory node */ |
| 4337 | return; |
| 4338 | } |
| 4339 | } |
| 4340 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4341 | parent->flags &= ~LYS_MAND_TRUE; |
| 4342 | } |
| 4343 | } |
| 4344 | } |
| 4345 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4346 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4347 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4348 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4349 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4350 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4351 | */ |
| 4352 | static void |
| 4353 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4354 | { |
| 4355 | unsigned int v; |
| 4356 | size_t len; |
| 4357 | |
| 4358 | len = strlen(aug_p->nodeid); |
| 4359 | LY_ARRAY_FOR(result, v) { |
| 4360 | if (strlen(result[v]->nodeid) <= len) { |
| 4361 | continue; |
| 4362 | } |
| 4363 | if (v < LY_ARRAY_SIZE(result)) { |
| 4364 | /* move the rest of array */ |
| 4365 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4366 | break; |
| 4367 | } |
| 4368 | } |
| 4369 | result[v] = aug_p; |
| 4370 | LY_ARRAY_INCREMENT(result); |
| 4371 | } |
| 4372 | |
| 4373 | /** |
| 4374 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4375 | * |
| 4376 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4377 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4378 | * |
| 4379 | * @param[in] ctx Compile context. |
| 4380 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4381 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4382 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4383 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4384 | * @return LY_ERR value. |
| 4385 | */ |
| 4386 | LY_ERR |
| 4387 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4388 | { |
| 4389 | struct lysp_augment **result = NULL; |
| 4390 | unsigned int u, v; |
| 4391 | size_t count = 0; |
| 4392 | |
| 4393 | assert(augments); |
| 4394 | |
| 4395 | /* get count of the augments in module and all its submodules */ |
| 4396 | if (aug_p) { |
| 4397 | count += LY_ARRAY_SIZE(aug_p); |
| 4398 | } |
| 4399 | LY_ARRAY_FOR(inc_p, u) { |
| 4400 | if (inc_p[u].submodule->augments) { |
| 4401 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4402 | } |
| 4403 | } |
| 4404 | |
| 4405 | if (!count) { |
| 4406 | *augments = NULL; |
| 4407 | return LY_SUCCESS; |
| 4408 | } |
| 4409 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4410 | |
| 4411 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4412 | * together, so there can be even /z/y betwwen them. */ |
| 4413 | LY_ARRAY_FOR(aug_p, u) { |
| 4414 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4415 | } |
| 4416 | LY_ARRAY_FOR(inc_p, u) { |
| 4417 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4418 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4419 | } |
| 4420 | } |
| 4421 | |
| 4422 | *augments = result; |
| 4423 | return LY_SUCCESS; |
| 4424 | } |
| 4425 | |
| 4426 | /** |
| 4427 | * @brief Compile the parsed augment connecting it into its target. |
| 4428 | * |
| 4429 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4430 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4431 | * are already implemented and compiled. |
| 4432 | * |
| 4433 | * @param[in] ctx Compile context. |
| 4434 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4435 | * @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 |
| 4436 | * children in case of the augmenting uses data. |
| 4437 | * @return LY_SUCCESS on success. |
| 4438 | * @return LY_EVALID on failure. |
| 4439 | */ |
| 4440 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4441 | 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] | 4442 | { |
| 4443 | LY_ERR ret = LY_SUCCESS; |
| 4444 | struct lysp_node *node_p, *case_node_p; |
| 4445 | struct lysc_node *target; /* target target of the augment */ |
| 4446 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4447 | struct lysc_when **when, *when_shared; |
| 4448 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4449 | uint16_t flags = 0; |
| 4450 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4451 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4452 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4453 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4454 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4455 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4456 | 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] | 4457 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4458 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4459 | if (ret != LY_SUCCESS) { |
| 4460 | if (ret == LY_EDENIED) { |
| 4461 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4462 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4463 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4464 | } |
| 4465 | return LY_EVALID; |
| 4466 | } |
| 4467 | |
| 4468 | /* check for mandatory nodes |
| 4469 | * - new cases augmenting some choice can have mandatory nodes |
| 4470 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4471 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4472 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4473 | allow_mandatory = 1; |
| 4474 | } |
| 4475 | |
| 4476 | when_shared = NULL; |
| 4477 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4478 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4479 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4480 | && !((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] | 4481 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4482 | && !(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] | 4483 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4484 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4485 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4486 | return LY_EVALID; |
| 4487 | } |
| 4488 | |
| 4489 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4490 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4491 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4492 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4493 | } else { |
| 4494 | 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] | 4495 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4496 | } |
| 4497 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4498 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4499 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4500 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4501 | * 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] | 4502 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4503 | /* 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] | 4504 | 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] | 4505 | } else if (target->nodetype == LYS_CHOICE) { |
| 4506 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4507 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4508 | } else { |
| 4509 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4510 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4511 | } |
| 4512 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4513 | 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] | 4514 | node->flags &= ~LYS_MAND_TRUE; |
| 4515 | lys_compile_mandatory_parents(target, 0); |
| 4516 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4517 | "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] | 4518 | return LY_EVALID; |
| 4519 | } |
| 4520 | |
| 4521 | /* pass augment's when to all the children */ |
| 4522 | if (aug_p->when) { |
| 4523 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4524 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4525 | ret = lys_compile_when(ctx, aug_p->when, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4526 | LY_CHECK_GOTO(ret, error); |
| 4527 | (*when)->context = lysc_xpath_context(target); |
| 4528 | when_shared = *when; |
| 4529 | } else { |
| 4530 | ++when_shared->refcount; |
| 4531 | (*when) = when_shared; |
| 4532 | } |
| 4533 | } |
| 4534 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4535 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4536 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4537 | switch (target->nodetype) { |
| 4538 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4539 | 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] | 4540 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4541 | 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] | 4542 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4543 | break; |
| 4544 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4545 | 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] | 4546 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4547 | 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] | 4548 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4549 | break; |
| 4550 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4551 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4552 | if (aug_p->actions) { |
| 4553 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4554 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4555 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4556 | return LY_EVALID; |
| 4557 | } |
| 4558 | if (aug_p->notifs) { |
| 4559 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4560 | "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".", |
| 4561 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4562 | return LY_EVALID; |
| 4563 | } |
| 4564 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4565 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4566 | lysc_update_path(ctx, NULL, NULL); |
| 4567 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4568 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4569 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4570 | return ret; |
| 4571 | } |
| 4572 | |
| 4573 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4574 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4575 | * |
| 4576 | * @param[in] ctx Compile context. |
| 4577 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4578 | * @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] | 4579 | * @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] | 4580 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4581 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4582 | * 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] | 4583 | * @return LY_ERR value. |
| 4584 | */ |
| 4585 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4586 | 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] | 4587 | { |
| 4588 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4589 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4590 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4591 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4592 | return LY_EVALID; |
| 4593 | } |
| 4594 | |
| 4595 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4596 | /* check if node has default value */ |
| 4597 | if (node->nodetype & LYS_LEAF) { |
| 4598 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4599 | if (refine_flag) { |
| 4600 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4601 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4602 | return LY_EVALID; |
| 4603 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4604 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4605 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4606 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4607 | |
| 4608 | /* update the list of incomplete default values if needed */ |
| 4609 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4610 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4611 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4612 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4613 | free(leaf->dflt); |
| 4614 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4615 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4616 | } |
| 4617 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4618 | if (refine_flag) { |
| 4619 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4620 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4621 | return LY_EVALID; |
| 4622 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4623 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4624 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4625 | 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] | 4626 | return LY_EVALID; |
| 4627 | } |
| 4628 | |
| 4629 | node->flags &= ~LYS_MAND_FALSE; |
| 4630 | node->flags |= LYS_MAND_TRUE; |
| 4631 | lys_compile_mandatory_parents(node->parent, 1); |
| 4632 | } else { |
| 4633 | /* make mandatory false */ |
| 4634 | node->flags &= ~LYS_MAND_TRUE; |
| 4635 | node->flags |= LYS_MAND_FALSE; |
| 4636 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4637 | 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] | 4638 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4639 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4640 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4641 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4642 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4643 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4644 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4645 | } |
| 4646 | } |
| 4647 | return LY_SUCCESS; |
| 4648 | } |
| 4649 | |
| 4650 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4651 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4652 | * If present, also apply uses's modificators. |
| 4653 | * |
| 4654 | * @param[in] ctx Compile context |
| 4655 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4656 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4657 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4658 | * the compile context. |
| 4659 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4660 | */ |
| 4661 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4662 | 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] | 4663 | { |
| 4664 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4665 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4666 | /* 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] | 4667 | struct lysc_node_container context_node_fake = |
| 4668 | {.nodetype = LYS_CONTAINER, |
| 4669 | .module = ctx->mod, |
| 4670 | .flags = parent ? parent->flags : 0, |
| 4671 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4672 | .prev = (struct lysc_node*)&context_node_fake, |
| 4673 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4674 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4675 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4676 | int found; |
| 4677 | const char *id, *name, *prefix; |
| 4678 | size_t prefix_len, name_len; |
| 4679 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4680 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4681 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4682 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4683 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4684 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4685 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4686 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4687 | unsigned int actions_index, notifs_index; |
| 4688 | struct lysc_notif **notifs = NULL; |
| 4689 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4690 | |
| 4691 | /* search for the grouping definition */ |
| 4692 | found = 0; |
| 4693 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4694 | 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] | 4695 | if (prefix) { |
| 4696 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4697 | if (!mod) { |
| 4698 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4699 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4700 | return LY_EVALID; |
| 4701 | } |
| 4702 | } else { |
| 4703 | mod = ctx->mod_def; |
| 4704 | } |
| 4705 | if (mod == ctx->mod_def) { |
| 4706 | 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] | 4707 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4708 | LY_ARRAY_FOR(grp, u) { |
| 4709 | if (!strcmp(grp[u].name, name)) { |
| 4710 | grp = &grp[u]; |
| 4711 | found = 1; |
| 4712 | break; |
| 4713 | } |
| 4714 | } |
| 4715 | } |
| 4716 | } |
| 4717 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4718 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4719 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4720 | if (grp) { |
| 4721 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4722 | if (!strcmp(grp[u].name, name)) { |
| 4723 | grp = &grp[u]; |
| 4724 | found = 1; |
| 4725 | } |
| 4726 | } |
| 4727 | } |
| 4728 | if (!found && mod->parsed->includes) { |
| 4729 | /* ... and all the submodules */ |
| 4730 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4731 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4732 | if (grp) { |
| 4733 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4734 | if (!strcmp(grp[v].name, name)) { |
| 4735 | grp = &grp[v]; |
| 4736 | found = 1; |
| 4737 | } |
| 4738 | } |
| 4739 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4740 | } |
| 4741 | } |
| 4742 | } |
| 4743 | if (!found) { |
| 4744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4745 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4746 | return LY_EVALID; |
| 4747 | } |
| 4748 | |
| 4749 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4750 | grp_stack_count = ctx->groupings.count; |
| 4751 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4752 | if (grp_stack_count == ctx->groupings.count) { |
| 4753 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4754 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4755 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4756 | return LY_EVALID; |
| 4757 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4758 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4759 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4760 | grp->flags |= LYS_USED_GRP; |
| 4761 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4762 | |
| 4763 | /* switch context's mod_def */ |
| 4764 | mod_old = ctx->mod_def; |
| 4765 | ctx->mod_def = mod; |
| 4766 | |
| 4767 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4768 | 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] | 4769 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4770 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4771 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4772 | /* 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] | 4773 | 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] | 4774 | |
| 4775 | /* some preparation for applying refines */ |
| 4776 | if (grp->data == node_p) { |
| 4777 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4778 | if (parent) { |
| 4779 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4780 | } else if (ctx->mod->compiled->data) { |
| 4781 | child = ctx->mod->compiled->data; |
| 4782 | } else { |
| 4783 | child = NULL; |
| 4784 | } |
| 4785 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4786 | } |
| 4787 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4788 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4789 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4790 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4791 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4792 | /* 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] | 4793 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4794 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4795 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4796 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4797 | (*when)->context = lysc_xpath_context(parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4798 | when_shared = *when; |
| 4799 | } else { |
| 4800 | ++when_shared->refcount; |
| 4801 | (*when) = when_shared; |
| 4802 | } |
| 4803 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4804 | } |
| 4805 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4806 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4807 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4808 | /* 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] | 4809 | 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] | 4810 | } |
| 4811 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4812 | /* compile actions */ |
| 4813 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4814 | if (actions) { |
| 4815 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4816 | 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] | 4817 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4818 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4819 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4820 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4821 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4822 | } |
| 4823 | } |
| 4824 | |
| 4825 | /* compile notifications */ |
| 4826 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4827 | if (notifs) { |
| 4828 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4829 | 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] | 4830 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4831 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4832 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4833 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4834 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4835 | } |
| 4836 | } |
| 4837 | |
| 4838 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4839 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4840 | 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] | 4841 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4842 | 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] | 4843 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4844 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4845 | /* reload previous context's mod_def */ |
| 4846 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4847 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4848 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4849 | /* apply refine */ |
| 4850 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4851 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4852 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4853 | 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] | 4854 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4855 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4856 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4857 | |
| 4858 | /* default value */ |
| 4859 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4860 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4861 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4862 | "Invalid refine of default - %s cannot hold %d default values.", |
| 4863 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4864 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4865 | } |
| 4866 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4867 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4868 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4869 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4870 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4871 | } |
| 4872 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4873 | struct ly_err_item *err = NULL; |
| 4874 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4875 | if (leaf->dflt) { |
| 4876 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4877 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4878 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4879 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4880 | } else { |
| 4881 | /* prepare a new one */ |
| 4882 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4883 | leaf->dflt->realtype = leaf->type; |
| 4884 | } |
| 4885 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4886 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4887 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4888 | 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] | 4889 | 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] | 4890 | leaf->dflt->realtype->refcount++; |
| 4891 | if (err) { |
| 4892 | ly_err_print(err); |
| 4893 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4894 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4895 | ly_err_free(err); |
| 4896 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4897 | if (rc == LY_EINCOMPLETE) { |
| 4898 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4899 | 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] | 4900 | |
| 4901 | /* but in general result is so far ok */ |
| 4902 | rc = LY_SUCCESS; |
| 4903 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4904 | LY_CHECK_GOTO(rc, cleanup); |
| 4905 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4906 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4907 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4908 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4909 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4910 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4911 | "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] | 4912 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4913 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4914 | |
| 4915 | /* remove previous set of default values */ |
| 4916 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4917 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4918 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 4919 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 4920 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4921 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4922 | LY_ARRAY_FREE(llist->dflts); |
| 4923 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4924 | LY_ARRAY_FREE(llist->dflts_mods); |
| 4925 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4926 | |
| 4927 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4928 | 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] | 4929 | 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] | 4930 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4931 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4932 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 4933 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4934 | LY_ARRAY_INCREMENT(llist->dflts); |
| 4935 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 4936 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4937 | 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] | 4938 | 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] | 4939 | 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] | 4940 | llist->dflts[u]->realtype->refcount++; |
| 4941 | if (err) { |
| 4942 | ly_err_print(err); |
| 4943 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4944 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 4945 | ly_err_free(err); |
| 4946 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4947 | if (rc == LY_EINCOMPLETE) { |
| 4948 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4949 | 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] | 4950 | |
| 4951 | /* but in general result is so far ok */ |
| 4952 | rc = LY_SUCCESS; |
| 4953 | } |
| 4954 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4955 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4956 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4957 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4958 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4959 | /* unset LYS_SET_DFLT from the current default case */ |
| 4960 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4961 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4962 | 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] | 4963 | } |
| 4964 | } |
| 4965 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4966 | /* description */ |
| 4967 | if (rfn->dsc) { |
| 4968 | FREE_STRING(ctx->ctx, node->dsc); |
| 4969 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4970 | } |
| 4971 | |
| 4972 | /* reference */ |
| 4973 | if (rfn->ref) { |
| 4974 | FREE_STRING(ctx->ctx, node->ref); |
| 4975 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4976 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4977 | |
| 4978 | /* config */ |
| 4979 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4980 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4981 | 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] | 4982 | } else { |
| 4983 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 4984 | flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path); |
| 4985 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4986 | } |
| 4987 | |
| 4988 | /* mandatory */ |
| 4989 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4990 | 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] | 4991 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4992 | |
| 4993 | /* presence */ |
| 4994 | if (rfn->presence) { |
| 4995 | if (node->nodetype != LYS_CONTAINER) { |
| 4996 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4997 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4998 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4999 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5000 | } |
| 5001 | node->flags |= LYS_PRESENCE; |
| 5002 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5003 | |
| 5004 | /* must */ |
| 5005 | if (rfn->musts) { |
| 5006 | switch (node->nodetype) { |
| 5007 | case LYS_LEAF: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5008 | 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] | 5009 | break; |
| 5010 | case LYS_LEAFLIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5011 | 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] | 5012 | break; |
| 5013 | case LYS_LIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5014 | 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] | 5015 | break; |
| 5016 | case LYS_CONTAINER: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5017 | 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] | 5018 | break; |
| 5019 | case LYS_ANYXML: |
| 5020 | case LYS_ANYDATA: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5021 | 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] | 5022 | break; |
| 5023 | default: |
| 5024 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5025 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5026 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5027 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5028 | } |
| 5029 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5030 | |
| 5031 | /* min/max-elements */ |
| 5032 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5033 | switch (node->nodetype) { |
| 5034 | case LYS_LEAFLIST: |
| 5035 | if (rfn->flags & LYS_SET_MAX) { |
| 5036 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5037 | } |
| 5038 | if (rfn->flags & LYS_SET_MIN) { |
| 5039 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5040 | if (rfn->min) { |
| 5041 | node->flags |= LYS_MAND_TRUE; |
| 5042 | lys_compile_mandatory_parents(node->parent, 1); |
| 5043 | } else { |
| 5044 | node->flags &= ~LYS_MAND_TRUE; |
| 5045 | lys_compile_mandatory_parents(node->parent, 0); |
| 5046 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5047 | } |
| 5048 | break; |
| 5049 | case LYS_LIST: |
| 5050 | if (rfn->flags & LYS_SET_MAX) { |
| 5051 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5052 | } |
| 5053 | if (rfn->flags & LYS_SET_MIN) { |
| 5054 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5055 | if (rfn->min) { |
| 5056 | node->flags |= LYS_MAND_TRUE; |
| 5057 | lys_compile_mandatory_parents(node->parent, 1); |
| 5058 | } else { |
| 5059 | node->flags &= ~LYS_MAND_TRUE; |
| 5060 | lys_compile_mandatory_parents(node->parent, 0); |
| 5061 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5062 | } |
| 5063 | break; |
| 5064 | default: |
| 5065 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5066 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5067 | (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] | 5068 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5069 | } |
| 5070 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5071 | |
| 5072 | /* if-feature */ |
| 5073 | if (rfn->iffeatures) { |
| 5074 | /* 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] | 5075 | 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] | 5076 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5077 | |
| 5078 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5079 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5080 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5081 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 5082 | for (u = 0; u < refined.count; ++u) { |
| 5083 | node = (struct lysc_node*)refined.objs[u]; |
| 5084 | rfn = &uses_p->refines[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5085 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5086 | |
| 5087 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5088 | if ((node->flags & LYS_MAND_TRUE) && |
| 5089 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5090 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5091 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5092 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5093 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5094 | } |
| 5095 | |
| 5096 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5097 | if (node->nodetype == LYS_LIST) { |
| 5098 | min = ((struct lysc_node_list*)node)->min; |
| 5099 | max = ((struct lysc_node_list*)node)->max; |
| 5100 | } else { |
| 5101 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5102 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5103 | } |
| 5104 | if (min > max) { |
| 5105 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5106 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5107 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5108 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5109 | } |
| 5110 | } |
| 5111 | } |
| 5112 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5113 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5114 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5115 | |
| 5116 | cleanup: |
| 5117 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5118 | if (context_node_fake.child) { |
| 5119 | context_node_fake.child->prev = child; |
| 5120 | } |
| 5121 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5122 | child->parent = parent; |
| 5123 | } |
| 5124 | |
| 5125 | if (uses_p->augments || uses_p->refines) { |
| 5126 | /* 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] | 5127 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5128 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5129 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5130 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5131 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5132 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5133 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5134 | } |
| 5135 | } |
| 5136 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5137 | /* reload previous context's mod_def */ |
| 5138 | ctx->mod_def = mod_old; |
| 5139 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5140 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5141 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5142 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5143 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5144 | |
| 5145 | return ret; |
| 5146 | } |
| 5147 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5148 | static int |
| 5149 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5150 | { |
| 5151 | struct lysp_node *iter; |
| 5152 | int len = 0; |
| 5153 | |
| 5154 | *path = NULL; |
| 5155 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5156 | char *s = *path; |
| 5157 | char *id; |
| 5158 | |
| 5159 | switch (iter->nodetype) { |
| 5160 | case LYS_USES: |
| 5161 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5162 | break; |
| 5163 | case LYS_GROUPING: |
| 5164 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5165 | break; |
| 5166 | case LYS_AUGMENT: |
| 5167 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5168 | break; |
| 5169 | default: |
| 5170 | id = strdup(iter->name); |
| 5171 | break; |
| 5172 | } |
| 5173 | |
| 5174 | if (!iter->parent) { |
| 5175 | /* print prefix */ |
| 5176 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5177 | } else { |
| 5178 | /* prefix is the same as in parent */ |
| 5179 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5180 | } |
| 5181 | free(s); |
| 5182 | free(id); |
| 5183 | } |
| 5184 | |
| 5185 | if (len < 0) { |
| 5186 | free(*path); |
| 5187 | *path = NULL; |
| 5188 | } else if (len == 0) { |
| 5189 | *path = strdup("/"); |
| 5190 | len = 1; |
| 5191 | } |
| 5192 | return len; |
| 5193 | } |
| 5194 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5195 | /** |
| 5196 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5197 | * |
| 5198 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5199 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5200 | */ |
| 5201 | static LY_ERR |
| 5202 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5203 | { |
| 5204 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5205 | char *path; |
| 5206 | int len; |
| 5207 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5208 | struct lysp_node_uses fake_uses = { |
| 5209 | .parent = node_p, |
| 5210 | .nodetype = LYS_USES, |
| 5211 | .flags = 0, .next = NULL, |
| 5212 | .name = grp->name, |
| 5213 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5214 | .refines = NULL, .augments = NULL |
| 5215 | }; |
| 5216 | struct lysc_node_container fake_container = { |
| 5217 | .nodetype = LYS_CONTAINER, |
| 5218 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5219 | .module = ctx->mod, |
| 5220 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5221 | .prev = (struct lysc_node*)&fake_container, |
| 5222 | .name = "fake", |
| 5223 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5224 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5225 | }; |
| 5226 | |
| 5227 | if (grp->parent) { |
| 5228 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5229 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5230 | |
| 5231 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5232 | if (len < 0) { |
| 5233 | LOGMEM(ctx->ctx); |
| 5234 | return LY_EMEM; |
| 5235 | } |
| 5236 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5237 | ctx->path_len = (uint16_t)len; |
| 5238 | free(path); |
| 5239 | |
| 5240 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5241 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5242 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5243 | lysc_update_path(ctx, NULL, NULL); |
| 5244 | lysc_update_path(ctx, NULL, NULL); |
| 5245 | |
| 5246 | ctx->path_len = 1; |
| 5247 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5248 | |
| 5249 | /* cleanup */ |
| 5250 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5251 | |
| 5252 | return ret; |
| 5253 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5254 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5255 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5256 | * @brief Compile parsed schema node information. |
| 5257 | * @param[in] ctx Compile context |
| 5258 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5259 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5260 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5261 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5262 | * @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). |
| 5263 | * 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] | 5264 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5265 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5266 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5267 | 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] | 5268 | { |
| 5269 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5270 | struct lysc_node *node; |
| 5271 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5272 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5273 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5274 | 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] | 5275 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5276 | if (node_p->nodetype != LYS_USES) { |
| 5277 | lysc_update_path(ctx, parent, node_p->name); |
| 5278 | } else { |
| 5279 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5280 | lysc_update_path(ctx, NULL, node_p->name); |
| 5281 | } |
| 5282 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5283 | switch (node_p->nodetype) { |
| 5284 | case LYS_CONTAINER: |
| 5285 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5286 | node_compile_spec = lys_compile_node_container; |
| 5287 | break; |
| 5288 | case LYS_LEAF: |
| 5289 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5290 | node_compile_spec = lys_compile_node_leaf; |
| 5291 | break; |
| 5292 | case LYS_LIST: |
| 5293 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5294 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5295 | break; |
| 5296 | case LYS_LEAFLIST: |
| 5297 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5298 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5299 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5300 | case LYS_CHOICE: |
| 5301 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5302 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5303 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5304 | case LYS_ANYXML: |
| 5305 | case LYS_ANYDATA: |
| 5306 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5307 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5308 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5309 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5310 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5311 | lysc_update_path(ctx, NULL, NULL); |
| 5312 | lysc_update_path(ctx, NULL, NULL); |
| 5313 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5314 | default: |
| 5315 | LOGINT(ctx->ctx); |
| 5316 | return LY_EINT; |
| 5317 | } |
| 5318 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5319 | node->nodetype = node_p->nodetype; |
| 5320 | node->module = ctx->mod; |
| 5321 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5322 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5323 | |
| 5324 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5325 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5326 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5327 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5328 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5329 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5330 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5331 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5332 | node->flags |= LYS_CONFIG_R; |
| 5333 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5334 | /* config not explicitely set, inherit it from parent */ |
| 5335 | if (parent) { |
| 5336 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5337 | } else { |
| 5338 | /* default is config true */ |
| 5339 | node->flags |= LYS_CONFIG_W; |
| 5340 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5341 | } else { |
| 5342 | /* config set explicitely */ |
| 5343 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5344 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5345 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5346 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5347 | "Configuration node cannot be child of any state data node."); |
| 5348 | goto error; |
| 5349 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5350 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5351 | /* *list ordering */ |
| 5352 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5353 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5354 | 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] | 5355 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5356 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5357 | node->flags &= ~LYS_ORDBY_MASK; |
| 5358 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5359 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5360 | /* default ordering is system */ |
| 5361 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5362 | } |
| 5363 | } |
| 5364 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5365 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5366 | * 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] | 5367 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5368 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5369 | * 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] | 5370 | 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] | 5371 | } |
| 5372 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5373 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5374 | node->sp = node_p; |
| 5375 | } |
| 5376 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5377 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5378 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5379 | if (node_p->when) { |
| 5380 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5381 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5382 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 5383 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5384 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5385 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error); |
| 5386 | COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5387 | |
| 5388 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5389 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5390 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5391 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5392 | if (node->flags & LYS_MAND_TRUE) { |
| 5393 | lys_compile_mandatory_parents(parent, 1); |
| 5394 | } |
| 5395 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5396 | lysc_update_path(ctx, NULL, NULL); |
| 5397 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5398 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5399 | if (parent) { |
| 5400 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5401 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5402 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5403 | } else { |
| 5404 | lysc_update_path(ctx, parent, node->name); |
| 5405 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5406 | 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] | 5407 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5408 | if (uses_status) { |
| 5409 | |
| 5410 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5411 | /* 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] | 5412 | * it directly gets the same status flags as the choice; |
| 5413 | * 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] | 5414 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5415 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5416 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5417 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5418 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5419 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5420 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5421 | 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] | 5422 | |
| 5423 | if (parent->nodetype == LYS_CHOICE) { |
| 5424 | lysc_update_path(ctx, NULL, NULL); |
| 5425 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5426 | } else { |
| 5427 | /* top-level element */ |
| 5428 | if (!ctx->mod->compiled->data) { |
| 5429 | ctx->mod->compiled->data = node; |
| 5430 | } else { |
| 5431 | /* insert at the end of the module's top-level nodes list */ |
| 5432 | ctx->mod->compiled->data->prev->next = node; |
| 5433 | node->prev = ctx->mod->compiled->data->prev; |
| 5434 | ctx->mod->compiled->data->prev = node; |
| 5435 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5436 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5437 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5438 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5439 | return LY_EVALID; |
| 5440 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5441 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5442 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5443 | |
| 5444 | return LY_SUCCESS; |
| 5445 | |
| 5446 | error: |
| 5447 | lysc_node_free(ctx->ctx, node); |
| 5448 | return ret; |
| 5449 | } |
| 5450 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5451 | static void |
| 5452 | lysc_disconnect(struct lysc_node *node) |
| 5453 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5454 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5455 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5456 | int remove_cs = 0; |
| 5457 | |
| 5458 | parent = node->parent; |
| 5459 | |
| 5460 | /* parent's first child */ |
| 5461 | if (!parent) { |
| 5462 | return; |
| 5463 | } |
| 5464 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5465 | cs = (struct lysc_node_case*)node; |
| 5466 | } else if (parent->nodetype == LYS_CASE) { |
| 5467 | /* disconnecting some node in a case */ |
| 5468 | cs = (struct lysc_node_case*)parent; |
| 5469 | parent = cs->parent; |
| 5470 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5471 | if (child == node) { |
| 5472 | if (cs->child == child) { |
| 5473 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5474 | /* case with a single child -> remove also the case */ |
| 5475 | child->parent = NULL; |
| 5476 | remove_cs = 1; |
| 5477 | } else { |
| 5478 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5479 | } |
| 5480 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5481 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5482 | } |
| 5483 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5484 | if (!remove_cs) { |
| 5485 | cs = NULL; |
| 5486 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5487 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5488 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5489 | } |
| 5490 | |
| 5491 | if (cs) { |
| 5492 | if (remove_cs) { |
| 5493 | /* cs has only one child which is being also removed */ |
| 5494 | lysc_disconnect((struct lysc_node*)cs); |
| 5495 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5496 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5497 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5498 | /* default case removed */ |
| 5499 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5500 | } |
| 5501 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5502 | /* first case removed */ |
| 5503 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5504 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5505 | if (cs->child) { |
| 5506 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5507 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5508 | prev = cs->child->prev; |
| 5509 | } /* else all the children are under a single case */ |
| 5510 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5511 | if (child->parent != (struct lysc_node*)cs) { |
| 5512 | break; |
| 5513 | } |
| 5514 | lysc_node_free(node->module->ctx, child); |
| 5515 | } |
| 5516 | if (prev) { |
| 5517 | if (prev->next) { |
| 5518 | prev->next = child; |
| 5519 | } |
| 5520 | if (child) { |
| 5521 | child->prev = prev; |
| 5522 | } else { |
| 5523 | /* link from the first child under the cases */ |
| 5524 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5525 | } |
| 5526 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5527 | } |
| 5528 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5532 | if (node->prev->next) { |
| 5533 | node->prev->next = node->next; |
| 5534 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5535 | if (node->next) { |
| 5536 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5537 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5538 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5539 | if (child) { |
| 5540 | child->prev = node->prev; |
| 5541 | } |
| 5542 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5543 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5544 | } |
| 5545 | } |
| 5546 | |
| 5547 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5548 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5549 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5550 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5551 | struct ly_set devs_p = {0}; |
| 5552 | struct ly_set targets = {0}; |
| 5553 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5554 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5555 | struct lysc_action *rpcs; |
| 5556 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5557 | struct lysp_deviation *dev; |
| 5558 | struct lysp_deviate *d, **dp_new; |
| 5559 | struct lysp_deviate_add *d_add; |
| 5560 | struct lysp_deviate_del *d_del; |
| 5561 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5562 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5563 | struct lysc_deviation { |
| 5564 | const char *nodeid; |
| 5565 | struct lysc_node *target; /* target node of the deviation */ |
| 5566 | 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] | 5567 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5568 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5569 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5570 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5571 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5572 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5573 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5574 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5575 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5576 | |
| 5577 | /* get all deviations from the module and all its submodules ... */ |
| 5578 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5579 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5580 | } |
| 5581 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5582 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5583 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5584 | } |
| 5585 | } |
| 5586 | if (!devs_p.count) { |
| 5587 | /* nothing to do */ |
| 5588 | return LY_SUCCESS; |
| 5589 | } |
| 5590 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5591 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5592 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5593 | /* ... and group them by the target node */ |
| 5594 | devs = calloc(devs_p.count, sizeof *devs); |
| 5595 | for (u = 0; u < devs_p.count; ++u) { |
| 5596 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5597 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5598 | |
| 5599 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5600 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5601 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5602 | if (target->nodetype == LYS_ACTION) { |
| 5603 | /* move the target pointer to input/output to make them different from the action and |
| 5604 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5605 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5606 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5607 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5608 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5609 | flags |= LYSC_OPT_INTERNAL; |
| 5610 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5611 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5612 | flags |= LYSC_OPT_INTERNAL; |
| 5613 | } |
| 5614 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5615 | /* insert into the set of targets with duplicity detection */ |
| 5616 | i = ly_set_add(&targets, target, 0); |
| 5617 | if (!devs[i]) { |
| 5618 | /* new record */ |
| 5619 | devs[i] = calloc(1, sizeof **devs); |
| 5620 | devs[i]->target = target; |
| 5621 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5622 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5623 | } |
| 5624 | /* add deviates into the deviation's list of deviates */ |
| 5625 | for (d = dev->deviates; d; d = d->next) { |
| 5626 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5627 | *dp_new = d; |
| 5628 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5629 | devs[i]->not_supported = 1; |
| 5630 | } |
| 5631 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5632 | |
| 5633 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5634 | } |
| 5635 | |
| 5636 | /* MACROS for deviates checking */ |
| 5637 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5638 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5639 | 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] | 5640 | goto cleanup; \ |
| 5641 | } |
| 5642 | |
| 5643 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5644 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5645 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \ |
| 5646 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5647 | goto cleanup; \ |
| 5648 | } |
| 5649 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5650 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5651 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5652 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5653 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5654 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5655 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5656 | goto cleanup; \ |
| 5657 | } |
| 5658 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5659 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5660 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5661 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5662 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5663 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5664 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5665 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5666 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5667 | goto cleanup; \ |
| 5668 | } |
| 5669 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5670 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5671 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5672 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5673 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5674 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5675 | goto cleanup; \ |
| 5676 | } |
| 5677 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5678 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5679 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5680 | 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] | 5681 | goto cleanup; \ |
| 5682 | } |
| 5683 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5684 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5685 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5686 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5687 | "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] | 5688 | goto cleanup; \ |
| 5689 | } |
| 5690 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5691 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5692 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5693 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5694 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5695 | 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] | 5696 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5697 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5698 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5699 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5700 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5701 | goto cleanup; \ |
| 5702 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5703 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5704 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5705 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5706 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5707 | (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] | 5708 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5709 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5710 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5711 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5712 | } |
| 5713 | |
| 5714 | /* apply deviations */ |
| 5715 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5716 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5717 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5718 | struct ly_err_item *err = NULL; |
| 5719 | |
| 5720 | dflt = NULL; |
| 5721 | changed_type = 0; |
| 5722 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5723 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5724 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5725 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5726 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5727 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5728 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5729 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5730 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5731 | } |
| 5732 | } |
| 5733 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5734 | /* not-supported */ |
| 5735 | if (devs[u]->not_supported) { |
| 5736 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 5737 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 5738 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5739 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5740 | |
| 5741 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5742 | if (devs[u]->target->parent) { \ |
| 5743 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5744 | } else { \ |
| 5745 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5746 | } \ |
| 5747 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5748 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5749 | } \ |
| 5750 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5751 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5752 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5753 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5754 | } |
| 5755 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5756 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 5757 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5758 | /* remove RPC's/action's input */ |
| 5759 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5760 | 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] | 5761 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5762 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5763 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5764 | /* remove RPC's/action's output */ |
| 5765 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5766 | 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] | 5767 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5768 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5769 | } else { |
| 5770 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5771 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5772 | } |
| 5773 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5774 | /* remove Notification */ |
| 5775 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5776 | } else { |
| 5777 | /* remove the target node */ |
| 5778 | lysc_disconnect(devs[u]->target); |
| 5779 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5780 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5781 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5782 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5783 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5784 | continue; |
| 5785 | } |
| 5786 | |
| 5787 | /* list of deviates (not-supported is not present in the list) */ |
| 5788 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5789 | d = devs[u]->deviates[v]; |
| 5790 | |
| 5791 | switch (d->mod) { |
| 5792 | case LYS_DEV_ADD: |
| 5793 | d_add = (struct lysp_deviate_add*)d; |
| 5794 | /* [units-stmt] */ |
| 5795 | if (d_add->units) { |
| 5796 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5797 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5798 | |
| 5799 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5800 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5801 | } |
| 5802 | |
| 5803 | /* *must-stmt */ |
| 5804 | if (d_add->musts) { |
| 5805 | switch (devs[u]->target->nodetype) { |
| 5806 | case LYS_CONTAINER: |
| 5807 | case LYS_LIST: |
| 5808 | 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] | 5809 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5810 | break; |
| 5811 | case LYS_LEAF: |
| 5812 | case LYS_LEAFLIST: |
| 5813 | case LYS_ANYDATA: |
| 5814 | 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] | 5815 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5816 | break; |
| 5817 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5818 | 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] | 5819 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5820 | break; |
| 5821 | case LYS_ACTION: |
| 5822 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5823 | 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] | 5824 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5825 | break; |
| 5826 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5827 | 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] | 5828 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5829 | break; |
| 5830 | } |
| 5831 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5832 | default: |
| 5833 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5834 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5835 | goto cleanup; |
| 5836 | } |
| 5837 | } |
| 5838 | |
| 5839 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5840 | if (d_add->uniques) { |
| 5841 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5842 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5843 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5844 | |
| 5845 | /* *default-stmt */ |
| 5846 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5847 | switch (devs[u]->target->nodetype) { |
| 5848 | case LYS_LEAF: |
| 5849 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5850 | 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] | 5851 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5852 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5853 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5854 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5855 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5856 | } else { |
| 5857 | /* prepare new default value storage */ |
| 5858 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5859 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5860 | dflt = d_add->dflts[0]; |
| 5861 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5862 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5863 | /* mark the new default values as leaf's own */ |
| 5864 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5865 | break; |
| 5866 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5867 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5868 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5869 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5870 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5871 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5872 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5873 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5874 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5875 | LY_ARRAY_FREE(llist->dflts); |
| 5876 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5877 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5878 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5879 | } |
| 5880 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5881 | 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] | 5882 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 5883 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5884 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5885 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5886 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5887 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5888 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5889 | llist->dflts[x]->realtype = llist->type; |
| 5890 | 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] | 5891 | 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] | 5892 | (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] | 5893 | llist->dflts[x]->realtype->refcount++; |
| 5894 | if (err) { |
| 5895 | ly_err_print(err); |
| 5896 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5897 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5898 | d_add->dflts[x - y], err->msg); |
| 5899 | ly_err_free(err); |
| 5900 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5901 | if (rc == LY_EINCOMPLETE) { |
| 5902 | /* postpone default compilation when the tree is complete */ |
| 5903 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5904 | |
| 5905 | /* but in general result is so far ok */ |
| 5906 | rc = LY_SUCCESS; |
| 5907 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5908 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5909 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5910 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5911 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5912 | break; |
| 5913 | case LYS_CHOICE: |
| 5914 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5915 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5916 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5917 | * 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] | 5918 | 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] | 5919 | goto cleanup; |
| 5920 | } |
| 5921 | break; |
| 5922 | default: |
| 5923 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5924 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5925 | goto cleanup; |
| 5926 | } |
| 5927 | } |
| 5928 | |
| 5929 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5930 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 5931 | 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] | 5932 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5933 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5934 | goto cleanup; |
| 5935 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5936 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5937 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 5938 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5939 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5940 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5941 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5942 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5943 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5944 | goto cleanup; |
| 5945 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5946 | 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] | 5947 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5948 | |
| 5949 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5950 | if (d_add->flags & LYS_MAND_MASK) { |
| 5951 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5952 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5953 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5954 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5955 | goto cleanup; |
| 5956 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5957 | 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] | 5958 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5959 | |
| 5960 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5961 | if (d_add->flags & LYS_SET_MIN) { |
| 5962 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5963 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5964 | /* change value */ |
| 5965 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5966 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5967 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5968 | /* change value */ |
| 5969 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5970 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5971 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5972 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5973 | goto cleanup; |
| 5974 | } |
| 5975 | if (d_add->min) { |
| 5976 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5977 | } |
| 5978 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5979 | |
| 5980 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5981 | if (d_add->flags & LYS_SET_MAX) { |
| 5982 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5983 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5984 | /* change value */ |
| 5985 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5986 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5987 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5988 | /* change value */ |
| 5989 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5990 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5991 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5992 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 5993 | goto cleanup; |
| 5994 | } |
| 5995 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5996 | |
| 5997 | break; |
| 5998 | case LYS_DEV_DELETE: |
| 5999 | d_del = (struct lysp_deviate_del*)d; |
| 6000 | |
| 6001 | /* [units-stmt] */ |
| 6002 | if (d_del->units) { |
| 6003 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6004 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6005 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6006 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6007 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6008 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6009 | goto cleanup; |
| 6010 | } |
| 6011 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6012 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6013 | } |
| 6014 | |
| 6015 | /* *must-stmt */ |
| 6016 | if (d_del->musts) { |
| 6017 | switch (devs[u]->target->nodetype) { |
| 6018 | case LYS_CONTAINER: |
| 6019 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6020 | 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] | 6021 | break; |
| 6022 | case LYS_LEAF: |
| 6023 | case LYS_LEAFLIST: |
| 6024 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6025 | 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] | 6026 | break; |
| 6027 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6028 | 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] | 6029 | break; |
| 6030 | case LYS_ACTION: |
| 6031 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6032 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6033 | break; |
| 6034 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6035 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6036 | break; |
| 6037 | } |
| 6038 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6039 | default: |
| 6040 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6041 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6042 | goto cleanup; |
| 6043 | } |
| 6044 | } |
| 6045 | |
| 6046 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6047 | if (d_del->uniques) { |
| 6048 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6049 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6050 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6051 | LY_ARRAY_FOR(list->uniques, z) { |
| 6052 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6053 | nodeid = strpbrk(name, " \t\n"); |
| 6054 | if (nodeid) { |
| 6055 | if (strncmp(name, list->uniques[z][y]->name, nodeid - name) |
| 6056 | || list->uniques[z][y]->name[nodeid - name] != '\0') { |
| 6057 | break; |
| 6058 | } |
| 6059 | while (isspace(*nodeid)) { |
| 6060 | ++nodeid; |
| 6061 | } |
| 6062 | } else { |
| 6063 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6064 | break; |
| 6065 | } |
| 6066 | } |
| 6067 | } |
| 6068 | if (!name) { |
| 6069 | /* complete match - remove the unique */ |
| 6070 | LY_ARRAY_DECREMENT(list->uniques); |
| 6071 | LY_ARRAY_FREE(list->uniques[z]); |
| 6072 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6073 | --z; |
| 6074 | break; |
| 6075 | } |
| 6076 | } |
| 6077 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6078 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6079 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6080 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6081 | goto cleanup; |
| 6082 | } |
| 6083 | } |
| 6084 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6085 | LY_ARRAY_FREE(list->uniques); |
| 6086 | list->uniques = NULL; |
| 6087 | } |
| 6088 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6089 | |
| 6090 | /* *default-stmt */ |
| 6091 | if (d_del->dflts) { |
| 6092 | switch (devs[u]->target->nodetype) { |
| 6093 | case LYS_LEAF: |
| 6094 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6095 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6096 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6097 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6098 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6099 | 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] | 6100 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6101 | if (i) { |
| 6102 | free((char*)dflt); |
| 6103 | } |
| 6104 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6105 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6106 | d_del->dflts[0], dflt); |
| 6107 | goto cleanup; |
| 6108 | } |
| 6109 | if (i) { |
| 6110 | free((char*)dflt); |
| 6111 | } |
| 6112 | dflt = NULL; |
| 6113 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6114 | /* update the list of incomplete default values if needed */ |
| 6115 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6116 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6117 | /* remove the default specification */ |
| 6118 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6119 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6120 | free(leaf->dflt); |
| 6121 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6122 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6123 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6124 | break; |
| 6125 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6126 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6127 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6128 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6129 | 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] | 6130 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6131 | if (i) { |
| 6132 | free((char*)dflt); |
| 6133 | } |
| 6134 | dflt = NULL; |
| 6135 | break; |
| 6136 | } |
| 6137 | if (i) { |
| 6138 | free((char*)dflt); |
| 6139 | } |
| 6140 | dflt = NULL; |
| 6141 | } |
| 6142 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6143 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6144 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6145 | goto cleanup; |
| 6146 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6147 | |
| 6148 | /* update the list of incomplete default values if needed */ |
| 6149 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6150 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6151 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6152 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6153 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6154 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6155 | free(llist->dflts[y]); |
| 6156 | 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] | 6157 | 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] | 6158 | } |
| 6159 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6160 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6161 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6162 | LY_ARRAY_FREE(llist->dflts); |
| 6163 | llist->dflts = NULL; |
| 6164 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6165 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6166 | break; |
| 6167 | case LYS_CHOICE: |
| 6168 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6169 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6170 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6171 | 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] | 6172 | if (prefix) { |
| 6173 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6174 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6175 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6176 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6177 | "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] | 6178 | goto cleanup; |
| 6179 | } |
| 6180 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6181 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6182 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6183 | "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] | 6184 | goto cleanup; |
| 6185 | } |
| 6186 | } |
| 6187 | /* else { |
| 6188 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6189 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6190 | * the module of the default case, just matching its name */ |
| 6191 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6192 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6193 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6194 | 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] | 6195 | goto cleanup; |
| 6196 | } |
| 6197 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6198 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6199 | break; |
| 6200 | default: |
| 6201 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6202 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6203 | goto cleanup; |
| 6204 | } |
| 6205 | } |
| 6206 | |
| 6207 | break; |
| 6208 | case LYS_DEV_REPLACE: |
| 6209 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6210 | |
| 6211 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6212 | if (d_rpl->type) { |
| 6213 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6214 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6215 | 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] | 6216 | |
| 6217 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6218 | /* the target has default from the previous type - remove it */ |
| 6219 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6220 | /* update the list of incomplete default values if needed */ |
| 6221 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6222 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6223 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6224 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6225 | free(leaf->dflt); |
| 6226 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6227 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6228 | } else { /* LYS_LEAFLIST */ |
| 6229 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6230 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6231 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6232 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6233 | free(llist->dflts[x]); |
| 6234 | } |
| 6235 | LY_ARRAY_FREE(llist->dflts); |
| 6236 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6237 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6238 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6239 | } |
| 6240 | } |
| 6241 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6242 | /* there is no default value, do not set changed_type after type compilation |
| 6243 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6244 | changed_type = -1; |
| 6245 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6246 | 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] | 6247 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6248 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6249 | |
| 6250 | /* [units-stmt] */ |
| 6251 | if (d_rpl->units) { |
| 6252 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6253 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6254 | units, "replacing", "units", d_rpl->units); |
| 6255 | |
| 6256 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6257 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6258 | } |
| 6259 | |
| 6260 | /* [default-stmt] */ |
| 6261 | if (d_rpl->dflt) { |
| 6262 | switch (devs[u]->target->nodetype) { |
| 6263 | case LYS_LEAF: |
| 6264 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6265 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6266 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6267 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6268 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6269 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6270 | dflt = d_rpl->dflt; |
| 6271 | /* 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] | 6272 | break; |
| 6273 | case LYS_CHOICE: |
| 6274 | 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] | 6275 | 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] | 6276 | goto cleanup; |
| 6277 | } |
| 6278 | break; |
| 6279 | default: |
| 6280 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6281 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6282 | goto cleanup; |
| 6283 | } |
| 6284 | } |
| 6285 | |
| 6286 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6287 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 6288 | 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] | 6289 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6290 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6291 | goto cleanup; |
| 6292 | } |
| 6293 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6294 | 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] | 6295 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6296 | goto cleanup; |
| 6297 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6298 | 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] | 6299 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6300 | |
| 6301 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6302 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6303 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6304 | 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] | 6305 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6306 | goto cleanup; |
| 6307 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6308 | 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] | 6309 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6310 | |
| 6311 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6312 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6313 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6314 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6315 | /* change value */ |
| 6316 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6317 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6318 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6319 | /* change value */ |
| 6320 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6321 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6322 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6323 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6324 | goto cleanup; |
| 6325 | } |
| 6326 | if (d_rpl->min) { |
| 6327 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6328 | } |
| 6329 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6330 | |
| 6331 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6332 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6333 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6334 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6335 | /* change value */ |
| 6336 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6337 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6338 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6339 | /* change value */ |
| 6340 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6341 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6342 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6343 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6344 | goto cleanup; |
| 6345 | } |
| 6346 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6347 | |
| 6348 | break; |
| 6349 | default: |
| 6350 | LOGINT(ctx->ctx); |
| 6351 | goto cleanup; |
| 6352 | } |
| 6353 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6354 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6355 | /* final check when all deviations of a single target node are applied */ |
| 6356 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6357 | /* check min-max compatibility */ |
| 6358 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6359 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6360 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6361 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6362 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6363 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6364 | } else { |
| 6365 | min = max = 0; |
| 6366 | } |
| 6367 | if (min > max) { |
| 6368 | 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] | 6369 | "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] | 6370 | goto cleanup; |
| 6371 | } |
| 6372 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6373 | if (dflt) { |
| 6374 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6375 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6376 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6377 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6378 | 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] | 6379 | 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] | 6380 | leaf->dflt->realtype->refcount++; |
| 6381 | if (err) { |
| 6382 | ly_err_print(err); |
| 6383 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6384 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6385 | ly_err_free(err); |
| 6386 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6387 | if (rc == LY_EINCOMPLETE) { |
| 6388 | /* postpone default compilation when the tree is complete */ |
| 6389 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6390 | |
| 6391 | /* but in general result is so far ok */ |
| 6392 | rc = LY_SUCCESS; |
| 6393 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6394 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6395 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6396 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6397 | int dynamic; |
| 6398 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6399 | 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] | 6400 | |
| 6401 | /* update the list of incomplete default values if needed */ |
| 6402 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6403 | |
| 6404 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6405 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6406 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6407 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6408 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6409 | 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] | 6410 | 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] | 6411 | leaf->dflt->realtype->refcount++; |
| 6412 | if (err) { |
| 6413 | ly_err_print(err); |
| 6414 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6415 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6416 | ly_err_free(err); |
| 6417 | } |
| 6418 | if (dynamic) { |
| 6419 | free((void*)dflt); |
| 6420 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6421 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6422 | if (rc == LY_EINCOMPLETE) { |
| 6423 | /* postpone default compilation when the tree is complete */ |
| 6424 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6425 | |
| 6426 | /* but in general result is so far ok */ |
| 6427 | rc = LY_SUCCESS; |
| 6428 | } |
| 6429 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6430 | } else { /* LYS_LEAFLIST */ |
| 6431 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6432 | 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] | 6433 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6434 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6435 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6436 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6437 | 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] | 6438 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6439 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6440 | llist->dflts[x]->realtype->refcount++; |
| 6441 | if (err) { |
| 6442 | ly_err_print(err); |
| 6443 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6444 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6445 | dflt, err->msg); |
| 6446 | ly_err_free(err); |
| 6447 | } |
| 6448 | if (dynamic) { |
| 6449 | free((void*)dflt); |
| 6450 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6451 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6452 | if (rc == LY_EINCOMPLETE) { |
| 6453 | /* postpone default compilation when the tree is complete */ |
| 6454 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6455 | |
| 6456 | /* but in general result is so far ok */ |
| 6457 | rc = LY_SUCCESS; |
| 6458 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6459 | LY_CHECK_GOTO(rc, cleanup); |
| 6460 | } |
| 6461 | } |
| 6462 | } |
| 6463 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6464 | /* check mandatory - default compatibility */ |
| 6465 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6466 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6467 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6468 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6469 | "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] | 6470 | goto cleanup; |
| 6471 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6472 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6473 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6474 | 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] | 6475 | goto cleanup; |
| 6476 | } |
| 6477 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6478 | /* mandatory node under a default case */ |
| 6479 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6480 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6481 | 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] | 6482 | goto cleanup; |
| 6483 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6484 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6485 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6486 | } |
| 6487 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6488 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6489 | ret = LY_SUCCESS; |
| 6490 | |
| 6491 | cleanup: |
| 6492 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6493 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6494 | free(devs[u]); |
| 6495 | } |
| 6496 | free(devs); |
| 6497 | ly_set_erase(&targets, NULL); |
| 6498 | ly_set_erase(&devs_p, NULL); |
| 6499 | |
| 6500 | return ret; |
| 6501 | } |
| 6502 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6503 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6504 | * @brief Compile the given YANG submodule into the main module. |
| 6505 | * @param[in] ctx Compile context |
| 6506 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6507 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6508 | */ |
| 6509 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6510 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6511 | { |
| 6512 | unsigned int u; |
| 6513 | LY_ERR ret = LY_SUCCESS; |
| 6514 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6515 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6516 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6517 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6518 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6519 | if (!mainmod->mod->off_features) { |
| 6520 | /* features are compiled directly into the compiled module structure, |
| 6521 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6522 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6523 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6524 | mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features); |
| 6525 | LY_CHECK_GOTO(ret, error); |
| 6526 | } |
| 6527 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6528 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6529 | 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] | 6530 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6531 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6532 | /* data nodes */ |
| 6533 | LY_LIST_FOR(submod->data, node_p) { |
| 6534 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6535 | LY_CHECK_GOTO(ret, error); |
| 6536 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6537 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6538 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6539 | 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] | 6540 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6541 | error: |
| 6542 | return ret; |
| 6543 | } |
| 6544 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6545 | LY_ERR |
| 6546 | lys_compile(struct lys_module *mod, int options) |
| 6547 | { |
| 6548 | struct lysc_ctx ctx = {0}; |
| 6549 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6550 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6551 | struct lysp_module *sp; |
| 6552 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6553 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6554 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6555 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6556 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6557 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6558 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6559 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 6560 | |
| 6561 | if (!mod->implemented) { |
| 6562 | /* just imported modules are not compiled */ |
| 6563 | return LY_SUCCESS; |
| 6564 | } |
| 6565 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6566 | sp = mod->parsed; |
| 6567 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6568 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6569 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6570 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6571 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6572 | ctx.path_len = 1; |
| 6573 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6574 | |
| 6575 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6576 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 6577 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6578 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6579 | 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] | 6580 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6581 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6582 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6583 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6584 | if (mod->off_features) { |
| 6585 | /* there is already precompiled array of features */ |
| 6586 | mod_c->features = mod->off_features; |
| 6587 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6588 | } else { |
| 6589 | /* features are compiled directly into the compiled module structure, |
| 6590 | * 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] | 6591 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6592 | LY_CHECK_GOTO(ret, error); |
| 6593 | } |
| 6594 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 6595 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 6596 | * are present. */ |
| 6597 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6598 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6599 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6600 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6601 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6602 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6603 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6604 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6605 | 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] | 6606 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6607 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6608 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6609 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6610 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6611 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6612 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6613 | 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] | 6614 | if (sp->identities) { |
| 6615 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 6616 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6617 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6618 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6619 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6620 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6621 | ret = lys_compile_node(&ctx, node_p, NULL, 0); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6622 | LY_CHECK_GOTO(ret, error); |
| 6623 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6624 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6625 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6626 | 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] | 6627 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6628 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 6629 | ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6630 | LY_CHECK_GOTO(ret, error); |
| 6631 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6632 | ret = lys_compile_augment(&ctx, augments[u], NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6633 | LY_CHECK_GOTO(ret, error); |
| 6634 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6635 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6636 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6637 | ret = lys_compile_deviations(&ctx, sp); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6638 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6639 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6640 | COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6641 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6642 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6643 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 6644 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 6645 | * 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] | 6646 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6647 | 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] | 6648 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6649 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6650 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6651 | 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] | 6652 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6653 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6654 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6655 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6656 | /* validate the path */ |
| 6657 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 6658 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 6659 | LY_CHECK_GOTO(ret, error); |
| 6660 | } |
| 6661 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6662 | } |
| 6663 | } |
| 6664 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6665 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6666 | 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] | 6667 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6668 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6669 | /* store pointer to the real type */ |
| 6670 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 6671 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6672 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6673 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6674 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6675 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6676 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6677 | /* store pointer to the real type */ |
| 6678 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 6679 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6680 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6681 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 6682 | } |
| 6683 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6684 | } |
| 6685 | } |
| 6686 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6687 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6688 | /* finish incomplete default values compilation */ |
| 6689 | for (u = 0; u < ctx.dflts.count; ++u) { |
| 6690 | struct ly_err_item *err = NULL; |
| 6691 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[u]; |
| 6692 | ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized), |
| 6693 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 6694 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 6695 | if (err) { |
| 6696 | ly_err_print(err); |
| 6697 | ctx.path[0] = '\0'; |
| 6698 | lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE); |
| 6699 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 6700 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6701 | ly_err_free(err); |
| 6702 | } |
| 6703 | LY_CHECK_GOTO(ret, error); |
| 6704 | } |
| 6705 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6706 | /* validate non-instantiated groupings from the parsed schema, |
| 6707 | * without it we would accept even the schemas with invalid grouping specification */ |
| 6708 | ctx.options |= LYSC_OPT_GROUPING; |
| 6709 | LY_ARRAY_FOR(sp->groupings, u) { |
| 6710 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
| 6711 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error); |
| 6712 | } |
| 6713 | } |
| 6714 | LY_LIST_FOR(sp->data, node_p) { |
| 6715 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 6716 | LY_ARRAY_FOR(grps, u) { |
| 6717 | if (!(grps[u].flags & LYS_USED_GRP)) { |
| 6718 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error); |
| 6719 | } |
| 6720 | } |
| 6721 | } |
| 6722 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6723 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 6724 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 6725 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 6726 | } |
| 6727 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6728 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6729 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6730 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6731 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6732 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6733 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6734 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6735 | lysp_module_free(mod->parsed); |
| 6736 | ((struct lys_module*)mod)->parsed = NULL; |
| 6737 | } |
| 6738 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6739 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6740 | /* remove flag of the modules implemented by dependency */ |
| 6741 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6742 | m = ctx.ctx->list.objs[u]; |
| 6743 | if (m->implemented == 2) { |
| 6744 | m->implemented = 1; |
| 6745 | } |
| 6746 | } |
| 6747 | } |
| 6748 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6749 | ((struct lys_module*)mod)->compiled = mod_c; |
| 6750 | return LY_SUCCESS; |
| 6751 | |
| 6752 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6753 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6754 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6755 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6756 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6757 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6758 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6759 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6760 | mod->compiled = NULL; |
| 6761 | |
| 6762 | /* revert compilation of modules implemented by dependency */ |
| 6763 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6764 | m = ctx.ctx->list.objs[u]; |
| 6765 | if (m->implemented == 2) { |
| 6766 | /* revert features list to the precompiled state */ |
| 6767 | lys_feature_precompile_revert(&ctx, m); |
| 6768 | /* mark module as imported-only / not-implemented */ |
| 6769 | m->implemented = 0; |
| 6770 | /* free the compiled version of the module */ |
| 6771 | lysc_module_free(m->compiled, NULL); |
| 6772 | m->compiled = NULL; |
| 6773 | } |
| 6774 | } |
| 6775 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6776 | return ret; |
| 6777 | } |