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) */ |
| 185 | ++ctx->path_len; |
| 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 | } |
| 212 | ctx->path_len += len; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 217 | * @brief Duplicate the compiled pattern structure. |
| 218 | * |
| 219 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 220 | * |
| 221 | * @param[in] orig The pattern structure to duplicate. |
| 222 | * @return The duplicated structure to use. |
| 223 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 224 | static struct lysc_pattern* |
| 225 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 226 | { |
| 227 | ++orig->refcount; |
| 228 | return orig; |
| 229 | } |
| 230 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 231 | /** |
| 232 | * @brief Duplicate the array of compiled patterns. |
| 233 | * |
| 234 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 235 | * |
| 236 | * @param[in] ctx Libyang context for logging. |
| 237 | * @param[in] orig The patterns sized array to duplicate. |
| 238 | * @return New sized array as a copy of @p orig. |
| 239 | * @return NULL in case of memory allocation error. |
| 240 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 241 | static struct lysc_pattern** |
| 242 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 243 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 244 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 245 | unsigned int u; |
| 246 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 247 | assert(orig); |
| 248 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 249 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 250 | LY_ARRAY_FOR(orig, u) { |
| 251 | dup[u] = lysc_pattern_dup(orig[u]); |
| 252 | LY_ARRAY_INCREMENT(dup); |
| 253 | } |
| 254 | return dup; |
| 255 | } |
| 256 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 257 | /** |
| 258 | * @brief Duplicate compiled range structure. |
| 259 | * |
| 260 | * @param[in] ctx Libyang context for logging. |
| 261 | * @param[in] orig The range structure to be duplicated. |
| 262 | * @return New compiled range structure as a copy of @p orig. |
| 263 | * @return NULL in case of memory allocation error. |
| 264 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 265 | struct lysc_range* |
| 266 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 267 | { |
| 268 | struct lysc_range *dup; |
| 269 | LY_ERR ret; |
| 270 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 271 | assert(orig); |
| 272 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 273 | dup = calloc(1, sizeof *dup); |
| 274 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 275 | if (orig->parts) { |
| 276 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 277 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 278 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 279 | } |
| 280 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 281 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 282 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 283 | |
| 284 | return dup; |
| 285 | cleanup: |
| 286 | free(dup); |
| 287 | (void) ret; /* set but not used due to the return type */ |
| 288 | return NULL; |
| 289 | } |
| 290 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 291 | /** |
| 292 | * @brief Stack for processing if-feature expressions. |
| 293 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 294 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 295 | int size; /**< number of items in the stack */ |
| 296 | int index; /**< first empty item */ |
| 297 | 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] | 298 | }; |
| 299 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 300 | /** |
| 301 | * @brief Add @ref ifftokens into the stack. |
| 302 | * @param[in] stack The if-feature stack to use. |
| 303 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 304 | * @return LY_EMEM in case of memory allocation error |
| 305 | * @return LY_ESUCCESS if the value successfully stored. |
| 306 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 307 | static LY_ERR |
| 308 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 309 | { |
| 310 | if (stack->index == stack->size) { |
| 311 | stack->size += 4; |
| 312 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 313 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 314 | } |
| 315 | stack->stack[stack->index++] = value; |
| 316 | return LY_SUCCESS; |
| 317 | } |
| 318 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 319 | /** |
| 320 | * @brief Get (and remove) the last item form the stack. |
| 321 | * @param[in] stack The if-feature stack to use. |
| 322 | * @return The value from the top of the stack. |
| 323 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 324 | static uint8_t |
| 325 | iff_stack_pop(struct iff_stack *stack) |
| 326 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 327 | assert(stack && stack->index); |
| 328 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 329 | stack->index--; |
| 330 | return stack->stack[stack->index]; |
| 331 | } |
| 332 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 333 | /** |
| 334 | * @brief Clean up the stack. |
| 335 | * @param[in] stack The if-feature stack to use. |
| 336 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 337 | static void |
| 338 | iff_stack_clean(struct iff_stack *stack) |
| 339 | { |
| 340 | stack->size = 0; |
| 341 | free(stack->stack); |
| 342 | } |
| 343 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 344 | /** |
| 345 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 346 | * (libyang format of the if-feature expression). |
| 347 | * @param[in,out] list The 2bits array to modify. |
| 348 | * @param[in] op The operand (@ref ifftokens) to store. |
| 349 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 350 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 351 | static void |
| 352 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 353 | { |
| 354 | uint8_t *item; |
| 355 | uint8_t mask = 3; |
| 356 | |
| 357 | assert(pos >= 0); |
| 358 | assert(op <= 3); /* max 2 bits */ |
| 359 | |
| 360 | item = &list[pos / 4]; |
| 361 | mask = mask << 2 * (pos % 4); |
| 362 | *item = (*item) & ~mask; |
| 363 | *item = (*item) | (op << 2 * (pos % 4)); |
| 364 | } |
| 365 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 366 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 367 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 368 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 369 | /** |
| 370 | * @brief Find a feature of the given name and referenced in the given module. |
| 371 | * |
| 372 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 373 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 374 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 375 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 376 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 377 | * assigned till that time will be still valid. |
| 378 | * |
| 379 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 380 | * @param[in] name Name of the feature including possible prefix. |
| 381 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 382 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 383 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 384 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 385 | 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] | 386 | { |
| 387 | size_t i; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 388 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 389 | |
| 390 | for (i = 0; i < len; ++i) { |
| 391 | if (name[i] == ':') { |
| 392 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 393 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 394 | LY_CHECK_RET(!mod, NULL); |
| 395 | |
| 396 | name = &name[i + 1]; |
| 397 | len = len - i - 1; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 402 | if (mod->implemented) { |
| 403 | /* module is implemented so there is already the compiled schema */ |
| 404 | flist = mod->compiled->features; |
| 405 | } else { |
| 406 | flist = mod->off_features; |
| 407 | } |
| 408 | LY_ARRAY_FOR(flist, i) { |
| 409 | f = &flist[i]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 410 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 411 | return f; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return NULL; |
| 416 | } |
| 417 | |
| 418 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 419 | 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] | 420 | { |
| 421 | const char *name; |
| 422 | unsigned int u; |
| 423 | const struct lys_module *mod; |
| 424 | struct lysp_ext *edef = NULL; |
| 425 | |
| 426 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 427 | ext->insubstmt = ext_p->insubstmt; |
| 428 | ext->insubstmt_index = ext_p->insubstmt_index; |
| 429 | |
| 430 | /* get module where the extension definition should be placed */ |
| 431 | for (u = 0; ext_p->name[u] != ':'; ++u); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 432 | mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 433 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 434 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 435 | LY_EVALID); |
| 436 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 437 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 438 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 439 | ext_p->name, mod->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 440 | LY_EVALID); |
| 441 | name = &ext_p->name[u + 1]; |
| 442 | /* find the extension definition there */ |
| 443 | for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) { |
| 444 | if (!strcmp(name, mod->parsed->extensions[u].name)) { |
| 445 | edef = &mod->parsed->extensions[u]; |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 450 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
| 451 | LY_EVALID); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 452 | /* TODO extension plugins */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 453 | |
| 454 | return LY_SUCCESS; |
| 455 | } |
| 456 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 457 | /** |
| 458 | * @brief Compile information from the if-feature statement |
| 459 | * @param[in] ctx Compile context. |
| 460 | * @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] | 461 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 462 | * @return LY_ERR value. |
| 463 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 464 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 465 | 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] | 466 | { |
| 467 | const char *c = *value; |
| 468 | int r, rc = EXIT_FAILURE; |
| 469 | int i, j, last_not, checkversion = 0; |
| 470 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 471 | uint8_t op; |
| 472 | struct iff_stack stack = {0, 0, NULL}; |
| 473 | struct lysc_feature *f; |
| 474 | |
| 475 | assert(c); |
| 476 | |
| 477 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 478 | for (i = j = last_not = 0; c[i]; i++) { |
| 479 | if (c[i] == '(') { |
| 480 | j++; |
| 481 | checkversion = 1; |
| 482 | continue; |
| 483 | } else if (c[i] == ')') { |
| 484 | j--; |
| 485 | continue; |
| 486 | } else if (isspace(c[i])) { |
| 487 | checkversion = 1; |
| 488 | continue; |
| 489 | } |
| 490 | |
| 491 | 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] | 492 | int sp; |
| 493 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 494 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 495 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 496 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 497 | return LY_EVALID; |
| 498 | } else if (!isspace(c[i + r])) { |
| 499 | /* feature name starting with the not/and/or */ |
| 500 | last_not = 0; |
| 501 | f_size++; |
| 502 | } else if (c[i] == 'n') { /* not operation */ |
| 503 | if (last_not) { |
| 504 | /* double not */ |
| 505 | expr_size = expr_size - 2; |
| 506 | last_not = 0; |
| 507 | } else { |
| 508 | last_not = 1; |
| 509 | } |
| 510 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 511 | if (f_exp != f_size) { |
| 512 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 513 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 514 | return LY_EVALID; |
| 515 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 516 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 517 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 518 | /* not a not operation */ |
| 519 | last_not = 0; |
| 520 | } |
| 521 | i += r; |
| 522 | } else { |
| 523 | f_size++; |
| 524 | last_not = 0; |
| 525 | } |
| 526 | expr_size++; |
| 527 | |
| 528 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 529 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 530 | i--; |
| 531 | break; |
| 532 | } |
| 533 | i++; |
| 534 | } |
| 535 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 536 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 537 | /* not matching count of ( and ) */ |
| 538 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 539 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 540 | return LY_EVALID; |
| 541 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 542 | if (f_exp != f_size) { |
| 543 | /* features do not match the needed arguments for the logical operations */ |
| 544 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 545 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 546 | "the required number of operands for the operations.", *value); |
| 547 | return LY_EVALID; |
| 548 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 549 | |
| 550 | if (checkversion || expr_size > 1) { |
| 551 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 552 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 553 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 554 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 555 | return LY_EVALID; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /* allocate the memory */ |
| 560 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 561 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 562 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 563 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 564 | |
| 565 | stack.size = expr_size; |
| 566 | f_size--; expr_size--; /* used as indexes from now */ |
| 567 | |
| 568 | for (i--; i >= 0; i--) { |
| 569 | if (c[i] == ')') { |
| 570 | /* push it on stack */ |
| 571 | iff_stack_push(&stack, LYS_IFF_RP); |
| 572 | continue; |
| 573 | } else if (c[i] == '(') { |
| 574 | /* pop from the stack into result all operators until ) */ |
| 575 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 576 | iff_setop(iff->expr, op, expr_size--); |
| 577 | } |
| 578 | continue; |
| 579 | } else if (isspace(c[i])) { |
| 580 | continue; |
| 581 | } |
| 582 | |
| 583 | /* end of operator or operand -> find beginning and get what is it */ |
| 584 | j = i + 1; |
| 585 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 586 | i--; |
| 587 | } |
| 588 | i++; /* go back by one step */ |
| 589 | |
| 590 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 591 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 592 | /* double not */ |
| 593 | iff_stack_pop(&stack); |
| 594 | } else { |
| 595 | /* not has the highest priority, so do not pop from the stack |
| 596 | * as in case of AND and OR */ |
| 597 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 598 | } |
| 599 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 600 | /* as for OR - pop from the stack all operators with the same or higher |
| 601 | * priority and store them to the result, then push the AND to the stack */ |
| 602 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 603 | op = iff_stack_pop(&stack); |
| 604 | iff_setop(iff->expr, op, expr_size--); |
| 605 | } |
| 606 | iff_stack_push(&stack, LYS_IFF_AND); |
| 607 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 608 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 609 | op = iff_stack_pop(&stack); |
| 610 | iff_setop(iff->expr, op, expr_size--); |
| 611 | } |
| 612 | iff_stack_push(&stack, LYS_IFF_OR); |
| 613 | } else { |
| 614 | /* feature name, length is j - i */ |
| 615 | |
| 616 | /* add it to the expression */ |
| 617 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 618 | |
| 619 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 620 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 621 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 622 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 623 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 624 | iff->features[f_size] = f; |
| 625 | LY_ARRAY_INCREMENT(iff->features); |
| 626 | f_size--; |
| 627 | } |
| 628 | } |
| 629 | while (stack.index) { |
| 630 | op = iff_stack_pop(&stack); |
| 631 | iff_setop(iff->expr, op, expr_size--); |
| 632 | } |
| 633 | |
| 634 | if (++expr_size || ++f_size) { |
| 635 | /* not all expected operators and operands found */ |
| 636 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 637 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 638 | rc = LY_EINT; |
| 639 | } else { |
| 640 | rc = LY_SUCCESS; |
| 641 | } |
| 642 | |
| 643 | error: |
| 644 | /* cleanup */ |
| 645 | iff_stack_clean(&stack); |
| 646 | |
| 647 | return rc; |
| 648 | } |
| 649 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 650 | /** |
| 651 | * @brief Compile information from the when statement |
| 652 | * @param[in] ctx Compile context. |
| 653 | * @param[in] when_p The parsed when statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 654 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 655 | * @return LY_ERR value. |
| 656 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 657 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 658 | 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] | 659 | { |
| 660 | unsigned int u; |
| 661 | LY_ERR ret = LY_SUCCESS; |
| 662 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 663 | *when = calloc(1, sizeof **when); |
| 664 | (*when)->refcount = 1; |
| 665 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
| 666 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 667 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 668 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 669 | 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] | 670 | |
| 671 | done: |
| 672 | return ret; |
| 673 | } |
| 674 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 675 | /** |
| 676 | * @brief Compile information from the must statement |
| 677 | * @param[in] ctx Compile context. |
| 678 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 679 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 680 | * @return LY_ERR value. |
| 681 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 682 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 683 | 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] | 684 | { |
| 685 | unsigned int u; |
| 686 | LY_ERR ret = LY_SUCCESS; |
| 687 | |
| 688 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 689 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 690 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 691 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 692 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 693 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 694 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 695 | 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] | 696 | |
| 697 | done: |
| 698 | return ret; |
| 699 | } |
| 700 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 701 | /** |
| 702 | * @brief Compile information from the import statement |
| 703 | * @param[in] ctx Compile context. |
| 704 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 705 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 706 | * @return LY_ERR value. |
| 707 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 708 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 709 | 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] | 710 | { |
| 711 | unsigned int u; |
| 712 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 713 | LY_ERR ret = LY_SUCCESS; |
| 714 | |
| 715 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 716 | 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] | 717 | imp->module = imp_p->module; |
| 718 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 719 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 720 | * 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] | 721 | * 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] | 722 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 723 | /* try to use filepath if present */ |
| 724 | if (imp->module->filepath) { |
| 725 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 726 | !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] | 727 | if (mod != imp->module) { |
| 728 | 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] | 729 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 730 | mod = NULL; |
| 731 | } |
| 732 | } |
| 733 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 734 | 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] | 735 | 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] | 736 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 737 | return LY_ENOTFOUND; |
| 738 | } |
| 739 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | done: |
| 743 | return ret; |
| 744 | } |
| 745 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 746 | /** |
| 747 | * @brief Compile information from the identity statement |
| 748 | * |
| 749 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 750 | * |
| 751 | * @param[in] ctx Compile context. |
| 752 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 753 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 754 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 755 | * @return LY_ERR value. |
| 756 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 757 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 758 | 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] | 759 | { |
| 760 | unsigned int u; |
| 761 | LY_ERR ret = LY_SUCCESS; |
| 762 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 763 | lysc_update_path(ctx, NULL, ident_p->name); |
| 764 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 765 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 766 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 767 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 768 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 769 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 770 | 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] | 771 | /* 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] | 772 | 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] | 773 | ident->flags = ident_p->flags; |
| 774 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 775 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 776 | done: |
| 777 | return ret; |
| 778 | } |
| 779 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 780 | /** |
| 781 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 782 | * |
| 783 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 784 | * |
| 785 | * @param[in] ctx Compile context for logging. |
| 786 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 787 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 788 | * @return LY_SUCCESS if everything is ok. |
| 789 | * @return LY_EVALID if the identity is derived from itself. |
| 790 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 791 | static LY_ERR |
| 792 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 793 | { |
| 794 | LY_ERR ret = LY_EVALID; |
| 795 | unsigned int u, v; |
| 796 | struct ly_set recursion = {0}; |
| 797 | struct lysc_ident *drv; |
| 798 | |
| 799 | if (!derived) { |
| 800 | return LY_SUCCESS; |
| 801 | } |
| 802 | |
| 803 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 804 | if (ident == derived[u]) { |
| 805 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 806 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 807 | goto cleanup; |
| 808 | } |
| 809 | ly_set_add(&recursion, derived[u], 0); |
| 810 | } |
| 811 | |
| 812 | for (v = 0; v < recursion.count; ++v) { |
| 813 | drv = recursion.objs[v]; |
| 814 | if (!drv->derived) { |
| 815 | continue; |
| 816 | } |
| 817 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 818 | if (ident == drv->derived[u]) { |
| 819 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 820 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 821 | goto cleanup; |
| 822 | } |
| 823 | ly_set_add(&recursion, drv->derived[u], 0); |
| 824 | } |
| 825 | } |
| 826 | ret = LY_SUCCESS; |
| 827 | |
| 828 | cleanup: |
| 829 | ly_set_erase(&recursion, NULL); |
| 830 | return ret; |
| 831 | } |
| 832 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 833 | /** |
| 834 | * @brief Find and process the referenced base identities from another identity or identityref |
| 835 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 836 | * 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] | 837 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 838 | * to distinguish these two use cases. |
| 839 | * |
| 840 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 841 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 842 | * @param[in] ident Referencing identity to work with. |
| 843 | * @param[in] bases Array of bases of identityref to fill in. |
| 844 | * @return LY_ERR value. |
| 845 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 846 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 847 | 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] | 848 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 849 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 850 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 851 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 852 | struct lysc_ident **idref; |
| 853 | |
| 854 | assert(ident || bases); |
| 855 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 856 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 857 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 858 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 859 | return LY_EVALID; |
| 860 | } |
| 861 | |
| 862 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 863 | s = strchr(bases_p[u], ':'); |
| 864 | if (s) { |
| 865 | /* prefixed identity */ |
| 866 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 867 | 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] | 868 | } else { |
| 869 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 870 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 871 | } |
| 872 | if (!mod) { |
| 873 | if (ident) { |
| 874 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 875 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 876 | } else { |
| 877 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 878 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 879 | } |
| 880 | return LY_EVALID; |
| 881 | } |
| 882 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 883 | if (mod->compiled && mod->compiled->identities) { |
| 884 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 885 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 886 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 887 | if (ident == &mod->compiled->identities[v]) { |
| 888 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 889 | "Identity \"%s\" is derived from itself.", ident->name); |
| 890 | return LY_EVALID; |
| 891 | } |
| 892 | 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] | 893 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 894 | 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] | 895 | *idref = ident; |
| 896 | } else { |
| 897 | /* we have match! store the found identity */ |
| 898 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 899 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 900 | } |
| 901 | break; |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | if (!idref || !(*idref)) { |
| 906 | if (ident) { |
| 907 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 908 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 909 | } else { |
| 910 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 911 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 912 | } |
| 913 | return LY_EVALID; |
| 914 | } |
| 915 | } |
| 916 | return LY_SUCCESS; |
| 917 | } |
| 918 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 919 | /** |
| 920 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 921 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 922 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 923 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 924 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 925 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 926 | static LY_ERR |
| 927 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 928 | { |
| 929 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 930 | |
| 931 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 932 | if (!idents_p[i].bases) { |
| 933 | continue; |
| 934 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 935 | lysc_update_path(ctx, NULL, idents[i].name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 936 | 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] | 937 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 938 | } |
| 939 | return LY_SUCCESS; |
| 940 | } |
| 941 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 942 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 943 | 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] | 944 | { |
| 945 | unsigned int offset = 0, u; |
| 946 | struct lysc_ctx context = {0}; |
| 947 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 948 | assert(ctx_sc || ctx); |
| 949 | |
| 950 | if (!ctx_sc) { |
| 951 | context.ctx = ctx; |
| 952 | context.mod = module; |
| 953 | context.path_len = 1; |
| 954 | context.path[0] = '/'; |
| 955 | ctx_sc = &context; |
| 956 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 957 | |
| 958 | if (!features_p) { |
| 959 | return LY_SUCCESS; |
| 960 | } |
| 961 | if (*features) { |
| 962 | offset = LY_ARRAY_SIZE(*features); |
| 963 | } |
| 964 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 965 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 966 | 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] | 967 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 968 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 969 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 970 | LY_ARRAY_INCREMENT(*features); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 971 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 972 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 973 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 974 | 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] | 975 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 976 | (*features)[offset + u].module = ctx_sc->mod; |
| 977 | |
| 978 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 979 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 980 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 981 | |
| 982 | return LY_SUCCESS; |
| 983 | } |
| 984 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 985 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 986 | * @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] | 987 | * |
| 988 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 989 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 990 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 991 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 992 | * being currently processed). |
| 993 | * @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] | 994 | * @return LY_SUCCESS if everything is ok. |
| 995 | * @return LY_EVALID if the feature references indirectly itself. |
| 996 | */ |
| 997 | static LY_ERR |
| 998 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 999 | { |
| 1000 | LY_ERR ret = LY_EVALID; |
| 1001 | unsigned int u, v; |
| 1002 | struct ly_set recursion = {0}; |
| 1003 | struct lysc_feature *drv; |
| 1004 | |
| 1005 | if (!depfeatures) { |
| 1006 | return LY_SUCCESS; |
| 1007 | } |
| 1008 | |
| 1009 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1010 | if (feature == depfeatures[u]) { |
| 1011 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1012 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1013 | goto cleanup; |
| 1014 | } |
| 1015 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1016 | } |
| 1017 | |
| 1018 | for (v = 0; v < recursion.count; ++v) { |
| 1019 | drv = recursion.objs[v]; |
| 1020 | if (!drv->depfeatures) { |
| 1021 | continue; |
| 1022 | } |
| 1023 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1024 | if (feature == drv->depfeatures[u]) { |
| 1025 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1026 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1027 | goto cleanup; |
| 1028 | } |
| 1029 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1030 | } |
| 1031 | } |
| 1032 | ret = LY_SUCCESS; |
| 1033 | |
| 1034 | cleanup: |
| 1035 | ly_set_erase(&recursion, NULL); |
| 1036 | return ret; |
| 1037 | } |
| 1038 | |
| 1039 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1040 | * @brief Create pre-compiled features array. |
| 1041 | * |
| 1042 | * See lys_feature_precompile() for more details. |
| 1043 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1044 | * @param[in] ctx Compile context. |
| 1045 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1046 | * @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] | 1047 | * @return LY_ERR value. |
| 1048 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1049 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1050 | 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] | 1051 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1052 | unsigned int u, v, x; |
| 1053 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1054 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1055 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1056 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1057 | /* find the preprecompiled feature */ |
| 1058 | LY_ARRAY_FOR(features, x) { |
| 1059 | if (strcmp(features[x].name, feature_p->name)) { |
| 1060 | continue; |
| 1061 | } |
| 1062 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1063 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1064 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1065 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1066 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1067 | COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, u, lys_compile_ext, ret, done); |
| 1068 | 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] | 1069 | if (feature->iffeatures) { |
| 1070 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1071 | if (feature->iffeatures[u].features) { |
| 1072 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1073 | /* check for circular dependency - direct reference first,... */ |
| 1074 | if (feature == feature->iffeatures[u].features[v]) { |
| 1075 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1076 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1077 | return LY_EVALID; |
| 1078 | } |
| 1079 | /* ... and indirect circular reference */ |
| 1080 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1081 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1082 | /* add itself into the dependants list */ |
| 1083 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1084 | *df = feature; |
| 1085 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1086 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1087 | } |
| 1088 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1089 | lysc_update_path(ctx, NULL, NULL); |
| 1090 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1091 | done: |
| 1092 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1093 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1094 | |
| 1095 | LOGINT(ctx->ctx); |
| 1096 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1099 | /** |
| 1100 | * @brief Revert compiled list of features back to the precompiled state. |
| 1101 | * |
| 1102 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1103 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1104 | * |
| 1105 | * @param[in] ctx Compilation context. |
| 1106 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1107 | * and supposed to hold the off_features list. |
| 1108 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1109 | static void |
| 1110 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1111 | { |
| 1112 | unsigned int u, v; |
| 1113 | |
| 1114 | /* keep the off_features list until the complete lys_module is freed */ |
| 1115 | mod->off_features = mod->compiled->features; |
| 1116 | mod->compiled->features = NULL; |
| 1117 | |
| 1118 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1119 | * which may points into the data being freed here */ |
| 1120 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1121 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1122 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1123 | } |
| 1124 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1125 | mod->off_features[u].iffeatures = NULL; |
| 1126 | |
| 1127 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1128 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1129 | } |
| 1130 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1131 | mod->off_features[u].exts = NULL; |
| 1132 | } |
| 1133 | } |
| 1134 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1135 | /** |
| 1136 | * @brief Validate and normalize numeric value from a range definition. |
| 1137 | * @param[in] ctx Compile context. |
| 1138 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1139 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1140 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1141 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1142 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1143 | * @param[in] value String value of the range boundary. |
| 1144 | * @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. |
| 1145 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1146 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1147 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1148 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1149 | 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] | 1150 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1151 | size_t fraction = 0, size; |
| 1152 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1153 | *len = 0; |
| 1154 | |
| 1155 | assert(value); |
| 1156 | /* parse value */ |
| 1157 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1158 | return LY_EVALID; |
| 1159 | } |
| 1160 | |
| 1161 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1162 | ++(*len); |
| 1163 | } |
| 1164 | |
| 1165 | while (isdigit(value[*len])) { |
| 1166 | ++(*len); |
| 1167 | } |
| 1168 | |
| 1169 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1170 | if (basetype == LY_TYPE_DEC64) { |
| 1171 | goto decimal; |
| 1172 | } else { |
| 1173 | *valcopy = strndup(value, *len); |
| 1174 | return LY_SUCCESS; |
| 1175 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1176 | } |
| 1177 | fraction = *len; |
| 1178 | |
| 1179 | ++(*len); |
| 1180 | while (isdigit(value[*len])) { |
| 1181 | ++(*len); |
| 1182 | } |
| 1183 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1184 | if (basetype == LY_TYPE_DEC64) { |
| 1185 | decimal: |
| 1186 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1187 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1188 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1189 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1190 | *len, value, frdigits); |
| 1191 | return LY_EINVAL; |
| 1192 | } |
| 1193 | if (fraction) { |
| 1194 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1195 | } else { |
| 1196 | size = (*len) + frdigits + 1; |
| 1197 | } |
| 1198 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1199 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1200 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1201 | (*valcopy)[size - 1] = '\0'; |
| 1202 | if (fraction) { |
| 1203 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1204 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1205 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1206 | } else { |
| 1207 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1208 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1209 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1210 | } |
| 1211 | return LY_SUCCESS; |
| 1212 | } |
| 1213 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1214 | /** |
| 1215 | * @brief Check that values in range are in ascendant order. |
| 1216 | * @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] | 1217 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1218 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1219 | * @param[in] value Current value to check. |
| 1220 | * @param[in] prev_value The last seen value. |
| 1221 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1222 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1223 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1224 | 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] | 1225 | { |
| 1226 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1227 | 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] | 1228 | return LY_EEXIST; |
| 1229 | } |
| 1230 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1231 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1232 | return LY_EEXIST; |
| 1233 | } |
| 1234 | } |
| 1235 | return LY_SUCCESS; |
| 1236 | } |
| 1237 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1238 | /** |
| 1239 | * @brief Set min/max value of the range part. |
| 1240 | * @param[in] ctx Compile context. |
| 1241 | * @param[in] part Range part structure to fill. |
| 1242 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1243 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1244 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1245 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1246 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1247 | * @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] | 1248 | * @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] | 1249 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1250 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1251 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1252 | * frdigits value), LY_EMEM. |
| 1253 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1254 | static LY_ERR |
| 1255 | 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] | 1256 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1257 | { |
| 1258 | LY_ERR ret = LY_SUCCESS; |
| 1259 | char *valcopy = NULL; |
| 1260 | size_t len; |
| 1261 | |
| 1262 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1263 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1264 | LY_CHECK_GOTO(ret, finalize); |
| 1265 | } |
| 1266 | if (!valcopy && base_range) { |
| 1267 | if (max) { |
| 1268 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1269 | } else { |
| 1270 | part->min_64 = base_range->parts[0].min_64; |
| 1271 | } |
| 1272 | if (!first) { |
| 1273 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1274 | } |
| 1275 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1279 | case LY_TYPE_INT8: /* range */ |
| 1280 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1281 | 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] | 1282 | } else if (max) { |
| 1283 | part->max_64 = INT64_C(127); |
| 1284 | } else { |
| 1285 | part->min_64 = INT64_C(-128); |
| 1286 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1287 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1288 | 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] | 1289 | } |
| 1290 | break; |
| 1291 | case LY_TYPE_INT16: /* range */ |
| 1292 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1293 | 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] | 1294 | } else if (max) { |
| 1295 | part->max_64 = INT64_C(32767); |
| 1296 | } else { |
| 1297 | part->min_64 = INT64_C(-32768); |
| 1298 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1299 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1300 | 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] | 1301 | } |
| 1302 | break; |
| 1303 | case LY_TYPE_INT32: /* range */ |
| 1304 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1305 | 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] | 1306 | } else if (max) { |
| 1307 | part->max_64 = INT64_C(2147483647); |
| 1308 | } else { |
| 1309 | part->min_64 = INT64_C(-2147483648); |
| 1310 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1311 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1312 | 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] | 1313 | } |
| 1314 | break; |
| 1315 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1316 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1317 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1318 | 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] | 1319 | max ? &part->max_64 : &part->min_64); |
| 1320 | } else if (max) { |
| 1321 | part->max_64 = INT64_C(9223372036854775807); |
| 1322 | } else { |
| 1323 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1324 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1325 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1326 | 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] | 1327 | } |
| 1328 | break; |
| 1329 | case LY_TYPE_UINT8: /* range */ |
| 1330 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1331 | 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] | 1332 | } else if (max) { |
| 1333 | part->max_u64 = UINT64_C(255); |
| 1334 | } else { |
| 1335 | part->min_u64 = UINT64_C(0); |
| 1336 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1337 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1338 | 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] | 1339 | } |
| 1340 | break; |
| 1341 | case LY_TYPE_UINT16: /* range */ |
| 1342 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1343 | 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] | 1344 | } else if (max) { |
| 1345 | part->max_u64 = UINT64_C(65535); |
| 1346 | } else { |
| 1347 | part->min_u64 = UINT64_C(0); |
| 1348 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1349 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1350 | 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] | 1351 | } |
| 1352 | break; |
| 1353 | case LY_TYPE_UINT32: /* range */ |
| 1354 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1355 | 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] | 1356 | } else if (max) { |
| 1357 | part->max_u64 = UINT64_C(4294967295); |
| 1358 | } else { |
| 1359 | part->min_u64 = UINT64_C(0); |
| 1360 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1361 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1362 | 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] | 1363 | } |
| 1364 | break; |
| 1365 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1366 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1367 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1368 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1369 | 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] | 1370 | } else if (max) { |
| 1371 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1372 | } else { |
| 1373 | part->min_u64 = UINT64_C(0); |
| 1374 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1375 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1376 | 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] | 1377 | } |
| 1378 | break; |
| 1379 | default: |
| 1380 | LOGINT(ctx->ctx); |
| 1381 | ret = LY_EINT; |
| 1382 | } |
| 1383 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1384 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1385 | if (ret == LY_EDENIED) { |
| 1386 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1387 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1388 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1389 | } else if (ret == LY_EVALID) { |
| 1390 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1391 | "Invalid %s restriction - invalid value \"%s\".", |
| 1392 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1393 | } else if (ret == LY_EEXIST) { |
| 1394 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1395 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1396 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1397 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1398 | } else if (!ret && value) { |
| 1399 | *value = *value + len; |
| 1400 | } |
| 1401 | free(valcopy); |
| 1402 | return ret; |
| 1403 | } |
| 1404 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1405 | /** |
| 1406 | * @brief Compile the parsed range restriction. |
| 1407 | * @param[in] ctx Compile context. |
| 1408 | * @param[in] range_p Parsed range structure to compile. |
| 1409 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1410 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1411 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1412 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1413 | * range restriction must be more restrictive than the base_range. |
| 1414 | * @param[in,out] range Pointer to the created current range structure. |
| 1415 | * @return LY_ERR value. |
| 1416 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1417 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1418 | 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] | 1419 | struct lysc_range *base_range, struct lysc_range **range) |
| 1420 | { |
| 1421 | LY_ERR ret = LY_EVALID; |
| 1422 | const char *expr; |
| 1423 | struct lysc_range_part *parts = NULL, *part; |
| 1424 | int range_expected = 0, uns; |
| 1425 | unsigned int parts_done = 0, u, v; |
| 1426 | |
| 1427 | assert(range); |
| 1428 | assert(range_p); |
| 1429 | |
| 1430 | expr = range_p->arg; |
| 1431 | while(1) { |
| 1432 | if (isspace(*expr)) { |
| 1433 | ++expr; |
| 1434 | } else if (*expr == '\0') { |
| 1435 | if (range_expected) { |
| 1436 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1437 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1438 | length_restr ? "length" : "range", range_p->arg); |
| 1439 | goto cleanup; |
| 1440 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1441 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1442 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1443 | length_restr ? "length" : "range", range_p->arg); |
| 1444 | goto cleanup; |
| 1445 | } |
| 1446 | parts_done++; |
| 1447 | break; |
| 1448 | } else if (!strncmp(expr, "min", 3)) { |
| 1449 | if (parts) { |
| 1450 | /* min cannot be used elsewhere than in the first part */ |
| 1451 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1452 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1453 | expr - range_p->arg, range_p->arg); |
| 1454 | goto cleanup; |
| 1455 | } |
| 1456 | expr += 3; |
| 1457 | |
| 1458 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1459 | 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] | 1460 | part->max_64 = part->min_64; |
| 1461 | } else if (*expr == '|') { |
| 1462 | if (!parts || range_expected) { |
| 1463 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1464 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1465 | goto cleanup; |
| 1466 | } |
| 1467 | expr++; |
| 1468 | parts_done++; |
| 1469 | /* process next part of the expression */ |
| 1470 | } else if (!strncmp(expr, "..", 2)) { |
| 1471 | expr += 2; |
| 1472 | while (isspace(*expr)) { |
| 1473 | expr++; |
| 1474 | } |
| 1475 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1476 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1477 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1478 | goto cleanup; |
| 1479 | } |
| 1480 | /* continue expecting the upper boundary */ |
| 1481 | range_expected = 1; |
| 1482 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1483 | /* number */ |
| 1484 | if (range_expected) { |
| 1485 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1486 | 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] | 1487 | range_expected = 0; |
| 1488 | } else { |
| 1489 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1490 | 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] | 1491 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1492 | part->max_64 = part->min_64; |
| 1493 | } |
| 1494 | |
| 1495 | /* continue with possible another expression part */ |
| 1496 | } else if (!strncmp(expr, "max", 3)) { |
| 1497 | expr += 3; |
| 1498 | while (isspace(*expr)) { |
| 1499 | expr++; |
| 1500 | } |
| 1501 | if (*expr != '\0') { |
| 1502 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1503 | length_restr ? "length" : "range", expr); |
| 1504 | goto cleanup; |
| 1505 | } |
| 1506 | if (range_expected) { |
| 1507 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1508 | 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] | 1509 | range_expected = 0; |
| 1510 | } else { |
| 1511 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1512 | 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] | 1513 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1514 | part->min_64 = part->max_64; |
| 1515 | } |
| 1516 | } else { |
| 1517 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1518 | length_restr ? "length" : "range", expr); |
| 1519 | goto cleanup; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /* check with the previous range/length restriction */ |
| 1524 | if (base_range) { |
| 1525 | switch (basetype) { |
| 1526 | case LY_TYPE_BINARY: |
| 1527 | case LY_TYPE_UINT8: |
| 1528 | case LY_TYPE_UINT16: |
| 1529 | case LY_TYPE_UINT32: |
| 1530 | case LY_TYPE_UINT64: |
| 1531 | case LY_TYPE_STRING: |
| 1532 | uns = 1; |
| 1533 | break; |
| 1534 | case LY_TYPE_DEC64: |
| 1535 | case LY_TYPE_INT8: |
| 1536 | case LY_TYPE_INT16: |
| 1537 | case LY_TYPE_INT32: |
| 1538 | case LY_TYPE_INT64: |
| 1539 | uns = 0; |
| 1540 | break; |
| 1541 | default: |
| 1542 | LOGINT(ctx->ctx); |
| 1543 | ret = LY_EINT; |
| 1544 | goto cleanup; |
| 1545 | } |
| 1546 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1547 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1548 | goto baseerror; |
| 1549 | } |
| 1550 | /* current lower bound is not lower than the base */ |
| 1551 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1552 | /* base has single value */ |
| 1553 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1554 | /* both lower bounds are the same */ |
| 1555 | if (parts[u].min_64 != parts[u].max_64) { |
| 1556 | /* current continues with a range */ |
| 1557 | goto baseerror; |
| 1558 | } else { |
| 1559 | /* equal single values, move both forward */ |
| 1560 | ++v; |
| 1561 | continue; |
| 1562 | } |
| 1563 | } else { |
| 1564 | /* base is single value lower than current range, so the |
| 1565 | * value from base range is removed in the current, |
| 1566 | * move only base and repeat checking */ |
| 1567 | ++v; |
| 1568 | --u; |
| 1569 | continue; |
| 1570 | } |
| 1571 | } else { |
| 1572 | /* base is the range */ |
| 1573 | if (parts[u].min_64 == parts[u].max_64) { |
| 1574 | /* current is a single value */ |
| 1575 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1576 | /* current is behind the base range, so base range is omitted, |
| 1577 | * move the base and keep the current for further check */ |
| 1578 | ++v; |
| 1579 | --u; |
| 1580 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1581 | continue; |
| 1582 | } else { |
| 1583 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1584 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1585 | /* higher bound is higher than the current higher bound */ |
| 1586 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1587 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1588 | * continue with the same current, but move the base */ |
| 1589 | --u; |
| 1590 | ++v; |
| 1591 | continue; |
| 1592 | } |
| 1593 | /* current range starts within the base range but end behind it */ |
| 1594 | goto baseerror; |
| 1595 | } else { |
| 1596 | /* current range is smaller than the base, |
| 1597 | * move current, but stay with the base */ |
| 1598 | continue; |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | } |
| 1603 | if (u != parts_done) { |
| 1604 | baseerror: |
| 1605 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1606 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1607 | length_restr ? "length" : "range", range_p->arg); |
| 1608 | goto cleanup; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | if (!(*range)) { |
| 1613 | *range = calloc(1, sizeof **range); |
| 1614 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1615 | } |
| 1616 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1617 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1618 | if (range_p->eapptag) { |
| 1619 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1620 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1621 | } |
| 1622 | if (range_p->emsg) { |
| 1623 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1624 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1625 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1626 | if (range_p->dsc) { |
| 1627 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1628 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1629 | } |
| 1630 | if (range_p->ref) { |
| 1631 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1632 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1633 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1634 | /* extensions are taken only from the last range by the caller */ |
| 1635 | |
| 1636 | (*range)->parts = parts; |
| 1637 | parts = NULL; |
| 1638 | ret = LY_SUCCESS; |
| 1639 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1640 | LY_ARRAY_FREE(parts); |
| 1641 | |
| 1642 | return ret; |
| 1643 | } |
| 1644 | |
| 1645 | /** |
| 1646 | * @brief Checks pattern syntax. |
| 1647 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1648 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1649 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1650 | * @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] | 1651 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1652 | */ |
| 1653 | static LY_ERR |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1654 | 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] | 1655 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1656 | int idx, idx2, start, end, count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1657 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1658 | int err_code; |
| 1659 | const char *orig_ptr; |
| 1660 | PCRE2_SIZE err_offset; |
| 1661 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1662 | #define URANGE_LEN 19 |
| 1663 | char *ublock2urange[][2] = { |
| 1664 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1665 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1666 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1667 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1668 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1669 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1670 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1671 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1672 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1673 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1674 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1675 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1676 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1677 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1678 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1679 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1680 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1681 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1682 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1683 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1684 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1685 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1686 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1687 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1688 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1689 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1690 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1691 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1692 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1693 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1694 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1695 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1696 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1697 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1698 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1699 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1700 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1701 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1702 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1703 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1704 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1705 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1706 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1707 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1708 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1709 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1710 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1711 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1712 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1713 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1714 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1715 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1716 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1717 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1718 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1719 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1720 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1721 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1722 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1723 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1724 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1725 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1726 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1727 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1728 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1729 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1730 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1731 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1732 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1733 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1734 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1735 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1736 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1737 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1738 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1739 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1740 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1741 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1742 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1743 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1744 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1745 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1746 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1747 | {NULL, NULL} |
| 1748 | }; |
| 1749 | |
| 1750 | /* adjust the expression to a Perl equivalent |
| 1751 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1752 | |
| 1753 | /* we need to replace all "$" with "\$", count them now */ |
| 1754 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1755 | |
| 1756 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1757 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1758 | perl_regex[0] = '\0'; |
| 1759 | |
| 1760 | ptr = perl_regex; |
| 1761 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1762 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1763 | if (orig_ptr[0] == '$') { |
| 1764 | ptr += sprintf(ptr, "\\$"); |
| 1765 | } else if (orig_ptr[0] == '^') { |
| 1766 | ptr += sprintf(ptr, "\\^"); |
| 1767 | } else { |
| 1768 | ptr[0] = orig_ptr[0]; |
| 1769 | ++ptr; |
| 1770 | } |
| 1771 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1772 | ptr[0] = '\0'; |
| 1773 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1774 | |
| 1775 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1776 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1777 | start = ptr - perl_regex; |
| 1778 | |
| 1779 | ptr = strchr(ptr, '}'); |
| 1780 | if (!ptr) { |
| 1781 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1782 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1783 | free(perl_regex); |
| 1784 | return LY_EVALID; |
| 1785 | } |
| 1786 | end = (ptr - perl_regex) + 1; |
| 1787 | |
| 1788 | /* need more space */ |
| 1789 | if (end - start < URANGE_LEN) { |
| 1790 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1791 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1792 | } |
| 1793 | |
| 1794 | /* find our range */ |
| 1795 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1796 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1797 | break; |
| 1798 | } |
| 1799 | } |
| 1800 | if (!ublock2urange[idx][0]) { |
| 1801 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1802 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1803 | free(perl_regex); |
| 1804 | return LY_EVALID; |
| 1805 | } |
| 1806 | |
| 1807 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1808 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1809 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1810 | ++count; |
| 1811 | } |
| 1812 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1813 | --count; |
| 1814 | } |
| 1815 | } |
| 1816 | if (count) { |
| 1817 | /* skip brackets */ |
| 1818 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1819 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1820 | } else { |
| 1821 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1822 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1827 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 1828 | 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] | 1829 | &err_code, &err_offset, NULL); |
| 1830 | if (!code_local) { |
| 1831 | PCRE2_UCHAR err_msg[256] = {0}; |
| 1832 | pcre2_get_error_message(err_code, err_msg, 256); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1833 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1834 | free(perl_regex); |
| 1835 | return LY_EVALID; |
| 1836 | } |
| 1837 | free(perl_regex); |
| 1838 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1839 | if (code) { |
| 1840 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1841 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1842 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | return LY_SUCCESS; |
| 1846 | |
| 1847 | #undef URANGE_LEN |
| 1848 | } |
| 1849 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1850 | /** |
| 1851 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1852 | * @param[in] ctx Compile context. |
| 1853 | * @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] | 1854 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1855 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1856 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1857 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1858 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1859 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1860 | 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] | 1861 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1862 | { |
| 1863 | struct lysc_pattern **pattern; |
| 1864 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1865 | LY_ERR ret = LY_SUCCESS; |
| 1866 | |
| 1867 | /* first, copy the patterns from the base type */ |
| 1868 | if (base_patterns) { |
| 1869 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1870 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1871 | } |
| 1872 | |
| 1873 | LY_ARRAY_FOR(patterns_p, u) { |
| 1874 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1875 | *pattern = calloc(1, sizeof **pattern); |
| 1876 | ++(*pattern)->refcount; |
| 1877 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1878 | 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] | 1879 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1880 | |
| 1881 | if (patterns_p[u].arg[0] == 0x15) { |
| 1882 | (*pattern)->inverted = 1; |
| 1883 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1884 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1885 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1886 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1887 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 1888 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1889 | 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] | 1890 | } |
| 1891 | done: |
| 1892 | return ret; |
| 1893 | } |
| 1894 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1895 | /** |
| 1896 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1897 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1898 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1899 | 0 /* LY_TYPE_UNKNOWN */, |
| 1900 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1901 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1902 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1903 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1904 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 1905 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1906 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1907 | 0 /* LY_TYPE_BOOL */, |
| 1908 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 1909 | 0 /* LY_TYPE_EMPTY */, |
| 1910 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 1911 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 1912 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 1913 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1914 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 1915 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1916 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1917 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1918 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 1919 | }; |
| 1920 | |
| 1921 | /** |
| 1922 | * @brief stringification of the YANG built-in data types |
| 1923 | */ |
| 1924 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 1925 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 1926 | "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] | 1927 | }; |
| 1928 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1929 | /** |
| 1930 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 1931 | * @param[in] ctx Compile context. |
| 1932 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 1933 | * @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] | 1934 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 1935 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 1936 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1937 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1938 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1939 | 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] | 1940 | 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] | 1941 | { |
| 1942 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 1943 | unsigned int u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1944 | int32_t value = 0; |
| 1945 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1946 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1947 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1948 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1949 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 1950 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 1951 | return LY_EVALID; |
| 1952 | } |
| 1953 | |
| 1954 | LY_ARRAY_FOR(enums_p, u) { |
| 1955 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 1956 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1957 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 1958 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1959 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1960 | if (base_enums) { |
| 1961 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 1962 | LY_ARRAY_FOR(base_enums, v) { |
| 1963 | if (!strcmp(e->name, base_enums[v].name)) { |
| 1964 | break; |
| 1965 | } |
| 1966 | } |
| 1967 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 1968 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1969 | "Invalid %s - derived type adds new item \"%s\".", |
| 1970 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 1971 | return LY_EVALID; |
| 1972 | } |
| 1973 | match = v; |
| 1974 | } |
| 1975 | |
| 1976 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1977 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1978 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1979 | e->value = (int32_t)enums_p[u].value; |
| 1980 | if (!u || e->value >= value) { |
| 1981 | value = e->value + 1; |
| 1982 | } |
| 1983 | /* check collision with other values */ |
| 1984 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1985 | if (e->value == (*enums)[v].value) { |
| 1986 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1987 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 1988 | e->value, e->name, (*enums)[v].name); |
| 1989 | return LY_EVALID; |
| 1990 | } |
| 1991 | } |
| 1992 | } else if (base_enums) { |
| 1993 | /* inherit the assigned value */ |
| 1994 | e->value = base_enums[match].value; |
| 1995 | if (!u || e->value >= value) { |
| 1996 | value = e->value + 1; |
| 1997 | } |
| 1998 | } else { |
| 1999 | /* assign value automatically */ |
| 2000 | if (u && value == INT32_MIN) { |
| 2001 | /* counter overflow */ |
| 2002 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2003 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2004 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2005 | return LY_EVALID; |
| 2006 | } |
| 2007 | e->value = value++; |
| 2008 | } |
| 2009 | } else { /* LY_TYPE_BITS */ |
| 2010 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2011 | e->value = (int32_t)enums_p[u].value; |
| 2012 | if (!u || (uint32_t)e->value >= position) { |
| 2013 | position = (uint32_t)e->value + 1; |
| 2014 | } |
| 2015 | /* check collision with other values */ |
| 2016 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2017 | if (e->value == (*enums)[v].value) { |
| 2018 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2019 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2020 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2021 | return LY_EVALID; |
| 2022 | } |
| 2023 | } |
| 2024 | } else if (base_enums) { |
| 2025 | /* inherit the assigned value */ |
| 2026 | e->value = base_enums[match].value; |
| 2027 | if (!u || (uint32_t)e->value >= position) { |
| 2028 | position = (uint32_t)e->value + 1; |
| 2029 | } |
| 2030 | } else { |
| 2031 | /* assign value automatically */ |
| 2032 | if (u && position == 0) { |
| 2033 | /* counter overflow */ |
| 2034 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2035 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2036 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2037 | return LY_EVALID; |
| 2038 | } |
| 2039 | e->value = position++; |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | if (base_enums) { |
| 2044 | /* the assigned values must not change from the derived type */ |
| 2045 | if (e->value != base_enums[match].value) { |
| 2046 | if (basetype == LY_TYPE_ENUM) { |
| 2047 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2048 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2049 | e->name, base_enums[match].value, e->value); |
| 2050 | } else { |
| 2051 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2052 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2053 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2054 | } |
| 2055 | return LY_EVALID; |
| 2056 | } |
| 2057 | } |
| 2058 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2059 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done); |
| 2060 | 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] | 2061 | |
| 2062 | if (basetype == LY_TYPE_BITS) { |
| 2063 | /* keep bits ordered by position */ |
| 2064 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2065 | if (v != u) { |
| 2066 | memcpy(&storage, e, sizeof *e); |
| 2067 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2068 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2069 | } |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | done: |
| 2074 | return ret; |
| 2075 | } |
| 2076 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2077 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2078 | for ((NODE) = (NODE)->parent; \ |
| 2079 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 2080 | (NODE) = (NODE)->parent); \ |
| 2081 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2082 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2083 | TERM; \ |
| 2084 | } |
| 2085 | |
| 2086 | /** |
| 2087 | * @brief Validate the predicate(s) from the leafref path. |
| 2088 | * @param[in] ctx Compile context |
| 2089 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2090 | * 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] | 2091 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2092 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2093 | * @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] | 2094 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2095 | */ |
| 2096 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2097 | 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] | 2098 | 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] | 2099 | { |
| 2100 | LY_ERR ret = LY_EVALID; |
| 2101 | const struct lys_module *mod; |
| 2102 | const struct lysc_node *src_node, *dst_node; |
| 2103 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2104 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2105 | unsigned int dest_parent_times, c, u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2106 | const char *start, *end, *pke_end; |
| 2107 | struct ly_set keys = {0}; |
| 2108 | int i; |
| 2109 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2110 | assert(path_context); |
| 2111 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2112 | while (**predicate == '[') { |
| 2113 | start = (*predicate)++; |
| 2114 | |
| 2115 | while (isspace(**predicate)) { |
| 2116 | ++(*predicate); |
| 2117 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2118 | 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] | 2119 | while (isspace(**predicate)) { |
| 2120 | ++(*predicate); |
| 2121 | } |
| 2122 | if (**predicate != '=') { |
| 2123 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2124 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2125 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2126 | goto cleanup; |
| 2127 | } |
| 2128 | ++(*predicate); |
| 2129 | while (isspace(**predicate)) { |
| 2130 | ++(*predicate); |
| 2131 | } |
| 2132 | |
| 2133 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2134 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2135 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2136 | goto cleanup; |
| 2137 | } |
| 2138 | --pke_end; |
| 2139 | while (isspace(*pke_end)) { |
| 2140 | --pke_end; |
| 2141 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2142 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2143 | /* localize path-key-expr */ |
| 2144 | pke_start = path_key_expr = *predicate; |
| 2145 | /* move after the current predicate */ |
| 2146 | *predicate = end + 1; |
| 2147 | |
| 2148 | /* source (must be leaf or leaf-list) */ |
| 2149 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2150 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2151 | if (!mod) { |
| 2152 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2153 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2154 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2155 | goto cleanup; |
| 2156 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2157 | if (!mod->implemented) { |
| 2158 | /* make the module implemented */ |
| 2159 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2160 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2161 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2162 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2163 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2164 | src_node = NULL; |
| 2165 | if (context_node->keys) { |
| 2166 | for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) { |
| 2167 | if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') { |
| 2168 | src_node = (const struct lysc_node*)context_node->keys[u]; |
| 2169 | break; |
| 2170 | } |
| 2171 | } |
| 2172 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2173 | if (!src_node) { |
| 2174 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2175 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2176 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2177 | goto cleanup; |
| 2178 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2179 | |
| 2180 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2181 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2182 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2183 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2184 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2185 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2186 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2187 | *predicate - start, start, src_node->name); |
| 2188 | goto cleanup; |
| 2189 | } |
| 2190 | |
| 2191 | /* destination */ |
| 2192 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2193 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2194 | |
| 2195 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2196 | if (strncmp(path_key_expr, "current", 7)) { |
| 2197 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2198 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2199 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2200 | *predicate - start, start); |
| 2201 | goto cleanup; |
| 2202 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2203 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2204 | if (*path_key_expr != '(') { |
| 2205 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2206 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2207 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2208 | if (*path_key_expr != ')') { |
| 2209 | goto error_current_function_invocation; |
| 2210 | } |
| 2211 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2212 | |
| 2213 | if (*path_key_expr != '/') { |
| 2214 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2215 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2216 | *predicate - start, start); |
| 2217 | goto cleanup; |
| 2218 | } |
| 2219 | ++path_key_expr; |
| 2220 | while (isspace(*path_key_expr)) { |
| 2221 | ++path_key_expr; |
| 2222 | } |
| 2223 | |
| 2224 | /* rel-path-keyexpr: |
| 2225 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2226 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2227 | ++dest_parent_times; |
| 2228 | path_key_expr += 2; |
| 2229 | while (isspace(*path_key_expr)) { |
| 2230 | ++path_key_expr; |
| 2231 | } |
| 2232 | if (*path_key_expr != '/') { |
| 2233 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2234 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2235 | *predicate - start, start); |
| 2236 | goto cleanup; |
| 2237 | } |
| 2238 | ++path_key_expr; |
| 2239 | while (isspace(*path_key_expr)) { |
| 2240 | ++path_key_expr; |
| 2241 | } |
| 2242 | |
| 2243 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2244 | * all schema nodes that cannot be instantiated in data tree */ |
| 2245 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2246 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2247 | *predicate - start, start); |
| 2248 | } |
| 2249 | if (!dest_parent_times) { |
| 2250 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2251 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2252 | *predicate - start, start); |
| 2253 | goto cleanup; |
| 2254 | } |
| 2255 | if (path_key_expr == pke_end) { |
| 2256 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2257 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2258 | *predicate - start, start); |
| 2259 | goto cleanup; |
| 2260 | } |
| 2261 | |
| 2262 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2263 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2264 | 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] | 2265 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2266 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2267 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2268 | goto cleanup; |
| 2269 | } |
| 2270 | |
| 2271 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2272 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2273 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2274 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2275 | } |
| 2276 | if (!mod) { |
| 2277 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2278 | "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] | 2279 | *predicate - start, start, dst_len, dst); |
| 2280 | goto cleanup; |
| 2281 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2282 | if (!mod->implemented) { |
| 2283 | /* make the module implemented */ |
| 2284 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2285 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2286 | |
| 2287 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2288 | if (!dst_node) { |
| 2289 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2290 | "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] | 2291 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2292 | goto cleanup; |
| 2293 | } |
| 2294 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2295 | 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] | 2296 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2297 | "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] | 2298 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2299 | goto cleanup; |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | ret = LY_SUCCESS; |
| 2304 | cleanup: |
| 2305 | ly_set_erase(&keys, NULL); |
| 2306 | return ret; |
| 2307 | } |
| 2308 | |
| 2309 | /** |
| 2310 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2311 | * |
| 2312 | * path-arg = absolute-path / relative-path |
| 2313 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2314 | * relative-path = 1*(".." "/") descendant-path |
| 2315 | * |
| 2316 | * @param[in,out] path Path to parse. |
| 2317 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2318 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2319 | * @param[out] name Name of the token. |
| 2320 | * @param[out] nam_len Length of the name. |
| 2321 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2322 | * must not be changed between consecutive calls. -1 if the |
| 2323 | * path is absolute. |
| 2324 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2325 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2326 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2327 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2328 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2329 | int *parent_times, int *has_predicate) |
| 2330 | { |
| 2331 | int par_times = 0; |
| 2332 | |
| 2333 | assert(path && *path); |
| 2334 | assert(parent_times); |
| 2335 | assert(prefix); |
| 2336 | assert(prefix_len); |
| 2337 | assert(name); |
| 2338 | assert(name_len); |
| 2339 | assert(has_predicate); |
| 2340 | |
| 2341 | *prefix = NULL; |
| 2342 | *prefix_len = 0; |
| 2343 | *name = NULL; |
| 2344 | *name_len = 0; |
| 2345 | *has_predicate = 0; |
| 2346 | |
| 2347 | if (!*parent_times) { |
| 2348 | if (!strncmp(*path, "..", 2)) { |
| 2349 | *path += 2; |
| 2350 | ++par_times; |
| 2351 | while (!strncmp(*path, "/..", 3)) { |
| 2352 | *path += 3; |
| 2353 | ++par_times; |
| 2354 | } |
| 2355 | } |
| 2356 | if (par_times) { |
| 2357 | *parent_times = par_times; |
| 2358 | } else { |
| 2359 | *parent_times = -1; |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | if (**path != '/') { |
| 2364 | return LY_EINVAL; |
| 2365 | } |
| 2366 | /* skip '/' */ |
| 2367 | ++(*path); |
| 2368 | |
| 2369 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2370 | 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] | 2371 | |
| 2372 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2373 | /* path continues by another token or this is the last token */ |
| 2374 | return LY_SUCCESS; |
| 2375 | } else if ((*path)[0] != '[') { |
| 2376 | /* unexpected character */ |
| 2377 | return LY_EINVAL; |
| 2378 | } else { |
| 2379 | /* predicate starting with [ */ |
| 2380 | *has_predicate = 1; |
| 2381 | return LY_SUCCESS; |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2386 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2387 | * |
| 2388 | * The set of features used for target must be a subset of features used for the leafref. |
| 2389 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2390 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2391 | * |
| 2392 | * @param[in] refnode The leafref node. |
| 2393 | * @param[in] target Tha target node of the leafref. |
| 2394 | * @return LY_SUCCESS or LY_EVALID; |
| 2395 | */ |
| 2396 | static LY_ERR |
| 2397 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2398 | { |
| 2399 | LY_ERR ret = LY_EVALID; |
| 2400 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2401 | unsigned int u, v, count; |
| 2402 | struct ly_set features = {0}; |
| 2403 | |
| 2404 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2405 | if (iter->iffeatures) { |
| 2406 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2407 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2408 | 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] | 2409 | } |
| 2410 | } |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2415 | * So when adding features applicable to the target into the features set, we should not be |
| 2416 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2417 | * to the leafref itself. */ |
| 2418 | count = features.count; |
| 2419 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2420 | if (iter->iffeatures) { |
| 2421 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2422 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2423 | 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] | 2424 | /* new feature was added (or LY_EMEM) */ |
| 2425 | goto cleanup; |
| 2426 | } |
| 2427 | } |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | ret = LY_SUCCESS; |
| 2432 | |
| 2433 | cleanup: |
| 2434 | ly_set_erase(&features, NULL); |
| 2435 | return ret; |
| 2436 | } |
| 2437 | |
| 2438 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2439 | * @brief Validate the leafref path. |
| 2440 | * @param[in] ctx Compile context |
| 2441 | * @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] | 2442 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2443 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2444 | */ |
| 2445 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2446 | 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] | 2447 | { |
| 2448 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2449 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2450 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2451 | const char *id, *prefix, *name; |
| 2452 | size_t prefix_len, name_len; |
| 2453 | int parent_times = 0, has_predicate; |
| 2454 | unsigned int iter, u; |
| 2455 | LY_ERR ret = LY_SUCCESS; |
| 2456 | |
| 2457 | assert(ctx); |
| 2458 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2459 | assert(leafref); |
| 2460 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2461 | ctx->path[0] = '\0'; |
| 2462 | lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 2463 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2464 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2465 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2466 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2467 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2468 | if (!iter) { /* first iteration */ |
| 2469 | /* precess ".." in relative paths */ |
| 2470 | if (parent_times > 0) { |
| 2471 | /* move from the context node */ |
| 2472 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2473 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2474 | * all schema nodes that cannot be instantiated in data tree */ |
| 2475 | 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] | 2476 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2477 | } |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2482 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2483 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2484 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2485 | } |
| 2486 | if (!mod) { |
| 2487 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2488 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2489 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2490 | return LY_EVALID; |
| 2491 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2492 | if (!mod->implemented) { |
| 2493 | /* make the module implemented */ |
| 2494 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2495 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2496 | |
| 2497 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2498 | if (!node) { |
| 2499 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2500 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2501 | return LY_EVALID; |
| 2502 | } |
| 2503 | parent = node; |
| 2504 | |
| 2505 | if (has_predicate) { |
| 2506 | /* we have predicate, so the current result must be list */ |
| 2507 | if (node->nodetype != LYS_LIST) { |
| 2508 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2509 | "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] | 2510 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2511 | return LY_EVALID; |
| 2512 | } |
| 2513 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2514 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2515 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | ++iter; |
| 2519 | } |
| 2520 | if (ret) { |
| 2521 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2522 | "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] | 2523 | return LY_EVALID; |
| 2524 | } |
| 2525 | |
| 2526 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2527 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2528 | "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] | 2529 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2530 | return LY_EVALID; |
| 2531 | } |
| 2532 | |
| 2533 | /* check status */ |
| 2534 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2535 | return LY_EVALID; |
| 2536 | } |
| 2537 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2538 | /* check config */ |
| 2539 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2540 | if (node->flags & LYS_CONFIG_R) { |
| 2541 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2542 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2543 | leafref->path); |
| 2544 | return LY_EVALID; |
| 2545 | } |
| 2546 | } |
| 2547 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2548 | /* store the target's type and check for circular chain of leafrefs */ |
| 2549 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2550 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2551 | if (type == (struct lysc_type*)leafref) { |
| 2552 | /* circular chain detected */ |
| 2553 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2554 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2555 | return LY_EVALID; |
| 2556 | } |
| 2557 | } |
| 2558 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2559 | /* check if leafref and its target are under common if-features */ |
| 2560 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2561 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2562 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2563 | leafref->path); |
| 2564 | return LY_EVALID; |
| 2565 | } |
| 2566 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2567 | ctx->path_len = 1; |
| 2568 | ctx->path[1] = '\0'; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2569 | return LY_SUCCESS; |
| 2570 | } |
| 2571 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2572 | 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] | 2573 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2574 | /** |
| 2575 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2576 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2577 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2578 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2579 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2580 | * @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] | 2581 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2582 | * @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] | 2583 | * @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] | 2584 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2585 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2586 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2587 | * @return LY_ERR value. |
| 2588 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2589 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2590 | 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] | 2591 | 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] | 2592 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2593 | { |
| 2594 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2595 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2596 | struct lysc_type_bin *bin; |
| 2597 | struct lysc_type_num *num; |
| 2598 | struct lysc_type_str *str; |
| 2599 | struct lysc_type_bits *bits; |
| 2600 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2601 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2602 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2603 | struct lysc_type_union *un, *un_aux; |
| 2604 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2605 | |
| 2606 | switch (basetype) { |
| 2607 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2608 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2609 | |
| 2610 | /* 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] | 2611 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2612 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2613 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2614 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2615 | 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] | 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | if (tpdfname) { |
| 2620 | type_p->compiled = *type; |
| 2621 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2622 | } |
| 2623 | break; |
| 2624 | case LY_TYPE_BITS: |
| 2625 | /* RFC 7950 9.7 - bits */ |
| 2626 | bits = (struct lysc_type_bits*)(*type); |
| 2627 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2628 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2629 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2630 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2631 | } |
| 2632 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2633 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2634 | /* 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] | 2635 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2636 | 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] | 2637 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2638 | 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] | 2639 | free(*type); |
| 2640 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2641 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2642 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | if (tpdfname) { |
| 2646 | type_p->compiled = *type; |
| 2647 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2648 | } |
| 2649 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2650 | case LY_TYPE_DEC64: |
| 2651 | dec = (struct lysc_type_dec*)(*type); |
| 2652 | |
| 2653 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2654 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2655 | if (!type_p->fraction_digits) { |
| 2656 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2657 | 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] | 2658 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2659 | 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] | 2660 | free(*type); |
| 2661 | *type = NULL; |
| 2662 | } |
| 2663 | return LY_EVALID; |
| 2664 | } |
| 2665 | } else if (type_p->fraction_digits) { |
| 2666 | /* 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] | 2667 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2668 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2669 | "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] | 2670 | tpdfname); |
| 2671 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2672 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2673 | "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] | 2674 | free(*type); |
| 2675 | *type = NULL; |
| 2676 | } |
| 2677 | return LY_EVALID; |
| 2678 | } |
| 2679 | dec->fraction_digits = type_p->fraction_digits; |
| 2680 | |
| 2681 | /* RFC 7950 9.2.4 - range */ |
| 2682 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2683 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2684 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2685 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2686 | 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] | 2687 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | if (tpdfname) { |
| 2691 | type_p->compiled = *type; |
| 2692 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2693 | } |
| 2694 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2695 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2696 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2697 | |
| 2698 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2699 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2700 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2701 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2702 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2703 | 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] | 2704 | } |
| 2705 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2706 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2707 | } |
| 2708 | |
| 2709 | /* RFC 7950 9.4.5 - pattern */ |
| 2710 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2711 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2712 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2713 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2714 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2715 | } |
| 2716 | |
| 2717 | if (tpdfname) { |
| 2718 | type_p->compiled = *type; |
| 2719 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2720 | } |
| 2721 | break; |
| 2722 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2723 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2724 | |
| 2725 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2726 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2727 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2728 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2729 | } |
| 2730 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2731 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2732 | /* 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] | 2733 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2734 | 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] | 2735 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2736 | 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] | 2737 | free(*type); |
| 2738 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2739 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2740 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | if (tpdfname) { |
| 2744 | type_p->compiled = *type; |
| 2745 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2746 | } |
| 2747 | break; |
| 2748 | case LY_TYPE_INT8: |
| 2749 | case LY_TYPE_UINT8: |
| 2750 | case LY_TYPE_INT16: |
| 2751 | case LY_TYPE_UINT16: |
| 2752 | case LY_TYPE_INT32: |
| 2753 | case LY_TYPE_UINT32: |
| 2754 | case LY_TYPE_INT64: |
| 2755 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2756 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2757 | |
| 2758 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2759 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2760 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2761 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2762 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2763 | 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] | 2764 | } |
| 2765 | } |
| 2766 | |
| 2767 | if (tpdfname) { |
| 2768 | type_p->compiled = *type; |
| 2769 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2770 | } |
| 2771 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2772 | case LY_TYPE_IDENT: |
| 2773 | idref = (struct lysc_type_identityref*)(*type); |
| 2774 | |
| 2775 | /* RFC 7950 9.10.2 - base */ |
| 2776 | if (type_p->bases) { |
| 2777 | if (base) { |
| 2778 | /* only the directly derived identityrefs can contain base specification */ |
| 2779 | if (tpdfname) { |
| 2780 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2781 | "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] | 2782 | tpdfname); |
| 2783 | } else { |
| 2784 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2785 | "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] | 2786 | free(*type); |
| 2787 | *type = NULL; |
| 2788 | } |
| 2789 | return LY_EVALID; |
| 2790 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2791 | 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] | 2792 | } |
| 2793 | |
| 2794 | if (!base && !type_p->flags) { |
| 2795 | /* type derived from identityref built-in type must contain at least one base */ |
| 2796 | if (tpdfname) { |
| 2797 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2798 | } else { |
| 2799 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2800 | free(*type); |
| 2801 | *type = NULL; |
| 2802 | } |
| 2803 | return LY_EVALID; |
| 2804 | } |
| 2805 | |
| 2806 | if (tpdfname) { |
| 2807 | type_p->compiled = *type; |
| 2808 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2809 | } |
| 2810 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2811 | case LY_TYPE_LEAFREF: |
| 2812 | /* RFC 7950 9.9.3 - require-instance */ |
| 2813 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2814 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2815 | if (tpdfname) { |
| 2816 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2817 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2818 | } else { |
| 2819 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2820 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2821 | free(*type); |
| 2822 | *type = NULL; |
| 2823 | } |
| 2824 | return LY_EVALID; |
| 2825 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2826 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2827 | } else if (base) { |
| 2828 | /* inherit */ |
| 2829 | ((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] | 2830 | } else { |
| 2831 | /* default is true */ |
| 2832 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2833 | } |
| 2834 | if (type_p->path) { |
| 2835 | 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] | 2836 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2837 | } else if (base) { |
| 2838 | 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] | 2839 | ((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] | 2840 | } else if (tpdfname) { |
| 2841 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2842 | return LY_EVALID; |
| 2843 | } else { |
| 2844 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2845 | free(*type); |
| 2846 | *type = NULL; |
| 2847 | return LY_EVALID; |
| 2848 | } |
| 2849 | if (tpdfname) { |
| 2850 | type_p->compiled = *type; |
| 2851 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2852 | } |
| 2853 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2854 | case LY_TYPE_INST: |
| 2855 | /* RFC 7950 9.9.3 - require-instance */ |
| 2856 | if (type_p->flags & LYS_SET_REQINST) { |
| 2857 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2858 | } else { |
| 2859 | /* default is true */ |
| 2860 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2861 | } |
| 2862 | |
| 2863 | if (tpdfname) { |
| 2864 | type_p->compiled = *type; |
| 2865 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2866 | } |
| 2867 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2868 | case LY_TYPE_UNION: |
| 2869 | un = (struct lysc_type_union*)(*type); |
| 2870 | |
| 2871 | /* RFC 7950 7.4 - type */ |
| 2872 | if (type_p->types) { |
| 2873 | if (base) { |
| 2874 | /* only the directly derived union can contain types specification */ |
| 2875 | if (tpdfname) { |
| 2876 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2877 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2878 | tpdfname); |
| 2879 | } else { |
| 2880 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2881 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2882 | free(*type); |
| 2883 | *type = NULL; |
| 2884 | } |
| 2885 | return LY_EVALID; |
| 2886 | } |
| 2887 | /* compile the type */ |
| 2888 | additional = 0; |
| 2889 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 2890 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2891 | 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] | 2892 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2893 | /* add space for additional types from the union subtype */ |
| 2894 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 2895 | 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))); |
| 2896 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2897 | un->types = (void*)((uint32_t*)(p) + 1); |
| 2898 | |
| 2899 | /* copy subtypes of the subtype union */ |
| 2900 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 2901 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2902 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2903 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2904 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2905 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 2906 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 2907 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 2908 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2909 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 2910 | /* TODO extensions */ |
| 2911 | |
| 2912 | } else { |
| 2913 | un->types[u + additional] = un_aux->types[v]; |
| 2914 | ++un_aux->types[v]->refcount; |
| 2915 | } |
| 2916 | ++additional; |
| 2917 | LY_ARRAY_INCREMENT(un->types); |
| 2918 | } |
| 2919 | /* compensate u increment in main loop */ |
| 2920 | --additional; |
| 2921 | |
| 2922 | /* free the replaced union subtype */ |
| 2923 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2924 | } else { |
| 2925 | LY_ARRAY_INCREMENT(un->types); |
| 2926 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | if (!base && !type_p->flags) { |
| 2931 | /* type derived from union built-in type must contain at least one type */ |
| 2932 | if (tpdfname) { |
| 2933 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2934 | } else { |
| 2935 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2936 | free(*type); |
| 2937 | *type = NULL; |
| 2938 | } |
| 2939 | return LY_EVALID; |
| 2940 | } |
| 2941 | |
| 2942 | if (tpdfname) { |
| 2943 | type_p->compiled = *type; |
| 2944 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2945 | } |
| 2946 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2947 | case LY_TYPE_BOOL: |
| 2948 | case LY_TYPE_EMPTY: |
| 2949 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2950 | break; |
| 2951 | } |
| 2952 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2953 | done: |
| 2954 | return ret; |
| 2955 | } |
| 2956 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2957 | /** |
| 2958 | * @brief Compile information about the leaf/leaf-list's type. |
| 2959 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2960 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2961 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2962 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2963 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2964 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2965 | * @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] | 2966 | * @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] | 2967 | * @return LY_ERR value. |
| 2968 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2969 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2970 | 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] | 2971 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2972 | { |
| 2973 | LY_ERR ret = LY_SUCCESS; |
| 2974 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2975 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2976 | struct type_context { |
| 2977 | const struct lysp_tpdf *tpdf; |
| 2978 | struct lysp_node *node; |
| 2979 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2980 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2981 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2982 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2983 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2984 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2985 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2986 | |
| 2987 | (*type) = NULL; |
| 2988 | |
| 2989 | tctx = calloc(1, sizeof *tctx); |
| 2990 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2991 | 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] | 2992 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2993 | ret == LY_SUCCESS; |
| 2994 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2995 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2996 | if (basetype) { |
| 2997 | break; |
| 2998 | } |
| 2999 | |
| 3000 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3001 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3002 | 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] | 3003 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3004 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3005 | if (units && !*units) { |
| 3006 | /* inherit units */ |
| 3007 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3008 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3009 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3010 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3011 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3012 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3013 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3014 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3015 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3016 | break; |
| 3017 | } |
| 3018 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3019 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3020 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3021 | * 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] | 3022 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3023 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3024 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3025 | dummyloops = 1; |
| 3026 | goto preparenext; |
| 3027 | } else { |
| 3028 | tctx = NULL; |
| 3029 | break; |
| 3030 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3031 | } |
| 3032 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3033 | /* circular typedef reference detection */ |
| 3034 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3035 | /* local part */ |
| 3036 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3037 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3038 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3039 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3040 | free(tctx); |
| 3041 | ret = LY_EVALID; |
| 3042 | goto cleanup; |
| 3043 | } |
| 3044 | } |
| 3045 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3046 | /* global part for unions corner case */ |
| 3047 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3048 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3049 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3050 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3051 | free(tctx); |
| 3052 | ret = LY_EVALID; |
| 3053 | goto cleanup; |
| 3054 | } |
| 3055 | } |
| 3056 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3057 | /* store information for the following processing */ |
| 3058 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3059 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3060 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3061 | /* prepare next loop */ |
| 3062 | tctx_prev = tctx; |
| 3063 | tctx = calloc(1, sizeof *tctx); |
| 3064 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3065 | } |
| 3066 | free(tctx); |
| 3067 | |
| 3068 | /* allocate type according to the basetype */ |
| 3069 | switch (basetype) { |
| 3070 | case LY_TYPE_BINARY: |
| 3071 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3072 | break; |
| 3073 | case LY_TYPE_BITS: |
| 3074 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3075 | break; |
| 3076 | case LY_TYPE_BOOL: |
| 3077 | case LY_TYPE_EMPTY: |
| 3078 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3079 | break; |
| 3080 | case LY_TYPE_DEC64: |
| 3081 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3082 | break; |
| 3083 | case LY_TYPE_ENUM: |
| 3084 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3085 | break; |
| 3086 | case LY_TYPE_IDENT: |
| 3087 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3088 | break; |
| 3089 | case LY_TYPE_INST: |
| 3090 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3091 | break; |
| 3092 | case LY_TYPE_LEAFREF: |
| 3093 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3094 | break; |
| 3095 | case LY_TYPE_STRING: |
| 3096 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3097 | break; |
| 3098 | case LY_TYPE_UNION: |
| 3099 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3100 | break; |
| 3101 | case LY_TYPE_INT8: |
| 3102 | case LY_TYPE_UINT8: |
| 3103 | case LY_TYPE_INT16: |
| 3104 | case LY_TYPE_UINT16: |
| 3105 | case LY_TYPE_INT32: |
| 3106 | case LY_TYPE_UINT32: |
| 3107 | case LY_TYPE_INT64: |
| 3108 | case LY_TYPE_UINT64: |
| 3109 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3110 | break; |
| 3111 | case LY_TYPE_UNKNOWN: |
| 3112 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3113 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3114 | ret = LY_EVALID; |
| 3115 | goto cleanup; |
| 3116 | } |
| 3117 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3118 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3119 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3120 | ly_data_type2str[basetype]); |
| 3121 | free(*type); |
| 3122 | (*type) = NULL; |
| 3123 | ret = LY_EVALID; |
| 3124 | goto cleanup; |
| 3125 | } |
| 3126 | |
| 3127 | /* get restrictions from the referred typedefs */ |
| 3128 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3129 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3130 | |
| 3131 | /* remember the typedef context for circular check */ |
| 3132 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3133 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3134 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3135 | base = tctx->tpdf->type.compiled; |
| 3136 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3137 | } 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] | 3138 | /* no change, just use the type information from the base */ |
| 3139 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3140 | ++base->refcount; |
| 3141 | continue; |
| 3142 | } |
| 3143 | |
| 3144 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3145 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3146 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3147 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3148 | ret = LY_EVALID; |
| 3149 | goto cleanup; |
| 3150 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3151 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3152 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3153 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3154 | ret = LY_EVALID; |
| 3155 | goto cleanup; |
| 3156 | } |
| 3157 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3158 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3159 | /* TODO user type plugins */ |
| 3160 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3161 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3162 | 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] | 3163 | 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] | 3164 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3165 | LY_CHECK_GOTO(ret, cleanup); |
| 3166 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3167 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3168 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3169 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3170 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3171 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3172 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3173 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3174 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3175 | /* TODO user type plugins */ |
| 3176 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3177 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3178 | 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] | 3179 | LY_CHECK_GOTO(ret, cleanup); |
| 3180 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3181 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3182 | free(*type); |
| 3183 | (*type) = base; |
| 3184 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3185 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3186 | if (dflt && !(*type)->dflt) { |
| 3187 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3188 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3189 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3190 | (*type)->dflt->realtype = (*type); |
| 3191 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3192 | 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] | 3193 | 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] | 3194 | if (err) { |
| 3195 | ly_err_print(err); |
| 3196 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3197 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3198 | ly_err_free(err); |
| 3199 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3200 | if (ret == LY_EINCOMPLETE) { |
| 3201 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3202 | 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] | 3203 | |
| 3204 | /* but in general result is so far ok */ |
| 3205 | ret = LY_SUCCESS; |
| 3206 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3207 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3208 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3209 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3210 | 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] | 3211 | |
| 3212 | cleanup: |
| 3213 | ly_set_erase(&tpdf_chain, free); |
| 3214 | return ret; |
| 3215 | } |
| 3216 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3217 | /** |
| 3218 | * @brief Compile status information of the given node. |
| 3219 | * |
| 3220 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3221 | * has the status correctly set during the compilation. |
| 3222 | * |
| 3223 | * @param[in] ctx Compile context |
| 3224 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3225 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3226 | * the compatibility with the parent's status value. |
| 3227 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3228 | * @return LY_ERR value. |
| 3229 | */ |
| 3230 | static LY_ERR |
| 3231 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3232 | { |
| 3233 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3234 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3235 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3236 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3237 | if ((parent_flags & 0x3) != 0x3) { |
| 3238 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3239 | * combination of bits (0x3) which marks the uses_status value */ |
| 3240 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3241 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3242 | } |
| 3243 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3244 | } else { |
| 3245 | (*node_flags) |= LYS_STATUS_CURR; |
| 3246 | } |
| 3247 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3248 | /* check status compatibility with the parent */ |
| 3249 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3250 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3251 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3252 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3253 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3254 | } else { /* LYS_STATUS_DEPRC */ |
| 3255 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3256 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3257 | } |
| 3258 | return LY_EVALID; |
| 3259 | } |
| 3260 | } |
| 3261 | return LY_SUCCESS; |
| 3262 | } |
| 3263 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3264 | /** |
| 3265 | * @brief Check uniqness of the node/action/notification name. |
| 3266 | * |
| 3267 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3268 | * structures, but they share the namespace so we need to check their name collisions. |
| 3269 | * |
| 3270 | * @param[in] ctx Compile context. |
| 3271 | * @param[in] children List (linked list) of data nodes to go through. |
| 3272 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3273 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3274 | * @param[in] name Name of the item to find in the given lists. |
| 3275 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3276 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3277 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3278 | */ |
| 3279 | static LY_ERR |
| 3280 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3281 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3282 | const char *name, void *exclude) |
| 3283 | { |
| 3284 | const struct lysc_node *iter; |
| 3285 | unsigned int u; |
| 3286 | |
| 3287 | LY_LIST_FOR(children, iter) { |
| 3288 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3289 | goto error; |
| 3290 | } |
| 3291 | } |
| 3292 | LY_ARRAY_FOR(actions, u) { |
| 3293 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3294 | goto error; |
| 3295 | } |
| 3296 | } |
| 3297 | LY_ARRAY_FOR(notifs, u) { |
| 3298 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3299 | goto error; |
| 3300 | } |
| 3301 | } |
| 3302 | return LY_SUCCESS; |
| 3303 | error: |
| 3304 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification"); |
| 3305 | return LY_EEXIST; |
| 3306 | } |
| 3307 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3308 | 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] | 3309 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3310 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3311 | * @brief Compile parsed RPC/action schema node information. |
| 3312 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3313 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3314 | * @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] | 3315 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3316 | * @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). |
| 3317 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3318 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3319 | */ |
| 3320 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3321 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3322 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3323 | { |
| 3324 | LY_ERR ret = LY_SUCCESS; |
| 3325 | struct lysp_node *child_p; |
| 3326 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3327 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3328 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3329 | lysc_update_path(ctx, parent, action_p->name); |
| 3330 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3331 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3332 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3333 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3334 | action_p->name, action)) { |
| 3335 | return LY_EVALID; |
| 3336 | } |
| 3337 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3338 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3339 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3340 | "Action \"%s\" is placed inside %s.", action_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3341 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3342 | return LY_EVALID; |
| 3343 | } |
| 3344 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3345 | action->nodetype = LYS_ACTION; |
| 3346 | action->module = ctx->mod; |
| 3347 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3348 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3349 | action->sp = action_p; |
| 3350 | } |
| 3351 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3352 | |
| 3353 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3354 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3355 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3356 | |
| 3357 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3358 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3359 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3360 | COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
| 3361 | 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] | 3362 | |
| 3363 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3364 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3365 | COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup); |
| 3366 | COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup); |
| 3367 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3368 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3369 | 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] | 3370 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3371 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3372 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3373 | |
| 3374 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3375 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3376 | COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup); |
| 3377 | COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup); |
| 3378 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3379 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3380 | 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] | 3381 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3382 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3383 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3384 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3385 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3386 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3387 | return ret; |
| 3388 | } |
| 3389 | |
| 3390 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3391 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3392 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3393 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3394 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3395 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3396 | * @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] | 3397 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3398 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3399 | */ |
| 3400 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3401 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3402 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3403 | { |
| 3404 | LY_ERR ret = LY_SUCCESS; |
| 3405 | struct lysp_node *child_p; |
| 3406 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3407 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3408 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3409 | lysc_update_path(ctx, parent, notif_p->name); |
| 3410 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3411 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3412 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3413 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3414 | notif_p->name, notif)) { |
| 3415 | return LY_EVALID; |
| 3416 | } |
| 3417 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3418 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3419 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3420 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3421 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3422 | return LY_EVALID; |
| 3423 | } |
| 3424 | |
| 3425 | notif->nodetype = LYS_NOTIF; |
| 3426 | notif->module = ctx->mod; |
| 3427 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3428 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3429 | notif->sp = notif_p; |
| 3430 | } |
| 3431 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3432 | |
| 3433 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3434 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3435 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3436 | |
| 3437 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3438 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3439 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3440 | COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
| 3441 | COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup); |
| 3442 | 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] | 3443 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3444 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3445 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3446 | 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] | 3447 | } |
| 3448 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3449 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3450 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3451 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3452 | return ret; |
| 3453 | } |
| 3454 | |
| 3455 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3456 | * @brief Compile parsed container node information. |
| 3457 | * @param[in] ctx Compile context |
| 3458 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3459 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3460 | * is enriched with the container-specific information. |
| 3461 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3462 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3463 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3464 | 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] | 3465 | { |
| 3466 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3467 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3468 | struct lysp_node *child_p; |
| 3469 | unsigned int u; |
| 3470 | LY_ERR ret = LY_SUCCESS; |
| 3471 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3472 | if (cont_p->presence) { |
| 3473 | cont->flags |= LYS_PRESENCE; |
| 3474 | } |
| 3475 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3476 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3477 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3478 | } |
| 3479 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3480 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done); |
| 3481 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3482 | 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] | 3483 | |
| 3484 | done: |
| 3485 | return ret; |
| 3486 | } |
| 3487 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3488 | /* |
| 3489 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3490 | * @param[in] ctx Compile context. |
| 3491 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3492 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3493 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3494 | * @return LY_ERR value. |
| 3495 | */ |
| 3496 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3497 | 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] | 3498 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3499 | unsigned int u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3500 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3501 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3502 | 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] | 3503 | leaf->units ? NULL : &leaf->units)); |
| 3504 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3505 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3506 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3507 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3508 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3509 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3510 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3511 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3512 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3513 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3514 | } |
| 3515 | } else { |
| 3516 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3517 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3518 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3519 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3520 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3521 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3522 | } |
| 3523 | } |
| 3524 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3525 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3526 | ly_set_add(&ctx->unres, leaf, 0); |
| 3527 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3528 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3529 | if (((struct lysc_type_union*)leaf->type)->types[u]->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 | } |
| 3533 | } |
| 3534 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3535 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3536 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3537 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3538 | return LY_EVALID; |
| 3539 | } |
| 3540 | } |
| 3541 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3542 | return LY_SUCCESS; |
| 3543 | } |
| 3544 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3545 | /** |
| 3546 | * @brief Compile parsed leaf node information. |
| 3547 | * @param[in] ctx Compile context |
| 3548 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3549 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3550 | * is enriched with the leaf-specific information. |
| 3551 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3552 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3553 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3554 | 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] | 3555 | { |
| 3556 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3557 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3558 | unsigned int u; |
| 3559 | LY_ERR ret = LY_SUCCESS; |
| 3560 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3561 | 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] | 3562 | if (leaf_p->units) { |
| 3563 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3564 | leaf->flags |= LYS_SET_UNITS; |
| 3565 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3566 | |
| 3567 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3568 | leaf->dflt = (void*)leaf_p->dflt; |
| 3569 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
| 3570 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3571 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3572 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3573 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3574 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3575 | leaf->dflt->realtype = leaf->type; |
| 3576 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3577 | 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] | 3578 | 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] | 3579 | leaf->dflt->realtype->refcount++; |
| 3580 | if (err) { |
| 3581 | ly_err_print(err); |
| 3582 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3583 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3584 | ly_err_free(err); |
| 3585 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3586 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3587 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3588 | 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] | 3589 | |
| 3590 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3591 | ret = LY_SUCCESS; |
| 3592 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3593 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3594 | leaf->flags |= LYS_SET_DFLT; |
| 3595 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3596 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3597 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3598 | done: |
| 3599 | return ret; |
| 3600 | } |
| 3601 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3602 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3603 | * @brief Compile parsed leaf-list node information. |
| 3604 | * @param[in] ctx Compile context |
| 3605 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3606 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3607 | * is enriched with the leaf-list-specific information. |
| 3608 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3609 | */ |
| 3610 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3611 | 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] | 3612 | { |
| 3613 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3614 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3615 | unsigned int u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3616 | LY_ERR ret = LY_SUCCESS; |
| 3617 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3618 | 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] | 3619 | if (llist_p->units) { |
| 3620 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3621 | llist->flags |= LYS_SET_UNITS; |
| 3622 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3623 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3624 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3625 | llist->dflts = (void*)llist_p->dflts; |
| 3626 | 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] | 3627 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3628 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3629 | 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] | 3630 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3631 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3632 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3633 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3634 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3635 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3636 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3637 | llist->dflts[u]->realtype = llist->type; |
| 3638 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3639 | 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] | 3640 | 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] | 3641 | llist->dflts[u]->realtype->refcount++; |
| 3642 | if (err) { |
| 3643 | ly_err_print(err); |
| 3644 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3645 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3646 | ly_err_free(err); |
| 3647 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3648 | if (ret == LY_EINCOMPLETE) { |
| 3649 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3650 | 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] | 3651 | |
| 3652 | /* but in general result is so far ok */ |
| 3653 | ret = LY_SUCCESS; |
| 3654 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3655 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3656 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3657 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3658 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3659 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3660 | /* configuration data values must be unique - so check the default values */ |
| 3661 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3662 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3663 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3664 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3665 | 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] | 3666 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3667 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3668 | if (dynamic) { |
| 3669 | free((char*)val); |
| 3670 | } |
| 3671 | return LY_EVALID; |
| 3672 | } |
| 3673 | } |
| 3674 | } |
| 3675 | } |
| 3676 | |
| 3677 | /* 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] | 3678 | |
| 3679 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3680 | if (llist->min) { |
| 3681 | llist->flags |= LYS_MAND_TRUE; |
| 3682 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3683 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3684 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3685 | done: |
| 3686 | return ret; |
| 3687 | } |
| 3688 | |
| 3689 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3690 | * @brief Compile information about list's uniques. |
| 3691 | * @param[in] ctx Compile context. |
| 3692 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3693 | * @param[in] uniques Sized array list of unique statements. |
| 3694 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3695 | * @return LY_ERR value. |
| 3696 | */ |
| 3697 | static LY_ERR |
| 3698 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3699 | { |
| 3700 | LY_ERR ret = LY_SUCCESS; |
| 3701 | struct lysc_node_leaf **key, ***unique; |
| 3702 | const char *keystr, *delim; |
| 3703 | size_t len; |
| 3704 | unsigned int v; |
| 3705 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3706 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3707 | |
| 3708 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3709 | config = -1; |
| 3710 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3711 | keystr = uniques[v]; |
| 3712 | while (keystr) { |
| 3713 | delim = strpbrk(keystr, " \t\n"); |
| 3714 | if (delim) { |
| 3715 | len = delim - keystr; |
| 3716 | while (isspace(*delim)) { |
| 3717 | ++delim; |
| 3718 | } |
| 3719 | } else { |
| 3720 | len = strlen(keystr); |
| 3721 | } |
| 3722 | |
| 3723 | /* unique node must be present */ |
| 3724 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3725 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3726 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3727 | if (ret != LY_SUCCESS) { |
| 3728 | if (ret == LY_EDENIED) { |
| 3729 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3730 | "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] | 3731 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3732 | } |
| 3733 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3734 | } else if (flags) { |
| 3735 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3736 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3737 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
| 3738 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3739 | } |
| 3740 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3741 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3742 | /* all referenced leafs must be of the same config type */ |
| 3743 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3745 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3746 | return LY_EVALID; |
| 3747 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3748 | config = 1; |
| 3749 | } else { /* LYS_CONFIG_R */ |
| 3750 | config = 0; |
| 3751 | } |
| 3752 | |
| 3753 | /* check status */ |
| 3754 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3755 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3756 | |
| 3757 | /* mark leaf as unique */ |
| 3758 | (*key)->flags |= LYS_UNIQUE; |
| 3759 | |
| 3760 | /* next unique value in line */ |
| 3761 | keystr = delim; |
| 3762 | } |
| 3763 | /* next unique definition */ |
| 3764 | } |
| 3765 | |
| 3766 | return LY_SUCCESS; |
| 3767 | } |
| 3768 | |
| 3769 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3770 | * @brief Compile parsed list node information. |
| 3771 | * @param[in] ctx Compile context |
| 3772 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3773 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3774 | * is enriched with the list-specific information. |
| 3775 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3776 | */ |
| 3777 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3778 | 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] | 3779 | { |
| 3780 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3781 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3782 | struct lysp_node *child_p; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3783 | struct lysc_node_leaf **key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3784 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3785 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3786 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3787 | LY_ERR ret = LY_SUCCESS; |
| 3788 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3789 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3790 | if (list->min) { |
| 3791 | list->flags |= LYS_MAND_TRUE; |
| 3792 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3793 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3794 | |
| 3795 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3796 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3797 | } |
| 3798 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3799 | 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] | 3800 | |
| 3801 | /* keys */ |
| 3802 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3803 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3804 | return LY_EVALID; |
| 3805 | } |
| 3806 | |
| 3807 | /* find all the keys (must be direct children) */ |
| 3808 | keystr = list_p->key; |
| 3809 | while (keystr) { |
| 3810 | delim = strpbrk(keystr, " \t\n"); |
| 3811 | if (delim) { |
| 3812 | len = delim - keystr; |
| 3813 | while (isspace(*delim)) { |
| 3814 | ++delim; |
| 3815 | } |
| 3816 | } else { |
| 3817 | len = strlen(keystr); |
| 3818 | } |
| 3819 | |
| 3820 | /* key node must be present */ |
| 3821 | LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM); |
| 3822 | *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 3823 | if (!(*key)) { |
| 3824 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3825 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3826 | return LY_EVALID; |
| 3827 | } |
| 3828 | /* keys must be unique */ |
| 3829 | for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) { |
| 3830 | if (*key == list->keys[u]) { |
| 3831 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3832 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3833 | return LY_EVALID; |
| 3834 | } |
| 3835 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3836 | lysc_update_path(ctx, (struct lysc_node*)list, (*key)->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3837 | /* key must have the same config flag as the list itself */ |
| 3838 | if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) { |
| 3839 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3840 | return LY_EVALID; |
| 3841 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3842 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3843 | /* YANG 1.0 denies key to be of empty type */ |
| 3844 | if ((*key)->type->basetype == LY_TYPE_EMPTY) { |
| 3845 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3846 | "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] | 3847 | return LY_EVALID; |
| 3848 | } |
| 3849 | } else { |
| 3850 | /* when and if-feature are illegal on list keys */ |
| 3851 | if ((*key)->when) { |
| 3852 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3853 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3854 | return LY_EVALID; |
| 3855 | } |
| 3856 | if ((*key)->iffeatures) { |
| 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 \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3859 | return LY_EVALID; |
| 3860 | } |
| 3861 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3862 | |
| 3863 | /* check status */ |
| 3864 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3865 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3866 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3867 | /* ignore default values of the key */ |
| 3868 | if ((*key)->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3869 | (*key)->dflt->realtype->plugin->free(ctx->ctx, (*key)->dflt); |
| 3870 | lysc_type_free(ctx->ctx, (*key)->dflt->realtype); |
| 3871 | free((*key)->dflt); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3872 | (*key)->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3873 | (*key)->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3874 | } |
| 3875 | /* mark leaf as key */ |
| 3876 | (*key)->flags |= LYS_KEY; |
| 3877 | |
| 3878 | /* next key value */ |
| 3879 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3880 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | /* uniques */ |
| 3884 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3885 | 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] | 3886 | } |
| 3887 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3888 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3889 | 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] | 3890 | |
| 3891 | done: |
| 3892 | return ret; |
| 3893 | } |
| 3894 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3895 | /** |
| 3896 | * @brief Do some checks and set the default choice's case. |
| 3897 | * |
| 3898 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3899 | * |
| 3900 | * @param[in] ctx Compile context. |
| 3901 | * @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, |
| 3902 | * not the reference to the imported module. |
| 3903 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3904 | * @return LY_ERR value. |
| 3905 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3906 | static LY_ERR |
| 3907 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3908 | { |
| 3909 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3910 | const char *prefix = NULL, *name; |
| 3911 | size_t prefix_len = 0; |
| 3912 | |
| 3913 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3914 | name = strchr(dflt, ':'); |
| 3915 | if (name) { |
| 3916 | prefix = dflt; |
| 3917 | prefix_len = name - prefix; |
| 3918 | ++name; |
| 3919 | } else { |
| 3920 | name = dflt; |
| 3921 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3922 | 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] | 3923 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3924 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3925 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3926 | prefix_len, prefix); |
| 3927 | return LY_EVALID; |
| 3928 | } |
| 3929 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3930 | if (!ch->dflt) { |
| 3931 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3932 | "Default case \"%s\" not found.", dflt); |
| 3933 | return LY_EVALID; |
| 3934 | } |
| 3935 | /* no mandatory nodes directly under the default case */ |
| 3936 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3937 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3938 | break; |
| 3939 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3940 | if (iter->flags & LYS_MAND_TRUE) { |
| 3941 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3942 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3943 | return LY_EVALID; |
| 3944 | } |
| 3945 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3946 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3947 | return LY_SUCCESS; |
| 3948 | } |
| 3949 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3950 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3951 | 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] | 3952 | { |
| 3953 | struct lys_module *mod; |
| 3954 | const char *prefix = NULL, *name; |
| 3955 | size_t prefix_len = 0; |
| 3956 | struct lysc_node_case *cs; |
| 3957 | struct lysc_node *node; |
| 3958 | |
| 3959 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3960 | name = strchr(dflt, ':'); |
| 3961 | if (name) { |
| 3962 | prefix = dflt; |
| 3963 | prefix_len = name - prefix; |
| 3964 | ++name; |
| 3965 | } else { |
| 3966 | name = dflt; |
| 3967 | } |
| 3968 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3969 | if (prefix) { |
| 3970 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3971 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3972 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3973 | "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] | 3974 | return LY_EVALID; |
| 3975 | } |
| 3976 | } else { |
| 3977 | mod = ctx->mod; |
| 3978 | } |
| 3979 | /* get the default case */ |
| 3980 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3981 | if (!cs) { |
| 3982 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3983 | "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] | 3984 | return LY_EVALID; |
| 3985 | } |
| 3986 | |
| 3987 | /* check that there is no mandatory node */ |
| 3988 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3989 | if (node->parent != (struct lysc_node*)cs) { |
| 3990 | break; |
| 3991 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3992 | if (node->flags & LYS_MAND_TRUE) { |
| 3993 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3994 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3995 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3996 | return LY_EVALID; |
| 3997 | } |
| 3998 | } |
| 3999 | |
| 4000 | /* set the default case in choice */ |
| 4001 | ch->dflt = cs; |
| 4002 | cs->flags |= LYS_SET_DFLT; |
| 4003 | |
| 4004 | return LY_SUCCESS; |
| 4005 | } |
| 4006 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4007 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4008 | * @brief Compile parsed choice node information. |
| 4009 | * @param[in] ctx Compile context |
| 4010 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4011 | * @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] | 4012 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4013 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4014 | */ |
| 4015 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4016 | 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] | 4017 | { |
| 4018 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4019 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4020 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4021 | LY_ERR ret = LY_SUCCESS; |
| 4022 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4023 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4024 | if (child_p->nodetype == LYS_CASE) { |
| 4025 | 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] | 4026 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4027 | } |
| 4028 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4029 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4030 | } |
| 4031 | } |
| 4032 | |
| 4033 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4034 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4035 | 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] | 4036 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4037 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4038 | return ret; |
| 4039 | } |
| 4040 | |
| 4041 | /** |
| 4042 | * @brief Compile parsed anydata or anyxml node information. |
| 4043 | * @param[in] ctx Compile context |
| 4044 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4045 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4046 | * is enriched with the any-specific information. |
| 4047 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4048 | */ |
| 4049 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4050 | 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] | 4051 | { |
| 4052 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4053 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4054 | unsigned int u; |
| 4055 | LY_ERR ret = LY_SUCCESS; |
| 4056 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4057 | 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] | 4058 | |
| 4059 | if (any->flags & LYS_CONFIG_W) { |
| 4060 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
| 4061 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML)); |
| 4062 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4063 | done: |
| 4064 | return ret; |
| 4065 | } |
| 4066 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4067 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4068 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4069 | * |
| 4070 | * @param[in] ctx Compile context |
| 4071 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4072 | * the choice itself is expected instead of a specific case node. |
| 4073 | * @param[in] node Schema node to connect into the list. |
| 4074 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 4075 | */ |
| 4076 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4077 | 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] | 4078 | { |
| 4079 | struct lysc_node **children; |
| 4080 | |
| 4081 | if (node->nodetype == LYS_CASE) { |
| 4082 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4083 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4084 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4085 | } |
| 4086 | if (children) { |
| 4087 | if (!(*children)) { |
| 4088 | /* first child */ |
| 4089 | *children = node; |
| 4090 | } else if (*children != node) { |
| 4091 | /* by the condition in previous branch we cover the choice/case children |
| 4092 | * - the children list is shared by the choice and the the first case, in addition |
| 4093 | * the first child of each case must be referenced from the case node. So the node is |
| 4094 | * actually always already inserted in case it is the first children - so here such |
| 4095 | * a situation actually corresponds to the first branch */ |
| 4096 | /* insert at the end of the parent's children list */ |
| 4097 | (*children)->prev->next = node; |
| 4098 | node->prev = (*children)->prev; |
| 4099 | (*children)->prev = node; |
| 4100 | |
| 4101 | /* check the name uniqueness */ |
| 4102 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4103 | lysc_node_notifs(parent), node->name, node)) { |
| 4104 | return LY_EEXIST; |
| 4105 | } |
| 4106 | } |
| 4107 | } |
| 4108 | return LY_SUCCESS; |
| 4109 | } |
| 4110 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4111 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4112 | * @brief Get the XPath context node for the given schema node. |
| 4113 | * @param[in] start The schema node where the XPath expression appears. |
| 4114 | * @return The context node to evaluate XPath expression in given schema node. |
| 4115 | * @return NULL in case the context node is the root node. |
| 4116 | */ |
| 4117 | static struct lysc_node * |
| 4118 | lysc_xpath_context(struct lysc_node *start) |
| 4119 | { |
| 4120 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 4121 | start = start->parent); |
| 4122 | return start; |
| 4123 | } |
| 4124 | |
| 4125 | /** |
| 4126 | * @brief Prepare the case structure in choice node for the new data node. |
| 4127 | * |
| 4128 | * 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 |
| 4129 | * created in the choice when the first child was processed. |
| 4130 | * |
| 4131 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4132 | * @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, |
| 4133 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4134 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4135 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4136 | * @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, |
| 4137 | * 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] | 4138 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4139 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4140 | 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] | 4141 | { |
| 4142 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4143 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4144 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4145 | unsigned int u; |
| 4146 | LY_ERR ret; |
| 4147 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4148 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4149 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4150 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4151 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4152 | return NULL; \ |
| 4153 | } \ |
| 4154 | } |
| 4155 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4156 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4157 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4158 | |
| 4159 | /* we have to add an implicit case node into the parent choice */ |
| 4160 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4161 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 4162 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4163 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4164 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4165 | /* the case is already present since the child is not its first children */ |
| 4166 | return (struct lysc_node_case*)ch->cases->prev; |
| 4167 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4168 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4169 | |
| 4170 | /* explicit parent case is not present (this is its first child) */ |
| 4171 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4172 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 4173 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4174 | cs->sp = node_p; |
| 4175 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4176 | /* 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] | 4177 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4178 | |
| 4179 | if (node_p->when) { |
| 4180 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4181 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4182 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4183 | (*when)->context = lysc_xpath_context(ch->parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4184 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4185 | 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] | 4186 | } else { |
| 4187 | LOGINT(ctx->ctx); |
| 4188 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4189 | } |
| 4190 | cs->module = ctx->mod; |
| 4191 | cs->prev = (struct lysc_node*)cs; |
| 4192 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4193 | 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] | 4194 | cs->parent = (struct lysc_node*)ch; |
| 4195 | cs->child = child; |
| 4196 | |
| 4197 | return cs; |
| 4198 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4199 | if (cs) { |
| 4200 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4201 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4202 | return NULL; |
| 4203 | |
| 4204 | #undef UNIQUE_CHECK |
| 4205 | } |
| 4206 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4207 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4208 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4209 | * |
| 4210 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4211 | * @param[in] node Target node where the config is supposed to be changed. |
| 4212 | * @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] | 4213 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4214 | * 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] | 4215 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4216 | * @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] | 4217 | * @return LY_ERR value. |
| 4218 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4219 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4220 | 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] | 4221 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4222 | { |
| 4223 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4224 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4225 | |
| 4226 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4227 | /* nothing to do */ |
| 4228 | return LY_SUCCESS; |
| 4229 | } |
| 4230 | |
| 4231 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4232 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4233 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4234 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4235 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4236 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4237 | return LY_EVALID; |
| 4238 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4239 | node->flags |= LYS_SET_CONFIG; |
| 4240 | } else { |
| 4241 | if (node->flags & LYS_SET_CONFIG) { |
| 4242 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4243 | /* setting config flags, but have node with explicit config true */ |
| 4244 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4245 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4246 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4247 | return LY_EVALID; |
| 4248 | } |
| 4249 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4250 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4251 | return LY_SUCCESS; |
| 4252 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4253 | } |
| 4254 | node->flags &= ~LYS_CONFIG_MASK; |
| 4255 | node->flags |= config; |
| 4256 | |
| 4257 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4258 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4259 | 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] | 4260 | } |
| 4261 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4262 | return LY_SUCCESS; |
| 4263 | } |
| 4264 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4265 | /** |
| 4266 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4267 | * |
| 4268 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4269 | * the flag to such parents from a mandatory children. |
| 4270 | * |
| 4271 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4272 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4273 | * (mandatory children was removed). |
| 4274 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4275 | void |
| 4276 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4277 | { |
| 4278 | struct lysc_node *iter; |
| 4279 | |
| 4280 | if (add) { /* set flag */ |
| 4281 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4282 | parent = parent->parent) { |
| 4283 | parent->flags |= LYS_MAND_TRUE; |
| 4284 | } |
| 4285 | } else { /* unset flag */ |
| 4286 | 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] | 4287 | 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] | 4288 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4289 | /* there is another mandatory node */ |
| 4290 | return; |
| 4291 | } |
| 4292 | } |
| 4293 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4294 | parent->flags &= ~LYS_MAND_TRUE; |
| 4295 | } |
| 4296 | } |
| 4297 | } |
| 4298 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4299 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4300 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4301 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4302 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4303 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4304 | */ |
| 4305 | static void |
| 4306 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4307 | { |
| 4308 | unsigned int v; |
| 4309 | size_t len; |
| 4310 | |
| 4311 | len = strlen(aug_p->nodeid); |
| 4312 | LY_ARRAY_FOR(result, v) { |
| 4313 | if (strlen(result[v]->nodeid) <= len) { |
| 4314 | continue; |
| 4315 | } |
| 4316 | if (v < LY_ARRAY_SIZE(result)) { |
| 4317 | /* move the rest of array */ |
| 4318 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4319 | break; |
| 4320 | } |
| 4321 | } |
| 4322 | result[v] = aug_p; |
| 4323 | LY_ARRAY_INCREMENT(result); |
| 4324 | } |
| 4325 | |
| 4326 | /** |
| 4327 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4328 | * |
| 4329 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4330 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4331 | * |
| 4332 | * @param[in] ctx Compile context. |
| 4333 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4334 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4335 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4336 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4337 | * @return LY_ERR value. |
| 4338 | */ |
| 4339 | LY_ERR |
| 4340 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4341 | { |
| 4342 | struct lysp_augment **result = NULL; |
| 4343 | unsigned int u, v; |
| 4344 | size_t count = 0; |
| 4345 | |
| 4346 | assert(augments); |
| 4347 | |
| 4348 | /* get count of the augments in module and all its submodules */ |
| 4349 | if (aug_p) { |
| 4350 | count += LY_ARRAY_SIZE(aug_p); |
| 4351 | } |
| 4352 | LY_ARRAY_FOR(inc_p, u) { |
| 4353 | if (inc_p[u].submodule->augments) { |
| 4354 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4355 | } |
| 4356 | } |
| 4357 | |
| 4358 | if (!count) { |
| 4359 | *augments = NULL; |
| 4360 | return LY_SUCCESS; |
| 4361 | } |
| 4362 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4363 | |
| 4364 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4365 | * together, so there can be even /z/y betwwen them. */ |
| 4366 | LY_ARRAY_FOR(aug_p, u) { |
| 4367 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4368 | } |
| 4369 | LY_ARRAY_FOR(inc_p, u) { |
| 4370 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4371 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4372 | } |
| 4373 | } |
| 4374 | |
| 4375 | *augments = result; |
| 4376 | return LY_SUCCESS; |
| 4377 | } |
| 4378 | |
| 4379 | /** |
| 4380 | * @brief Compile the parsed augment connecting it into its target. |
| 4381 | * |
| 4382 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4383 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4384 | * are already implemented and compiled. |
| 4385 | * |
| 4386 | * @param[in] ctx Compile context. |
| 4387 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4388 | * @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 |
| 4389 | * children in case of the augmenting uses data. |
| 4390 | * @return LY_SUCCESS on success. |
| 4391 | * @return LY_EVALID on failure. |
| 4392 | */ |
| 4393 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4394 | 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] | 4395 | { |
| 4396 | LY_ERR ret = LY_SUCCESS; |
| 4397 | struct lysp_node *node_p, *case_node_p; |
| 4398 | struct lysc_node *target; /* target target of the augment */ |
| 4399 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4400 | struct lysc_when **when, *when_shared; |
| 4401 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4402 | uint16_t flags = 0; |
| 4403 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4404 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4405 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4406 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4407 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4408 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4409 | 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] | 4410 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4411 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4412 | if (ret != LY_SUCCESS) { |
| 4413 | if (ret == LY_EDENIED) { |
| 4414 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4415 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4416 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4417 | } |
| 4418 | return LY_EVALID; |
| 4419 | } |
| 4420 | |
| 4421 | /* check for mandatory nodes |
| 4422 | * - new cases augmenting some choice can have mandatory nodes |
| 4423 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4424 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4425 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4426 | allow_mandatory = 1; |
| 4427 | } |
| 4428 | |
| 4429 | when_shared = NULL; |
| 4430 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4431 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4432 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4433 | && !((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] | 4434 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4435 | && !(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] | 4436 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4437 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4438 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4439 | return LY_EVALID; |
| 4440 | } |
| 4441 | |
| 4442 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4443 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4444 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4445 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4446 | } else { |
| 4447 | 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] | 4448 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4449 | } |
| 4450 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4451 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4452 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4453 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4454 | * 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] | 4455 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4456 | /* 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] | 4457 | 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] | 4458 | } else if (target->nodetype == LYS_CHOICE) { |
| 4459 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4460 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4461 | } else { |
| 4462 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4463 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4464 | } |
| 4465 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4466 | 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] | 4467 | node->flags &= ~LYS_MAND_TRUE; |
| 4468 | lys_compile_mandatory_parents(target, 0); |
| 4469 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4470 | "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] | 4471 | return LY_EVALID; |
| 4472 | } |
| 4473 | |
| 4474 | /* pass augment's when to all the children */ |
| 4475 | if (aug_p->when) { |
| 4476 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4477 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4478 | ret = lys_compile_when(ctx, aug_p->when, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4479 | LY_CHECK_GOTO(ret, error); |
| 4480 | (*when)->context = lysc_xpath_context(target); |
| 4481 | when_shared = *when; |
| 4482 | } else { |
| 4483 | ++when_shared->refcount; |
| 4484 | (*when) = when_shared; |
| 4485 | } |
| 4486 | } |
| 4487 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4488 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4489 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4490 | switch (target->nodetype) { |
| 4491 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4492 | 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] | 4493 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4494 | 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] | 4495 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4496 | break; |
| 4497 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4498 | 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] | 4499 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4500 | 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] | 4501 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4502 | break; |
| 4503 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4504 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4505 | if (aug_p->actions) { |
| 4506 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4507 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4508 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4509 | return LY_EVALID; |
| 4510 | } |
| 4511 | if (aug_p->notifs) { |
| 4512 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4513 | "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".", |
| 4514 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4515 | return LY_EVALID; |
| 4516 | } |
| 4517 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4518 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4519 | lysc_update_path(ctx, NULL, NULL); |
| 4520 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4521 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4522 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4523 | return ret; |
| 4524 | } |
| 4525 | |
| 4526 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4527 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4528 | * |
| 4529 | * @param[in] ctx Compile context. |
| 4530 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4531 | * @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] | 4532 | * @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] | 4533 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4534 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4535 | * 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] | 4536 | * @return LY_ERR value. |
| 4537 | */ |
| 4538 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4539 | 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] | 4540 | { |
| 4541 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4542 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4543 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4544 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4545 | return LY_EVALID; |
| 4546 | } |
| 4547 | |
| 4548 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4549 | /* check if node has default value */ |
| 4550 | if (node->nodetype & LYS_LEAF) { |
| 4551 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4552 | if (refine_flag) { |
| 4553 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4554 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4555 | return LY_EVALID; |
| 4556 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4557 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4558 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4559 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4560 | |
| 4561 | /* update the list of incomplete default values if needed */ |
| 4562 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4563 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4564 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4565 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4566 | free(leaf->dflt); |
| 4567 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4568 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4569 | } |
| 4570 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4571 | if (refine_flag) { |
| 4572 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4573 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4574 | return LY_EVALID; |
| 4575 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4576 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4577 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4578 | 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] | 4579 | return LY_EVALID; |
| 4580 | } |
| 4581 | |
| 4582 | node->flags &= ~LYS_MAND_FALSE; |
| 4583 | node->flags |= LYS_MAND_TRUE; |
| 4584 | lys_compile_mandatory_parents(node->parent, 1); |
| 4585 | } else { |
| 4586 | /* make mandatory false */ |
| 4587 | node->flags &= ~LYS_MAND_TRUE; |
| 4588 | node->flags |= LYS_MAND_FALSE; |
| 4589 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4590 | 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] | 4591 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4592 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4593 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4594 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4595 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4596 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4597 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4598 | } |
| 4599 | } |
| 4600 | return LY_SUCCESS; |
| 4601 | } |
| 4602 | |
| 4603 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4604 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4605 | * If present, also apply uses's modificators. |
| 4606 | * |
| 4607 | * @param[in] ctx Compile context |
| 4608 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4609 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4610 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4611 | * the compile context. |
| 4612 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4613 | */ |
| 4614 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4615 | 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] | 4616 | { |
| 4617 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4618 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4619 | /* 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] | 4620 | struct lysc_node_container context_node_fake = |
| 4621 | {.nodetype = LYS_CONTAINER, |
| 4622 | .module = ctx->mod, |
| 4623 | .flags = parent ? parent->flags : 0, |
| 4624 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4625 | .prev = (struct lysc_node*)&context_node_fake, |
| 4626 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4627 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4628 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4629 | int found; |
| 4630 | const char *id, *name, *prefix; |
| 4631 | size_t prefix_len, name_len; |
| 4632 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4633 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4634 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4635 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4636 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4637 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4638 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4639 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4640 | unsigned int actions_index, notifs_index; |
| 4641 | struct lysc_notif **notifs = NULL; |
| 4642 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4643 | |
| 4644 | /* search for the grouping definition */ |
| 4645 | found = 0; |
| 4646 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4647 | 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] | 4648 | if (prefix) { |
| 4649 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4650 | if (!mod) { |
| 4651 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4652 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4653 | return LY_EVALID; |
| 4654 | } |
| 4655 | } else { |
| 4656 | mod = ctx->mod_def; |
| 4657 | } |
| 4658 | if (mod == ctx->mod_def) { |
| 4659 | 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] | 4660 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4661 | LY_ARRAY_FOR(grp, u) { |
| 4662 | if (!strcmp(grp[u].name, name)) { |
| 4663 | grp = &grp[u]; |
| 4664 | found = 1; |
| 4665 | break; |
| 4666 | } |
| 4667 | } |
| 4668 | } |
| 4669 | } |
| 4670 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4671 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4672 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4673 | if (grp) { |
| 4674 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4675 | if (!strcmp(grp[u].name, name)) { |
| 4676 | grp = &grp[u]; |
| 4677 | found = 1; |
| 4678 | } |
| 4679 | } |
| 4680 | } |
| 4681 | if (!found && mod->parsed->includes) { |
| 4682 | /* ... and all the submodules */ |
| 4683 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4684 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4685 | if (grp) { |
| 4686 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4687 | if (!strcmp(grp[v].name, name)) { |
| 4688 | grp = &grp[v]; |
| 4689 | found = 1; |
| 4690 | } |
| 4691 | } |
| 4692 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4693 | } |
| 4694 | } |
| 4695 | } |
| 4696 | if (!found) { |
| 4697 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4698 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4699 | return LY_EVALID; |
| 4700 | } |
| 4701 | |
| 4702 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4703 | grp_stack_count = ctx->groupings.count; |
| 4704 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4705 | if (grp_stack_count == ctx->groupings.count) { |
| 4706 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4707 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4708 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4709 | return LY_EVALID; |
| 4710 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4711 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4712 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4713 | grp->flags |= LYS_USED_GRP; |
| 4714 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4715 | |
| 4716 | /* switch context's mod_def */ |
| 4717 | mod_old = ctx->mod_def; |
| 4718 | ctx->mod_def = mod; |
| 4719 | |
| 4720 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4721 | 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] | 4722 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4723 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4724 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4725 | /* 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] | 4726 | 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] | 4727 | |
| 4728 | /* some preparation for applying refines */ |
| 4729 | if (grp->data == node_p) { |
| 4730 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4731 | if (parent) { |
| 4732 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4733 | } else if (ctx->mod->compiled->data) { |
| 4734 | child = ctx->mod->compiled->data; |
| 4735 | } else { |
| 4736 | child = NULL; |
| 4737 | } |
| 4738 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4739 | } |
| 4740 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4741 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4742 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4743 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4744 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4745 | /* 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] | 4746 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4747 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4748 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4749 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4750 | (*when)->context = lysc_xpath_context(parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4751 | when_shared = *when; |
| 4752 | } else { |
| 4753 | ++when_shared->refcount; |
| 4754 | (*when) = when_shared; |
| 4755 | } |
| 4756 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4757 | } |
| 4758 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4759 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4760 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4761 | /* 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] | 4762 | 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] | 4763 | } |
| 4764 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4765 | /* compile actions */ |
| 4766 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4767 | if (actions) { |
| 4768 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4769 | 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] | 4770 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4771 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4772 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4773 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4774 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4775 | } |
| 4776 | } |
| 4777 | |
| 4778 | /* compile notifications */ |
| 4779 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4780 | if (notifs) { |
| 4781 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4782 | 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] | 4783 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4784 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4785 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4786 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4787 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4788 | } |
| 4789 | } |
| 4790 | |
| 4791 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4792 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4793 | 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] | 4794 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4795 | 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] | 4796 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4797 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4798 | /* reload previous context's mod_def */ |
| 4799 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4800 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4801 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4802 | /* apply refine */ |
| 4803 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4804 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4805 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4806 | 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] | 4807 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4808 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4809 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4810 | |
| 4811 | /* default value */ |
| 4812 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4813 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4814 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4815 | "Invalid refine of default - %s cannot hold %d default values.", |
| 4816 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4817 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4818 | } |
| 4819 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4820 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4821 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4822 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4823 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4824 | } |
| 4825 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4826 | struct ly_err_item *err = NULL; |
| 4827 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4828 | if (leaf->dflt) { |
| 4829 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4830 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4831 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4832 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4833 | } else { |
| 4834 | /* prepare a new one */ |
| 4835 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4836 | leaf->dflt->realtype = leaf->type; |
| 4837 | } |
| 4838 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4839 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4840 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4841 | 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] | 4842 | 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] | 4843 | leaf->dflt->realtype->refcount++; |
| 4844 | if (err) { |
| 4845 | ly_err_print(err); |
| 4846 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4847 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4848 | ly_err_free(err); |
| 4849 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4850 | if (rc == LY_EINCOMPLETE) { |
| 4851 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4852 | 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] | 4853 | |
| 4854 | /* but in general result is so far ok */ |
| 4855 | rc = LY_SUCCESS; |
| 4856 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4857 | LY_CHECK_GOTO(rc, cleanup); |
| 4858 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4859 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4860 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4861 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4862 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4863 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4864 | "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] | 4865 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4866 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4867 | |
| 4868 | /* remove previous set of default values */ |
| 4869 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4870 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4871 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 4872 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 4873 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4874 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4875 | LY_ARRAY_FREE(llist->dflts); |
| 4876 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4877 | LY_ARRAY_FREE(llist->dflts_mods); |
| 4878 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4879 | |
| 4880 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4881 | 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] | 4882 | 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] | 4883 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4884 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4885 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 4886 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4887 | LY_ARRAY_INCREMENT(llist->dflts); |
| 4888 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 4889 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4890 | 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] | 4891 | 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] | 4892 | 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] | 4893 | llist->dflts[u]->realtype->refcount++; |
| 4894 | if (err) { |
| 4895 | ly_err_print(err); |
| 4896 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4897 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 4898 | ly_err_free(err); |
| 4899 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4900 | if (rc == LY_EINCOMPLETE) { |
| 4901 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4902 | 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] | 4903 | |
| 4904 | /* but in general result is so far ok */ |
| 4905 | rc = LY_SUCCESS; |
| 4906 | } |
| 4907 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4908 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4909 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4910 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4911 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4912 | /* unset LYS_SET_DFLT from the current default case */ |
| 4913 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4914 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4915 | 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] | 4916 | } |
| 4917 | } |
| 4918 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4919 | /* description */ |
| 4920 | if (rfn->dsc) { |
| 4921 | FREE_STRING(ctx->ctx, node->dsc); |
| 4922 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4923 | } |
| 4924 | |
| 4925 | /* reference */ |
| 4926 | if (rfn->ref) { |
| 4927 | FREE_STRING(ctx->ctx, node->ref); |
| 4928 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4929 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4930 | |
| 4931 | /* config */ |
| 4932 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4933 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4934 | 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] | 4935 | } else { |
| 4936 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 4937 | flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path); |
| 4938 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | /* mandatory */ |
| 4942 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4943 | 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] | 4944 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4945 | |
| 4946 | /* presence */ |
| 4947 | if (rfn->presence) { |
| 4948 | if (node->nodetype != LYS_CONTAINER) { |
| 4949 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4950 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4951 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4952 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4953 | } |
| 4954 | node->flags |= LYS_PRESENCE; |
| 4955 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4956 | |
| 4957 | /* must */ |
| 4958 | if (rfn->musts) { |
| 4959 | switch (node->nodetype) { |
| 4960 | case LYS_LEAF: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4961 | 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] | 4962 | break; |
| 4963 | case LYS_LEAFLIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4964 | 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] | 4965 | break; |
| 4966 | case LYS_LIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4967 | 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] | 4968 | break; |
| 4969 | case LYS_CONTAINER: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4970 | 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] | 4971 | break; |
| 4972 | case LYS_ANYXML: |
| 4973 | case LYS_ANYDATA: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4974 | 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] | 4975 | break; |
| 4976 | default: |
| 4977 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4978 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 4979 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4980 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4981 | } |
| 4982 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4983 | |
| 4984 | /* min/max-elements */ |
| 4985 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4986 | switch (node->nodetype) { |
| 4987 | case LYS_LEAFLIST: |
| 4988 | if (rfn->flags & LYS_SET_MAX) { |
| 4989 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4990 | } |
| 4991 | if (rfn->flags & LYS_SET_MIN) { |
| 4992 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4993 | if (rfn->min) { |
| 4994 | node->flags |= LYS_MAND_TRUE; |
| 4995 | lys_compile_mandatory_parents(node->parent, 1); |
| 4996 | } else { |
| 4997 | node->flags &= ~LYS_MAND_TRUE; |
| 4998 | lys_compile_mandatory_parents(node->parent, 0); |
| 4999 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5000 | } |
| 5001 | break; |
| 5002 | case LYS_LIST: |
| 5003 | if (rfn->flags & LYS_SET_MAX) { |
| 5004 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5005 | } |
| 5006 | if (rfn->flags & LYS_SET_MIN) { |
| 5007 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5008 | if (rfn->min) { |
| 5009 | node->flags |= LYS_MAND_TRUE; |
| 5010 | lys_compile_mandatory_parents(node->parent, 1); |
| 5011 | } else { |
| 5012 | node->flags &= ~LYS_MAND_TRUE; |
| 5013 | lys_compile_mandatory_parents(node->parent, 0); |
| 5014 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5015 | } |
| 5016 | break; |
| 5017 | default: |
| 5018 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5019 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5020 | (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] | 5021 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5022 | } |
| 5023 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5024 | |
| 5025 | /* if-feature */ |
| 5026 | if (rfn->iffeatures) { |
| 5027 | /* 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] | 5028 | 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] | 5029 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5030 | |
| 5031 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5032 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5033 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5034 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 5035 | for (u = 0; u < refined.count; ++u) { |
| 5036 | node = (struct lysc_node*)refined.objs[u]; |
| 5037 | rfn = &uses_p->refines[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5038 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5039 | |
| 5040 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5041 | if ((node->flags & LYS_MAND_TRUE) && |
| 5042 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5043 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5044 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5045 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5046 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5047 | } |
| 5048 | |
| 5049 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5050 | if (node->nodetype == LYS_LIST) { |
| 5051 | min = ((struct lysc_node_list*)node)->min; |
| 5052 | max = ((struct lysc_node_list*)node)->max; |
| 5053 | } else { |
| 5054 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5055 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5056 | } |
| 5057 | if (min > max) { |
| 5058 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5059 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5060 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5061 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5062 | } |
| 5063 | } |
| 5064 | } |
| 5065 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5066 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5067 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5068 | |
| 5069 | cleanup: |
| 5070 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5071 | if (context_node_fake.child) { |
| 5072 | context_node_fake.child->prev = child; |
| 5073 | } |
| 5074 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5075 | child->parent = parent; |
| 5076 | } |
| 5077 | |
| 5078 | if (uses_p->augments || uses_p->refines) { |
| 5079 | /* 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] | 5080 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5081 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5082 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5083 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5084 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5085 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5086 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5087 | } |
| 5088 | } |
| 5089 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5090 | /* reload previous context's mod_def */ |
| 5091 | ctx->mod_def = mod_old; |
| 5092 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5093 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5094 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5095 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5096 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5097 | |
| 5098 | return ret; |
| 5099 | } |
| 5100 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5101 | static int |
| 5102 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5103 | { |
| 5104 | struct lysp_node *iter; |
| 5105 | int len = 0; |
| 5106 | |
| 5107 | *path = NULL; |
| 5108 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5109 | char *s = *path; |
| 5110 | char *id; |
| 5111 | |
| 5112 | switch (iter->nodetype) { |
| 5113 | case LYS_USES: |
| 5114 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5115 | break; |
| 5116 | case LYS_GROUPING: |
| 5117 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5118 | break; |
| 5119 | case LYS_AUGMENT: |
| 5120 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5121 | break; |
| 5122 | default: |
| 5123 | id = strdup(iter->name); |
| 5124 | break; |
| 5125 | } |
| 5126 | |
| 5127 | if (!iter->parent) { |
| 5128 | /* print prefix */ |
| 5129 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5130 | } else { |
| 5131 | /* prefix is the same as in parent */ |
| 5132 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5133 | } |
| 5134 | free(s); |
| 5135 | free(id); |
| 5136 | } |
| 5137 | |
| 5138 | if (len < 0) { |
| 5139 | free(*path); |
| 5140 | *path = NULL; |
| 5141 | } else if (len == 0) { |
| 5142 | *path = strdup("/"); |
| 5143 | len = 1; |
| 5144 | } |
| 5145 | return len; |
| 5146 | } |
| 5147 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5148 | /** |
| 5149 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5150 | * |
| 5151 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5152 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5153 | */ |
| 5154 | static LY_ERR |
| 5155 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5156 | { |
| 5157 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5158 | char *path; |
| 5159 | int len; |
| 5160 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5161 | struct lysp_node_uses fake_uses = { |
| 5162 | .parent = node_p, |
| 5163 | .nodetype = LYS_USES, |
| 5164 | .flags = 0, .next = NULL, |
| 5165 | .name = grp->name, |
| 5166 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5167 | .refines = NULL, .augments = NULL |
| 5168 | }; |
| 5169 | struct lysc_node_container fake_container = { |
| 5170 | .nodetype = LYS_CONTAINER, |
| 5171 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5172 | .module = ctx->mod, |
| 5173 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5174 | .prev = (struct lysc_node*)&fake_container, |
| 5175 | .name = "fake", |
| 5176 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5177 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5178 | }; |
| 5179 | |
| 5180 | if (grp->parent) { |
| 5181 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5182 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5183 | |
| 5184 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5185 | if (len < 0) { |
| 5186 | LOGMEM(ctx->ctx); |
| 5187 | return LY_EMEM; |
| 5188 | } |
| 5189 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5190 | ctx->path_len = (uint16_t)len; |
| 5191 | free(path); |
| 5192 | |
| 5193 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5194 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5195 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5196 | lysc_update_path(ctx, NULL, NULL); |
| 5197 | lysc_update_path(ctx, NULL, NULL); |
| 5198 | |
| 5199 | ctx->path_len = 1; |
| 5200 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5201 | |
| 5202 | /* cleanup */ |
| 5203 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5204 | |
| 5205 | return ret; |
| 5206 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5207 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5208 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5209 | * @brief Compile parsed schema node information. |
| 5210 | * @param[in] ctx Compile context |
| 5211 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5212 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5213 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5214 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5215 | * @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). |
| 5216 | * 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] | 5217 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5218 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5219 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5220 | 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] | 5221 | { |
| 5222 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5223 | struct lysc_node *node; |
| 5224 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5225 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5226 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5227 | 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] | 5228 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5229 | if (node_p->nodetype != LYS_USES) { |
| 5230 | lysc_update_path(ctx, parent, node_p->name); |
| 5231 | } else { |
| 5232 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5233 | lysc_update_path(ctx, NULL, node_p->name); |
| 5234 | } |
| 5235 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5236 | switch (node_p->nodetype) { |
| 5237 | case LYS_CONTAINER: |
| 5238 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5239 | node_compile_spec = lys_compile_node_container; |
| 5240 | break; |
| 5241 | case LYS_LEAF: |
| 5242 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5243 | node_compile_spec = lys_compile_node_leaf; |
| 5244 | break; |
| 5245 | case LYS_LIST: |
| 5246 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5247 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5248 | break; |
| 5249 | case LYS_LEAFLIST: |
| 5250 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5251 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5252 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5253 | case LYS_CHOICE: |
| 5254 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5255 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5256 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5257 | case LYS_ANYXML: |
| 5258 | case LYS_ANYDATA: |
| 5259 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5260 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5261 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5262 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5263 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5264 | lysc_update_path(ctx, NULL, NULL); |
| 5265 | lysc_update_path(ctx, NULL, NULL); |
| 5266 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5267 | default: |
| 5268 | LOGINT(ctx->ctx); |
| 5269 | return LY_EINT; |
| 5270 | } |
| 5271 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5272 | node->nodetype = node_p->nodetype; |
| 5273 | node->module = ctx->mod; |
| 5274 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5275 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5276 | |
| 5277 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5278 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5279 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5280 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5281 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5282 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5283 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5284 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5285 | node->flags |= LYS_CONFIG_R; |
| 5286 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5287 | /* config not explicitely set, inherit it from parent */ |
| 5288 | if (parent) { |
| 5289 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5290 | } else { |
| 5291 | /* default is config true */ |
| 5292 | node->flags |= LYS_CONFIG_W; |
| 5293 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5294 | } else { |
| 5295 | /* config set explicitely */ |
| 5296 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5297 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5298 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5299 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5300 | "Configuration node cannot be child of any state data node."); |
| 5301 | goto error; |
| 5302 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5303 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5304 | /* *list ordering */ |
| 5305 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5306 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5307 | 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] | 5308 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5309 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5310 | node->flags &= ~LYS_ORDBY_MASK; |
| 5311 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5312 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5313 | /* default ordering is system */ |
| 5314 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5315 | } |
| 5316 | } |
| 5317 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5318 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5319 | * 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] | 5320 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5321 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5322 | * 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] | 5323 | 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] | 5324 | } |
| 5325 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5326 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5327 | node->sp = node_p; |
| 5328 | } |
| 5329 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5330 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5331 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5332 | if (node_p->when) { |
| 5333 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5334 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5335 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 5336 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5337 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5338 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error); |
| 5339 | 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] | 5340 | |
| 5341 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5342 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5343 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5344 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5345 | if (node->flags & LYS_MAND_TRUE) { |
| 5346 | lys_compile_mandatory_parents(parent, 1); |
| 5347 | } |
| 5348 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5349 | lysc_update_path(ctx, NULL, NULL); |
| 5350 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5351 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5352 | if (parent) { |
| 5353 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5354 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5355 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5356 | } else { |
| 5357 | lysc_update_path(ctx, parent, node->name); |
| 5358 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5359 | 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] | 5360 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5361 | if (uses_status) { |
| 5362 | |
| 5363 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5364 | /* 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] | 5365 | * it directly gets the same status flags as the choice; |
| 5366 | * 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] | 5367 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5368 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5369 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5370 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5371 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5372 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5373 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5374 | 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] | 5375 | |
| 5376 | if (parent->nodetype == LYS_CHOICE) { |
| 5377 | lysc_update_path(ctx, NULL, NULL); |
| 5378 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5379 | } else { |
| 5380 | /* top-level element */ |
| 5381 | if (!ctx->mod->compiled->data) { |
| 5382 | ctx->mod->compiled->data = node; |
| 5383 | } else { |
| 5384 | /* insert at the end of the module's top-level nodes list */ |
| 5385 | ctx->mod->compiled->data->prev->next = node; |
| 5386 | node->prev = ctx->mod->compiled->data->prev; |
| 5387 | ctx->mod->compiled->data->prev = node; |
| 5388 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5389 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5390 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5391 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5392 | return LY_EVALID; |
| 5393 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5394 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5395 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5396 | |
| 5397 | return LY_SUCCESS; |
| 5398 | |
| 5399 | error: |
| 5400 | lysc_node_free(ctx->ctx, node); |
| 5401 | return ret; |
| 5402 | } |
| 5403 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5404 | static void |
| 5405 | lysc_disconnect(struct lysc_node *node) |
| 5406 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5407 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5408 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5409 | int remove_cs = 0; |
| 5410 | |
| 5411 | parent = node->parent; |
| 5412 | |
| 5413 | /* parent's first child */ |
| 5414 | if (!parent) { |
| 5415 | return; |
| 5416 | } |
| 5417 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5418 | cs = (struct lysc_node_case*)node; |
| 5419 | } else if (parent->nodetype == LYS_CASE) { |
| 5420 | /* disconnecting some node in a case */ |
| 5421 | cs = (struct lysc_node_case*)parent; |
| 5422 | parent = cs->parent; |
| 5423 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5424 | if (child == node) { |
| 5425 | if (cs->child == child) { |
| 5426 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5427 | /* case with a single child -> remove also the case */ |
| 5428 | child->parent = NULL; |
| 5429 | remove_cs = 1; |
| 5430 | } else { |
| 5431 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5432 | } |
| 5433 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5434 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5435 | } |
| 5436 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5437 | if (!remove_cs) { |
| 5438 | cs = NULL; |
| 5439 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5440 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5441 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5442 | } |
| 5443 | |
| 5444 | if (cs) { |
| 5445 | if (remove_cs) { |
| 5446 | /* cs has only one child which is being also removed */ |
| 5447 | lysc_disconnect((struct lysc_node*)cs); |
| 5448 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5449 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5450 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5451 | /* default case removed */ |
| 5452 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5453 | } |
| 5454 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5455 | /* first case removed */ |
| 5456 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5457 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5458 | if (cs->child) { |
| 5459 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5460 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5461 | prev = cs->child->prev; |
| 5462 | } /* else all the children are under a single case */ |
| 5463 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5464 | if (child->parent != (struct lysc_node*)cs) { |
| 5465 | break; |
| 5466 | } |
| 5467 | lysc_node_free(node->module->ctx, child); |
| 5468 | } |
| 5469 | if (prev) { |
| 5470 | if (prev->next) { |
| 5471 | prev->next = child; |
| 5472 | } |
| 5473 | if (child) { |
| 5474 | child->prev = prev; |
| 5475 | } else { |
| 5476 | /* link from the first child under the cases */ |
| 5477 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5478 | } |
| 5479 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5480 | } |
| 5481 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5485 | if (node->prev->next) { |
| 5486 | node->prev->next = node->next; |
| 5487 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5488 | if (node->next) { |
| 5489 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5490 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5491 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5492 | if (child) { |
| 5493 | child->prev = node->prev; |
| 5494 | } |
| 5495 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5496 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5497 | } |
| 5498 | } |
| 5499 | |
| 5500 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5501 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5502 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5503 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5504 | struct ly_set devs_p = {0}; |
| 5505 | struct ly_set targets = {0}; |
| 5506 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5507 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5508 | struct lysc_action *rpcs; |
| 5509 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5510 | struct lysp_deviation *dev; |
| 5511 | struct lysp_deviate *d, **dp_new; |
| 5512 | struct lysp_deviate_add *d_add; |
| 5513 | struct lysp_deviate_del *d_del; |
| 5514 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5515 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5516 | struct lysc_deviation { |
| 5517 | const char *nodeid; |
| 5518 | struct lysc_node *target; /* target node of the deviation */ |
| 5519 | 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] | 5520 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5521 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5522 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5523 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5524 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5525 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5526 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5527 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5528 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5529 | |
| 5530 | /* get all deviations from the module and all its submodules ... */ |
| 5531 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5532 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5533 | } |
| 5534 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5535 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5536 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5537 | } |
| 5538 | } |
| 5539 | if (!devs_p.count) { |
| 5540 | /* nothing to do */ |
| 5541 | return LY_SUCCESS; |
| 5542 | } |
| 5543 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5544 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5545 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5546 | /* ... and group them by the target node */ |
| 5547 | devs = calloc(devs_p.count, sizeof *devs); |
| 5548 | for (u = 0; u < devs_p.count; ++u) { |
| 5549 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5550 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5551 | |
| 5552 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5553 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5554 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5555 | if (target->nodetype == LYS_ACTION) { |
| 5556 | /* move the target pointer to input/output to make them different from the action and |
| 5557 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5558 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5559 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5560 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5561 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5562 | flags |= LYSC_OPT_INTERNAL; |
| 5563 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5564 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5565 | flags |= LYSC_OPT_INTERNAL; |
| 5566 | } |
| 5567 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5568 | /* insert into the set of targets with duplicity detection */ |
| 5569 | i = ly_set_add(&targets, target, 0); |
| 5570 | if (!devs[i]) { |
| 5571 | /* new record */ |
| 5572 | devs[i] = calloc(1, sizeof **devs); |
| 5573 | devs[i]->target = target; |
| 5574 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5575 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5576 | } |
| 5577 | /* add deviates into the deviation's list of deviates */ |
| 5578 | for (d = dev->deviates; d; d = d->next) { |
| 5579 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5580 | *dp_new = d; |
| 5581 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5582 | devs[i]->not_supported = 1; |
| 5583 | } |
| 5584 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5585 | |
| 5586 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5587 | } |
| 5588 | |
| 5589 | /* MACROS for deviates checking */ |
| 5590 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5591 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5592 | 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] | 5593 | goto cleanup; \ |
| 5594 | } |
| 5595 | |
| 5596 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5597 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5598 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \ |
| 5599 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5600 | goto cleanup; \ |
| 5601 | } |
| 5602 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5603 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5604 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5605 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5606 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5607 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5608 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5609 | goto cleanup; \ |
| 5610 | } |
| 5611 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5612 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5613 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5614 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5615 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5616 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5617 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5618 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5619 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5620 | goto cleanup; \ |
| 5621 | } |
| 5622 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5623 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5624 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5625 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5626 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5627 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5628 | goto cleanup; \ |
| 5629 | } |
| 5630 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5631 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5632 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5633 | 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] | 5634 | goto cleanup; \ |
| 5635 | } |
| 5636 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5637 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5638 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5639 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5640 | "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] | 5641 | goto cleanup; \ |
| 5642 | } |
| 5643 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5644 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5645 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5646 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5647 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5648 | 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] | 5649 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5650 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5651 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5652 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5653 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5654 | goto cleanup; \ |
| 5655 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5656 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5657 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5658 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5659 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5660 | (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] | 5661 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5662 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5663 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5664 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5665 | } |
| 5666 | |
| 5667 | /* apply deviations */ |
| 5668 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5669 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5670 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5671 | struct ly_err_item *err = NULL; |
| 5672 | |
| 5673 | dflt = NULL; |
| 5674 | changed_type = 0; |
| 5675 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5676 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5677 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5678 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5679 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5680 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5681 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5682 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5683 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5684 | } |
| 5685 | } |
| 5686 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5687 | /* not-supported */ |
| 5688 | if (devs[u]->not_supported) { |
| 5689 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 5690 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 5691 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5692 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5693 | |
| 5694 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5695 | if (devs[u]->target->parent) { \ |
| 5696 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5697 | } else { \ |
| 5698 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5699 | } \ |
| 5700 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5701 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5702 | } \ |
| 5703 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5704 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5705 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5706 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5707 | } |
| 5708 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5709 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 5710 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5711 | /* remove RPC's/action's input */ |
| 5712 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5713 | 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] | 5714 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5715 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5716 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5717 | /* remove RPC's/action's output */ |
| 5718 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5719 | 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] | 5720 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5721 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5722 | } else { |
| 5723 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5724 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5725 | } |
| 5726 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5727 | /* remove Notification */ |
| 5728 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5729 | } else { |
| 5730 | /* remove the target node */ |
| 5731 | lysc_disconnect(devs[u]->target); |
| 5732 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5733 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5734 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5735 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5736 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5737 | continue; |
| 5738 | } |
| 5739 | |
| 5740 | /* list of deviates (not-supported is not present in the list) */ |
| 5741 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5742 | d = devs[u]->deviates[v]; |
| 5743 | |
| 5744 | switch (d->mod) { |
| 5745 | case LYS_DEV_ADD: |
| 5746 | d_add = (struct lysp_deviate_add*)d; |
| 5747 | /* [units-stmt] */ |
| 5748 | if (d_add->units) { |
| 5749 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5750 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5751 | |
| 5752 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5753 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5754 | } |
| 5755 | |
| 5756 | /* *must-stmt */ |
| 5757 | if (d_add->musts) { |
| 5758 | switch (devs[u]->target->nodetype) { |
| 5759 | case LYS_CONTAINER: |
| 5760 | case LYS_LIST: |
| 5761 | 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] | 5762 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5763 | break; |
| 5764 | case LYS_LEAF: |
| 5765 | case LYS_LEAFLIST: |
| 5766 | case LYS_ANYDATA: |
| 5767 | 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] | 5768 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5769 | break; |
| 5770 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5771 | 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] | 5772 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5773 | break; |
| 5774 | case LYS_ACTION: |
| 5775 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5776 | 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] | 5777 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5778 | break; |
| 5779 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5780 | 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] | 5781 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5782 | break; |
| 5783 | } |
| 5784 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5785 | default: |
| 5786 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5787 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5788 | goto cleanup; |
| 5789 | } |
| 5790 | } |
| 5791 | |
| 5792 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5793 | if (d_add->uniques) { |
| 5794 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5795 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5796 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5797 | |
| 5798 | /* *default-stmt */ |
| 5799 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5800 | switch (devs[u]->target->nodetype) { |
| 5801 | case LYS_LEAF: |
| 5802 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5803 | 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] | 5804 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5805 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5806 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5807 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5808 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5809 | } else { |
| 5810 | /* prepare new default value storage */ |
| 5811 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5812 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5813 | dflt = d_add->dflts[0]; |
| 5814 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5815 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5816 | /* mark the new default values as leaf's own */ |
| 5817 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5818 | break; |
| 5819 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5820 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5821 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5822 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5823 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5824 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5825 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5826 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5827 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5828 | LY_ARRAY_FREE(llist->dflts); |
| 5829 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5830 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5831 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5832 | } |
| 5833 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5834 | 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] | 5835 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 5836 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5837 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5838 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5839 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5840 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5841 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5842 | llist->dflts[x]->realtype = llist->type; |
| 5843 | 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] | 5844 | 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] | 5845 | (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] | 5846 | llist->dflts[x]->realtype->refcount++; |
| 5847 | if (err) { |
| 5848 | ly_err_print(err); |
| 5849 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5850 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5851 | d_add->dflts[x - y], err->msg); |
| 5852 | ly_err_free(err); |
| 5853 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5854 | if (rc == LY_EINCOMPLETE) { |
| 5855 | /* postpone default compilation when the tree is complete */ |
| 5856 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5857 | |
| 5858 | /* but in general result is so far ok */ |
| 5859 | rc = LY_SUCCESS; |
| 5860 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5861 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5862 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5863 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5864 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5865 | break; |
| 5866 | case LYS_CHOICE: |
| 5867 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5868 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5869 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5870 | * 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] | 5871 | 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] | 5872 | goto cleanup; |
| 5873 | } |
| 5874 | break; |
| 5875 | default: |
| 5876 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5877 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5878 | goto cleanup; |
| 5879 | } |
| 5880 | } |
| 5881 | |
| 5882 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5883 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 5884 | 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] | 5885 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5886 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5887 | goto cleanup; |
| 5888 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5889 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5890 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 5891 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5892 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5893 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5894 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5895 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5896 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5897 | goto cleanup; |
| 5898 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5899 | 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] | 5900 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5901 | |
| 5902 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5903 | if (d_add->flags & LYS_MAND_MASK) { |
| 5904 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5905 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5906 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5907 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5908 | goto cleanup; |
| 5909 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5910 | 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] | 5911 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5912 | |
| 5913 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5914 | if (d_add->flags & LYS_SET_MIN) { |
| 5915 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5916 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5917 | /* change value */ |
| 5918 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5919 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5920 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5921 | /* change value */ |
| 5922 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5923 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5924 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5925 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5926 | goto cleanup; |
| 5927 | } |
| 5928 | if (d_add->min) { |
| 5929 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5930 | } |
| 5931 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5932 | |
| 5933 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5934 | if (d_add->flags & LYS_SET_MAX) { |
| 5935 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5936 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5937 | /* change value */ |
| 5938 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5939 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5940 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5941 | /* change value */ |
| 5942 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5943 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5944 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5945 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 5946 | goto cleanup; |
| 5947 | } |
| 5948 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5949 | |
| 5950 | break; |
| 5951 | case LYS_DEV_DELETE: |
| 5952 | d_del = (struct lysp_deviate_del*)d; |
| 5953 | |
| 5954 | /* [units-stmt] */ |
| 5955 | if (d_del->units) { |
| 5956 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5957 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 5958 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 5959 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5960 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5961 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5962 | goto cleanup; |
| 5963 | } |
| 5964 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5965 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5966 | } |
| 5967 | |
| 5968 | /* *must-stmt */ |
| 5969 | if (d_del->musts) { |
| 5970 | switch (devs[u]->target->nodetype) { |
| 5971 | case LYS_CONTAINER: |
| 5972 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5973 | 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] | 5974 | break; |
| 5975 | case LYS_LEAF: |
| 5976 | case LYS_LEAFLIST: |
| 5977 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5978 | 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] | 5979 | break; |
| 5980 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5981 | 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] | 5982 | break; |
| 5983 | case LYS_ACTION: |
| 5984 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5985 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5986 | break; |
| 5987 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5988 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5989 | break; |
| 5990 | } |
| 5991 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5992 | default: |
| 5993 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5994 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5995 | goto cleanup; |
| 5996 | } |
| 5997 | } |
| 5998 | |
| 5999 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6000 | if (d_del->uniques) { |
| 6001 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6002 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6003 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6004 | LY_ARRAY_FOR(list->uniques, z) { |
| 6005 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6006 | nodeid = strpbrk(name, " \t\n"); |
| 6007 | if (nodeid) { |
| 6008 | if (strncmp(name, list->uniques[z][y]->name, nodeid - name) |
| 6009 | || list->uniques[z][y]->name[nodeid - name] != '\0') { |
| 6010 | break; |
| 6011 | } |
| 6012 | while (isspace(*nodeid)) { |
| 6013 | ++nodeid; |
| 6014 | } |
| 6015 | } else { |
| 6016 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6017 | break; |
| 6018 | } |
| 6019 | } |
| 6020 | } |
| 6021 | if (!name) { |
| 6022 | /* complete match - remove the unique */ |
| 6023 | LY_ARRAY_DECREMENT(list->uniques); |
| 6024 | LY_ARRAY_FREE(list->uniques[z]); |
| 6025 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6026 | --z; |
| 6027 | break; |
| 6028 | } |
| 6029 | } |
| 6030 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6031 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6032 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6033 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6034 | goto cleanup; |
| 6035 | } |
| 6036 | } |
| 6037 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6038 | LY_ARRAY_FREE(list->uniques); |
| 6039 | list->uniques = NULL; |
| 6040 | } |
| 6041 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6042 | |
| 6043 | /* *default-stmt */ |
| 6044 | if (d_del->dflts) { |
| 6045 | switch (devs[u]->target->nodetype) { |
| 6046 | case LYS_LEAF: |
| 6047 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6048 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6049 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6050 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6051 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6052 | 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] | 6053 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6054 | if (i) { |
| 6055 | free((char*)dflt); |
| 6056 | } |
| 6057 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6058 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6059 | d_del->dflts[0], dflt); |
| 6060 | goto cleanup; |
| 6061 | } |
| 6062 | if (i) { |
| 6063 | free((char*)dflt); |
| 6064 | } |
| 6065 | dflt = NULL; |
| 6066 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6067 | /* update the list of incomplete default values if needed */ |
| 6068 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6069 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6070 | /* remove the default specification */ |
| 6071 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6072 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6073 | free(leaf->dflt); |
| 6074 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6075 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6076 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6077 | break; |
| 6078 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6079 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6080 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6081 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6082 | 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] | 6083 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6084 | if (i) { |
| 6085 | free((char*)dflt); |
| 6086 | } |
| 6087 | dflt = NULL; |
| 6088 | break; |
| 6089 | } |
| 6090 | if (i) { |
| 6091 | free((char*)dflt); |
| 6092 | } |
| 6093 | dflt = NULL; |
| 6094 | } |
| 6095 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6096 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6097 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6098 | goto cleanup; |
| 6099 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6100 | |
| 6101 | /* update the list of incomplete default values if needed */ |
| 6102 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6103 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6104 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6105 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6106 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6107 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6108 | free(llist->dflts[y]); |
| 6109 | 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] | 6110 | 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] | 6111 | } |
| 6112 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6113 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6114 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6115 | LY_ARRAY_FREE(llist->dflts); |
| 6116 | llist->dflts = NULL; |
| 6117 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6118 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6119 | break; |
| 6120 | case LYS_CHOICE: |
| 6121 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6122 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6123 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6124 | 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] | 6125 | if (prefix) { |
| 6126 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6127 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6128 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6129 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6130 | "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] | 6131 | goto cleanup; |
| 6132 | } |
| 6133 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6134 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6135 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6136 | "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] | 6137 | goto cleanup; |
| 6138 | } |
| 6139 | } |
| 6140 | /* else { |
| 6141 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6142 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6143 | * the module of the default case, just matching its name */ |
| 6144 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6145 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6146 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6147 | 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] | 6148 | goto cleanup; |
| 6149 | } |
| 6150 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6151 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6152 | break; |
| 6153 | default: |
| 6154 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6155 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6156 | goto cleanup; |
| 6157 | } |
| 6158 | } |
| 6159 | |
| 6160 | break; |
| 6161 | case LYS_DEV_REPLACE: |
| 6162 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6163 | |
| 6164 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6165 | if (d_rpl->type) { |
| 6166 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6167 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6168 | 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] | 6169 | |
| 6170 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6171 | /* the target has default from the previous type - remove it */ |
| 6172 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6173 | /* update the list of incomplete default values if needed */ |
| 6174 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6175 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6176 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6177 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6178 | free(leaf->dflt); |
| 6179 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6180 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6181 | } else { /* LYS_LEAFLIST */ |
| 6182 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6183 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6184 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6185 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6186 | free(llist->dflts[x]); |
| 6187 | } |
| 6188 | LY_ARRAY_FREE(llist->dflts); |
| 6189 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6190 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6191 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6192 | } |
| 6193 | } |
| 6194 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6195 | /* there is no default value, do not set changed_type after type compilation |
| 6196 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6197 | changed_type = -1; |
| 6198 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6199 | 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] | 6200 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6201 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6202 | |
| 6203 | /* [units-stmt] */ |
| 6204 | if (d_rpl->units) { |
| 6205 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6206 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6207 | units, "replacing", "units", d_rpl->units); |
| 6208 | |
| 6209 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6210 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6211 | } |
| 6212 | |
| 6213 | /* [default-stmt] */ |
| 6214 | if (d_rpl->dflt) { |
| 6215 | switch (devs[u]->target->nodetype) { |
| 6216 | case LYS_LEAF: |
| 6217 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6218 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6219 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6220 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6221 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6222 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6223 | dflt = d_rpl->dflt; |
| 6224 | /* 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] | 6225 | break; |
| 6226 | case LYS_CHOICE: |
| 6227 | 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] | 6228 | 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] | 6229 | goto cleanup; |
| 6230 | } |
| 6231 | break; |
| 6232 | default: |
| 6233 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6234 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6235 | goto cleanup; |
| 6236 | } |
| 6237 | } |
| 6238 | |
| 6239 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6240 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 6241 | 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] | 6242 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6243 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6244 | goto cleanup; |
| 6245 | } |
| 6246 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6247 | 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] | 6248 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6249 | goto cleanup; |
| 6250 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6251 | 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] | 6252 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6253 | |
| 6254 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6255 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6256 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6257 | 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] | 6258 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6259 | goto cleanup; |
| 6260 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6261 | 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] | 6262 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6263 | |
| 6264 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6265 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6266 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6267 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6268 | /* change value */ |
| 6269 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6270 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6271 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6272 | /* change value */ |
| 6273 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6274 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6275 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6276 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6277 | goto cleanup; |
| 6278 | } |
| 6279 | if (d_rpl->min) { |
| 6280 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6281 | } |
| 6282 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6283 | |
| 6284 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6285 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6286 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6287 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6288 | /* change value */ |
| 6289 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6290 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6291 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6292 | /* change value */ |
| 6293 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6294 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6295 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6296 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6297 | goto cleanup; |
| 6298 | } |
| 6299 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6300 | |
| 6301 | break; |
| 6302 | default: |
| 6303 | LOGINT(ctx->ctx); |
| 6304 | goto cleanup; |
| 6305 | } |
| 6306 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6307 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6308 | /* final check when all deviations of a single target node are applied */ |
| 6309 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6310 | /* check min-max compatibility */ |
| 6311 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6312 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6313 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6314 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6315 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6316 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6317 | } else { |
| 6318 | min = max = 0; |
| 6319 | } |
| 6320 | if (min > max) { |
| 6321 | 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] | 6322 | "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] | 6323 | goto cleanup; |
| 6324 | } |
| 6325 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6326 | if (dflt) { |
| 6327 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6328 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6329 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6330 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6331 | 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] | 6332 | 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] | 6333 | leaf->dflt->realtype->refcount++; |
| 6334 | if (err) { |
| 6335 | ly_err_print(err); |
| 6336 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6337 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6338 | ly_err_free(err); |
| 6339 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6340 | if (rc == LY_EINCOMPLETE) { |
| 6341 | /* postpone default compilation when the tree is complete */ |
| 6342 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6343 | |
| 6344 | /* but in general result is so far ok */ |
| 6345 | rc = LY_SUCCESS; |
| 6346 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6347 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6348 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6349 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6350 | int dynamic; |
| 6351 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6352 | 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] | 6353 | |
| 6354 | /* update the list of incomplete default values if needed */ |
| 6355 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6356 | |
| 6357 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6358 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6359 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6360 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6361 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6362 | 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] | 6363 | 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] | 6364 | leaf->dflt->realtype->refcount++; |
| 6365 | if (err) { |
| 6366 | ly_err_print(err); |
| 6367 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6368 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6369 | ly_err_free(err); |
| 6370 | } |
| 6371 | if (dynamic) { |
| 6372 | free((void*)dflt); |
| 6373 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6374 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6375 | if (rc == LY_EINCOMPLETE) { |
| 6376 | /* postpone default compilation when the tree is complete */ |
| 6377 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6378 | |
| 6379 | /* but in general result is so far ok */ |
| 6380 | rc = LY_SUCCESS; |
| 6381 | } |
| 6382 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6383 | } else { /* LYS_LEAFLIST */ |
| 6384 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6385 | 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] | 6386 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6387 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6388 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6389 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6390 | 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] | 6391 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6392 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6393 | llist->dflts[x]->realtype->refcount++; |
| 6394 | if (err) { |
| 6395 | ly_err_print(err); |
| 6396 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6397 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6398 | dflt, err->msg); |
| 6399 | ly_err_free(err); |
| 6400 | } |
| 6401 | if (dynamic) { |
| 6402 | free((void*)dflt); |
| 6403 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6404 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6405 | if (rc == LY_EINCOMPLETE) { |
| 6406 | /* postpone default compilation when the tree is complete */ |
| 6407 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6408 | |
| 6409 | /* but in general result is so far ok */ |
| 6410 | rc = LY_SUCCESS; |
| 6411 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6412 | LY_CHECK_GOTO(rc, cleanup); |
| 6413 | } |
| 6414 | } |
| 6415 | } |
| 6416 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6417 | /* check mandatory - default compatibility */ |
| 6418 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6419 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6420 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6421 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6422 | "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] | 6423 | goto cleanup; |
| 6424 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6425 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6426 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6427 | 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] | 6428 | goto cleanup; |
| 6429 | } |
| 6430 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6431 | /* mandatory node under a default case */ |
| 6432 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6433 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6434 | 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] | 6435 | goto cleanup; |
| 6436 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6437 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6438 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6439 | } |
| 6440 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6441 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6442 | ret = LY_SUCCESS; |
| 6443 | |
| 6444 | cleanup: |
| 6445 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6446 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6447 | free(devs[u]); |
| 6448 | } |
| 6449 | free(devs); |
| 6450 | ly_set_erase(&targets, NULL); |
| 6451 | ly_set_erase(&devs_p, NULL); |
| 6452 | |
| 6453 | return ret; |
| 6454 | } |
| 6455 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6456 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6457 | * @brief Compile the given YANG submodule into the main module. |
| 6458 | * @param[in] ctx Compile context |
| 6459 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6460 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6461 | */ |
| 6462 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6463 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6464 | { |
| 6465 | unsigned int u; |
| 6466 | LY_ERR ret = LY_SUCCESS; |
| 6467 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6468 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6469 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6470 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6471 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6472 | if (!mainmod->mod->off_features) { |
| 6473 | /* features are compiled directly into the compiled module structure, |
| 6474 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6475 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6476 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6477 | mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features); |
| 6478 | LY_CHECK_GOTO(ret, error); |
| 6479 | } |
| 6480 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6481 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6482 | 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] | 6483 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6484 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6485 | /* data nodes */ |
| 6486 | LY_LIST_FOR(submod->data, node_p) { |
| 6487 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6488 | LY_CHECK_GOTO(ret, error); |
| 6489 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6490 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6491 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6492 | 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] | 6493 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6494 | error: |
| 6495 | return ret; |
| 6496 | } |
| 6497 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6498 | LY_ERR |
| 6499 | lys_compile(struct lys_module *mod, int options) |
| 6500 | { |
| 6501 | struct lysc_ctx ctx = {0}; |
| 6502 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6503 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6504 | struct lysp_module *sp; |
| 6505 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6506 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6507 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6508 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6509 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6510 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6511 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6512 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 6513 | |
| 6514 | if (!mod->implemented) { |
| 6515 | /* just imported modules are not compiled */ |
| 6516 | return LY_SUCCESS; |
| 6517 | } |
| 6518 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6519 | sp = mod->parsed; |
| 6520 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6521 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6522 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6523 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6524 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6525 | ctx.path_len = 1; |
| 6526 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6527 | |
| 6528 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6529 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 6530 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6531 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6532 | 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] | 6533 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6534 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6535 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6536 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6537 | if (mod->off_features) { |
| 6538 | /* there is already precompiled array of features */ |
| 6539 | mod_c->features = mod->off_features; |
| 6540 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6541 | } else { |
| 6542 | /* features are compiled directly into the compiled module structure, |
| 6543 | * 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] | 6544 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6545 | LY_CHECK_GOTO(ret, error); |
| 6546 | } |
| 6547 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 6548 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 6549 | * are present. */ |
| 6550 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6551 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6552 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6553 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6554 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6555 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6556 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6557 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6558 | 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] | 6559 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6560 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6561 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6562 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6563 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6564 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6565 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6566 | 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] | 6567 | if (sp->identities) { |
| 6568 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 6569 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6570 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6571 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6572 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6573 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6574 | ret = lys_compile_node(&ctx, node_p, NULL, 0); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6575 | LY_CHECK_GOTO(ret, error); |
| 6576 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6577 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6578 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6579 | 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] | 6580 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6581 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 6582 | ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6583 | LY_CHECK_GOTO(ret, error); |
| 6584 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6585 | ret = lys_compile_augment(&ctx, augments[u], NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6586 | LY_CHECK_GOTO(ret, error); |
| 6587 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6588 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6589 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6590 | ret = lys_compile_deviations(&ctx, sp); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6591 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6592 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6593 | 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] | 6594 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6595 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6596 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 6597 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 6598 | * 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] | 6599 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6600 | 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] | 6601 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6602 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6603 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6604 | 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] | 6605 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6606 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6607 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6608 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6609 | /* validate the path */ |
| 6610 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 6611 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 6612 | LY_CHECK_GOTO(ret, error); |
| 6613 | } |
| 6614 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6615 | } |
| 6616 | } |
| 6617 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6618 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6619 | 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] | 6620 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6621 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6622 | /* store pointer to the real type */ |
| 6623 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 6624 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6625 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6626 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6627 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6628 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6629 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6630 | /* store pointer to the real type */ |
| 6631 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 6632 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6633 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6634 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 6635 | } |
| 6636 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6637 | } |
| 6638 | } |
| 6639 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6640 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6641 | /* finish incomplete default values compilation */ |
| 6642 | for (u = 0; u < ctx.dflts.count; ++u) { |
| 6643 | struct ly_err_item *err = NULL; |
| 6644 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[u]; |
| 6645 | ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized), |
| 6646 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 6647 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 6648 | if (err) { |
| 6649 | ly_err_print(err); |
| 6650 | ctx.path[0] = '\0'; |
| 6651 | lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE); |
| 6652 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 6653 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6654 | ly_err_free(err); |
| 6655 | } |
| 6656 | LY_CHECK_GOTO(ret, error); |
| 6657 | } |
| 6658 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6659 | /* validate non-instantiated groupings from the parsed schema, |
| 6660 | * without it we would accept even the schemas with invalid grouping specification */ |
| 6661 | ctx.options |= LYSC_OPT_GROUPING; |
| 6662 | LY_ARRAY_FOR(sp->groupings, u) { |
| 6663 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
| 6664 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error); |
| 6665 | } |
| 6666 | } |
| 6667 | LY_LIST_FOR(sp->data, node_p) { |
| 6668 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 6669 | LY_ARRAY_FOR(grps, u) { |
| 6670 | if (!(grps[u].flags & LYS_USED_GRP)) { |
| 6671 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error); |
| 6672 | } |
| 6673 | } |
| 6674 | } |
| 6675 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6676 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 6677 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 6678 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 6679 | } |
| 6680 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6681 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6682 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6683 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6684 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6685 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6686 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6687 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6688 | lysp_module_free(mod->parsed); |
| 6689 | ((struct lys_module*)mod)->parsed = NULL; |
| 6690 | } |
| 6691 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6692 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6693 | /* remove flag of the modules implemented by dependency */ |
| 6694 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6695 | m = ctx.ctx->list.objs[u]; |
| 6696 | if (m->implemented == 2) { |
| 6697 | m->implemented = 1; |
| 6698 | } |
| 6699 | } |
| 6700 | } |
| 6701 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6702 | ((struct lys_module*)mod)->compiled = mod_c; |
| 6703 | return LY_SUCCESS; |
| 6704 | |
| 6705 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6706 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6707 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6708 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6709 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6710 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6711 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6712 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6713 | mod->compiled = NULL; |
| 6714 | |
| 6715 | /* revert compilation of modules implemented by dependency */ |
| 6716 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6717 | m = ctx.ctx->list.objs[u]; |
| 6718 | if (m->implemented == 2) { |
| 6719 | /* revert features list to the precompiled state */ |
| 6720 | lys_feature_precompile_revert(&ctx, m); |
| 6721 | /* mark module as imported-only / not-implemented */ |
| 6722 | m->implemented = 0; |
| 6723 | /* free the compiled version of the module */ |
| 6724 | lysc_module_free(m->compiled, NULL); |
| 6725 | m->compiled = NULL; |
| 6726 | } |
| 6727 | } |
| 6728 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6729 | return ret; |
| 6730 | } |