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 | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2110 | unsigned int dest_parent_times, c, u; |
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; |
| 2170 | if (context_node->keys) { |
| 2171 | for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) { |
| 2172 | if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') { |
| 2173 | src_node = (const struct lysc_node*)context_node->keys[u]; |
| 2174 | break; |
| 2175 | } |
| 2176 | } |
| 2177 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2178 | if (!src_node) { |
| 2179 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2180 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2181 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2182 | goto cleanup; |
| 2183 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2184 | |
| 2185 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2186 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2187 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2188 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2189 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2190 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2191 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2192 | *predicate - start, start, src_node->name); |
| 2193 | goto cleanup; |
| 2194 | } |
| 2195 | |
| 2196 | /* destination */ |
| 2197 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2198 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2199 | |
| 2200 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2201 | if (strncmp(path_key_expr, "current", 7)) { |
| 2202 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2203 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2204 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2205 | *predicate - start, start); |
| 2206 | goto cleanup; |
| 2207 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2208 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2209 | if (*path_key_expr != '(') { |
| 2210 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2211 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2212 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2213 | if (*path_key_expr != ')') { |
| 2214 | goto error_current_function_invocation; |
| 2215 | } |
| 2216 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2217 | |
| 2218 | if (*path_key_expr != '/') { |
| 2219 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2220 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2221 | *predicate - start, start); |
| 2222 | goto cleanup; |
| 2223 | } |
| 2224 | ++path_key_expr; |
| 2225 | while (isspace(*path_key_expr)) { |
| 2226 | ++path_key_expr; |
| 2227 | } |
| 2228 | |
| 2229 | /* rel-path-keyexpr: |
| 2230 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2231 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2232 | ++dest_parent_times; |
| 2233 | path_key_expr += 2; |
| 2234 | while (isspace(*path_key_expr)) { |
| 2235 | ++path_key_expr; |
| 2236 | } |
| 2237 | if (*path_key_expr != '/') { |
| 2238 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2239 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2240 | *predicate - start, start); |
| 2241 | goto cleanup; |
| 2242 | } |
| 2243 | ++path_key_expr; |
| 2244 | while (isspace(*path_key_expr)) { |
| 2245 | ++path_key_expr; |
| 2246 | } |
| 2247 | |
| 2248 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2249 | * all schema nodes that cannot be instantiated in data tree */ |
| 2250 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2251 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2252 | *predicate - start, start); |
| 2253 | } |
| 2254 | if (!dest_parent_times) { |
| 2255 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2256 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2257 | *predicate - start, start); |
| 2258 | goto cleanup; |
| 2259 | } |
| 2260 | if (path_key_expr == pke_end) { |
| 2261 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2262 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2263 | *predicate - start, start); |
| 2264 | goto cleanup; |
| 2265 | } |
| 2266 | |
| 2267 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2268 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2269 | 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] | 2270 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2271 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2272 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2273 | goto cleanup; |
| 2274 | } |
| 2275 | |
| 2276 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2277 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2278 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2279 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2280 | } |
| 2281 | if (!mod) { |
| 2282 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2283 | "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] | 2284 | *predicate - start, start, dst_len, dst); |
| 2285 | goto cleanup; |
| 2286 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2287 | if (!mod->implemented) { |
| 2288 | /* make the module implemented */ |
| 2289 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2290 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2291 | |
| 2292 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2293 | if (!dst_node) { |
| 2294 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2295 | "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] | 2296 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2297 | goto cleanup; |
| 2298 | } |
| 2299 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2300 | 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] | 2301 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2302 | "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] | 2303 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2304 | goto cleanup; |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | ret = LY_SUCCESS; |
| 2309 | cleanup: |
| 2310 | ly_set_erase(&keys, NULL); |
| 2311 | return ret; |
| 2312 | } |
| 2313 | |
| 2314 | /** |
| 2315 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2316 | * |
| 2317 | * path-arg = absolute-path / relative-path |
| 2318 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2319 | * relative-path = 1*(".." "/") descendant-path |
| 2320 | * |
| 2321 | * @param[in,out] path Path to parse. |
| 2322 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2323 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2324 | * @param[out] name Name of the token. |
| 2325 | * @param[out] nam_len Length of the name. |
| 2326 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2327 | * must not be changed between consecutive calls. -1 if the |
| 2328 | * path is absolute. |
| 2329 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2330 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2331 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2332 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2333 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2334 | int *parent_times, int *has_predicate) |
| 2335 | { |
| 2336 | int par_times = 0; |
| 2337 | |
| 2338 | assert(path && *path); |
| 2339 | assert(parent_times); |
| 2340 | assert(prefix); |
| 2341 | assert(prefix_len); |
| 2342 | assert(name); |
| 2343 | assert(name_len); |
| 2344 | assert(has_predicate); |
| 2345 | |
| 2346 | *prefix = NULL; |
| 2347 | *prefix_len = 0; |
| 2348 | *name = NULL; |
| 2349 | *name_len = 0; |
| 2350 | *has_predicate = 0; |
| 2351 | |
| 2352 | if (!*parent_times) { |
| 2353 | if (!strncmp(*path, "..", 2)) { |
| 2354 | *path += 2; |
| 2355 | ++par_times; |
| 2356 | while (!strncmp(*path, "/..", 3)) { |
| 2357 | *path += 3; |
| 2358 | ++par_times; |
| 2359 | } |
| 2360 | } |
| 2361 | if (par_times) { |
| 2362 | *parent_times = par_times; |
| 2363 | } else { |
| 2364 | *parent_times = -1; |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | if (**path != '/') { |
| 2369 | return LY_EINVAL; |
| 2370 | } |
| 2371 | /* skip '/' */ |
| 2372 | ++(*path); |
| 2373 | |
| 2374 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2375 | 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] | 2376 | |
| 2377 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2378 | /* path continues by another token or this is the last token */ |
| 2379 | return LY_SUCCESS; |
| 2380 | } else if ((*path)[0] != '[') { |
| 2381 | /* unexpected character */ |
| 2382 | return LY_EINVAL; |
| 2383 | } else { |
| 2384 | /* predicate starting with [ */ |
| 2385 | *has_predicate = 1; |
| 2386 | return LY_SUCCESS; |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2391 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2392 | * |
| 2393 | * The set of features used for target must be a subset of features used for the leafref. |
| 2394 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2395 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2396 | * |
| 2397 | * @param[in] refnode The leafref node. |
| 2398 | * @param[in] target Tha target node of the leafref. |
| 2399 | * @return LY_SUCCESS or LY_EVALID; |
| 2400 | */ |
| 2401 | static LY_ERR |
| 2402 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2403 | { |
| 2404 | LY_ERR ret = LY_EVALID; |
| 2405 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2406 | unsigned int u, v, count; |
| 2407 | struct ly_set features = {0}; |
| 2408 | |
| 2409 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2410 | if (iter->iffeatures) { |
| 2411 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2412 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2413 | 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] | 2414 | } |
| 2415 | } |
| 2416 | } |
| 2417 | } |
| 2418 | |
| 2419 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2420 | * So when adding features applicable to the target into the features set, we should not be |
| 2421 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2422 | * to the leafref itself. */ |
| 2423 | count = features.count; |
| 2424 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2425 | if (iter->iffeatures) { |
| 2426 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2427 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2428 | 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] | 2429 | /* new feature was added (or LY_EMEM) */ |
| 2430 | goto cleanup; |
| 2431 | } |
| 2432 | } |
| 2433 | } |
| 2434 | } |
| 2435 | } |
| 2436 | ret = LY_SUCCESS; |
| 2437 | |
| 2438 | cleanup: |
| 2439 | ly_set_erase(&features, NULL); |
| 2440 | return ret; |
| 2441 | } |
| 2442 | |
| 2443 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2444 | * @brief Validate the leafref path. |
| 2445 | * @param[in] ctx Compile context |
| 2446 | * @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] | 2447 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2448 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2449 | */ |
| 2450 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2451 | 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] | 2452 | { |
| 2453 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2454 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2455 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2456 | const char *id, *prefix, *name; |
| 2457 | size_t prefix_len, name_len; |
| 2458 | int parent_times = 0, has_predicate; |
| 2459 | unsigned int iter, u; |
| 2460 | LY_ERR ret = LY_SUCCESS; |
| 2461 | |
| 2462 | assert(ctx); |
| 2463 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2464 | assert(leafref); |
| 2465 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2466 | ctx->path[0] = '\0'; |
| 2467 | lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 2468 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2469 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2470 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2471 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2472 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2473 | if (!iter) { /* first iteration */ |
| 2474 | /* precess ".." in relative paths */ |
| 2475 | if (parent_times > 0) { |
| 2476 | /* move from the context node */ |
| 2477 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2478 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2479 | * all schema nodes that cannot be instantiated in data tree */ |
| 2480 | 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] | 2481 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2482 | } |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2487 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2488 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2489 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2490 | } |
| 2491 | if (!mod) { |
| 2492 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2493 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2494 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2495 | return LY_EVALID; |
| 2496 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2497 | if (!mod->implemented) { |
| 2498 | /* make the module implemented */ |
| 2499 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2500 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2501 | |
| 2502 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2503 | if (!node) { |
| 2504 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2505 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2506 | return LY_EVALID; |
| 2507 | } |
| 2508 | parent = node; |
| 2509 | |
| 2510 | if (has_predicate) { |
| 2511 | /* we have predicate, so the current result must be list */ |
| 2512 | if (node->nodetype != LYS_LIST) { |
| 2513 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2514 | "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] | 2515 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2516 | return LY_EVALID; |
| 2517 | } |
| 2518 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2519 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2520 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | ++iter; |
| 2524 | } |
| 2525 | if (ret) { |
| 2526 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2527 | "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] | 2528 | return LY_EVALID; |
| 2529 | } |
| 2530 | |
| 2531 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2532 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2533 | "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] | 2534 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2535 | return LY_EVALID; |
| 2536 | } |
| 2537 | |
| 2538 | /* check status */ |
| 2539 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2540 | return LY_EVALID; |
| 2541 | } |
| 2542 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2543 | /* check config */ |
| 2544 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2545 | if (node->flags & LYS_CONFIG_R) { |
| 2546 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2547 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2548 | leafref->path); |
| 2549 | return LY_EVALID; |
| 2550 | } |
| 2551 | } |
| 2552 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2553 | /* store the target's type and check for circular chain of leafrefs */ |
| 2554 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2555 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2556 | if (type == (struct lysc_type*)leafref) { |
| 2557 | /* circular chain detected */ |
| 2558 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2559 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2560 | return LY_EVALID; |
| 2561 | } |
| 2562 | } |
| 2563 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2564 | /* check if leafref and its target are under common if-features */ |
| 2565 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2566 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2567 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2568 | leafref->path); |
| 2569 | return LY_EVALID; |
| 2570 | } |
| 2571 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2572 | ctx->path_len = 1; |
| 2573 | ctx->path[1] = '\0'; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2574 | return LY_SUCCESS; |
| 2575 | } |
| 2576 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2577 | 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] | 2578 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2579 | /** |
| 2580 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2581 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2582 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2583 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2584 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2585 | * @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] | 2586 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2587 | * @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] | 2588 | * @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] | 2589 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2590 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2591 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2592 | * @return LY_ERR value. |
| 2593 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2594 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2595 | 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] | 2596 | 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] | 2597 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2598 | { |
| 2599 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2600 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2601 | struct lysc_type_bin *bin; |
| 2602 | struct lysc_type_num *num; |
| 2603 | struct lysc_type_str *str; |
| 2604 | struct lysc_type_bits *bits; |
| 2605 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2606 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2607 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2608 | struct lysc_type_union *un, *un_aux; |
| 2609 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2610 | |
| 2611 | switch (basetype) { |
| 2612 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2613 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2614 | |
| 2615 | /* 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] | 2616 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2617 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2618 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2619 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2620 | 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] | 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | if (tpdfname) { |
| 2625 | type_p->compiled = *type; |
| 2626 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2627 | } |
| 2628 | break; |
| 2629 | case LY_TYPE_BITS: |
| 2630 | /* RFC 7950 9.7 - bits */ |
| 2631 | bits = (struct lysc_type_bits*)(*type); |
| 2632 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2633 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2634 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2635 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2636 | } |
| 2637 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2638 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2639 | /* 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] | 2640 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2641 | 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] | 2642 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2643 | 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] | 2644 | free(*type); |
| 2645 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2646 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2647 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | if (tpdfname) { |
| 2651 | type_p->compiled = *type; |
| 2652 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2653 | } |
| 2654 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2655 | case LY_TYPE_DEC64: |
| 2656 | dec = (struct lysc_type_dec*)(*type); |
| 2657 | |
| 2658 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2659 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2660 | if (!type_p->fraction_digits) { |
| 2661 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2662 | 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] | 2663 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2664 | 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] | 2665 | free(*type); |
| 2666 | *type = NULL; |
| 2667 | } |
| 2668 | return LY_EVALID; |
| 2669 | } |
| 2670 | } else if (type_p->fraction_digits) { |
| 2671 | /* 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] | 2672 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2673 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2674 | "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] | 2675 | tpdfname); |
| 2676 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2677 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2678 | "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] | 2679 | free(*type); |
| 2680 | *type = NULL; |
| 2681 | } |
| 2682 | return LY_EVALID; |
| 2683 | } |
| 2684 | dec->fraction_digits = type_p->fraction_digits; |
| 2685 | |
| 2686 | /* RFC 7950 9.2.4 - range */ |
| 2687 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2688 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2689 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2690 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2691 | 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] | 2692 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | if (tpdfname) { |
| 2696 | type_p->compiled = *type; |
| 2697 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2698 | } |
| 2699 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2700 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2701 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2702 | |
| 2703 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2704 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2705 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2706 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2707 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2708 | 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] | 2709 | } |
| 2710 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2711 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2712 | } |
| 2713 | |
| 2714 | /* RFC 7950 9.4.5 - pattern */ |
| 2715 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2716 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2717 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2718 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2719 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2720 | } |
| 2721 | |
| 2722 | if (tpdfname) { |
| 2723 | type_p->compiled = *type; |
| 2724 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2725 | } |
| 2726 | break; |
| 2727 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2728 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2729 | |
| 2730 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2731 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2732 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2733 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2734 | } |
| 2735 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2736 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2737 | /* type derived from enumerations built-in type must contain at least one enum */ |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2738 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2739 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2740 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2742 | free(*type); |
| 2743 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2744 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2745 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | if (tpdfname) { |
| 2749 | type_p->compiled = *type; |
| 2750 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2751 | } |
| 2752 | break; |
| 2753 | case LY_TYPE_INT8: |
| 2754 | case LY_TYPE_UINT8: |
| 2755 | case LY_TYPE_INT16: |
| 2756 | case LY_TYPE_UINT16: |
| 2757 | case LY_TYPE_INT32: |
| 2758 | case LY_TYPE_UINT32: |
| 2759 | case LY_TYPE_INT64: |
| 2760 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2761 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2762 | |
| 2763 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2764 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2765 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2766 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2767 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2768 | 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] | 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | if (tpdfname) { |
| 2773 | type_p->compiled = *type; |
| 2774 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2775 | } |
| 2776 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2777 | case LY_TYPE_IDENT: |
| 2778 | idref = (struct lysc_type_identityref*)(*type); |
| 2779 | |
| 2780 | /* RFC 7950 9.10.2 - base */ |
| 2781 | if (type_p->bases) { |
| 2782 | if (base) { |
| 2783 | /* only the directly derived identityrefs can contain base specification */ |
| 2784 | if (tpdfname) { |
| 2785 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2786 | "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] | 2787 | tpdfname); |
| 2788 | } else { |
| 2789 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2790 | "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] | 2791 | free(*type); |
| 2792 | *type = NULL; |
| 2793 | } |
| 2794 | return LY_EVALID; |
| 2795 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2796 | 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] | 2797 | } |
| 2798 | |
| 2799 | if (!base && !type_p->flags) { |
| 2800 | /* type derived from identityref built-in type must contain at least one base */ |
| 2801 | if (tpdfname) { |
| 2802 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2803 | } else { |
| 2804 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2805 | free(*type); |
| 2806 | *type = NULL; |
| 2807 | } |
| 2808 | return LY_EVALID; |
| 2809 | } |
| 2810 | |
| 2811 | if (tpdfname) { |
| 2812 | type_p->compiled = *type; |
| 2813 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2814 | } |
| 2815 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2816 | case LY_TYPE_LEAFREF: |
| 2817 | /* RFC 7950 9.9.3 - require-instance */ |
| 2818 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2819 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2820 | if (tpdfname) { |
| 2821 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2822 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2823 | } else { |
| 2824 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2825 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2826 | free(*type); |
| 2827 | *type = NULL; |
| 2828 | } |
| 2829 | return LY_EVALID; |
| 2830 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2831 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2832 | } else if (base) { |
| 2833 | /* inherit */ |
| 2834 | ((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] | 2835 | } else { |
| 2836 | /* default is true */ |
| 2837 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2838 | } |
| 2839 | if (type_p->path) { |
| 2840 | 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] | 2841 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2842 | } else if (base) { |
| 2843 | 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] | 2844 | ((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] | 2845 | } else if (tpdfname) { |
| 2846 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2847 | return LY_EVALID; |
| 2848 | } else { |
| 2849 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2850 | free(*type); |
| 2851 | *type = NULL; |
| 2852 | return LY_EVALID; |
| 2853 | } |
| 2854 | if (tpdfname) { |
| 2855 | type_p->compiled = *type; |
| 2856 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2857 | } |
| 2858 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2859 | case LY_TYPE_INST: |
| 2860 | /* RFC 7950 9.9.3 - require-instance */ |
| 2861 | if (type_p->flags & LYS_SET_REQINST) { |
| 2862 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2863 | } else { |
| 2864 | /* default is true */ |
| 2865 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2866 | } |
| 2867 | |
| 2868 | if (tpdfname) { |
| 2869 | type_p->compiled = *type; |
| 2870 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2871 | } |
| 2872 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2873 | case LY_TYPE_UNION: |
| 2874 | un = (struct lysc_type_union*)(*type); |
| 2875 | |
| 2876 | /* RFC 7950 7.4 - type */ |
| 2877 | if (type_p->types) { |
| 2878 | if (base) { |
| 2879 | /* only the directly derived union can contain types specification */ |
| 2880 | if (tpdfname) { |
| 2881 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2882 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2883 | tpdfname); |
| 2884 | } else { |
| 2885 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2886 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2887 | free(*type); |
| 2888 | *type = NULL; |
| 2889 | } |
| 2890 | return LY_EVALID; |
| 2891 | } |
| 2892 | /* compile the type */ |
| 2893 | additional = 0; |
| 2894 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 2895 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2896 | 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] | 2897 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2898 | /* add space for additional types from the union subtype */ |
| 2899 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 2900 | 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))); |
| 2901 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2902 | un->types = (void*)((uint32_t*)(p) + 1); |
| 2903 | |
| 2904 | /* copy subtypes of the subtype union */ |
| 2905 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 2906 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2907 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2908 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2909 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2910 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 2911 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 2912 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 2913 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2914 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 2915 | /* TODO extensions */ |
| 2916 | |
| 2917 | } else { |
| 2918 | un->types[u + additional] = un_aux->types[v]; |
| 2919 | ++un_aux->types[v]->refcount; |
| 2920 | } |
| 2921 | ++additional; |
| 2922 | LY_ARRAY_INCREMENT(un->types); |
| 2923 | } |
| 2924 | /* compensate u increment in main loop */ |
| 2925 | --additional; |
| 2926 | |
| 2927 | /* free the replaced union subtype */ |
| 2928 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2929 | } else { |
| 2930 | LY_ARRAY_INCREMENT(un->types); |
| 2931 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2932 | } |
| 2933 | } |
| 2934 | |
| 2935 | if (!base && !type_p->flags) { |
| 2936 | /* type derived from union built-in type must contain at least one type */ |
| 2937 | if (tpdfname) { |
| 2938 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2939 | } else { |
| 2940 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2941 | free(*type); |
| 2942 | *type = NULL; |
| 2943 | } |
| 2944 | return LY_EVALID; |
| 2945 | } |
| 2946 | |
| 2947 | if (tpdfname) { |
| 2948 | type_p->compiled = *type; |
| 2949 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2950 | } |
| 2951 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2952 | case LY_TYPE_BOOL: |
| 2953 | case LY_TYPE_EMPTY: |
| 2954 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2955 | break; |
| 2956 | } |
| 2957 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2958 | done: |
| 2959 | return ret; |
| 2960 | } |
| 2961 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2962 | /** |
| 2963 | * @brief Compile information about the leaf/leaf-list's type. |
| 2964 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2965 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2966 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2967 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2968 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2969 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2970 | * @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] | 2971 | * @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] | 2972 | * @return LY_ERR value. |
| 2973 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2974 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2975 | 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] | 2976 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2977 | { |
| 2978 | LY_ERR ret = LY_SUCCESS; |
| 2979 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2980 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2981 | struct type_context { |
| 2982 | const struct lysp_tpdf *tpdf; |
| 2983 | struct lysp_node *node; |
| 2984 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2985 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2986 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2987 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2988 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2989 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2990 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2991 | |
| 2992 | (*type) = NULL; |
| 2993 | |
| 2994 | tctx = calloc(1, sizeof *tctx); |
| 2995 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2996 | 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] | 2997 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2998 | ret == LY_SUCCESS; |
| 2999 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 3000 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 3001 | if (basetype) { |
| 3002 | break; |
| 3003 | } |
| 3004 | |
| 3005 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3006 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3007 | 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] | 3008 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3009 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3010 | if (units && !*units) { |
| 3011 | /* inherit units */ |
| 3012 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3013 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3014 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3015 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3016 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3017 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3018 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3019 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3020 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3021 | break; |
| 3022 | } |
| 3023 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3024 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3025 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3026 | * 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] | 3027 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3028 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3029 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3030 | dummyloops = 1; |
| 3031 | goto preparenext; |
| 3032 | } else { |
| 3033 | tctx = NULL; |
| 3034 | break; |
| 3035 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3036 | } |
| 3037 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3038 | /* circular typedef reference detection */ |
| 3039 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3040 | /* local part */ |
| 3041 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3042 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3043 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3044 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3045 | free(tctx); |
| 3046 | ret = LY_EVALID; |
| 3047 | goto cleanup; |
| 3048 | } |
| 3049 | } |
| 3050 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3051 | /* global part for unions corner case */ |
| 3052 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3053 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3054 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3055 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3056 | free(tctx); |
| 3057 | ret = LY_EVALID; |
| 3058 | goto cleanup; |
| 3059 | } |
| 3060 | } |
| 3061 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3062 | /* store information for the following processing */ |
| 3063 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3064 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3065 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3066 | /* prepare next loop */ |
| 3067 | tctx_prev = tctx; |
| 3068 | tctx = calloc(1, sizeof *tctx); |
| 3069 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3070 | } |
| 3071 | free(tctx); |
| 3072 | |
| 3073 | /* allocate type according to the basetype */ |
| 3074 | switch (basetype) { |
| 3075 | case LY_TYPE_BINARY: |
| 3076 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3077 | break; |
| 3078 | case LY_TYPE_BITS: |
| 3079 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3080 | break; |
| 3081 | case LY_TYPE_BOOL: |
| 3082 | case LY_TYPE_EMPTY: |
| 3083 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3084 | break; |
| 3085 | case LY_TYPE_DEC64: |
| 3086 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3087 | break; |
| 3088 | case LY_TYPE_ENUM: |
| 3089 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3090 | break; |
| 3091 | case LY_TYPE_IDENT: |
| 3092 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3093 | break; |
| 3094 | case LY_TYPE_INST: |
| 3095 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3096 | break; |
| 3097 | case LY_TYPE_LEAFREF: |
| 3098 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3099 | break; |
| 3100 | case LY_TYPE_STRING: |
| 3101 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3102 | break; |
| 3103 | case LY_TYPE_UNION: |
| 3104 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3105 | break; |
| 3106 | case LY_TYPE_INT8: |
| 3107 | case LY_TYPE_UINT8: |
| 3108 | case LY_TYPE_INT16: |
| 3109 | case LY_TYPE_UINT16: |
| 3110 | case LY_TYPE_INT32: |
| 3111 | case LY_TYPE_UINT32: |
| 3112 | case LY_TYPE_INT64: |
| 3113 | case LY_TYPE_UINT64: |
| 3114 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3115 | break; |
| 3116 | case LY_TYPE_UNKNOWN: |
| 3117 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3118 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3119 | ret = LY_EVALID; |
| 3120 | goto cleanup; |
| 3121 | } |
| 3122 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3123 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3124 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3125 | ly_data_type2str[basetype]); |
| 3126 | free(*type); |
| 3127 | (*type) = NULL; |
| 3128 | ret = LY_EVALID; |
| 3129 | goto cleanup; |
| 3130 | } |
| 3131 | |
| 3132 | /* get restrictions from the referred typedefs */ |
| 3133 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3134 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3135 | |
| 3136 | /* remember the typedef context for circular check */ |
| 3137 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3138 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3139 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3140 | base = tctx->tpdf->type.compiled; |
| 3141 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3142 | } 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] | 3143 | /* no change, just use the type information from the base */ |
| 3144 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3145 | ++base->refcount; |
| 3146 | continue; |
| 3147 | } |
| 3148 | |
| 3149 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3150 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3151 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3152 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3153 | ret = LY_EVALID; |
| 3154 | goto cleanup; |
| 3155 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3156 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3157 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3158 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3159 | ret = LY_EVALID; |
| 3160 | goto cleanup; |
| 3161 | } |
| 3162 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3163 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3164 | /* TODO user type plugins */ |
| 3165 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3166 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3167 | 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] | 3168 | 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] | 3169 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3170 | LY_CHECK_GOTO(ret, cleanup); |
| 3171 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3172 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3173 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3174 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3175 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3176 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3177 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3178 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3179 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3180 | /* TODO user type plugins */ |
| 3181 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3182 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3183 | 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] | 3184 | LY_CHECK_GOTO(ret, cleanup); |
| 3185 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3186 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3187 | free(*type); |
| 3188 | (*type) = base; |
| 3189 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3190 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3191 | if (dflt && !(*type)->dflt) { |
| 3192 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3193 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3194 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3195 | (*type)->dflt->realtype = (*type); |
| 3196 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3197 | 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] | 3198 | 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] | 3199 | if (err) { |
| 3200 | ly_err_print(err); |
| 3201 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3202 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3203 | ly_err_free(err); |
| 3204 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3205 | if (ret == LY_EINCOMPLETE) { |
| 3206 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3207 | 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] | 3208 | |
| 3209 | /* but in general result is so far ok */ |
| 3210 | ret = LY_SUCCESS; |
| 3211 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3212 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3213 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3214 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3215 | 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] | 3216 | |
| 3217 | cleanup: |
| 3218 | ly_set_erase(&tpdf_chain, free); |
| 3219 | return ret; |
| 3220 | } |
| 3221 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3222 | /** |
| 3223 | * @brief Compile status information of the given node. |
| 3224 | * |
| 3225 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3226 | * has the status correctly set during the compilation. |
| 3227 | * |
| 3228 | * @param[in] ctx Compile context |
| 3229 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3230 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3231 | * the compatibility with the parent's status value. |
| 3232 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3233 | * @return LY_ERR value. |
| 3234 | */ |
| 3235 | static LY_ERR |
| 3236 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3237 | { |
| 3238 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3239 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3240 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3241 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3242 | if ((parent_flags & 0x3) != 0x3) { |
| 3243 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3244 | * combination of bits (0x3) which marks the uses_status value */ |
| 3245 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3246 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3247 | } |
| 3248 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3249 | } else { |
| 3250 | (*node_flags) |= LYS_STATUS_CURR; |
| 3251 | } |
| 3252 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3253 | /* check status compatibility with the parent */ |
| 3254 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3255 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3256 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3257 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3258 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3259 | } else { /* LYS_STATUS_DEPRC */ |
| 3260 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3261 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3262 | } |
| 3263 | return LY_EVALID; |
| 3264 | } |
| 3265 | } |
| 3266 | return LY_SUCCESS; |
| 3267 | } |
| 3268 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3269 | /** |
| 3270 | * @brief Check uniqness of the node/action/notification name. |
| 3271 | * |
| 3272 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3273 | * structures, but they share the namespace so we need to check their name collisions. |
| 3274 | * |
| 3275 | * @param[in] ctx Compile context. |
| 3276 | * @param[in] children List (linked list) of data nodes to go through. |
| 3277 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3278 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3279 | * @param[in] name Name of the item to find in the given lists. |
| 3280 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3281 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3282 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3283 | */ |
| 3284 | static LY_ERR |
| 3285 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3286 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3287 | const char *name, void *exclude) |
| 3288 | { |
| 3289 | const struct lysc_node *iter; |
| 3290 | unsigned int u; |
| 3291 | |
| 3292 | LY_LIST_FOR(children, iter) { |
| 3293 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3294 | goto error; |
| 3295 | } |
| 3296 | } |
| 3297 | LY_ARRAY_FOR(actions, u) { |
| 3298 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3299 | goto error; |
| 3300 | } |
| 3301 | } |
| 3302 | LY_ARRAY_FOR(notifs, u) { |
| 3303 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3304 | goto error; |
| 3305 | } |
| 3306 | } |
| 3307 | return LY_SUCCESS; |
| 3308 | error: |
| 3309 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification"); |
| 3310 | return LY_EEXIST; |
| 3311 | } |
| 3312 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3313 | 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] | 3314 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3315 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3316 | * @brief Compile parsed RPC/action schema node information. |
| 3317 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3318 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3319 | * @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] | 3320 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3321 | * @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). |
| 3322 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3323 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3324 | */ |
| 3325 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3326 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3327 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3328 | { |
| 3329 | LY_ERR ret = LY_SUCCESS; |
| 3330 | struct lysp_node *child_p; |
| 3331 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3332 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3333 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3334 | lysc_update_path(ctx, parent, action_p->name); |
| 3335 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3336 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3337 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3338 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3339 | action_p->name, action)) { |
| 3340 | return LY_EVALID; |
| 3341 | } |
| 3342 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3343 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3344 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3345 | "Action \"%s\" is placed inside %s.", action_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3346 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3347 | return LY_EVALID; |
| 3348 | } |
| 3349 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3350 | action->nodetype = LYS_ACTION; |
| 3351 | action->module = ctx->mod; |
| 3352 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3353 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3354 | action->sp = action_p; |
| 3355 | } |
| 3356 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3357 | |
| 3358 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3359 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3360 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3361 | |
| 3362 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3363 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3364 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3365 | COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
| 3366 | 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] | 3367 | |
| 3368 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3369 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3370 | COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup); |
| 3371 | COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup); |
| 3372 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3373 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3374 | 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] | 3375 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3376 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3377 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3378 | |
| 3379 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3380 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3381 | COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup); |
| 3382 | COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup); |
| 3383 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3384 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3385 | 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] | 3386 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3387 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3388 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3389 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3390 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3391 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3392 | return ret; |
| 3393 | } |
| 3394 | |
| 3395 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3396 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3397 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3398 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3399 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3400 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3401 | * @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] | 3402 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3403 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3404 | */ |
| 3405 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3406 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3407 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3408 | { |
| 3409 | LY_ERR ret = LY_SUCCESS; |
| 3410 | struct lysp_node *child_p; |
| 3411 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3412 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3413 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3414 | lysc_update_path(ctx, parent, notif_p->name); |
| 3415 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3416 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3417 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3418 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3419 | notif_p->name, notif)) { |
| 3420 | return LY_EVALID; |
| 3421 | } |
| 3422 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3423 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3424 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3425 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3426 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3427 | return LY_EVALID; |
| 3428 | } |
| 3429 | |
| 3430 | notif->nodetype = LYS_NOTIF; |
| 3431 | notif->module = ctx->mod; |
| 3432 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3433 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3434 | notif->sp = notif_p; |
| 3435 | } |
| 3436 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3437 | |
| 3438 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3439 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3440 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3441 | |
| 3442 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3443 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3444 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3445 | COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
| 3446 | COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup); |
| 3447 | 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] | 3448 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3449 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3450 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3451 | 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] | 3452 | } |
| 3453 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3454 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3455 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3456 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3457 | return ret; |
| 3458 | } |
| 3459 | |
| 3460 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3461 | * @brief Compile parsed container node information. |
| 3462 | * @param[in] ctx Compile context |
| 3463 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3464 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3465 | * is enriched with the container-specific information. |
| 3466 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3467 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3468 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3469 | 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] | 3470 | { |
| 3471 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3472 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3473 | struct lysp_node *child_p; |
| 3474 | unsigned int u; |
| 3475 | LY_ERR ret = LY_SUCCESS; |
| 3476 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3477 | if (cont_p->presence) { |
| 3478 | cont->flags |= LYS_PRESENCE; |
| 3479 | } |
| 3480 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3481 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3482 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3483 | } |
| 3484 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3485 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done); |
| 3486 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3487 | 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] | 3488 | |
| 3489 | done: |
| 3490 | return ret; |
| 3491 | } |
| 3492 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3493 | /* |
| 3494 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3495 | * @param[in] ctx Compile context. |
| 3496 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3497 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3498 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3499 | * @return LY_ERR value. |
| 3500 | */ |
| 3501 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3502 | 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] | 3503 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3504 | unsigned int u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3505 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3506 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3507 | 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] | 3508 | leaf->units ? NULL : &leaf->units)); |
| 3509 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3510 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3511 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3512 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3513 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3514 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3515 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3516 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3517 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3518 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3519 | } |
| 3520 | } else { |
| 3521 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3522 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3523 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3524 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3525 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3526 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3527 | } |
| 3528 | } |
| 3529 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3530 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3531 | ly_set_add(&ctx->unres, leaf, 0); |
| 3532 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3533 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3534 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3535 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3536 | ly_set_add(&ctx->unres, leaf, 0); |
| 3537 | } |
| 3538 | } |
| 3539 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3540 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3541 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3542 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3543 | return LY_EVALID; |
| 3544 | } |
| 3545 | } |
| 3546 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3547 | return LY_SUCCESS; |
| 3548 | } |
| 3549 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3550 | /** |
| 3551 | * @brief Compile parsed leaf node information. |
| 3552 | * @param[in] ctx Compile context |
| 3553 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3554 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3555 | * is enriched with the leaf-specific information. |
| 3556 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3557 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3558 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3559 | 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] | 3560 | { |
| 3561 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3562 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3563 | unsigned int u; |
| 3564 | LY_ERR ret = LY_SUCCESS; |
| 3565 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3566 | 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] | 3567 | if (leaf_p->units) { |
| 3568 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3569 | leaf->flags |= LYS_SET_UNITS; |
| 3570 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3571 | |
| 3572 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3573 | leaf->dflt = (void*)leaf_p->dflt; |
| 3574 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
| 3575 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3576 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3577 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3578 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3579 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3580 | leaf->dflt->realtype = leaf->type; |
| 3581 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3582 | 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] | 3583 | 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] | 3584 | leaf->dflt->realtype->refcount++; |
| 3585 | if (err) { |
| 3586 | ly_err_print(err); |
| 3587 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3588 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3589 | ly_err_free(err); |
| 3590 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3591 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3592 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3593 | 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] | 3594 | |
| 3595 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3596 | ret = LY_SUCCESS; |
| 3597 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3598 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3599 | leaf->flags |= LYS_SET_DFLT; |
| 3600 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3601 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3602 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3603 | done: |
| 3604 | return ret; |
| 3605 | } |
| 3606 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3607 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3608 | * @brief Compile parsed leaf-list node information. |
| 3609 | * @param[in] ctx Compile context |
| 3610 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3611 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3612 | * is enriched with the leaf-list-specific information. |
| 3613 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3614 | */ |
| 3615 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3616 | 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] | 3617 | { |
| 3618 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3619 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3620 | unsigned int u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3621 | LY_ERR ret = LY_SUCCESS; |
| 3622 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3623 | 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] | 3624 | if (llist_p->units) { |
| 3625 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3626 | llist->flags |= LYS_SET_UNITS; |
| 3627 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3628 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3629 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3630 | llist->dflts = (void*)llist_p->dflts; |
| 3631 | 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] | 3632 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3633 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3634 | 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] | 3635 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3636 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3637 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3638 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3639 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3640 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3641 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3642 | llist->dflts[u]->realtype = llist->type; |
| 3643 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3644 | 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] | 3645 | 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] | 3646 | llist->dflts[u]->realtype->refcount++; |
| 3647 | if (err) { |
| 3648 | ly_err_print(err); |
| 3649 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3650 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3651 | ly_err_free(err); |
| 3652 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3653 | if (ret == LY_EINCOMPLETE) { |
| 3654 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3655 | 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] | 3656 | |
| 3657 | /* but in general result is so far ok */ |
| 3658 | ret = LY_SUCCESS; |
| 3659 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3660 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3661 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3662 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3663 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3664 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3665 | /* configuration data values must be unique - so check the default values */ |
| 3666 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3667 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3668 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3669 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3670 | 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] | 3671 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3672 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3673 | if (dynamic) { |
| 3674 | free((char*)val); |
| 3675 | } |
| 3676 | return LY_EVALID; |
| 3677 | } |
| 3678 | } |
| 3679 | } |
| 3680 | } |
| 3681 | |
| 3682 | /* 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] | 3683 | |
| 3684 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3685 | if (llist->min) { |
| 3686 | llist->flags |= LYS_MAND_TRUE; |
| 3687 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3688 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3689 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3690 | done: |
| 3691 | return ret; |
| 3692 | } |
| 3693 | |
| 3694 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3695 | * @brief Compile information about list's uniques. |
| 3696 | * @param[in] ctx Compile context. |
| 3697 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3698 | * @param[in] uniques Sized array list of unique statements. |
| 3699 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3700 | * @return LY_ERR value. |
| 3701 | */ |
| 3702 | static LY_ERR |
| 3703 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3704 | { |
| 3705 | LY_ERR ret = LY_SUCCESS; |
| 3706 | struct lysc_node_leaf **key, ***unique; |
| 3707 | const char *keystr, *delim; |
| 3708 | size_t len; |
| 3709 | unsigned int v; |
| 3710 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3711 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3712 | |
| 3713 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3714 | config = -1; |
| 3715 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3716 | keystr = uniques[v]; |
| 3717 | while (keystr) { |
| 3718 | delim = strpbrk(keystr, " \t\n"); |
| 3719 | if (delim) { |
| 3720 | len = delim - keystr; |
| 3721 | while (isspace(*delim)) { |
| 3722 | ++delim; |
| 3723 | } |
| 3724 | } else { |
| 3725 | len = strlen(keystr); |
| 3726 | } |
| 3727 | |
| 3728 | /* unique node must be present */ |
| 3729 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3730 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3731 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3732 | if (ret != LY_SUCCESS) { |
| 3733 | if (ret == LY_EDENIED) { |
| 3734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3735 | "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] | 3736 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3737 | } |
| 3738 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3739 | } else if (flags) { |
| 3740 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3741 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3742 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
| 3743 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3744 | } |
| 3745 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3746 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3747 | /* all referenced leafs must be of the same config type */ |
| 3748 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3749 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3750 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3751 | return LY_EVALID; |
| 3752 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3753 | config = 1; |
| 3754 | } else { /* LYS_CONFIG_R */ |
| 3755 | config = 0; |
| 3756 | } |
| 3757 | |
| 3758 | /* check status */ |
| 3759 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3760 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3761 | |
| 3762 | /* mark leaf as unique */ |
| 3763 | (*key)->flags |= LYS_UNIQUE; |
| 3764 | |
| 3765 | /* next unique value in line */ |
| 3766 | keystr = delim; |
| 3767 | } |
| 3768 | /* next unique definition */ |
| 3769 | } |
| 3770 | |
| 3771 | return LY_SUCCESS; |
| 3772 | } |
| 3773 | |
| 3774 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3775 | * @brief Compile parsed list node information. |
| 3776 | * @param[in] ctx Compile context |
| 3777 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3778 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3779 | * is enriched with the list-specific information. |
| 3780 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3781 | */ |
| 3782 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3783 | 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] | 3784 | { |
| 3785 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3786 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3787 | struct lysp_node *child_p; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3788 | struct lysc_node_leaf **key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3789 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3790 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3791 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3792 | LY_ERR ret = LY_SUCCESS; |
| 3793 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3794 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3795 | if (list->min) { |
| 3796 | list->flags |= LYS_MAND_TRUE; |
| 3797 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3798 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3799 | |
| 3800 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3801 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3802 | } |
| 3803 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3804 | 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] | 3805 | |
| 3806 | /* keys */ |
| 3807 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3808 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3809 | return LY_EVALID; |
| 3810 | } |
| 3811 | |
| 3812 | /* find all the keys (must be direct children) */ |
| 3813 | keystr = list_p->key; |
| 3814 | while (keystr) { |
| 3815 | delim = strpbrk(keystr, " \t\n"); |
| 3816 | if (delim) { |
| 3817 | len = delim - keystr; |
| 3818 | while (isspace(*delim)) { |
| 3819 | ++delim; |
| 3820 | } |
| 3821 | } else { |
| 3822 | len = strlen(keystr); |
| 3823 | } |
| 3824 | |
| 3825 | /* key node must be present */ |
| 3826 | LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM); |
| 3827 | *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 3828 | if (!(*key)) { |
| 3829 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3830 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3831 | return LY_EVALID; |
| 3832 | } |
| 3833 | /* keys must be unique */ |
| 3834 | for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) { |
| 3835 | if (*key == list->keys[u]) { |
| 3836 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3837 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3838 | return LY_EVALID; |
| 3839 | } |
| 3840 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3841 | lysc_update_path(ctx, (struct lysc_node*)list, (*key)->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3842 | /* key must have the same config flag as the list itself */ |
| 3843 | if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) { |
| 3844 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3845 | return LY_EVALID; |
| 3846 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3847 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3848 | /* YANG 1.0 denies key to be of empty type */ |
| 3849 | if ((*key)->type->basetype == LY_TYPE_EMPTY) { |
| 3850 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3851 | "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] | 3852 | return LY_EVALID; |
| 3853 | } |
| 3854 | } else { |
| 3855 | /* when and if-feature are illegal on list keys */ |
| 3856 | if ((*key)->when) { |
| 3857 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3858 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3859 | return LY_EVALID; |
| 3860 | } |
| 3861 | if ((*key)->iffeatures) { |
| 3862 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3863 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3864 | return LY_EVALID; |
| 3865 | } |
| 3866 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3867 | |
| 3868 | /* check status */ |
| 3869 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3870 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3871 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3872 | /* ignore default values of the key */ |
| 3873 | if ((*key)->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3874 | (*key)->dflt->realtype->plugin->free(ctx->ctx, (*key)->dflt); |
| 3875 | lysc_type_free(ctx->ctx, (*key)->dflt->realtype); |
| 3876 | free((*key)->dflt); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3877 | (*key)->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3878 | (*key)->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3879 | } |
| 3880 | /* mark leaf as key */ |
| 3881 | (*key)->flags |= LYS_KEY; |
| 3882 | |
| 3883 | /* next key value */ |
| 3884 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3885 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3886 | } |
| 3887 | |
| 3888 | /* uniques */ |
| 3889 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3890 | 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] | 3891 | } |
| 3892 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3893 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3894 | 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] | 3895 | |
| 3896 | done: |
| 3897 | return ret; |
| 3898 | } |
| 3899 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3900 | /** |
| 3901 | * @brief Do some checks and set the default choice's case. |
| 3902 | * |
| 3903 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3904 | * |
| 3905 | * @param[in] ctx Compile context. |
| 3906 | * @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, |
| 3907 | * not the reference to the imported module. |
| 3908 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3909 | * @return LY_ERR value. |
| 3910 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3911 | static LY_ERR |
| 3912 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3913 | { |
| 3914 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3915 | const char *prefix = NULL, *name; |
| 3916 | size_t prefix_len = 0; |
| 3917 | |
| 3918 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3919 | name = strchr(dflt, ':'); |
| 3920 | if (name) { |
| 3921 | prefix = dflt; |
| 3922 | prefix_len = name - prefix; |
| 3923 | ++name; |
| 3924 | } else { |
| 3925 | name = dflt; |
| 3926 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3927 | 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] | 3928 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3929 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3930 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3931 | prefix_len, prefix); |
| 3932 | return LY_EVALID; |
| 3933 | } |
| 3934 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3935 | if (!ch->dflt) { |
| 3936 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3937 | "Default case \"%s\" not found.", dflt); |
| 3938 | return LY_EVALID; |
| 3939 | } |
| 3940 | /* no mandatory nodes directly under the default case */ |
| 3941 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3942 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3943 | break; |
| 3944 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3945 | if (iter->flags & LYS_MAND_TRUE) { |
| 3946 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3947 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3948 | return LY_EVALID; |
| 3949 | } |
| 3950 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3951 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3952 | return LY_SUCCESS; |
| 3953 | } |
| 3954 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3955 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3956 | 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] | 3957 | { |
| 3958 | struct lys_module *mod; |
| 3959 | const char *prefix = NULL, *name; |
| 3960 | size_t prefix_len = 0; |
| 3961 | struct lysc_node_case *cs; |
| 3962 | struct lysc_node *node; |
| 3963 | |
| 3964 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3965 | name = strchr(dflt, ':'); |
| 3966 | if (name) { |
| 3967 | prefix = dflt; |
| 3968 | prefix_len = name - prefix; |
| 3969 | ++name; |
| 3970 | } else { |
| 3971 | name = dflt; |
| 3972 | } |
| 3973 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3974 | if (prefix) { |
| 3975 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3976 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3977 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3978 | "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] | 3979 | return LY_EVALID; |
| 3980 | } |
| 3981 | } else { |
| 3982 | mod = ctx->mod; |
| 3983 | } |
| 3984 | /* get the default case */ |
| 3985 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3986 | if (!cs) { |
| 3987 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3988 | "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] | 3989 | return LY_EVALID; |
| 3990 | } |
| 3991 | |
| 3992 | /* check that there is no mandatory node */ |
| 3993 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3994 | if (node->parent != (struct lysc_node*)cs) { |
| 3995 | break; |
| 3996 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3997 | if (node->flags & LYS_MAND_TRUE) { |
| 3998 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3999 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4000 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4001 | return LY_EVALID; |
| 4002 | } |
| 4003 | } |
| 4004 | |
| 4005 | /* set the default case in choice */ |
| 4006 | ch->dflt = cs; |
| 4007 | cs->flags |= LYS_SET_DFLT; |
| 4008 | |
| 4009 | return LY_SUCCESS; |
| 4010 | } |
| 4011 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4012 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4013 | * @brief Compile parsed choice node information. |
| 4014 | * @param[in] ctx Compile context |
| 4015 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4016 | * @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] | 4017 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4018 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4019 | */ |
| 4020 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4021 | 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] | 4022 | { |
| 4023 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4024 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4025 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4026 | LY_ERR ret = LY_SUCCESS; |
| 4027 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4028 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4029 | if (child_p->nodetype == LYS_CASE) { |
| 4030 | 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] | 4031 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4032 | } |
| 4033 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4034 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4035 | } |
| 4036 | } |
| 4037 | |
| 4038 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4039 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4040 | 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] | 4041 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4042 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4043 | return ret; |
| 4044 | } |
| 4045 | |
| 4046 | /** |
| 4047 | * @brief Compile parsed anydata or anyxml node information. |
| 4048 | * @param[in] ctx Compile context |
| 4049 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4050 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4051 | * is enriched with the any-specific information. |
| 4052 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4053 | */ |
| 4054 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4055 | 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] | 4056 | { |
| 4057 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4058 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4059 | unsigned int u; |
| 4060 | LY_ERR ret = LY_SUCCESS; |
| 4061 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4062 | 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] | 4063 | |
| 4064 | if (any->flags & LYS_CONFIG_W) { |
| 4065 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
| 4066 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML)); |
| 4067 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4068 | done: |
| 4069 | return ret; |
| 4070 | } |
| 4071 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4072 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4073 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4074 | * |
| 4075 | * @param[in] ctx Compile context |
| 4076 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4077 | * the choice itself is expected instead of a specific case node. |
| 4078 | * @param[in] node Schema node to connect into the list. |
| 4079 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 4080 | */ |
| 4081 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4082 | 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] | 4083 | { |
| 4084 | struct lysc_node **children; |
| 4085 | |
| 4086 | if (node->nodetype == LYS_CASE) { |
| 4087 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4088 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4089 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4090 | } |
| 4091 | if (children) { |
| 4092 | if (!(*children)) { |
| 4093 | /* first child */ |
| 4094 | *children = node; |
| 4095 | } else if (*children != node) { |
| 4096 | /* by the condition in previous branch we cover the choice/case children |
| 4097 | * - the children list is shared by the choice and the the first case, in addition |
| 4098 | * the first child of each case must be referenced from the case node. So the node is |
| 4099 | * actually always already inserted in case it is the first children - so here such |
| 4100 | * a situation actually corresponds to the first branch */ |
| 4101 | /* insert at the end of the parent's children list */ |
| 4102 | (*children)->prev->next = node; |
| 4103 | node->prev = (*children)->prev; |
| 4104 | (*children)->prev = node; |
| 4105 | |
| 4106 | /* check the name uniqueness */ |
| 4107 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4108 | lysc_node_notifs(parent), node->name, node)) { |
| 4109 | return LY_EEXIST; |
| 4110 | } |
| 4111 | } |
| 4112 | } |
| 4113 | return LY_SUCCESS; |
| 4114 | } |
| 4115 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4116 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4117 | * @brief Get the XPath context node for the given schema node. |
| 4118 | * @param[in] start The schema node where the XPath expression appears. |
| 4119 | * @return The context node to evaluate XPath expression in given schema node. |
| 4120 | * @return NULL in case the context node is the root node. |
| 4121 | */ |
| 4122 | static struct lysc_node * |
| 4123 | lysc_xpath_context(struct lysc_node *start) |
| 4124 | { |
| 4125 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 4126 | start = start->parent); |
| 4127 | return start; |
| 4128 | } |
| 4129 | |
| 4130 | /** |
| 4131 | * @brief Prepare the case structure in choice node for the new data node. |
| 4132 | * |
| 4133 | * 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 |
| 4134 | * created in the choice when the first child was processed. |
| 4135 | * |
| 4136 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4137 | * @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, |
| 4138 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4139 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4140 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4141 | * @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, |
| 4142 | * 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] | 4143 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4144 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4145 | 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] | 4146 | { |
| 4147 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4148 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4149 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4150 | unsigned int u; |
| 4151 | LY_ERR ret; |
| 4152 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4153 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4154 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4155 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4156 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4157 | return NULL; \ |
| 4158 | } \ |
| 4159 | } |
| 4160 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4161 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4162 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4163 | |
| 4164 | /* we have to add an implicit case node into the parent choice */ |
| 4165 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4166 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 4167 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4168 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4169 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4170 | /* the case is already present since the child is not its first children */ |
| 4171 | return (struct lysc_node_case*)ch->cases->prev; |
| 4172 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4173 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4174 | |
| 4175 | /* explicit parent case is not present (this is its first child) */ |
| 4176 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4177 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 4178 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4179 | cs->sp = node_p; |
| 4180 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4181 | /* 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] | 4182 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4183 | |
| 4184 | if (node_p->when) { |
| 4185 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4186 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4187 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4188 | (*when)->context = lysc_xpath_context(ch->parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4189 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4190 | 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] | 4191 | } else { |
| 4192 | LOGINT(ctx->ctx); |
| 4193 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4194 | } |
| 4195 | cs->module = ctx->mod; |
| 4196 | cs->prev = (struct lysc_node*)cs; |
| 4197 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4198 | 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] | 4199 | cs->parent = (struct lysc_node*)ch; |
| 4200 | cs->child = child; |
| 4201 | |
| 4202 | return cs; |
| 4203 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4204 | if (cs) { |
| 4205 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4206 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4207 | return NULL; |
| 4208 | |
| 4209 | #undef UNIQUE_CHECK |
| 4210 | } |
| 4211 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4212 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4213 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4214 | * |
| 4215 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4216 | * @param[in] node Target node where the config is supposed to be changed. |
| 4217 | * @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] | 4218 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4219 | * 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] | 4220 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4221 | * @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] | 4222 | * @return LY_ERR value. |
| 4223 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4224 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4225 | 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] | 4226 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4227 | { |
| 4228 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4229 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4230 | |
| 4231 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4232 | /* nothing to do */ |
| 4233 | return LY_SUCCESS; |
| 4234 | } |
| 4235 | |
| 4236 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4237 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4238 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4239 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4240 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4241 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4242 | return LY_EVALID; |
| 4243 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4244 | node->flags |= LYS_SET_CONFIG; |
| 4245 | } else { |
| 4246 | if (node->flags & LYS_SET_CONFIG) { |
| 4247 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4248 | /* setting config flags, but have node with explicit config true */ |
| 4249 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4250 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4251 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4252 | return LY_EVALID; |
| 4253 | } |
| 4254 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4255 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4256 | return LY_SUCCESS; |
| 4257 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4258 | } |
| 4259 | node->flags &= ~LYS_CONFIG_MASK; |
| 4260 | node->flags |= config; |
| 4261 | |
| 4262 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4263 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4264 | 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] | 4265 | } |
| 4266 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4267 | return LY_SUCCESS; |
| 4268 | } |
| 4269 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4270 | /** |
| 4271 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4272 | * |
| 4273 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4274 | * the flag to such parents from a mandatory children. |
| 4275 | * |
| 4276 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4277 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4278 | * (mandatory children was removed). |
| 4279 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4280 | void |
| 4281 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4282 | { |
| 4283 | struct lysc_node *iter; |
| 4284 | |
| 4285 | if (add) { /* set flag */ |
| 4286 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4287 | parent = parent->parent) { |
| 4288 | parent->flags |= LYS_MAND_TRUE; |
| 4289 | } |
| 4290 | } else { /* unset flag */ |
| 4291 | 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] | 4292 | 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] | 4293 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4294 | /* there is another mandatory node */ |
| 4295 | return; |
| 4296 | } |
| 4297 | } |
| 4298 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4299 | parent->flags &= ~LYS_MAND_TRUE; |
| 4300 | } |
| 4301 | } |
| 4302 | } |
| 4303 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4304 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4305 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4306 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4307 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4308 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4309 | */ |
| 4310 | static void |
| 4311 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4312 | { |
| 4313 | unsigned int v; |
| 4314 | size_t len; |
| 4315 | |
| 4316 | len = strlen(aug_p->nodeid); |
| 4317 | LY_ARRAY_FOR(result, v) { |
| 4318 | if (strlen(result[v]->nodeid) <= len) { |
| 4319 | continue; |
| 4320 | } |
| 4321 | if (v < LY_ARRAY_SIZE(result)) { |
| 4322 | /* move the rest of array */ |
| 4323 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4324 | break; |
| 4325 | } |
| 4326 | } |
| 4327 | result[v] = aug_p; |
| 4328 | LY_ARRAY_INCREMENT(result); |
| 4329 | } |
| 4330 | |
| 4331 | /** |
| 4332 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4333 | * |
| 4334 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4335 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4336 | * |
| 4337 | * @param[in] ctx Compile context. |
| 4338 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4339 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4340 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4341 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4342 | * @return LY_ERR value. |
| 4343 | */ |
| 4344 | LY_ERR |
| 4345 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4346 | { |
| 4347 | struct lysp_augment **result = NULL; |
| 4348 | unsigned int u, v; |
| 4349 | size_t count = 0; |
| 4350 | |
| 4351 | assert(augments); |
| 4352 | |
| 4353 | /* get count of the augments in module and all its submodules */ |
| 4354 | if (aug_p) { |
| 4355 | count += LY_ARRAY_SIZE(aug_p); |
| 4356 | } |
| 4357 | LY_ARRAY_FOR(inc_p, u) { |
| 4358 | if (inc_p[u].submodule->augments) { |
| 4359 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4360 | } |
| 4361 | } |
| 4362 | |
| 4363 | if (!count) { |
| 4364 | *augments = NULL; |
| 4365 | return LY_SUCCESS; |
| 4366 | } |
| 4367 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4368 | |
| 4369 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4370 | * together, so there can be even /z/y betwwen them. */ |
| 4371 | LY_ARRAY_FOR(aug_p, u) { |
| 4372 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4373 | } |
| 4374 | LY_ARRAY_FOR(inc_p, u) { |
| 4375 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4376 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | *augments = result; |
| 4381 | return LY_SUCCESS; |
| 4382 | } |
| 4383 | |
| 4384 | /** |
| 4385 | * @brief Compile the parsed augment connecting it into its target. |
| 4386 | * |
| 4387 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4388 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4389 | * are already implemented and compiled. |
| 4390 | * |
| 4391 | * @param[in] ctx Compile context. |
| 4392 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4393 | * @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 |
| 4394 | * children in case of the augmenting uses data. |
| 4395 | * @return LY_SUCCESS on success. |
| 4396 | * @return LY_EVALID on failure. |
| 4397 | */ |
| 4398 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4399 | 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] | 4400 | { |
| 4401 | LY_ERR ret = LY_SUCCESS; |
| 4402 | struct lysp_node *node_p, *case_node_p; |
| 4403 | struct lysc_node *target; /* target target of the augment */ |
| 4404 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4405 | struct lysc_when **when, *when_shared; |
| 4406 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4407 | uint16_t flags = 0; |
| 4408 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4409 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4410 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4411 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4412 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4413 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4414 | 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] | 4415 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4416 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4417 | if (ret != LY_SUCCESS) { |
| 4418 | if (ret == LY_EDENIED) { |
| 4419 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4420 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4421 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4422 | } |
| 4423 | return LY_EVALID; |
| 4424 | } |
| 4425 | |
| 4426 | /* check for mandatory nodes |
| 4427 | * - new cases augmenting some choice can have mandatory nodes |
| 4428 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4429 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4430 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4431 | allow_mandatory = 1; |
| 4432 | } |
| 4433 | |
| 4434 | when_shared = NULL; |
| 4435 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4436 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4437 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4438 | && !((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] | 4439 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4440 | && !(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] | 4441 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4442 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4443 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4444 | return LY_EVALID; |
| 4445 | } |
| 4446 | |
| 4447 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4448 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4449 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4450 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4451 | } else { |
| 4452 | 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] | 4453 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4454 | } |
| 4455 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4456 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4457 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4458 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4459 | * 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] | 4460 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4461 | /* 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] | 4462 | 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] | 4463 | } else if (target->nodetype == LYS_CHOICE) { |
| 4464 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4465 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4466 | } else { |
| 4467 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4468 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4469 | } |
| 4470 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4471 | 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] | 4472 | node->flags &= ~LYS_MAND_TRUE; |
| 4473 | lys_compile_mandatory_parents(target, 0); |
| 4474 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4475 | "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] | 4476 | return LY_EVALID; |
| 4477 | } |
| 4478 | |
| 4479 | /* pass augment's when to all the children */ |
| 4480 | if (aug_p->when) { |
| 4481 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4482 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4483 | ret = lys_compile_when(ctx, aug_p->when, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4484 | LY_CHECK_GOTO(ret, error); |
| 4485 | (*when)->context = lysc_xpath_context(target); |
| 4486 | when_shared = *when; |
| 4487 | } else { |
| 4488 | ++when_shared->refcount; |
| 4489 | (*when) = when_shared; |
| 4490 | } |
| 4491 | } |
| 4492 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4493 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4494 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4495 | switch (target->nodetype) { |
| 4496 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4497 | 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] | 4498 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4499 | 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] | 4500 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4501 | break; |
| 4502 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4503 | 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] | 4504 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4505 | 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] | 4506 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4507 | break; |
| 4508 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4509 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4510 | if (aug_p->actions) { |
| 4511 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4512 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4513 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4514 | return LY_EVALID; |
| 4515 | } |
| 4516 | if (aug_p->notifs) { |
| 4517 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4518 | "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".", |
| 4519 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4520 | return LY_EVALID; |
| 4521 | } |
| 4522 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4523 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4524 | lysc_update_path(ctx, NULL, NULL); |
| 4525 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4526 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4527 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4528 | return ret; |
| 4529 | } |
| 4530 | |
| 4531 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4532 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4533 | * |
| 4534 | * @param[in] ctx Compile context. |
| 4535 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4536 | * @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] | 4537 | * @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] | 4538 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4539 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4540 | * 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] | 4541 | * @return LY_ERR value. |
| 4542 | */ |
| 4543 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4544 | 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] | 4545 | { |
| 4546 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4547 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4548 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4549 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4550 | return LY_EVALID; |
| 4551 | } |
| 4552 | |
| 4553 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4554 | /* check if node has default value */ |
| 4555 | if (node->nodetype & LYS_LEAF) { |
| 4556 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4557 | if (refine_flag) { |
| 4558 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4559 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4560 | return LY_EVALID; |
| 4561 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4562 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4563 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4564 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4565 | |
| 4566 | /* update the list of incomplete default values if needed */ |
| 4567 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4568 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4569 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4570 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4571 | free(leaf->dflt); |
| 4572 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4573 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4574 | } |
| 4575 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4576 | if (refine_flag) { |
| 4577 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4578 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4579 | return LY_EVALID; |
| 4580 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4581 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4582 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4583 | 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] | 4584 | return LY_EVALID; |
| 4585 | } |
| 4586 | |
| 4587 | node->flags &= ~LYS_MAND_FALSE; |
| 4588 | node->flags |= LYS_MAND_TRUE; |
| 4589 | lys_compile_mandatory_parents(node->parent, 1); |
| 4590 | } else { |
| 4591 | /* make mandatory false */ |
| 4592 | node->flags &= ~LYS_MAND_TRUE; |
| 4593 | node->flags |= LYS_MAND_FALSE; |
| 4594 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4595 | 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] | 4596 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4597 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4598 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4599 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4600 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4601 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4602 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4603 | } |
| 4604 | } |
| 4605 | return LY_SUCCESS; |
| 4606 | } |
| 4607 | |
| 4608 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4609 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4610 | * If present, also apply uses's modificators. |
| 4611 | * |
| 4612 | * @param[in] ctx Compile context |
| 4613 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4614 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4615 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4616 | * the compile context. |
| 4617 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4618 | */ |
| 4619 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4620 | 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] | 4621 | { |
| 4622 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4623 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4624 | /* 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] | 4625 | struct lysc_node_container context_node_fake = |
| 4626 | {.nodetype = LYS_CONTAINER, |
| 4627 | .module = ctx->mod, |
| 4628 | .flags = parent ? parent->flags : 0, |
| 4629 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4630 | .prev = (struct lysc_node*)&context_node_fake, |
| 4631 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4632 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4633 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4634 | int found; |
| 4635 | const char *id, *name, *prefix; |
| 4636 | size_t prefix_len, name_len; |
| 4637 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4638 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4639 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4640 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4641 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4642 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4643 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4644 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4645 | unsigned int actions_index, notifs_index; |
| 4646 | struct lysc_notif **notifs = NULL; |
| 4647 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4648 | |
| 4649 | /* search for the grouping definition */ |
| 4650 | found = 0; |
| 4651 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4652 | 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] | 4653 | if (prefix) { |
| 4654 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4655 | if (!mod) { |
| 4656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4657 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4658 | return LY_EVALID; |
| 4659 | } |
| 4660 | } else { |
| 4661 | mod = ctx->mod_def; |
| 4662 | } |
| 4663 | if (mod == ctx->mod_def) { |
| 4664 | 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] | 4665 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4666 | LY_ARRAY_FOR(grp, u) { |
| 4667 | if (!strcmp(grp[u].name, name)) { |
| 4668 | grp = &grp[u]; |
| 4669 | found = 1; |
| 4670 | break; |
| 4671 | } |
| 4672 | } |
| 4673 | } |
| 4674 | } |
| 4675 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4676 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4677 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4678 | if (grp) { |
| 4679 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4680 | if (!strcmp(grp[u].name, name)) { |
| 4681 | grp = &grp[u]; |
| 4682 | found = 1; |
| 4683 | } |
| 4684 | } |
| 4685 | } |
| 4686 | if (!found && mod->parsed->includes) { |
| 4687 | /* ... and all the submodules */ |
| 4688 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4689 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4690 | if (grp) { |
| 4691 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4692 | if (!strcmp(grp[v].name, name)) { |
| 4693 | grp = &grp[v]; |
| 4694 | found = 1; |
| 4695 | } |
| 4696 | } |
| 4697 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4698 | } |
| 4699 | } |
| 4700 | } |
| 4701 | if (!found) { |
| 4702 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4703 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4704 | return LY_EVALID; |
| 4705 | } |
| 4706 | |
| 4707 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4708 | grp_stack_count = ctx->groupings.count; |
| 4709 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4710 | if (grp_stack_count == ctx->groupings.count) { |
| 4711 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4712 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4713 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4714 | return LY_EVALID; |
| 4715 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4716 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4717 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4718 | grp->flags |= LYS_USED_GRP; |
| 4719 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4720 | |
| 4721 | /* switch context's mod_def */ |
| 4722 | mod_old = ctx->mod_def; |
| 4723 | ctx->mod_def = mod; |
| 4724 | |
| 4725 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4726 | 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] | 4727 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4728 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4729 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4730 | /* 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] | 4731 | 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] | 4732 | |
| 4733 | /* some preparation for applying refines */ |
| 4734 | if (grp->data == node_p) { |
| 4735 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4736 | if (parent) { |
| 4737 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4738 | } else if (ctx->mod->compiled->data) { |
| 4739 | child = ctx->mod->compiled->data; |
| 4740 | } else { |
| 4741 | child = NULL; |
| 4742 | } |
| 4743 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4744 | } |
| 4745 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4746 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4747 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4748 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4749 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4750 | /* 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] | 4751 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4752 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4753 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4754 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4755 | (*when)->context = lysc_xpath_context(parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4756 | when_shared = *when; |
| 4757 | } else { |
| 4758 | ++when_shared->refcount; |
| 4759 | (*when) = when_shared; |
| 4760 | } |
| 4761 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4762 | } |
| 4763 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4764 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4765 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4766 | /* 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] | 4767 | 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] | 4768 | } |
| 4769 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4770 | /* compile actions */ |
| 4771 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4772 | if (actions) { |
| 4773 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4774 | 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] | 4775 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4776 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4777 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4778 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4779 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4780 | } |
| 4781 | } |
| 4782 | |
| 4783 | /* compile notifications */ |
| 4784 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4785 | if (notifs) { |
| 4786 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4787 | 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] | 4788 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4789 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4790 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4791 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4792 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4793 | } |
| 4794 | } |
| 4795 | |
| 4796 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4797 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4798 | 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] | 4799 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4800 | 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] | 4801 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4802 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4803 | /* reload previous context's mod_def */ |
| 4804 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4805 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4806 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4807 | /* apply refine */ |
| 4808 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4809 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4810 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4811 | 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] | 4812 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4813 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4814 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4815 | |
| 4816 | /* default value */ |
| 4817 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4818 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4819 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4820 | "Invalid refine of default - %s cannot hold %d default values.", |
| 4821 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4822 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4823 | } |
| 4824 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4825 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4826 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4827 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4828 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4829 | } |
| 4830 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4831 | struct ly_err_item *err = NULL; |
| 4832 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4833 | if (leaf->dflt) { |
| 4834 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4835 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4836 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4837 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4838 | } else { |
| 4839 | /* prepare a new one */ |
| 4840 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4841 | leaf->dflt->realtype = leaf->type; |
| 4842 | } |
| 4843 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4844 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4845 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4846 | 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] | 4847 | 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] | 4848 | leaf->dflt->realtype->refcount++; |
| 4849 | if (err) { |
| 4850 | ly_err_print(err); |
| 4851 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4852 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4853 | ly_err_free(err); |
| 4854 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4855 | if (rc == LY_EINCOMPLETE) { |
| 4856 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4857 | 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] | 4858 | |
| 4859 | /* but in general result is so far ok */ |
| 4860 | rc = LY_SUCCESS; |
| 4861 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4862 | LY_CHECK_GOTO(rc, cleanup); |
| 4863 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4864 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4865 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4866 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4867 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4868 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4869 | "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] | 4870 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4871 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4872 | |
| 4873 | /* remove previous set of default values */ |
| 4874 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4875 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4876 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 4877 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 4878 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4879 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4880 | LY_ARRAY_FREE(llist->dflts); |
| 4881 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4882 | LY_ARRAY_FREE(llist->dflts_mods); |
| 4883 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4884 | |
| 4885 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4886 | 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] | 4887 | 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] | 4888 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4889 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4890 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 4891 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4892 | LY_ARRAY_INCREMENT(llist->dflts); |
| 4893 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 4894 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4895 | 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] | 4896 | 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] | 4897 | 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] | 4898 | llist->dflts[u]->realtype->refcount++; |
| 4899 | if (err) { |
| 4900 | ly_err_print(err); |
| 4901 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4902 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 4903 | ly_err_free(err); |
| 4904 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4905 | if (rc == LY_EINCOMPLETE) { |
| 4906 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4907 | 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] | 4908 | |
| 4909 | /* but in general result is so far ok */ |
| 4910 | rc = LY_SUCCESS; |
| 4911 | } |
| 4912 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4913 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4914 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4915 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4916 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4917 | /* unset LYS_SET_DFLT from the current default case */ |
| 4918 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4919 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4920 | 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] | 4921 | } |
| 4922 | } |
| 4923 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4924 | /* description */ |
| 4925 | if (rfn->dsc) { |
| 4926 | FREE_STRING(ctx->ctx, node->dsc); |
| 4927 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4928 | } |
| 4929 | |
| 4930 | /* reference */ |
| 4931 | if (rfn->ref) { |
| 4932 | FREE_STRING(ctx->ctx, node->ref); |
| 4933 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4934 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4935 | |
| 4936 | /* config */ |
| 4937 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4938 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4939 | 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] | 4940 | } else { |
| 4941 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 4942 | flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path); |
| 4943 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | /* mandatory */ |
| 4947 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4948 | 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] | 4949 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4950 | |
| 4951 | /* presence */ |
| 4952 | if (rfn->presence) { |
| 4953 | if (node->nodetype != LYS_CONTAINER) { |
| 4954 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4955 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4956 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4957 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4958 | } |
| 4959 | node->flags |= LYS_PRESENCE; |
| 4960 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4961 | |
| 4962 | /* must */ |
| 4963 | if (rfn->musts) { |
| 4964 | switch (node->nodetype) { |
| 4965 | case LYS_LEAF: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4966 | 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] | 4967 | break; |
| 4968 | case LYS_LEAFLIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4969 | 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] | 4970 | break; |
| 4971 | case LYS_LIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4972 | 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] | 4973 | break; |
| 4974 | case LYS_CONTAINER: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4975 | 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] | 4976 | break; |
| 4977 | case LYS_ANYXML: |
| 4978 | case LYS_ANYDATA: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4979 | 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] | 4980 | break; |
| 4981 | default: |
| 4982 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4983 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 4984 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4985 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4986 | } |
| 4987 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4988 | |
| 4989 | /* min/max-elements */ |
| 4990 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4991 | switch (node->nodetype) { |
| 4992 | case LYS_LEAFLIST: |
| 4993 | if (rfn->flags & LYS_SET_MAX) { |
| 4994 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4995 | } |
| 4996 | if (rfn->flags & LYS_SET_MIN) { |
| 4997 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4998 | if (rfn->min) { |
| 4999 | node->flags |= LYS_MAND_TRUE; |
| 5000 | lys_compile_mandatory_parents(node->parent, 1); |
| 5001 | } else { |
| 5002 | node->flags &= ~LYS_MAND_TRUE; |
| 5003 | lys_compile_mandatory_parents(node->parent, 0); |
| 5004 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5005 | } |
| 5006 | break; |
| 5007 | case LYS_LIST: |
| 5008 | if (rfn->flags & LYS_SET_MAX) { |
| 5009 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5010 | } |
| 5011 | if (rfn->flags & LYS_SET_MIN) { |
| 5012 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5013 | if (rfn->min) { |
| 5014 | node->flags |= LYS_MAND_TRUE; |
| 5015 | lys_compile_mandatory_parents(node->parent, 1); |
| 5016 | } else { |
| 5017 | node->flags &= ~LYS_MAND_TRUE; |
| 5018 | lys_compile_mandatory_parents(node->parent, 0); |
| 5019 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5020 | } |
| 5021 | break; |
| 5022 | default: |
| 5023 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5024 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5025 | (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] | 5026 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5027 | } |
| 5028 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5029 | |
| 5030 | /* if-feature */ |
| 5031 | if (rfn->iffeatures) { |
| 5032 | /* 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] | 5033 | 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] | 5034 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5035 | |
| 5036 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5037 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5038 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5039 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 5040 | for (u = 0; u < refined.count; ++u) { |
| 5041 | node = (struct lysc_node*)refined.objs[u]; |
| 5042 | rfn = &uses_p->refines[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5043 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5044 | |
| 5045 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5046 | if ((node->flags & LYS_MAND_TRUE) && |
| 5047 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5048 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5049 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5050 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5051 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5055 | if (node->nodetype == LYS_LIST) { |
| 5056 | min = ((struct lysc_node_list*)node)->min; |
| 5057 | max = ((struct lysc_node_list*)node)->max; |
| 5058 | } else { |
| 5059 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5060 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5061 | } |
| 5062 | if (min > max) { |
| 5063 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5064 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5065 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5066 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5067 | } |
| 5068 | } |
| 5069 | } |
| 5070 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5071 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5072 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5073 | |
| 5074 | cleanup: |
| 5075 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5076 | if (context_node_fake.child) { |
| 5077 | context_node_fake.child->prev = child; |
| 5078 | } |
| 5079 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5080 | child->parent = parent; |
| 5081 | } |
| 5082 | |
| 5083 | if (uses_p->augments || uses_p->refines) { |
| 5084 | /* 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] | 5085 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5086 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5087 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5088 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5089 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5090 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5091 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5092 | } |
| 5093 | } |
| 5094 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5095 | /* reload previous context's mod_def */ |
| 5096 | ctx->mod_def = mod_old; |
| 5097 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5098 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5099 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5100 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5101 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5102 | |
| 5103 | return ret; |
| 5104 | } |
| 5105 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5106 | static int |
| 5107 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5108 | { |
| 5109 | struct lysp_node *iter; |
| 5110 | int len = 0; |
| 5111 | |
| 5112 | *path = NULL; |
| 5113 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5114 | char *s = *path; |
| 5115 | char *id; |
| 5116 | |
| 5117 | switch (iter->nodetype) { |
| 5118 | case LYS_USES: |
| 5119 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5120 | break; |
| 5121 | case LYS_GROUPING: |
| 5122 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5123 | break; |
| 5124 | case LYS_AUGMENT: |
| 5125 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5126 | break; |
| 5127 | default: |
| 5128 | id = strdup(iter->name); |
| 5129 | break; |
| 5130 | } |
| 5131 | |
| 5132 | if (!iter->parent) { |
| 5133 | /* print prefix */ |
| 5134 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5135 | } else { |
| 5136 | /* prefix is the same as in parent */ |
| 5137 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5138 | } |
| 5139 | free(s); |
| 5140 | free(id); |
| 5141 | } |
| 5142 | |
| 5143 | if (len < 0) { |
| 5144 | free(*path); |
| 5145 | *path = NULL; |
| 5146 | } else if (len == 0) { |
| 5147 | *path = strdup("/"); |
| 5148 | len = 1; |
| 5149 | } |
| 5150 | return len; |
| 5151 | } |
| 5152 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5153 | /** |
| 5154 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5155 | * |
| 5156 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5157 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5158 | */ |
| 5159 | static LY_ERR |
| 5160 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5161 | { |
| 5162 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5163 | char *path; |
| 5164 | int len; |
| 5165 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5166 | struct lysp_node_uses fake_uses = { |
| 5167 | .parent = node_p, |
| 5168 | .nodetype = LYS_USES, |
| 5169 | .flags = 0, .next = NULL, |
| 5170 | .name = grp->name, |
| 5171 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5172 | .refines = NULL, .augments = NULL |
| 5173 | }; |
| 5174 | struct lysc_node_container fake_container = { |
| 5175 | .nodetype = LYS_CONTAINER, |
| 5176 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5177 | .module = ctx->mod, |
| 5178 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5179 | .prev = (struct lysc_node*)&fake_container, |
| 5180 | .name = "fake", |
| 5181 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5182 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5183 | }; |
| 5184 | |
| 5185 | if (grp->parent) { |
| 5186 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5187 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5188 | |
| 5189 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5190 | if (len < 0) { |
| 5191 | LOGMEM(ctx->ctx); |
| 5192 | return LY_EMEM; |
| 5193 | } |
| 5194 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5195 | ctx->path_len = (uint16_t)len; |
| 5196 | free(path); |
| 5197 | |
| 5198 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5199 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5200 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5201 | lysc_update_path(ctx, NULL, NULL); |
| 5202 | lysc_update_path(ctx, NULL, NULL); |
| 5203 | |
| 5204 | ctx->path_len = 1; |
| 5205 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5206 | |
| 5207 | /* cleanup */ |
| 5208 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5209 | |
| 5210 | return ret; |
| 5211 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5212 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5213 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5214 | * @brief Compile parsed schema node information. |
| 5215 | * @param[in] ctx Compile context |
| 5216 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5217 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5218 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5219 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5220 | * @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). |
| 5221 | * 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] | 5222 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5223 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5224 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5225 | 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] | 5226 | { |
| 5227 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5228 | struct lysc_node *node; |
| 5229 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5230 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5231 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5232 | 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] | 5233 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5234 | if (node_p->nodetype != LYS_USES) { |
| 5235 | lysc_update_path(ctx, parent, node_p->name); |
| 5236 | } else { |
| 5237 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5238 | lysc_update_path(ctx, NULL, node_p->name); |
| 5239 | } |
| 5240 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5241 | switch (node_p->nodetype) { |
| 5242 | case LYS_CONTAINER: |
| 5243 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5244 | node_compile_spec = lys_compile_node_container; |
| 5245 | break; |
| 5246 | case LYS_LEAF: |
| 5247 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5248 | node_compile_spec = lys_compile_node_leaf; |
| 5249 | break; |
| 5250 | case LYS_LIST: |
| 5251 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5252 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5253 | break; |
| 5254 | case LYS_LEAFLIST: |
| 5255 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5256 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5257 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5258 | case LYS_CHOICE: |
| 5259 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5260 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5261 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5262 | case LYS_ANYXML: |
| 5263 | case LYS_ANYDATA: |
| 5264 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5265 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5266 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5267 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5268 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5269 | lysc_update_path(ctx, NULL, NULL); |
| 5270 | lysc_update_path(ctx, NULL, NULL); |
| 5271 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5272 | default: |
| 5273 | LOGINT(ctx->ctx); |
| 5274 | return LY_EINT; |
| 5275 | } |
| 5276 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5277 | node->nodetype = node_p->nodetype; |
| 5278 | node->module = ctx->mod; |
| 5279 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5280 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5281 | |
| 5282 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5283 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5284 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5285 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5286 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5287 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5288 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5289 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5290 | node->flags |= LYS_CONFIG_R; |
| 5291 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5292 | /* config not explicitely set, inherit it from parent */ |
| 5293 | if (parent) { |
| 5294 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5295 | } else { |
| 5296 | /* default is config true */ |
| 5297 | node->flags |= LYS_CONFIG_W; |
| 5298 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5299 | } else { |
| 5300 | /* config set explicitely */ |
| 5301 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5302 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5303 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5304 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5305 | "Configuration node cannot be child of any state data node."); |
| 5306 | goto error; |
| 5307 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5308 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5309 | /* *list ordering */ |
| 5310 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5311 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5312 | 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] | 5313 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5314 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5315 | node->flags &= ~LYS_ORDBY_MASK; |
| 5316 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5317 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5318 | /* default ordering is system */ |
| 5319 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5320 | } |
| 5321 | } |
| 5322 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5323 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5324 | * 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] | 5325 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5326 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5327 | * 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] | 5328 | 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] | 5329 | } |
| 5330 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5331 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5332 | node->sp = node_p; |
| 5333 | } |
| 5334 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5335 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5336 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5337 | if (node_p->when) { |
| 5338 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5339 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5340 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 5341 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5342 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5343 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error); |
| 5344 | 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] | 5345 | |
| 5346 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5347 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5348 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5349 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5350 | if (node->flags & LYS_MAND_TRUE) { |
| 5351 | lys_compile_mandatory_parents(parent, 1); |
| 5352 | } |
| 5353 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5354 | lysc_update_path(ctx, NULL, NULL); |
| 5355 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5356 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5357 | if (parent) { |
| 5358 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5359 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5360 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5361 | } else { |
| 5362 | lysc_update_path(ctx, parent, node->name); |
| 5363 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5364 | 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] | 5365 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5366 | if (uses_status) { |
| 5367 | |
| 5368 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5369 | /* 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] | 5370 | * it directly gets the same status flags as the choice; |
| 5371 | * 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] | 5372 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5373 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5374 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5375 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5376 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5377 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5378 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5379 | 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] | 5380 | |
| 5381 | if (parent->nodetype == LYS_CHOICE) { |
| 5382 | lysc_update_path(ctx, NULL, NULL); |
| 5383 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5384 | } else { |
| 5385 | /* top-level element */ |
| 5386 | if (!ctx->mod->compiled->data) { |
| 5387 | ctx->mod->compiled->data = node; |
| 5388 | } else { |
| 5389 | /* insert at the end of the module's top-level nodes list */ |
| 5390 | ctx->mod->compiled->data->prev->next = node; |
| 5391 | node->prev = ctx->mod->compiled->data->prev; |
| 5392 | ctx->mod->compiled->data->prev = node; |
| 5393 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5394 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5395 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5396 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5397 | return LY_EVALID; |
| 5398 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5399 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5400 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5401 | |
| 5402 | return LY_SUCCESS; |
| 5403 | |
| 5404 | error: |
| 5405 | lysc_node_free(ctx->ctx, node); |
| 5406 | return ret; |
| 5407 | } |
| 5408 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5409 | static void |
| 5410 | lysc_disconnect(struct lysc_node *node) |
| 5411 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5412 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5413 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5414 | int remove_cs = 0; |
| 5415 | |
| 5416 | parent = node->parent; |
| 5417 | |
| 5418 | /* parent's first child */ |
| 5419 | if (!parent) { |
| 5420 | return; |
| 5421 | } |
| 5422 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5423 | cs = (struct lysc_node_case*)node; |
| 5424 | } else if (parent->nodetype == LYS_CASE) { |
| 5425 | /* disconnecting some node in a case */ |
| 5426 | cs = (struct lysc_node_case*)parent; |
| 5427 | parent = cs->parent; |
| 5428 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5429 | if (child == node) { |
| 5430 | if (cs->child == child) { |
| 5431 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5432 | /* case with a single child -> remove also the case */ |
| 5433 | child->parent = NULL; |
| 5434 | remove_cs = 1; |
| 5435 | } else { |
| 5436 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5437 | } |
| 5438 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5439 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5440 | } |
| 5441 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5442 | if (!remove_cs) { |
| 5443 | cs = NULL; |
| 5444 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5445 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5446 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5447 | } |
| 5448 | |
| 5449 | if (cs) { |
| 5450 | if (remove_cs) { |
| 5451 | /* cs has only one child which is being also removed */ |
| 5452 | lysc_disconnect((struct lysc_node*)cs); |
| 5453 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5454 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5455 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5456 | /* default case removed */ |
| 5457 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5458 | } |
| 5459 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5460 | /* first case removed */ |
| 5461 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5462 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5463 | if (cs->child) { |
| 5464 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5465 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5466 | prev = cs->child->prev; |
| 5467 | } /* else all the children are under a single case */ |
| 5468 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5469 | if (child->parent != (struct lysc_node*)cs) { |
| 5470 | break; |
| 5471 | } |
| 5472 | lysc_node_free(node->module->ctx, child); |
| 5473 | } |
| 5474 | if (prev) { |
| 5475 | if (prev->next) { |
| 5476 | prev->next = child; |
| 5477 | } |
| 5478 | if (child) { |
| 5479 | child->prev = prev; |
| 5480 | } else { |
| 5481 | /* link from the first child under the cases */ |
| 5482 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5483 | } |
| 5484 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5485 | } |
| 5486 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5487 | } |
| 5488 | |
| 5489 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5490 | if (node->prev->next) { |
| 5491 | node->prev->next = node->next; |
| 5492 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5493 | if (node->next) { |
| 5494 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5495 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5496 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5497 | if (child) { |
| 5498 | child->prev = node->prev; |
| 5499 | } |
| 5500 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5501 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5502 | } |
| 5503 | } |
| 5504 | |
| 5505 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5506 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5507 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5508 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5509 | struct ly_set devs_p = {0}; |
| 5510 | struct ly_set targets = {0}; |
| 5511 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5512 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5513 | struct lysc_action *rpcs; |
| 5514 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5515 | struct lysp_deviation *dev; |
| 5516 | struct lysp_deviate *d, **dp_new; |
| 5517 | struct lysp_deviate_add *d_add; |
| 5518 | struct lysp_deviate_del *d_del; |
| 5519 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5520 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5521 | struct lysc_deviation { |
| 5522 | const char *nodeid; |
| 5523 | struct lysc_node *target; /* target node of the deviation */ |
| 5524 | 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] | 5525 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5526 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5527 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5528 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5529 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5530 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5531 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5532 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5533 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5534 | |
| 5535 | /* get all deviations from the module and all its submodules ... */ |
| 5536 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5537 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5538 | } |
| 5539 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5540 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5541 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5542 | } |
| 5543 | } |
| 5544 | if (!devs_p.count) { |
| 5545 | /* nothing to do */ |
| 5546 | return LY_SUCCESS; |
| 5547 | } |
| 5548 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5549 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5550 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5551 | /* ... and group them by the target node */ |
| 5552 | devs = calloc(devs_p.count, sizeof *devs); |
| 5553 | for (u = 0; u < devs_p.count; ++u) { |
| 5554 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5555 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5556 | |
| 5557 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5558 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5559 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5560 | if (target->nodetype == LYS_ACTION) { |
| 5561 | /* move the target pointer to input/output to make them different from the action and |
| 5562 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5563 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5564 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5565 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5566 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5567 | flags |= LYSC_OPT_INTERNAL; |
| 5568 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5569 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5570 | flags |= LYSC_OPT_INTERNAL; |
| 5571 | } |
| 5572 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5573 | /* insert into the set of targets with duplicity detection */ |
| 5574 | i = ly_set_add(&targets, target, 0); |
| 5575 | if (!devs[i]) { |
| 5576 | /* new record */ |
| 5577 | devs[i] = calloc(1, sizeof **devs); |
| 5578 | devs[i]->target = target; |
| 5579 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5580 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5581 | } |
| 5582 | /* add deviates into the deviation's list of deviates */ |
| 5583 | for (d = dev->deviates; d; d = d->next) { |
| 5584 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5585 | *dp_new = d; |
| 5586 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5587 | devs[i]->not_supported = 1; |
| 5588 | } |
| 5589 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5590 | |
| 5591 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5592 | } |
| 5593 | |
| 5594 | /* MACROS for deviates checking */ |
| 5595 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5596 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5597 | 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] | 5598 | goto cleanup; \ |
| 5599 | } |
| 5600 | |
| 5601 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5602 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5603 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \ |
| 5604 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5605 | goto cleanup; \ |
| 5606 | } |
| 5607 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5608 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5609 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5610 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5611 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5612 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5613 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5614 | goto cleanup; \ |
| 5615 | } |
| 5616 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5617 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5618 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5619 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5620 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5621 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5622 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5623 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5624 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5625 | goto cleanup; \ |
| 5626 | } |
| 5627 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5628 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5629 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5630 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5631 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5632 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5633 | goto cleanup; \ |
| 5634 | } |
| 5635 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5636 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5637 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5638 | 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] | 5639 | goto cleanup; \ |
| 5640 | } |
| 5641 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5642 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5643 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5644 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5645 | "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] | 5646 | goto cleanup; \ |
| 5647 | } |
| 5648 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5649 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5650 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5651 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5652 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5653 | 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] | 5654 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5655 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5657 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5658 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5659 | goto cleanup; \ |
| 5660 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5661 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5662 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5663 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5664 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5665 | (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] | 5666 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5667 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5668 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5669 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5670 | } |
| 5671 | |
| 5672 | /* apply deviations */ |
| 5673 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5674 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5675 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5676 | struct ly_err_item *err = NULL; |
| 5677 | |
| 5678 | dflt = NULL; |
| 5679 | changed_type = 0; |
| 5680 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5681 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5682 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5683 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5684 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5685 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5686 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5687 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5688 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5689 | } |
| 5690 | } |
| 5691 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5692 | /* not-supported */ |
| 5693 | if (devs[u]->not_supported) { |
| 5694 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 5695 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 5696 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5697 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5698 | |
| 5699 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5700 | if (devs[u]->target->parent) { \ |
| 5701 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5702 | } else { \ |
| 5703 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5704 | } \ |
| 5705 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5706 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5707 | } \ |
| 5708 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5709 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5710 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5711 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5712 | } |
| 5713 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5714 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 5715 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5716 | /* remove RPC's/action's input */ |
| 5717 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5718 | 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] | 5719 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5720 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5721 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5722 | /* remove RPC's/action's output */ |
| 5723 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5724 | 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] | 5725 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5726 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5727 | } else { |
| 5728 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5729 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5730 | } |
| 5731 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5732 | /* remove Notification */ |
| 5733 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5734 | } else { |
| 5735 | /* remove the target node */ |
| 5736 | lysc_disconnect(devs[u]->target); |
| 5737 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5738 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5739 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5740 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5741 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5742 | continue; |
| 5743 | } |
| 5744 | |
| 5745 | /* list of deviates (not-supported is not present in the list) */ |
| 5746 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5747 | d = devs[u]->deviates[v]; |
| 5748 | |
| 5749 | switch (d->mod) { |
| 5750 | case LYS_DEV_ADD: |
| 5751 | d_add = (struct lysp_deviate_add*)d; |
| 5752 | /* [units-stmt] */ |
| 5753 | if (d_add->units) { |
| 5754 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5755 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5756 | |
| 5757 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5758 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5759 | } |
| 5760 | |
| 5761 | /* *must-stmt */ |
| 5762 | if (d_add->musts) { |
| 5763 | switch (devs[u]->target->nodetype) { |
| 5764 | case LYS_CONTAINER: |
| 5765 | case LYS_LIST: |
| 5766 | 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] | 5767 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5768 | break; |
| 5769 | case LYS_LEAF: |
| 5770 | case LYS_LEAFLIST: |
| 5771 | case LYS_ANYDATA: |
| 5772 | 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] | 5773 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5774 | break; |
| 5775 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5776 | 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] | 5777 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5778 | break; |
| 5779 | case LYS_ACTION: |
| 5780 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5781 | 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] | 5782 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5783 | break; |
| 5784 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5785 | 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] | 5786 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5787 | break; |
| 5788 | } |
| 5789 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5790 | default: |
| 5791 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5792 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5793 | goto cleanup; |
| 5794 | } |
| 5795 | } |
| 5796 | |
| 5797 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5798 | if (d_add->uniques) { |
| 5799 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5800 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5801 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5802 | |
| 5803 | /* *default-stmt */ |
| 5804 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5805 | switch (devs[u]->target->nodetype) { |
| 5806 | case LYS_LEAF: |
| 5807 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5808 | 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] | 5809 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5810 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5811 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5812 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5813 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5814 | } else { |
| 5815 | /* prepare new default value storage */ |
| 5816 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5817 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5818 | dflt = d_add->dflts[0]; |
| 5819 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5820 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5821 | /* mark the new default values as leaf's own */ |
| 5822 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5823 | break; |
| 5824 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5825 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5826 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5827 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5828 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5829 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5830 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5831 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5832 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5833 | LY_ARRAY_FREE(llist->dflts); |
| 5834 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5835 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5836 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5837 | } |
| 5838 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5839 | 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] | 5840 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 5841 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5842 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5843 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5844 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5845 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5846 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5847 | llist->dflts[x]->realtype = llist->type; |
| 5848 | 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] | 5849 | 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] | 5850 | (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] | 5851 | llist->dflts[x]->realtype->refcount++; |
| 5852 | if (err) { |
| 5853 | ly_err_print(err); |
| 5854 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5855 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5856 | d_add->dflts[x - y], err->msg); |
| 5857 | ly_err_free(err); |
| 5858 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5859 | if (rc == LY_EINCOMPLETE) { |
| 5860 | /* postpone default compilation when the tree is complete */ |
| 5861 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5862 | |
| 5863 | /* but in general result is so far ok */ |
| 5864 | rc = LY_SUCCESS; |
| 5865 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5866 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5867 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5868 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5869 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5870 | break; |
| 5871 | case LYS_CHOICE: |
| 5872 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5873 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5874 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5875 | * 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] | 5876 | 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] | 5877 | goto cleanup; |
| 5878 | } |
| 5879 | break; |
| 5880 | default: |
| 5881 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5882 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5883 | goto cleanup; |
| 5884 | } |
| 5885 | } |
| 5886 | |
| 5887 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5888 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 5889 | 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] | 5890 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5891 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5892 | goto cleanup; |
| 5893 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5894 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5895 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 5896 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5897 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5898 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5899 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5900 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5901 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5902 | goto cleanup; |
| 5903 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5904 | 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] | 5905 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5906 | |
| 5907 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5908 | if (d_add->flags & LYS_MAND_MASK) { |
| 5909 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5910 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5911 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5912 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5913 | goto cleanup; |
| 5914 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5915 | 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] | 5916 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5917 | |
| 5918 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5919 | if (d_add->flags & LYS_SET_MIN) { |
| 5920 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5921 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5922 | /* change value */ |
| 5923 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5924 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5925 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5926 | /* change value */ |
| 5927 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5928 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5929 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5930 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5931 | goto cleanup; |
| 5932 | } |
| 5933 | if (d_add->min) { |
| 5934 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5935 | } |
| 5936 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5937 | |
| 5938 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5939 | if (d_add->flags & LYS_SET_MAX) { |
| 5940 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5941 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5942 | /* change value */ |
| 5943 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5944 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5945 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5946 | /* change value */ |
| 5947 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5948 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5949 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5950 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 5951 | goto cleanup; |
| 5952 | } |
| 5953 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5954 | |
| 5955 | break; |
| 5956 | case LYS_DEV_DELETE: |
| 5957 | d_del = (struct lysp_deviate_del*)d; |
| 5958 | |
| 5959 | /* [units-stmt] */ |
| 5960 | if (d_del->units) { |
| 5961 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5962 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 5963 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 5964 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5965 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5966 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5967 | goto cleanup; |
| 5968 | } |
| 5969 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5970 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5971 | } |
| 5972 | |
| 5973 | /* *must-stmt */ |
| 5974 | if (d_del->musts) { |
| 5975 | switch (devs[u]->target->nodetype) { |
| 5976 | case LYS_CONTAINER: |
| 5977 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5978 | 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] | 5979 | break; |
| 5980 | case LYS_LEAF: |
| 5981 | case LYS_LEAFLIST: |
| 5982 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5983 | 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] | 5984 | break; |
| 5985 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5986 | 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] | 5987 | break; |
| 5988 | case LYS_ACTION: |
| 5989 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5990 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5991 | break; |
| 5992 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5993 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5994 | break; |
| 5995 | } |
| 5996 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5997 | default: |
| 5998 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5999 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6000 | goto cleanup; |
| 6001 | } |
| 6002 | } |
| 6003 | |
| 6004 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6005 | if (d_del->uniques) { |
| 6006 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6007 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6008 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6009 | LY_ARRAY_FOR(list->uniques, z) { |
| 6010 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6011 | nodeid = strpbrk(name, " \t\n"); |
| 6012 | if (nodeid) { |
| 6013 | if (strncmp(name, list->uniques[z][y]->name, nodeid - name) |
| 6014 | || list->uniques[z][y]->name[nodeid - name] != '\0') { |
| 6015 | break; |
| 6016 | } |
| 6017 | while (isspace(*nodeid)) { |
| 6018 | ++nodeid; |
| 6019 | } |
| 6020 | } else { |
| 6021 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6022 | break; |
| 6023 | } |
| 6024 | } |
| 6025 | } |
| 6026 | if (!name) { |
| 6027 | /* complete match - remove the unique */ |
| 6028 | LY_ARRAY_DECREMENT(list->uniques); |
| 6029 | LY_ARRAY_FREE(list->uniques[z]); |
| 6030 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6031 | --z; |
| 6032 | break; |
| 6033 | } |
| 6034 | } |
| 6035 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6036 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6037 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6038 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6039 | goto cleanup; |
| 6040 | } |
| 6041 | } |
| 6042 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6043 | LY_ARRAY_FREE(list->uniques); |
| 6044 | list->uniques = NULL; |
| 6045 | } |
| 6046 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6047 | |
| 6048 | /* *default-stmt */ |
| 6049 | if (d_del->dflts) { |
| 6050 | switch (devs[u]->target->nodetype) { |
| 6051 | case LYS_LEAF: |
| 6052 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6053 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6054 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6055 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6056 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6057 | 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] | 6058 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6059 | if (i) { |
| 6060 | free((char*)dflt); |
| 6061 | } |
| 6062 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6063 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6064 | d_del->dflts[0], dflt); |
| 6065 | goto cleanup; |
| 6066 | } |
| 6067 | if (i) { |
| 6068 | free((char*)dflt); |
| 6069 | } |
| 6070 | dflt = NULL; |
| 6071 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6072 | /* update the list of incomplete default values if needed */ |
| 6073 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6074 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6075 | /* remove the default specification */ |
| 6076 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6077 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6078 | free(leaf->dflt); |
| 6079 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6080 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6081 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6082 | break; |
| 6083 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6084 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6085 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6086 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6087 | 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] | 6088 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6089 | if (i) { |
| 6090 | free((char*)dflt); |
| 6091 | } |
| 6092 | dflt = NULL; |
| 6093 | break; |
| 6094 | } |
| 6095 | if (i) { |
| 6096 | free((char*)dflt); |
| 6097 | } |
| 6098 | dflt = NULL; |
| 6099 | } |
| 6100 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6101 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6102 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6103 | goto cleanup; |
| 6104 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6105 | |
| 6106 | /* update the list of incomplete default values if needed */ |
| 6107 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6108 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6109 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6110 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6111 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6112 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6113 | free(llist->dflts[y]); |
| 6114 | 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] | 6115 | 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] | 6116 | } |
| 6117 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6118 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6119 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6120 | LY_ARRAY_FREE(llist->dflts); |
| 6121 | llist->dflts = NULL; |
| 6122 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6123 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6124 | break; |
| 6125 | case LYS_CHOICE: |
| 6126 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6127 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6128 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6129 | 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] | 6130 | if (prefix) { |
| 6131 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6132 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6133 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6134 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6135 | "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] | 6136 | goto cleanup; |
| 6137 | } |
| 6138 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6139 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6140 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6141 | "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] | 6142 | goto cleanup; |
| 6143 | } |
| 6144 | } |
| 6145 | /* else { |
| 6146 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6147 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6148 | * the module of the default case, just matching its name */ |
| 6149 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6150 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6151 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6152 | 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] | 6153 | goto cleanup; |
| 6154 | } |
| 6155 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6156 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6157 | break; |
| 6158 | default: |
| 6159 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6160 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6161 | goto cleanup; |
| 6162 | } |
| 6163 | } |
| 6164 | |
| 6165 | break; |
| 6166 | case LYS_DEV_REPLACE: |
| 6167 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6168 | |
| 6169 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6170 | if (d_rpl->type) { |
| 6171 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6172 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6173 | 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] | 6174 | |
| 6175 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6176 | /* the target has default from the previous type - remove it */ |
| 6177 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6178 | /* update the list of incomplete default values if needed */ |
| 6179 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6180 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6181 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6182 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6183 | free(leaf->dflt); |
| 6184 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6185 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6186 | } else { /* LYS_LEAFLIST */ |
| 6187 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6188 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6189 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6190 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6191 | free(llist->dflts[x]); |
| 6192 | } |
| 6193 | LY_ARRAY_FREE(llist->dflts); |
| 6194 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6195 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6196 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6197 | } |
| 6198 | } |
| 6199 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6200 | /* there is no default value, do not set changed_type after type compilation |
| 6201 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6202 | changed_type = -1; |
| 6203 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6204 | 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] | 6205 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6206 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6207 | |
| 6208 | /* [units-stmt] */ |
| 6209 | if (d_rpl->units) { |
| 6210 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6211 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6212 | units, "replacing", "units", d_rpl->units); |
| 6213 | |
| 6214 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6215 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6216 | } |
| 6217 | |
| 6218 | /* [default-stmt] */ |
| 6219 | if (d_rpl->dflt) { |
| 6220 | switch (devs[u]->target->nodetype) { |
| 6221 | case LYS_LEAF: |
| 6222 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6223 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6224 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6225 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6226 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6227 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6228 | dflt = d_rpl->dflt; |
| 6229 | /* 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] | 6230 | break; |
| 6231 | case LYS_CHOICE: |
| 6232 | 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] | 6233 | 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] | 6234 | goto cleanup; |
| 6235 | } |
| 6236 | break; |
| 6237 | default: |
| 6238 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6239 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6240 | goto cleanup; |
| 6241 | } |
| 6242 | } |
| 6243 | |
| 6244 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6245 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 6246 | 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] | 6247 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6248 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6249 | goto cleanup; |
| 6250 | } |
| 6251 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6252 | 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] | 6253 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6254 | goto cleanup; |
| 6255 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6256 | 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] | 6257 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6258 | |
| 6259 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6260 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6261 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6262 | 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] | 6263 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6264 | goto cleanup; |
| 6265 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6266 | 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] | 6267 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6268 | |
| 6269 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6270 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6271 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6272 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6273 | /* change value */ |
| 6274 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6275 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6276 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6277 | /* change value */ |
| 6278 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6279 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6280 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6281 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6282 | goto cleanup; |
| 6283 | } |
| 6284 | if (d_rpl->min) { |
| 6285 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6286 | } |
| 6287 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6288 | |
| 6289 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6290 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6291 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6292 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6293 | /* change value */ |
| 6294 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6295 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6296 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6297 | /* change value */ |
| 6298 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6299 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6300 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6301 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6302 | goto cleanup; |
| 6303 | } |
| 6304 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6305 | |
| 6306 | break; |
| 6307 | default: |
| 6308 | LOGINT(ctx->ctx); |
| 6309 | goto cleanup; |
| 6310 | } |
| 6311 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6312 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6313 | /* final check when all deviations of a single target node are applied */ |
| 6314 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6315 | /* check min-max compatibility */ |
| 6316 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6317 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6318 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6319 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6320 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6321 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6322 | } else { |
| 6323 | min = max = 0; |
| 6324 | } |
| 6325 | if (min > max) { |
| 6326 | 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] | 6327 | "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] | 6328 | goto cleanup; |
| 6329 | } |
| 6330 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6331 | if (dflt) { |
| 6332 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6333 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6334 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6335 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6336 | 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] | 6337 | 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] | 6338 | leaf->dflt->realtype->refcount++; |
| 6339 | if (err) { |
| 6340 | ly_err_print(err); |
| 6341 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6342 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6343 | ly_err_free(err); |
| 6344 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6345 | if (rc == LY_EINCOMPLETE) { |
| 6346 | /* postpone default compilation when the tree is complete */ |
| 6347 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6348 | |
| 6349 | /* but in general result is so far ok */ |
| 6350 | rc = LY_SUCCESS; |
| 6351 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6352 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6353 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6354 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6355 | int dynamic; |
| 6356 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6357 | 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] | 6358 | |
| 6359 | /* update the list of incomplete default values if needed */ |
| 6360 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6361 | |
| 6362 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6363 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6364 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6365 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6366 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6367 | 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] | 6368 | 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] | 6369 | leaf->dflt->realtype->refcount++; |
| 6370 | if (err) { |
| 6371 | ly_err_print(err); |
| 6372 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6373 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6374 | ly_err_free(err); |
| 6375 | } |
| 6376 | if (dynamic) { |
| 6377 | free((void*)dflt); |
| 6378 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6379 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6380 | if (rc == LY_EINCOMPLETE) { |
| 6381 | /* postpone default compilation when the tree is complete */ |
| 6382 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6383 | |
| 6384 | /* but in general result is so far ok */ |
| 6385 | rc = LY_SUCCESS; |
| 6386 | } |
| 6387 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6388 | } else { /* LYS_LEAFLIST */ |
| 6389 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6390 | 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] | 6391 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6392 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6393 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6394 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6395 | 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] | 6396 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6397 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6398 | llist->dflts[x]->realtype->refcount++; |
| 6399 | if (err) { |
| 6400 | ly_err_print(err); |
| 6401 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6402 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6403 | dflt, err->msg); |
| 6404 | ly_err_free(err); |
| 6405 | } |
| 6406 | if (dynamic) { |
| 6407 | free((void*)dflt); |
| 6408 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6409 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6410 | if (rc == LY_EINCOMPLETE) { |
| 6411 | /* postpone default compilation when the tree is complete */ |
| 6412 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6413 | |
| 6414 | /* but in general result is so far ok */ |
| 6415 | rc = LY_SUCCESS; |
| 6416 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6417 | LY_CHECK_GOTO(rc, cleanup); |
| 6418 | } |
| 6419 | } |
| 6420 | } |
| 6421 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6422 | /* check mandatory - default compatibility */ |
| 6423 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6424 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6425 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6426 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6427 | "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] | 6428 | goto cleanup; |
| 6429 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6430 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6431 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6432 | 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] | 6433 | goto cleanup; |
| 6434 | } |
| 6435 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6436 | /* mandatory node under a default case */ |
| 6437 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6438 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6439 | 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] | 6440 | goto cleanup; |
| 6441 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6442 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6443 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6444 | } |
| 6445 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6446 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6447 | ret = LY_SUCCESS; |
| 6448 | |
| 6449 | cleanup: |
| 6450 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6451 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6452 | free(devs[u]); |
| 6453 | } |
| 6454 | free(devs); |
| 6455 | ly_set_erase(&targets, NULL); |
| 6456 | ly_set_erase(&devs_p, NULL); |
| 6457 | |
| 6458 | return ret; |
| 6459 | } |
| 6460 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6461 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6462 | * @brief Compile the given YANG submodule into the main module. |
| 6463 | * @param[in] ctx Compile context |
| 6464 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6465 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6466 | */ |
| 6467 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6468 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6469 | { |
| 6470 | unsigned int u; |
| 6471 | LY_ERR ret = LY_SUCCESS; |
| 6472 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6473 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6474 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6475 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6476 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6477 | if (!mainmod->mod->off_features) { |
| 6478 | /* features are compiled directly into the compiled module structure, |
| 6479 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6480 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6481 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6482 | mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features); |
| 6483 | LY_CHECK_GOTO(ret, error); |
| 6484 | } |
| 6485 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6486 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6487 | 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] | 6488 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6489 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6490 | /* data nodes */ |
| 6491 | LY_LIST_FOR(submod->data, node_p) { |
| 6492 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6493 | LY_CHECK_GOTO(ret, error); |
| 6494 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6495 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6496 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6497 | 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] | 6498 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6499 | error: |
| 6500 | return ret; |
| 6501 | } |
| 6502 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6503 | LY_ERR |
| 6504 | lys_compile(struct lys_module *mod, int options) |
| 6505 | { |
| 6506 | struct lysc_ctx ctx = {0}; |
| 6507 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6508 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6509 | struct lysp_module *sp; |
| 6510 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6511 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6512 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6513 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6514 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6515 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6516 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6517 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 6518 | |
| 6519 | if (!mod->implemented) { |
| 6520 | /* just imported modules are not compiled */ |
| 6521 | return LY_SUCCESS; |
| 6522 | } |
| 6523 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6524 | sp = mod->parsed; |
| 6525 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6526 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6527 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6528 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6529 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6530 | ctx.path_len = 1; |
| 6531 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6532 | |
| 6533 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6534 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 6535 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6536 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6537 | 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] | 6538 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6539 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6540 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6541 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6542 | if (mod->off_features) { |
| 6543 | /* there is already precompiled array of features */ |
| 6544 | mod_c->features = mod->off_features; |
| 6545 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6546 | } else { |
| 6547 | /* features are compiled directly into the compiled module structure, |
| 6548 | * 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] | 6549 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6550 | LY_CHECK_GOTO(ret, error); |
| 6551 | } |
| 6552 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 6553 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 6554 | * are present. */ |
| 6555 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6556 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6557 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6558 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6559 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6560 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6561 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6562 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6563 | 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] | 6564 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6565 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6566 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6567 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6568 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6569 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6570 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6571 | 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] | 6572 | if (sp->identities) { |
| 6573 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 6574 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6575 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6576 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6577 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6578 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6579 | ret = lys_compile_node(&ctx, node_p, NULL, 0); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6580 | LY_CHECK_GOTO(ret, error); |
| 6581 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6582 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6583 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6584 | 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] | 6585 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6586 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 6587 | ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6588 | LY_CHECK_GOTO(ret, error); |
| 6589 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6590 | ret = lys_compile_augment(&ctx, augments[u], NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6591 | LY_CHECK_GOTO(ret, error); |
| 6592 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6593 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6594 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6595 | ret = lys_compile_deviations(&ctx, sp); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6596 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6597 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6598 | 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] | 6599 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6600 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6601 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 6602 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 6603 | * 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] | 6604 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6605 | 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] | 6606 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6607 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6608 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6609 | 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] | 6610 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6611 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6612 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6613 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6614 | /* validate the path */ |
| 6615 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 6616 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 6617 | LY_CHECK_GOTO(ret, error); |
| 6618 | } |
| 6619 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6620 | } |
| 6621 | } |
| 6622 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6623 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6624 | 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] | 6625 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6626 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6627 | /* store pointer to the real type */ |
| 6628 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 6629 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6630 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6631 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6632 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6633 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6634 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6635 | /* store pointer to the real type */ |
| 6636 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 6637 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6638 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6639 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 6640 | } |
| 6641 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6642 | } |
| 6643 | } |
| 6644 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6645 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6646 | /* finish incomplete default values compilation */ |
| 6647 | for (u = 0; u < ctx.dflts.count; ++u) { |
| 6648 | struct ly_err_item *err = NULL; |
| 6649 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[u]; |
| 6650 | ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized), |
| 6651 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 6652 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 6653 | if (err) { |
| 6654 | ly_err_print(err); |
| 6655 | ctx.path[0] = '\0'; |
| 6656 | lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE); |
| 6657 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 6658 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6659 | ly_err_free(err); |
| 6660 | } |
| 6661 | LY_CHECK_GOTO(ret, error); |
| 6662 | } |
| 6663 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6664 | /* validate non-instantiated groupings from the parsed schema, |
| 6665 | * without it we would accept even the schemas with invalid grouping specification */ |
| 6666 | ctx.options |= LYSC_OPT_GROUPING; |
| 6667 | LY_ARRAY_FOR(sp->groupings, u) { |
| 6668 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
| 6669 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error); |
| 6670 | } |
| 6671 | } |
| 6672 | LY_LIST_FOR(sp->data, node_p) { |
| 6673 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 6674 | LY_ARRAY_FOR(grps, u) { |
| 6675 | if (!(grps[u].flags & LYS_USED_GRP)) { |
| 6676 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error); |
| 6677 | } |
| 6678 | } |
| 6679 | } |
| 6680 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6681 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 6682 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 6683 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 6684 | } |
| 6685 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6686 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6687 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6688 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6689 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6690 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6691 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6692 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6693 | lysp_module_free(mod->parsed); |
| 6694 | ((struct lys_module*)mod)->parsed = NULL; |
| 6695 | } |
| 6696 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6697 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6698 | /* remove flag of the modules implemented by dependency */ |
| 6699 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6700 | m = ctx.ctx->list.objs[u]; |
| 6701 | if (m->implemented == 2) { |
| 6702 | m->implemented = 1; |
| 6703 | } |
| 6704 | } |
| 6705 | } |
| 6706 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6707 | ((struct lys_module*)mod)->compiled = mod_c; |
| 6708 | return LY_SUCCESS; |
| 6709 | |
| 6710 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6711 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6712 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6713 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6714 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6715 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6716 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6717 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6718 | mod->compiled = NULL; |
| 6719 | |
| 6720 | /* revert compilation of modules implemented by dependency */ |
| 6721 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6722 | m = ctx.ctx->list.objs[u]; |
| 6723 | if (m->implemented == 2) { |
| 6724 | /* revert features list to the precompiled state */ |
| 6725 | lys_feature_precompile_revert(&ctx, m); |
| 6726 | /* mark module as imported-only / not-implemented */ |
| 6727 | m->implemented = 0; |
| 6728 | /* free the compiled version of the module */ |
| 6729 | lysc_module_free(m->compiled, NULL); |
| 6730 | m->compiled = NULL; |
| 6731 | } |
| 6732 | } |
| 6733 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6734 | return ret; |
| 6735 | } |