Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #include "common.h" |
| 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 24 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 25 | #include "dict.h" |
| 26 | #include "log.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 27 | #include "plugins_exts.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "set.h" |
| 29 | #include "plugins_types.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 30 | #include "plugins_exts_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | #include "tree.h" |
| 32 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 33 | #include "tree_schema_internal.h" |
| 34 | #include "xpath.h" |
| 35 | |
| 36 | /** |
| 37 | * @brief Duplicate string into dictionary |
| 38 | * @param[in] CTX libyang context of the dictionary. |
| 39 | * @param[in] ORIG String to duplicate. |
| 40 | * @param[out] DUP Where to store the result. |
| 41 | */ |
| 42 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 43 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 44 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 45 | if (ARRAY_P) { \ |
| 46 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 47 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 48 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 49 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 50 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 51 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 52 | } \ |
| 53 | } |
| 54 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 55 | #define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 56 | if (ARRAY_P) { \ |
| 57 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 58 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 59 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 60 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 61 | RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 62 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 63 | } \ |
| 64 | } |
| 65 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 66 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 67 | if (EXTS_P) { \ |
| 68 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \ |
| 69 | for (uint32_t __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \ |
| 70 | LY_ARRAY_INCREMENT(EXT_C); \ |
| 71 | RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE); \ |
| 72 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 73 | } \ |
| 74 | } |
| 75 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 76 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 77 | if (ARRAY_P) { \ |
| 78 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 79 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 80 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 81 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 82 | RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 83 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 84 | } \ |
| 85 | } |
| 86 | |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 87 | #define COMPILE_ARRAY_MUST_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, PARENT, RET, GOTO) \ |
| 88 | if (ARRAY_P) { \ |
| 89 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 90 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 91 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 92 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
| 93 | RET = lys_compile_must(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
| 94 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 95 | (ARRAY_C)[ITER + __array_offset].context = lysc_xpath_context((struct lysc_node*)PARENT); \ |
| 96 | } \ |
| 97 | } |
| 98 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 99 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 100 | if (MEMBER_P) { \ |
| 101 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 102 | 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] | 103 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 104 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 105 | } |
| 106 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 107 | #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] | 108 | if (MEMBER_P) { \ |
| 109 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
| 110 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 111 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 112 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 113 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 114 | } |
| 115 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 116 | #define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 117 | if (ARRAY) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 118 | for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
| 119 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 120 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 121 | return LY_EVALID; \ |
| 122 | } \ |
| 123 | } \ |
| 124 | } |
| 125 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 126 | static struct lysc_ext_instance * |
| 127 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 128 | { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 129 | /* TODO - extensions */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 130 | (void) ctx; |
| 131 | (void) orig; |
| 132 | return NULL; |
| 133 | } |
| 134 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 135 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 136 | * @brief Add record into the compile context's list of incomplete default values. |
| 137 | * @param[in] ctx Compile context with the incomplete default values list. |
| 138 | * @param[in] context_node Context schema node to store in the record. |
| 139 | * @param[in] dflt Incomplete default value to store in the record. |
| 140 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 141 | * @return LY_EMEM in case of memory allocation failure. |
| 142 | * @return LY_SUCCESS |
| 143 | */ |
| 144 | static LY_ERR |
| 145 | lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod) |
| 146 | { |
| 147 | struct lysc_incomplete_dflt *r; |
| 148 | r = malloc(sizeof *r); |
| 149 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 150 | r->context_node = context_node; |
| 151 | r->dflt = dflt; |
| 152 | r->dflt_mod = dflt_mod; |
| 153 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 154 | |
| 155 | return LY_SUCCESS; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 160 | * @param[in] ctx Compile context with the incomplete default values list. |
| 161 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 162 | */ |
| 163 | static void |
| 164 | lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt) |
| 165 | { |
| 166 | unsigned int u; |
| 167 | struct lysc_incomplete_dflt *r; |
| 168 | |
| 169 | for (u = 0; u < ctx->dflts.count; ++u) { |
| 170 | r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u]; |
| 171 | if (r->dflt == dflt) { |
| 172 | free(ctx->dflts.objs[u]); |
| 173 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 174 | --ctx->dflts.count; |
| 175 | return; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 180 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 181 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 182 | { |
| 183 | int len; |
| 184 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 185 | |
| 186 | if (!name) { |
| 187 | /* removing last path segment */ |
| 188 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 189 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 190 | if (ctx->path[ctx->path_len] == '=') { |
| 191 | ctx->path[ctx->path_len++] = '}'; |
| 192 | } else { |
| 193 | /* not a top-level special tag, remove also preceiding '/' */ |
| 194 | goto remove_nodelevel; |
| 195 | } |
| 196 | } else { |
| 197 | remove_nodelevel: |
| 198 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 199 | if (ctx->path_len == 0) { |
| 200 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 201 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | /* set new terminating NULL-byte */ |
| 205 | ctx->path[ctx->path_len] = '\0'; |
| 206 | } else { |
| 207 | if (ctx->path_len > 1) { |
| 208 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 209 | /* extension of the special tag */ |
| 210 | nextlevel = 2; |
| 211 | --ctx->path_len; |
| 212 | } else { |
| 213 | /* there is already some path, so add next level */ |
| 214 | nextlevel = 1; |
| 215 | } |
| 216 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 217 | |
| 218 | if (nextlevel != 2) { |
| 219 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 220 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 221 | 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] | 222 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 223 | 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] | 224 | } |
| 225 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 226 | 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] | 227 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 228 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 229 | /* output truncated */ |
| 230 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 231 | } else { |
| 232 | ctx->path_len += len; |
| 233 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 238 | * @brief Duplicate the compiled pattern structure. |
| 239 | * |
| 240 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 241 | * |
| 242 | * @param[in] orig The pattern structure to duplicate. |
| 243 | * @return The duplicated structure to use. |
| 244 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 245 | static struct lysc_pattern* |
| 246 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 247 | { |
| 248 | ++orig->refcount; |
| 249 | return orig; |
| 250 | } |
| 251 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 252 | /** |
| 253 | * @brief Duplicate the array of compiled patterns. |
| 254 | * |
| 255 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 256 | * |
| 257 | * @param[in] ctx Libyang context for logging. |
| 258 | * @param[in] orig The patterns sized array to duplicate. |
| 259 | * @return New sized array as a copy of @p orig. |
| 260 | * @return NULL in case of memory allocation error. |
| 261 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 262 | static struct lysc_pattern** |
| 263 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 264 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 265 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 266 | unsigned int u; |
| 267 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 268 | assert(orig); |
| 269 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 270 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 271 | LY_ARRAY_FOR(orig, u) { |
| 272 | dup[u] = lysc_pattern_dup(orig[u]); |
| 273 | LY_ARRAY_INCREMENT(dup); |
| 274 | } |
| 275 | return dup; |
| 276 | } |
| 277 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 278 | /** |
| 279 | * @brief Duplicate compiled range structure. |
| 280 | * |
| 281 | * @param[in] ctx Libyang context for logging. |
| 282 | * @param[in] orig The range structure to be duplicated. |
| 283 | * @return New compiled range structure as a copy of @p orig. |
| 284 | * @return NULL in case of memory allocation error. |
| 285 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 286 | struct lysc_range* |
| 287 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 288 | { |
| 289 | struct lysc_range *dup; |
| 290 | LY_ERR ret; |
| 291 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 292 | assert(orig); |
| 293 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 294 | dup = calloc(1, sizeof *dup); |
| 295 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 296 | if (orig->parts) { |
| 297 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 298 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 299 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 300 | } |
| 301 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 302 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 303 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 304 | |
| 305 | return dup; |
| 306 | cleanup: |
| 307 | free(dup); |
| 308 | (void) ret; /* set but not used due to the return type */ |
| 309 | return NULL; |
| 310 | } |
| 311 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 312 | /** |
| 313 | * @brief Stack for processing if-feature expressions. |
| 314 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 315 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 316 | int size; /**< number of items in the stack */ |
| 317 | int index; /**< first empty item */ |
| 318 | 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] | 319 | }; |
| 320 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 321 | /** |
| 322 | * @brief Add @ref ifftokens into the stack. |
| 323 | * @param[in] stack The if-feature stack to use. |
| 324 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 325 | * @return LY_EMEM in case of memory allocation error |
| 326 | * @return LY_ESUCCESS if the value successfully stored. |
| 327 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 328 | static LY_ERR |
| 329 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 330 | { |
| 331 | if (stack->index == stack->size) { |
| 332 | stack->size += 4; |
| 333 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 334 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 335 | } |
| 336 | stack->stack[stack->index++] = value; |
| 337 | return LY_SUCCESS; |
| 338 | } |
| 339 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 340 | /** |
| 341 | * @brief Get (and remove) the last item form the stack. |
| 342 | * @param[in] stack The if-feature stack to use. |
| 343 | * @return The value from the top of the stack. |
| 344 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 345 | static uint8_t |
| 346 | iff_stack_pop(struct iff_stack *stack) |
| 347 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 348 | assert(stack && stack->index); |
| 349 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 350 | stack->index--; |
| 351 | return stack->stack[stack->index]; |
| 352 | } |
| 353 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 354 | /** |
| 355 | * @brief Clean up the stack. |
| 356 | * @param[in] stack The if-feature stack to use. |
| 357 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 358 | static void |
| 359 | iff_stack_clean(struct iff_stack *stack) |
| 360 | { |
| 361 | stack->size = 0; |
| 362 | free(stack->stack); |
| 363 | } |
| 364 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 365 | /** |
| 366 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 367 | * (libyang format of the if-feature expression). |
| 368 | * @param[in,out] list The 2bits array to modify. |
| 369 | * @param[in] op The operand (@ref ifftokens) to store. |
| 370 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 371 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 372 | static void |
| 373 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 374 | { |
| 375 | uint8_t *item; |
| 376 | uint8_t mask = 3; |
| 377 | |
| 378 | assert(pos >= 0); |
| 379 | assert(op <= 3); /* max 2 bits */ |
| 380 | |
| 381 | item = &list[pos / 4]; |
| 382 | mask = mask << 2 * (pos % 4); |
| 383 | *item = (*item) & ~mask; |
| 384 | *item = (*item) | (op << 2 * (pos % 4)); |
| 385 | } |
| 386 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 387 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 388 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 389 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 390 | /** |
| 391 | * @brief Find a feature of the given name and referenced in the given module. |
| 392 | * |
| 393 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 394 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 395 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 396 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 397 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 398 | * assigned till that time will be still valid. |
| 399 | * |
| 400 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 401 | * @param[in] name Name of the feature including possible prefix. |
| 402 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 403 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 404 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 405 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 406 | 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] | 407 | { |
| 408 | size_t i; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 409 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 410 | |
| 411 | for (i = 0; i < len; ++i) { |
| 412 | if (name[i] == ':') { |
| 413 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 414 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 415 | LY_CHECK_RET(!mod, NULL); |
| 416 | |
| 417 | name = &name[i + 1]; |
| 418 | len = len - i - 1; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 423 | if (mod->implemented) { |
| 424 | /* module is implemented so there is already the compiled schema */ |
| 425 | flist = mod->compiled->features; |
| 426 | } else { |
| 427 | flist = mod->off_features; |
| 428 | } |
| 429 | LY_ARRAY_FOR(flist, i) { |
| 430 | f = &flist[i]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 431 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 432 | return f; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | return NULL; |
| 437 | } |
| 438 | |
| 439 | static LY_ERR |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 440 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, LYEXT_PARENT parent_type) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 441 | { |
| 442 | const char *name; |
| 443 | unsigned int u; |
| 444 | const struct lys_module *mod; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 445 | struct lysc_ext *elist = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 446 | |
| 447 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 448 | ext->insubstmt = ext_p->insubstmt; |
| 449 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 450 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 451 | ext->parent = parent; |
| 452 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 453 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 454 | lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}"); |
| 455 | lysc_update_path(ctx, NULL, ext_p->name ); |
| 456 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 457 | /* get module where the extension definition should be placed */ |
| 458 | for (u = 0; ext_p->name[u] != ':'; ++u); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 459 | mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 460 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 461 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 462 | LY_EVALID); |
| 463 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 464 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 465 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 466 | ext_p->name, mod->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 467 | LY_EVALID); |
| 468 | name = &ext_p->name[u + 1]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 469 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 470 | /* find the extension definition there */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 471 | if (mod->off_extensions) { |
| 472 | elist = mod->off_extensions; |
| 473 | } else { |
| 474 | elist = mod->compiled->extensions; |
| 475 | } |
| 476 | LY_ARRAY_FOR(elist, u) { |
| 477 | if (!strcmp(name, elist[u].name)) { |
| 478 | ext->def = &elist[u]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 479 | break; |
| 480 | } |
| 481 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 482 | LY_CHECK_ERR_RET(!ext->def, |
| 483 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 484 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 485 | LY_EVALID); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 486 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 487 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 488 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 489 | * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */ |
| 490 | if (ext_p->yin && ext->def->argument) { |
| 491 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 492 | struct lysp_stmt *stmt = NULL; |
| 493 | |
| 494 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 495 | /* ... argument was the first XML child element */ |
| 496 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 497 | /* TODO check namespace of the statement */ |
| 498 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 499 | stmt = ext_p->child; |
| 500 | } |
| 501 | } |
| 502 | } else { |
| 503 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 504 | * with LYS_YIN_ATTR flag */ |
| 505 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 506 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 507 | /* this is the extension's argument */ |
| 508 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | if (!stmt) { |
| 514 | /* missing extension's argument */ |
| 515 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 516 | "Extension instance \"%s\" misses argument \"%s\".", ext_p->name, ext->def->argument); |
| 517 | return LY_EVALID; |
| 518 | |
| 519 | } |
| 520 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 521 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 522 | } |
| 523 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 524 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 525 | if (ext->argument) { |
| 526 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 527 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 528 | LY_CHECK_RET(ext->def->plugin->compile(ctx, ext_p, ext), LY_EVALID); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 529 | if (ext->argument) { |
| 530 | lysc_update_path(ctx, NULL, NULL); |
| 531 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 532 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 533 | ext_p->compiled = ext; |
| 534 | |
| 535 | lysc_update_path(ctx, NULL, NULL); |
| 536 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 537 | |
| 538 | return LY_SUCCESS; |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 543 | */ |
| 544 | static LY_ERR |
| 545 | lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext) |
| 546 | { |
| 547 | LY_ERR ret = LY_SUCCESS; |
| 548 | |
| 549 | DUP_STRING(ctx->ctx, ext_p->name, ext->name); |
| 550 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 551 | ext->module = ctx->mod_def; |
| 552 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done); |
| 553 | |
| 554 | done: |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * @brief Link the extensions definitions with the available extension plugins. |
| 560 | * |
| 561 | * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules |
| 562 | * are not connected with even available extension plugins. |
| 563 | * |
| 564 | * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)). |
| 565 | */ |
| 566 | static void |
| 567 | lys_compile_extension_plugins(struct lysc_ext *extensions) |
| 568 | { |
| 569 | unsigned int u; |
| 570 | |
| 571 | LY_ARRAY_FOR(extensions, u) { |
| 572 | extensions[u].plugin = lyext_get_plugin(&extensions[u]); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | LY_ERR |
| 577 | lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 578 | struct lysp_ext *extensions_p, struct lysc_ext **extensions) |
| 579 | { |
| 580 | unsigned int offset = 0, u; |
| 581 | struct lysc_ctx context = {0}; |
| 582 | |
| 583 | assert(ctx_sc || ctx); |
| 584 | |
| 585 | if (!ctx_sc) { |
| 586 | context.ctx = ctx; |
| 587 | context.mod = module; |
| 588 | context.path_len = 1; |
| 589 | context.path[0] = '/'; |
| 590 | ctx_sc = &context; |
| 591 | } |
| 592 | |
| 593 | if (!extensions_p) { |
| 594 | return LY_SUCCESS; |
| 595 | } |
| 596 | if (*extensions) { |
| 597 | offset = LY_ARRAY_SIZE(*extensions); |
| 598 | } |
| 599 | |
| 600 | lysc_update_path(ctx_sc, NULL, "{extension}"); |
| 601 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM); |
| 602 | LY_ARRAY_FOR(extensions_p, u) { |
| 603 | lysc_update_path(ctx_sc, NULL, extensions_p[u].name); |
| 604 | LY_ARRAY_INCREMENT(*extensions); |
| 605 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name); |
| 606 | LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u])); |
| 607 | lysc_update_path(ctx_sc, NULL, NULL); |
| 608 | } |
| 609 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 610 | |
| 611 | return LY_SUCCESS; |
| 612 | } |
| 613 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 614 | /** |
| 615 | * @brief Compile information from the if-feature statement |
| 616 | * @param[in] ctx Compile context. |
| 617 | * @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] | 618 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 619 | * @return LY_ERR value. |
| 620 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 621 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 622 | 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] | 623 | { |
| 624 | const char *c = *value; |
| 625 | int r, rc = EXIT_FAILURE; |
| 626 | int i, j, last_not, checkversion = 0; |
| 627 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 628 | uint8_t op; |
| 629 | struct iff_stack stack = {0, 0, NULL}; |
| 630 | struct lysc_feature *f; |
| 631 | |
| 632 | assert(c); |
| 633 | |
| 634 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 635 | for (i = j = last_not = 0; c[i]; i++) { |
| 636 | if (c[i] == '(') { |
| 637 | j++; |
| 638 | checkversion = 1; |
| 639 | continue; |
| 640 | } else if (c[i] == ')') { |
| 641 | j--; |
| 642 | continue; |
| 643 | } else if (isspace(c[i])) { |
| 644 | checkversion = 1; |
| 645 | continue; |
| 646 | } |
| 647 | |
| 648 | 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] | 649 | int sp; |
| 650 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 651 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 652 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 653 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 654 | return LY_EVALID; |
| 655 | } else if (!isspace(c[i + r])) { |
| 656 | /* feature name starting with the not/and/or */ |
| 657 | last_not = 0; |
| 658 | f_size++; |
| 659 | } else if (c[i] == 'n') { /* not operation */ |
| 660 | if (last_not) { |
| 661 | /* double not */ |
| 662 | expr_size = expr_size - 2; |
| 663 | last_not = 0; |
| 664 | } else { |
| 665 | last_not = 1; |
| 666 | } |
| 667 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 668 | if (f_exp != f_size) { |
| 669 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 670 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 671 | return LY_EVALID; |
| 672 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 673 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 674 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 675 | /* not a not operation */ |
| 676 | last_not = 0; |
| 677 | } |
| 678 | i += r; |
| 679 | } else { |
| 680 | f_size++; |
| 681 | last_not = 0; |
| 682 | } |
| 683 | expr_size++; |
| 684 | |
| 685 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 686 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 687 | i--; |
| 688 | break; |
| 689 | } |
| 690 | i++; |
| 691 | } |
| 692 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 693 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 694 | /* not matching count of ( and ) */ |
| 695 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 696 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 697 | return LY_EVALID; |
| 698 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 699 | if (f_exp != f_size) { |
| 700 | /* features do not match the needed arguments for the logical operations */ |
| 701 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 702 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 703 | "the required number of operands for the operations.", *value); |
| 704 | return LY_EVALID; |
| 705 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 706 | |
| 707 | if (checkversion || expr_size > 1) { |
| 708 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 709 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 710 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 711 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 712 | return LY_EVALID; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | /* allocate the memory */ |
| 717 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 718 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 719 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 720 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 721 | |
| 722 | stack.size = expr_size; |
| 723 | f_size--; expr_size--; /* used as indexes from now */ |
| 724 | |
| 725 | for (i--; i >= 0; i--) { |
| 726 | if (c[i] == ')') { |
| 727 | /* push it on stack */ |
| 728 | iff_stack_push(&stack, LYS_IFF_RP); |
| 729 | continue; |
| 730 | } else if (c[i] == '(') { |
| 731 | /* pop from the stack into result all operators until ) */ |
| 732 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 733 | iff_setop(iff->expr, op, expr_size--); |
| 734 | } |
| 735 | continue; |
| 736 | } else if (isspace(c[i])) { |
| 737 | continue; |
| 738 | } |
| 739 | |
| 740 | /* end of operator or operand -> find beginning and get what is it */ |
| 741 | j = i + 1; |
| 742 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 743 | i--; |
| 744 | } |
| 745 | i++; /* go back by one step */ |
| 746 | |
| 747 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 748 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 749 | /* double not */ |
| 750 | iff_stack_pop(&stack); |
| 751 | } else { |
| 752 | /* not has the highest priority, so do not pop from the stack |
| 753 | * as in case of AND and OR */ |
| 754 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 755 | } |
| 756 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 757 | /* as for OR - pop from the stack all operators with the same or higher |
| 758 | * priority and store them to the result, then push the AND to the stack */ |
| 759 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 760 | op = iff_stack_pop(&stack); |
| 761 | iff_setop(iff->expr, op, expr_size--); |
| 762 | } |
| 763 | iff_stack_push(&stack, LYS_IFF_AND); |
| 764 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 765 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 766 | op = iff_stack_pop(&stack); |
| 767 | iff_setop(iff->expr, op, expr_size--); |
| 768 | } |
| 769 | iff_stack_push(&stack, LYS_IFF_OR); |
| 770 | } else { |
| 771 | /* feature name, length is j - i */ |
| 772 | |
| 773 | /* add it to the expression */ |
| 774 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 775 | |
| 776 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 777 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 778 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 779 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 780 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 781 | iff->features[f_size] = f; |
| 782 | LY_ARRAY_INCREMENT(iff->features); |
| 783 | f_size--; |
| 784 | } |
| 785 | } |
| 786 | while (stack.index) { |
| 787 | op = iff_stack_pop(&stack); |
| 788 | iff_setop(iff->expr, op, expr_size--); |
| 789 | } |
| 790 | |
| 791 | if (++expr_size || ++f_size) { |
| 792 | /* not all expected operators and operands found */ |
| 793 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 794 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 795 | rc = LY_EINT; |
| 796 | } else { |
| 797 | rc = LY_SUCCESS; |
| 798 | } |
| 799 | |
| 800 | error: |
| 801 | /* cleanup */ |
| 802 | iff_stack_clean(&stack); |
| 803 | |
| 804 | return rc; |
| 805 | } |
| 806 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 807 | /** |
| 808 | * @brief Compile information from the when statement |
| 809 | * @param[in] ctx Compile context. |
| 810 | * @param[in] when_p The parsed when statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 811 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 812 | * @return LY_ERR value. |
| 813 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 814 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 815 | 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] | 816 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 817 | LY_ERR ret = LY_SUCCESS; |
| 818 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 819 | *when = calloc(1, sizeof **when); |
| 820 | (*when)->refcount = 1; |
| 821 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 822 | (*when)->module = ctx->mod_def; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 823 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 824 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 825 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 826 | COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 827 | |
| 828 | done: |
| 829 | return ret; |
| 830 | } |
| 831 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 832 | /** |
| 833 | * @brief Compile information from the must statement |
| 834 | * @param[in] ctx Compile context. |
| 835 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 836 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 837 | * @return LY_ERR value. |
| 838 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 839 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 840 | 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] | 841 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 842 | LY_ERR ret = LY_SUCCESS; |
| 843 | |
| 844 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 845 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 846 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 847 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 848 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 849 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 850 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 851 | COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 852 | |
| 853 | done: |
| 854 | return ret; |
| 855 | } |
| 856 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 857 | /** |
| 858 | * @brief Compile information from the import statement |
| 859 | * @param[in] ctx Compile context. |
| 860 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 861 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 862 | * @return LY_ERR value. |
| 863 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 864 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 865 | 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] | 866 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 867 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 868 | LY_ERR ret = LY_SUCCESS; |
| 869 | |
| 870 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 871 | COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 872 | imp->module = imp_p->module; |
| 873 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 874 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 875 | * 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] | 876 | * 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] | 877 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 878 | /* try to use filepath if present */ |
| 879 | if (imp->module->filepath) { |
| 880 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 881 | !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] | 882 | if (mod != imp->module) { |
| 883 | 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] | 884 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 885 | mod = NULL; |
| 886 | } |
| 887 | } |
| 888 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 889 | 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] | 890 | 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] | 891 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 892 | return LY_ENOTFOUND; |
| 893 | } |
| 894 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | done: |
| 898 | return ret; |
| 899 | } |
| 900 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 901 | /** |
| 902 | * @brief Compile information from the identity statement |
| 903 | * |
| 904 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 905 | * |
| 906 | * @param[in] ctx Compile context. |
| 907 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 908 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 909 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 910 | * @return LY_ERR value. |
| 911 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 912 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 913 | 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] | 914 | { |
| 915 | unsigned int u; |
| 916 | LY_ERR ret = LY_SUCCESS; |
| 917 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 918 | lysc_update_path(ctx, NULL, ident_p->name); |
| 919 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 920 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 921 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 922 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 923 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 924 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 925 | 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] | 926 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 927 | COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 928 | ident->flags = ident_p->flags; |
| 929 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 930 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 931 | done: |
| 932 | return ret; |
| 933 | } |
| 934 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 935 | /** |
| 936 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 937 | * |
| 938 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 939 | * |
| 940 | * @param[in] ctx Compile context for logging. |
| 941 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 942 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 943 | * @return LY_SUCCESS if everything is ok. |
| 944 | * @return LY_EVALID if the identity is derived from itself. |
| 945 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 946 | static LY_ERR |
| 947 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 948 | { |
| 949 | LY_ERR ret = LY_EVALID; |
| 950 | unsigned int u, v; |
| 951 | struct ly_set recursion = {0}; |
| 952 | struct lysc_ident *drv; |
| 953 | |
| 954 | if (!derived) { |
| 955 | return LY_SUCCESS; |
| 956 | } |
| 957 | |
| 958 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 959 | if (ident == derived[u]) { |
| 960 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 961 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 962 | goto cleanup; |
| 963 | } |
| 964 | ly_set_add(&recursion, derived[u], 0); |
| 965 | } |
| 966 | |
| 967 | for (v = 0; v < recursion.count; ++v) { |
| 968 | drv = recursion.objs[v]; |
| 969 | if (!drv->derived) { |
| 970 | continue; |
| 971 | } |
| 972 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 973 | if (ident == drv->derived[u]) { |
| 974 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 975 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 976 | goto cleanup; |
| 977 | } |
| 978 | ly_set_add(&recursion, drv->derived[u], 0); |
| 979 | } |
| 980 | } |
| 981 | ret = LY_SUCCESS; |
| 982 | |
| 983 | cleanup: |
| 984 | ly_set_erase(&recursion, NULL); |
| 985 | return ret; |
| 986 | } |
| 987 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 988 | /** |
| 989 | * @brief Find and process the referenced base identities from another identity or identityref |
| 990 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 991 | * 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] | 992 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 993 | * to distinguish these two use cases. |
| 994 | * |
| 995 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 996 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 997 | * @param[in] ident Referencing identity to work with. |
| 998 | * @param[in] bases Array of bases of identityref to fill in. |
| 999 | * @return LY_ERR value. |
| 1000 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1001 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1002 | 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] | 1003 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1004 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1005 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1006 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1007 | struct lysc_ident **idref; |
| 1008 | |
| 1009 | assert(ident || bases); |
| 1010 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1011 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1012 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1013 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1014 | return LY_EVALID; |
| 1015 | } |
| 1016 | |
| 1017 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 1018 | s = strchr(bases_p[u], ':'); |
| 1019 | if (s) { |
| 1020 | /* prefixed identity */ |
| 1021 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1022 | 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] | 1023 | } else { |
| 1024 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1025 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1026 | } |
| 1027 | if (!mod) { |
| 1028 | if (ident) { |
| 1029 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1030 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1031 | } else { |
| 1032 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1033 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1034 | } |
| 1035 | return LY_EVALID; |
| 1036 | } |
| 1037 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1038 | if (mod->compiled && mod->compiled->identities) { |
| 1039 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 1040 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1041 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1042 | if (ident == &mod->compiled->identities[v]) { |
| 1043 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1044 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1045 | return LY_EVALID; |
| 1046 | } |
| 1047 | 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] | 1048 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1049 | 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] | 1050 | *idref = ident; |
| 1051 | } else { |
| 1052 | /* we have match! store the found identity */ |
| 1053 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1054 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1055 | } |
| 1056 | break; |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | if (!idref || !(*idref)) { |
| 1061 | if (ident) { |
| 1062 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1063 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1064 | } else { |
| 1065 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1066 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1067 | } |
| 1068 | return LY_EVALID; |
| 1069 | } |
| 1070 | } |
| 1071 | return LY_SUCCESS; |
| 1072 | } |
| 1073 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1074 | /** |
| 1075 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1076 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1077 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1078 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1079 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1080 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1081 | static LY_ERR |
| 1082 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1083 | { |
| 1084 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1085 | |
| 1086 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 1087 | if (!idents_p[i].bases) { |
| 1088 | continue; |
| 1089 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1090 | lysc_update_path(ctx, NULL, idents[i].name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1091 | 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] | 1092 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1093 | } |
| 1094 | return LY_SUCCESS; |
| 1095 | } |
| 1096 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1097 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1098 | 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] | 1099 | { |
| 1100 | unsigned int offset = 0, u; |
| 1101 | struct lysc_ctx context = {0}; |
| 1102 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1103 | assert(ctx_sc || ctx); |
| 1104 | |
| 1105 | if (!ctx_sc) { |
| 1106 | context.ctx = ctx; |
| 1107 | context.mod = module; |
| 1108 | context.path_len = 1; |
| 1109 | context.path[0] = '/'; |
| 1110 | ctx_sc = &context; |
| 1111 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1112 | |
| 1113 | if (!features_p) { |
| 1114 | return LY_SUCCESS; |
| 1115 | } |
| 1116 | if (*features) { |
| 1117 | offset = LY_ARRAY_SIZE(*features); |
| 1118 | } |
| 1119 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1120 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 1121 | 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] | 1122 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1123 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1124 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1125 | LY_ARRAY_INCREMENT(*features); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1126 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 1127 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1128 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1129 | 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] | 1130 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1131 | (*features)[offset + u].module = ctx_sc->mod; |
| 1132 | |
| 1133 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1134 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1135 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1136 | |
| 1137 | return LY_SUCCESS; |
| 1138 | } |
| 1139 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1140 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1141 | * @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] | 1142 | * |
| 1143 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1144 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1145 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1146 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1147 | * being currently processed). |
| 1148 | * @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] | 1149 | * @return LY_SUCCESS if everything is ok. |
| 1150 | * @return LY_EVALID if the feature references indirectly itself. |
| 1151 | */ |
| 1152 | static LY_ERR |
| 1153 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1154 | { |
| 1155 | LY_ERR ret = LY_EVALID; |
| 1156 | unsigned int u, v; |
| 1157 | struct ly_set recursion = {0}; |
| 1158 | struct lysc_feature *drv; |
| 1159 | |
| 1160 | if (!depfeatures) { |
| 1161 | return LY_SUCCESS; |
| 1162 | } |
| 1163 | |
| 1164 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1165 | if (feature == depfeatures[u]) { |
| 1166 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1167 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1168 | goto cleanup; |
| 1169 | } |
| 1170 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1171 | } |
| 1172 | |
| 1173 | for (v = 0; v < recursion.count; ++v) { |
| 1174 | drv = recursion.objs[v]; |
| 1175 | if (!drv->depfeatures) { |
| 1176 | continue; |
| 1177 | } |
| 1178 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1179 | if (feature == drv->depfeatures[u]) { |
| 1180 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1181 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1182 | goto cleanup; |
| 1183 | } |
| 1184 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1185 | } |
| 1186 | } |
| 1187 | ret = LY_SUCCESS; |
| 1188 | |
| 1189 | cleanup: |
| 1190 | ly_set_erase(&recursion, NULL); |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
| 1194 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1195 | * @brief Create pre-compiled features array. |
| 1196 | * |
| 1197 | * See lys_feature_precompile() for more details. |
| 1198 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1199 | * @param[in] ctx Compile context. |
| 1200 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1201 | * @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] | 1202 | * @return LY_ERR value. |
| 1203 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1204 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1205 | 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] | 1206 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1207 | unsigned int u, v, x; |
| 1208 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1209 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1210 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1211 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1212 | /* find the preprecompiled feature */ |
| 1213 | LY_ARRAY_FOR(features, x) { |
| 1214 | if (strcmp(features[x].name, feature_p->name)) { |
| 1215 | continue; |
| 1216 | } |
| 1217 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1218 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1219 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1220 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1221 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1222 | COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1223 | 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] | 1224 | if (feature->iffeatures) { |
| 1225 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1226 | if (feature->iffeatures[u].features) { |
| 1227 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1228 | /* check for circular dependency - direct reference first,... */ |
| 1229 | if (feature == feature->iffeatures[u].features[v]) { |
| 1230 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1231 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1232 | return LY_EVALID; |
| 1233 | } |
| 1234 | /* ... and indirect circular reference */ |
| 1235 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1236 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1237 | /* add itself into the dependants list */ |
| 1238 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1239 | *df = feature; |
| 1240 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1241 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1242 | } |
| 1243 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1244 | lysc_update_path(ctx, NULL, NULL); |
| 1245 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1246 | done: |
| 1247 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1248 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1249 | |
| 1250 | LOGINT(ctx->ctx); |
| 1251 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1252 | } |
| 1253 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1254 | /** |
| 1255 | * @brief Revert compiled list of features back to the precompiled state. |
| 1256 | * |
| 1257 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1258 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1259 | * |
| 1260 | * @param[in] ctx Compilation context. |
| 1261 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1262 | * and supposed to hold the off_features list. |
| 1263 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1264 | static void |
| 1265 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1266 | { |
| 1267 | unsigned int u, v; |
| 1268 | |
| 1269 | /* keep the off_features list until the complete lys_module is freed */ |
| 1270 | mod->off_features = mod->compiled->features; |
| 1271 | mod->compiled->features = NULL; |
| 1272 | |
| 1273 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1274 | * which may points into the data being freed here */ |
| 1275 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1276 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1277 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1278 | } |
| 1279 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1280 | mod->off_features[u].iffeatures = NULL; |
| 1281 | |
| 1282 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1283 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1284 | } |
| 1285 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1286 | mod->off_features[u].exts = NULL; |
| 1287 | } |
| 1288 | } |
| 1289 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1290 | /** |
| 1291 | * @brief Validate and normalize numeric value from a range definition. |
| 1292 | * @param[in] ctx Compile context. |
| 1293 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1294 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1295 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1296 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1297 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1298 | * @param[in] value String value of the range boundary. |
| 1299 | * @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. |
| 1300 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1301 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1302 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1303 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1304 | 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] | 1305 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1306 | size_t fraction = 0, size; |
| 1307 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1308 | *len = 0; |
| 1309 | |
| 1310 | assert(value); |
| 1311 | /* parse value */ |
| 1312 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1313 | return LY_EVALID; |
| 1314 | } |
| 1315 | |
| 1316 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1317 | ++(*len); |
| 1318 | } |
| 1319 | |
| 1320 | while (isdigit(value[*len])) { |
| 1321 | ++(*len); |
| 1322 | } |
| 1323 | |
| 1324 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1325 | if (basetype == LY_TYPE_DEC64) { |
| 1326 | goto decimal; |
| 1327 | } else { |
| 1328 | *valcopy = strndup(value, *len); |
| 1329 | return LY_SUCCESS; |
| 1330 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1331 | } |
| 1332 | fraction = *len; |
| 1333 | |
| 1334 | ++(*len); |
| 1335 | while (isdigit(value[*len])) { |
| 1336 | ++(*len); |
| 1337 | } |
| 1338 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1339 | if (basetype == LY_TYPE_DEC64) { |
| 1340 | decimal: |
| 1341 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1342 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1343 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1344 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1345 | *len, value, frdigits); |
| 1346 | return LY_EINVAL; |
| 1347 | } |
| 1348 | if (fraction) { |
| 1349 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1350 | } else { |
| 1351 | size = (*len) + frdigits + 1; |
| 1352 | } |
| 1353 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1354 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1355 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1356 | (*valcopy)[size - 1] = '\0'; |
| 1357 | if (fraction) { |
| 1358 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1359 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1360 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1361 | } else { |
| 1362 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1363 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1364 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1365 | } |
| 1366 | return LY_SUCCESS; |
| 1367 | } |
| 1368 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1369 | /** |
| 1370 | * @brief Check that values in range are in ascendant order. |
| 1371 | * @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] | 1372 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1373 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1374 | * @param[in] value Current value to check. |
| 1375 | * @param[in] prev_value The last seen value. |
| 1376 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1377 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1378 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1379 | 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] | 1380 | { |
| 1381 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1382 | 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] | 1383 | return LY_EEXIST; |
| 1384 | } |
| 1385 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1386 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1387 | return LY_EEXIST; |
| 1388 | } |
| 1389 | } |
| 1390 | return LY_SUCCESS; |
| 1391 | } |
| 1392 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1393 | /** |
| 1394 | * @brief Set min/max value of the range part. |
| 1395 | * @param[in] ctx Compile context. |
| 1396 | * @param[in] part Range part structure to fill. |
| 1397 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1398 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1399 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1400 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1401 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1402 | * @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] | 1403 | * @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] | 1404 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1405 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1406 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1407 | * frdigits value), LY_EMEM. |
| 1408 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1409 | static LY_ERR |
| 1410 | 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] | 1411 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1412 | { |
| 1413 | LY_ERR ret = LY_SUCCESS; |
| 1414 | char *valcopy = NULL; |
| 1415 | size_t len; |
| 1416 | |
| 1417 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1418 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1419 | LY_CHECK_GOTO(ret, finalize); |
| 1420 | } |
| 1421 | if (!valcopy && base_range) { |
| 1422 | if (max) { |
| 1423 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1424 | } else { |
| 1425 | part->min_64 = base_range->parts[0].min_64; |
| 1426 | } |
| 1427 | if (!first) { |
| 1428 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1429 | } |
| 1430 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1434 | case LY_TYPE_INT8: /* range */ |
| 1435 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1436 | 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] | 1437 | } else if (max) { |
| 1438 | part->max_64 = INT64_C(127); |
| 1439 | } else { |
| 1440 | part->min_64 = INT64_C(-128); |
| 1441 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1442 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1443 | 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] | 1444 | } |
| 1445 | break; |
| 1446 | case LY_TYPE_INT16: /* range */ |
| 1447 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1448 | 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] | 1449 | } else if (max) { |
| 1450 | part->max_64 = INT64_C(32767); |
| 1451 | } else { |
| 1452 | part->min_64 = INT64_C(-32768); |
| 1453 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1454 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1455 | 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] | 1456 | } |
| 1457 | break; |
| 1458 | case LY_TYPE_INT32: /* range */ |
| 1459 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1460 | 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] | 1461 | } else if (max) { |
| 1462 | part->max_64 = INT64_C(2147483647); |
| 1463 | } else { |
| 1464 | part->min_64 = INT64_C(-2147483648); |
| 1465 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1466 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1467 | 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] | 1468 | } |
| 1469 | break; |
| 1470 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1471 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1472 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1473 | 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] | 1474 | max ? &part->max_64 : &part->min_64); |
| 1475 | } else if (max) { |
| 1476 | part->max_64 = INT64_C(9223372036854775807); |
| 1477 | } else { |
| 1478 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1479 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1480 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1481 | 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] | 1482 | } |
| 1483 | break; |
| 1484 | case LY_TYPE_UINT8: /* range */ |
| 1485 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1486 | 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] | 1487 | } else if (max) { |
| 1488 | part->max_u64 = UINT64_C(255); |
| 1489 | } else { |
| 1490 | part->min_u64 = UINT64_C(0); |
| 1491 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1492 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1493 | 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] | 1494 | } |
| 1495 | break; |
| 1496 | case LY_TYPE_UINT16: /* range */ |
| 1497 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1498 | 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] | 1499 | } else if (max) { |
| 1500 | part->max_u64 = UINT64_C(65535); |
| 1501 | } else { |
| 1502 | part->min_u64 = UINT64_C(0); |
| 1503 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1504 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1505 | 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] | 1506 | } |
| 1507 | break; |
| 1508 | case LY_TYPE_UINT32: /* range */ |
| 1509 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1510 | 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] | 1511 | } else if (max) { |
| 1512 | part->max_u64 = UINT64_C(4294967295); |
| 1513 | } else { |
| 1514 | part->min_u64 = UINT64_C(0); |
| 1515 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1516 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1517 | 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] | 1518 | } |
| 1519 | break; |
| 1520 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1521 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1522 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1523 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1524 | ret = ly_parse_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] | 1525 | } else if (max) { |
| 1526 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1527 | } else { |
| 1528 | part->min_u64 = UINT64_C(0); |
| 1529 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1530 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1531 | 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] | 1532 | } |
| 1533 | break; |
| 1534 | default: |
| 1535 | LOGINT(ctx->ctx); |
| 1536 | ret = LY_EINT; |
| 1537 | } |
| 1538 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1539 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1540 | if (ret == LY_EDENIED) { |
| 1541 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1542 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1543 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1544 | } else if (ret == LY_EVALID) { |
| 1545 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1546 | "Invalid %s restriction - invalid value \"%s\".", |
| 1547 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1548 | } else if (ret == LY_EEXIST) { |
| 1549 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1550 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1551 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1552 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1553 | } else if (!ret && value) { |
| 1554 | *value = *value + len; |
| 1555 | } |
| 1556 | free(valcopy); |
| 1557 | return ret; |
| 1558 | } |
| 1559 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1560 | /** |
| 1561 | * @brief Compile the parsed range restriction. |
| 1562 | * @param[in] ctx Compile context. |
| 1563 | * @param[in] range_p Parsed range structure to compile. |
| 1564 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1565 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1566 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1567 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1568 | * range restriction must be more restrictive than the base_range. |
| 1569 | * @param[in,out] range Pointer to the created current range structure. |
| 1570 | * @return LY_ERR value. |
| 1571 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1572 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1573 | 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] | 1574 | struct lysc_range *base_range, struct lysc_range **range) |
| 1575 | { |
| 1576 | LY_ERR ret = LY_EVALID; |
| 1577 | const char *expr; |
| 1578 | struct lysc_range_part *parts = NULL, *part; |
| 1579 | int range_expected = 0, uns; |
| 1580 | unsigned int parts_done = 0, u, v; |
| 1581 | |
| 1582 | assert(range); |
| 1583 | assert(range_p); |
| 1584 | |
| 1585 | expr = range_p->arg; |
| 1586 | while(1) { |
| 1587 | if (isspace(*expr)) { |
| 1588 | ++expr; |
| 1589 | } else if (*expr == '\0') { |
| 1590 | if (range_expected) { |
| 1591 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1592 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1593 | length_restr ? "length" : "range", range_p->arg); |
| 1594 | goto cleanup; |
| 1595 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1596 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1597 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1598 | length_restr ? "length" : "range", range_p->arg); |
| 1599 | goto cleanup; |
| 1600 | } |
| 1601 | parts_done++; |
| 1602 | break; |
| 1603 | } else if (!strncmp(expr, "min", 3)) { |
| 1604 | if (parts) { |
| 1605 | /* min cannot be used elsewhere than in the first part */ |
| 1606 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1607 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1608 | expr - range_p->arg, range_p->arg); |
| 1609 | goto cleanup; |
| 1610 | } |
| 1611 | expr += 3; |
| 1612 | |
| 1613 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1614 | 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] | 1615 | part->max_64 = part->min_64; |
| 1616 | } else if (*expr == '|') { |
| 1617 | if (!parts || range_expected) { |
| 1618 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1619 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1620 | goto cleanup; |
| 1621 | } |
| 1622 | expr++; |
| 1623 | parts_done++; |
| 1624 | /* process next part of the expression */ |
| 1625 | } else if (!strncmp(expr, "..", 2)) { |
| 1626 | expr += 2; |
| 1627 | while (isspace(*expr)) { |
| 1628 | expr++; |
| 1629 | } |
| 1630 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1631 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1632 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1633 | goto cleanup; |
| 1634 | } |
| 1635 | /* continue expecting the upper boundary */ |
| 1636 | range_expected = 1; |
| 1637 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1638 | /* number */ |
| 1639 | if (range_expected) { |
| 1640 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1641 | 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] | 1642 | range_expected = 0; |
| 1643 | } else { |
| 1644 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1645 | 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] | 1646 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1647 | part->max_64 = part->min_64; |
| 1648 | } |
| 1649 | |
| 1650 | /* continue with possible another expression part */ |
| 1651 | } else if (!strncmp(expr, "max", 3)) { |
| 1652 | expr += 3; |
| 1653 | while (isspace(*expr)) { |
| 1654 | expr++; |
| 1655 | } |
| 1656 | if (*expr != '\0') { |
| 1657 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1658 | length_restr ? "length" : "range", expr); |
| 1659 | goto cleanup; |
| 1660 | } |
| 1661 | if (range_expected) { |
| 1662 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1663 | 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] | 1664 | range_expected = 0; |
| 1665 | } else { |
| 1666 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1667 | 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] | 1668 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1669 | part->min_64 = part->max_64; |
| 1670 | } |
| 1671 | } else { |
| 1672 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1673 | length_restr ? "length" : "range", expr); |
| 1674 | goto cleanup; |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | /* check with the previous range/length restriction */ |
| 1679 | if (base_range) { |
| 1680 | switch (basetype) { |
| 1681 | case LY_TYPE_BINARY: |
| 1682 | case LY_TYPE_UINT8: |
| 1683 | case LY_TYPE_UINT16: |
| 1684 | case LY_TYPE_UINT32: |
| 1685 | case LY_TYPE_UINT64: |
| 1686 | case LY_TYPE_STRING: |
| 1687 | uns = 1; |
| 1688 | break; |
| 1689 | case LY_TYPE_DEC64: |
| 1690 | case LY_TYPE_INT8: |
| 1691 | case LY_TYPE_INT16: |
| 1692 | case LY_TYPE_INT32: |
| 1693 | case LY_TYPE_INT64: |
| 1694 | uns = 0; |
| 1695 | break; |
| 1696 | default: |
| 1697 | LOGINT(ctx->ctx); |
| 1698 | ret = LY_EINT; |
| 1699 | goto cleanup; |
| 1700 | } |
| 1701 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1702 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1703 | goto baseerror; |
| 1704 | } |
| 1705 | /* current lower bound is not lower than the base */ |
| 1706 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1707 | /* base has single value */ |
| 1708 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1709 | /* both lower bounds are the same */ |
| 1710 | if (parts[u].min_64 != parts[u].max_64) { |
| 1711 | /* current continues with a range */ |
| 1712 | goto baseerror; |
| 1713 | } else { |
| 1714 | /* equal single values, move both forward */ |
| 1715 | ++v; |
| 1716 | continue; |
| 1717 | } |
| 1718 | } else { |
| 1719 | /* base is single value lower than current range, so the |
| 1720 | * value from base range is removed in the current, |
| 1721 | * move only base and repeat checking */ |
| 1722 | ++v; |
| 1723 | --u; |
| 1724 | continue; |
| 1725 | } |
| 1726 | } else { |
| 1727 | /* base is the range */ |
| 1728 | if (parts[u].min_64 == parts[u].max_64) { |
| 1729 | /* current is a single value */ |
| 1730 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1731 | /* current is behind the base range, so base range is omitted, |
| 1732 | * move the base and keep the current for further check */ |
| 1733 | ++v; |
| 1734 | --u; |
| 1735 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1736 | continue; |
| 1737 | } else { |
| 1738 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1739 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1740 | /* higher bound is higher than the current higher bound */ |
| 1741 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1742 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1743 | * continue with the same current, but move the base */ |
| 1744 | --u; |
| 1745 | ++v; |
| 1746 | continue; |
| 1747 | } |
| 1748 | /* current range starts within the base range but end behind it */ |
| 1749 | goto baseerror; |
| 1750 | } else { |
| 1751 | /* current range is smaller than the base, |
| 1752 | * move current, but stay with the base */ |
| 1753 | continue; |
| 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | if (u != parts_done) { |
| 1759 | baseerror: |
| 1760 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1761 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1762 | length_restr ? "length" : "range", range_p->arg); |
| 1763 | goto cleanup; |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | if (!(*range)) { |
| 1768 | *range = calloc(1, sizeof **range); |
| 1769 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1770 | } |
| 1771 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1772 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1773 | if (range_p->eapptag) { |
| 1774 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1775 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1776 | } |
| 1777 | if (range_p->emsg) { |
| 1778 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1779 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1780 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1781 | if (range_p->dsc) { |
| 1782 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1783 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1784 | } |
| 1785 | if (range_p->ref) { |
| 1786 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1787 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1788 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1789 | /* extensions are taken only from the last range by the caller */ |
| 1790 | |
| 1791 | (*range)->parts = parts; |
| 1792 | parts = NULL; |
| 1793 | ret = LY_SUCCESS; |
| 1794 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1795 | LY_ARRAY_FREE(parts); |
| 1796 | |
| 1797 | return ret; |
| 1798 | } |
| 1799 | |
| 1800 | /** |
| 1801 | * @brief Checks pattern syntax. |
| 1802 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1803 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1804 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1805 | * @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] | 1806 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1807 | */ |
| 1808 | static LY_ERR |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1809 | 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] | 1810 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1811 | int idx, idx2, start, end, count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1812 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1813 | int err_code; |
| 1814 | const char *orig_ptr; |
| 1815 | PCRE2_SIZE err_offset; |
| 1816 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1817 | #define URANGE_LEN 19 |
| 1818 | char *ublock2urange[][2] = { |
| 1819 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1820 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1821 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1822 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1823 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1824 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1825 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1826 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1827 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1828 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1829 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1830 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1831 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1832 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1833 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1834 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1835 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1836 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1837 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1838 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1839 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1840 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1841 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1842 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1843 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1844 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1845 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1846 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1847 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1848 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1849 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1850 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1851 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1852 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1853 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1854 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1855 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1856 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1857 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1858 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1859 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1860 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1861 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1862 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1863 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1864 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1865 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1866 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1867 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1868 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1869 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1870 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1871 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1872 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1873 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1874 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1875 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1876 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1877 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1878 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1879 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1880 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1881 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1882 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1883 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1884 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1885 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1886 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1887 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1888 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1889 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1890 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1891 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1892 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1893 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1894 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1895 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1896 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1897 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1898 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1899 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1900 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1901 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1902 | {NULL, NULL} |
| 1903 | }; |
| 1904 | |
| 1905 | /* adjust the expression to a Perl equivalent |
| 1906 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1907 | |
| 1908 | /* we need to replace all "$" with "\$", count them now */ |
| 1909 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1910 | |
| 1911 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1912 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1913 | perl_regex[0] = '\0'; |
| 1914 | |
| 1915 | ptr = perl_regex; |
| 1916 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1917 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1918 | if (orig_ptr[0] == '$') { |
| 1919 | ptr += sprintf(ptr, "\\$"); |
| 1920 | } else if (orig_ptr[0] == '^') { |
| 1921 | ptr += sprintf(ptr, "\\^"); |
| 1922 | } else { |
| 1923 | ptr[0] = orig_ptr[0]; |
| 1924 | ++ptr; |
| 1925 | } |
| 1926 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1927 | ptr[0] = '\0'; |
| 1928 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1929 | |
| 1930 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1931 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1932 | start = ptr - perl_regex; |
| 1933 | |
| 1934 | ptr = strchr(ptr, '}'); |
| 1935 | if (!ptr) { |
| 1936 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1937 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1938 | free(perl_regex); |
| 1939 | return LY_EVALID; |
| 1940 | } |
| 1941 | end = (ptr - perl_regex) + 1; |
| 1942 | |
| 1943 | /* need more space */ |
| 1944 | if (end - start < URANGE_LEN) { |
| 1945 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1946 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1947 | } |
| 1948 | |
| 1949 | /* find our range */ |
| 1950 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1951 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1952 | break; |
| 1953 | } |
| 1954 | } |
| 1955 | if (!ublock2urange[idx][0]) { |
| 1956 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1957 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1958 | free(perl_regex); |
| 1959 | return LY_EVALID; |
| 1960 | } |
| 1961 | |
| 1962 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1963 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1964 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1965 | ++count; |
| 1966 | } |
| 1967 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1968 | --count; |
| 1969 | } |
| 1970 | } |
| 1971 | if (count) { |
| 1972 | /* skip brackets */ |
| 1973 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1974 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1975 | } else { |
| 1976 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1977 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1982 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 1983 | 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] | 1984 | &err_code, &err_offset, NULL); |
| 1985 | if (!code_local) { |
| 1986 | PCRE2_UCHAR err_msg[256] = {0}; |
| 1987 | pcre2_get_error_message(err_code, err_msg, 256); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1988 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1989 | free(perl_regex); |
| 1990 | return LY_EVALID; |
| 1991 | } |
| 1992 | free(perl_regex); |
| 1993 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1994 | if (code) { |
| 1995 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1996 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1997 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | return LY_SUCCESS; |
| 2001 | |
| 2002 | #undef URANGE_LEN |
| 2003 | } |
| 2004 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2005 | /** |
| 2006 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2007 | * @param[in] ctx Compile context. |
| 2008 | * @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] | 2009 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2010 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2011 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2012 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2013 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2014 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2015 | 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] | 2016 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2017 | { |
| 2018 | struct lysc_pattern **pattern; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2019 | unsigned int u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2020 | LY_ERR ret = LY_SUCCESS; |
| 2021 | |
| 2022 | /* first, copy the patterns from the base type */ |
| 2023 | if (base_patterns) { |
| 2024 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2025 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2026 | } |
| 2027 | |
| 2028 | LY_ARRAY_FOR(patterns_p, u) { |
| 2029 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2030 | *pattern = calloc(1, sizeof **pattern); |
| 2031 | ++(*pattern)->refcount; |
| 2032 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2033 | 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] | 2034 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2035 | |
| 2036 | if (patterns_p[u].arg[0] == 0x15) { |
| 2037 | (*pattern)->inverted = 1; |
| 2038 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2039 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2040 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2041 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2042 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2043 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2044 | COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2045 | } |
| 2046 | done: |
| 2047 | return ret; |
| 2048 | } |
| 2049 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2050 | /** |
| 2051 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2052 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2053 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2054 | 0 /* LY_TYPE_UNKNOWN */, |
| 2055 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2056 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2057 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2058 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2059 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2060 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2061 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2062 | 0 /* LY_TYPE_BOOL */, |
| 2063 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2064 | 0 /* LY_TYPE_EMPTY */, |
| 2065 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2066 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2067 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2068 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2069 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2070 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2071 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2072 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2073 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2074 | }; |
| 2075 | |
| 2076 | /** |
| 2077 | * @brief stringification of the YANG built-in data types |
| 2078 | */ |
| 2079 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2080 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2081 | "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] | 2082 | }; |
| 2083 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2084 | /** |
| 2085 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2086 | * @param[in] ctx Compile context. |
| 2087 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2088 | * @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] | 2089 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2090 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2091 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2092 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2093 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2094 | 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] | 2095 | 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] | 2096 | { |
| 2097 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 2098 | unsigned int u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2099 | int32_t value = 0; |
| 2100 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2101 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2102 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2103 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2104 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2105 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2106 | return LY_EVALID; |
| 2107 | } |
| 2108 | |
| 2109 | LY_ARRAY_FOR(enums_p, u) { |
| 2110 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2111 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2112 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2113 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2114 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2115 | if (base_enums) { |
| 2116 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2117 | LY_ARRAY_FOR(base_enums, v) { |
| 2118 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2119 | break; |
| 2120 | } |
| 2121 | } |
| 2122 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 2123 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2124 | "Invalid %s - derived type adds new item \"%s\".", |
| 2125 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2126 | return LY_EVALID; |
| 2127 | } |
| 2128 | match = v; |
| 2129 | } |
| 2130 | |
| 2131 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2132 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2133 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2134 | e->value = (int32_t)enums_p[u].value; |
| 2135 | if (!u || e->value >= value) { |
| 2136 | value = e->value + 1; |
| 2137 | } |
| 2138 | /* check collision with other values */ |
| 2139 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2140 | if (e->value == (*enums)[v].value) { |
| 2141 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2142 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2143 | e->value, e->name, (*enums)[v].name); |
| 2144 | return LY_EVALID; |
| 2145 | } |
| 2146 | } |
| 2147 | } else if (base_enums) { |
| 2148 | /* inherit the assigned value */ |
| 2149 | e->value = base_enums[match].value; |
| 2150 | if (!u || e->value >= value) { |
| 2151 | value = e->value + 1; |
| 2152 | } |
| 2153 | } else { |
| 2154 | /* assign value automatically */ |
| 2155 | if (u && value == INT32_MIN) { |
| 2156 | /* counter overflow */ |
| 2157 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2158 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2159 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2160 | return LY_EVALID; |
| 2161 | } |
| 2162 | e->value = value++; |
| 2163 | } |
| 2164 | } else { /* LY_TYPE_BITS */ |
| 2165 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2166 | e->value = (int32_t)enums_p[u].value; |
| 2167 | if (!u || (uint32_t)e->value >= position) { |
| 2168 | position = (uint32_t)e->value + 1; |
| 2169 | } |
| 2170 | /* check collision with other values */ |
| 2171 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2172 | if (e->value == (*enums)[v].value) { |
| 2173 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2174 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2175 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2176 | return LY_EVALID; |
| 2177 | } |
| 2178 | } |
| 2179 | } else if (base_enums) { |
| 2180 | /* inherit the assigned value */ |
| 2181 | e->value = base_enums[match].value; |
| 2182 | if (!u || (uint32_t)e->value >= position) { |
| 2183 | position = (uint32_t)e->value + 1; |
| 2184 | } |
| 2185 | } else { |
| 2186 | /* assign value automatically */ |
| 2187 | if (u && position == 0) { |
| 2188 | /* counter overflow */ |
| 2189 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2190 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2191 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2192 | return LY_EVALID; |
| 2193 | } |
| 2194 | e->value = position++; |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | if (base_enums) { |
| 2199 | /* the assigned values must not change from the derived type */ |
| 2200 | if (e->value != base_enums[match].value) { |
| 2201 | if (basetype == LY_TYPE_ENUM) { |
| 2202 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2203 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2204 | e->name, base_enums[match].value, e->value); |
| 2205 | } else { |
| 2206 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2207 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2208 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2209 | } |
| 2210 | return LY_EVALID; |
| 2211 | } |
| 2212 | } |
| 2213 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2214 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2215 | COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2216 | |
| 2217 | if (basetype == LY_TYPE_BITS) { |
| 2218 | /* keep bits ordered by position */ |
| 2219 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2220 | if (v != u) { |
| 2221 | memcpy(&storage, e, sizeof *e); |
| 2222 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2223 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2224 | } |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | done: |
| 2229 | return ret; |
| 2230 | } |
| 2231 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2232 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2233 | for ((NODE) = (NODE)->parent; \ |
| 2234 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 2235 | (NODE) = (NODE)->parent); \ |
| 2236 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2237 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2238 | TERM; \ |
| 2239 | } |
| 2240 | |
| 2241 | /** |
| 2242 | * @brief Validate the predicate(s) from the leafref path. |
| 2243 | * @param[in] ctx Compile context |
| 2244 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2245 | * 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] | 2246 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2247 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2248 | * @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] | 2249 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2250 | */ |
| 2251 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2252 | 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] | 2253 | 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] | 2254 | { |
| 2255 | LY_ERR ret = LY_EVALID; |
| 2256 | const struct lys_module *mod; |
| 2257 | const struct lysc_node *src_node, *dst_node; |
| 2258 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2259 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2260 | unsigned int dest_parent_times, c; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2261 | const char *start, *end, *pke_end; |
| 2262 | struct ly_set keys = {0}; |
| 2263 | int i; |
| 2264 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2265 | assert(path_context); |
| 2266 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2267 | while (**predicate == '[') { |
| 2268 | start = (*predicate)++; |
| 2269 | |
| 2270 | while (isspace(**predicate)) { |
| 2271 | ++(*predicate); |
| 2272 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2273 | 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] | 2274 | while (isspace(**predicate)) { |
| 2275 | ++(*predicate); |
| 2276 | } |
| 2277 | if (**predicate != '=') { |
| 2278 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2279 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2280 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2281 | goto cleanup; |
| 2282 | } |
| 2283 | ++(*predicate); |
| 2284 | while (isspace(**predicate)) { |
| 2285 | ++(*predicate); |
| 2286 | } |
| 2287 | |
| 2288 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2289 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2290 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2291 | goto cleanup; |
| 2292 | } |
| 2293 | --pke_end; |
| 2294 | while (isspace(*pke_end)) { |
| 2295 | --pke_end; |
| 2296 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2297 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2298 | /* localize path-key-expr */ |
| 2299 | pke_start = path_key_expr = *predicate; |
| 2300 | /* move after the current predicate */ |
| 2301 | *predicate = end + 1; |
| 2302 | |
| 2303 | /* source (must be leaf or leaf-list) */ |
| 2304 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2305 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2306 | if (!mod) { |
| 2307 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2308 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2309 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2310 | goto cleanup; |
| 2311 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2312 | if (!mod->implemented) { |
| 2313 | /* make the module implemented */ |
| 2314 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2315 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2316 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2317 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2318 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2319 | src_node = NULL; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2320 | if (!(context_node->flags & LYS_KEYLESS)) { |
| 2321 | struct lysc_node *key; |
| 2322 | for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) { |
| 2323 | if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') { |
| 2324 | src_node = key; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2325 | break; |
| 2326 | } |
| 2327 | } |
| 2328 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2329 | if (!src_node) { |
| 2330 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2331 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2332 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2333 | goto cleanup; |
| 2334 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2335 | |
| 2336 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2337 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2338 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2339 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2340 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2341 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2342 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2343 | *predicate - start, start, src_node->name); |
| 2344 | goto cleanup; |
| 2345 | } |
| 2346 | |
| 2347 | /* destination */ |
| 2348 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2349 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2350 | |
| 2351 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2352 | if (strncmp(path_key_expr, "current", 7)) { |
| 2353 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2354 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2355 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2356 | *predicate - start, start); |
| 2357 | goto cleanup; |
| 2358 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2359 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2360 | if (*path_key_expr != '(') { |
| 2361 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2362 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2363 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2364 | if (*path_key_expr != ')') { |
| 2365 | goto error_current_function_invocation; |
| 2366 | } |
| 2367 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2368 | |
| 2369 | if (*path_key_expr != '/') { |
| 2370 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2371 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2372 | *predicate - start, start); |
| 2373 | goto cleanup; |
| 2374 | } |
| 2375 | ++path_key_expr; |
| 2376 | while (isspace(*path_key_expr)) { |
| 2377 | ++path_key_expr; |
| 2378 | } |
| 2379 | |
| 2380 | /* rel-path-keyexpr: |
| 2381 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2382 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2383 | ++dest_parent_times; |
| 2384 | path_key_expr += 2; |
| 2385 | while (isspace(*path_key_expr)) { |
| 2386 | ++path_key_expr; |
| 2387 | } |
| 2388 | if (*path_key_expr != '/') { |
| 2389 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2390 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2391 | *predicate - start, start); |
| 2392 | goto cleanup; |
| 2393 | } |
| 2394 | ++path_key_expr; |
| 2395 | while (isspace(*path_key_expr)) { |
| 2396 | ++path_key_expr; |
| 2397 | } |
| 2398 | |
| 2399 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2400 | * all schema nodes that cannot be instantiated in data tree */ |
| 2401 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2402 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2403 | *predicate - start, start); |
| 2404 | } |
| 2405 | if (!dest_parent_times) { |
| 2406 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2407 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2408 | *predicate - start, start); |
| 2409 | goto cleanup; |
| 2410 | } |
| 2411 | if (path_key_expr == pke_end) { |
| 2412 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2413 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2414 | *predicate - start, start); |
| 2415 | goto cleanup; |
| 2416 | } |
| 2417 | |
| 2418 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2419 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2420 | 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] | 2421 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2422 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2423 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2424 | goto cleanup; |
| 2425 | } |
| 2426 | |
| 2427 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2428 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2429 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2430 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2431 | } |
| 2432 | if (!mod) { |
| 2433 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2434 | "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] | 2435 | *predicate - start, start, dst_len, dst); |
| 2436 | goto cleanup; |
| 2437 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2438 | if (!mod->implemented) { |
| 2439 | /* make the module implemented */ |
| 2440 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2441 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2442 | |
| 2443 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2444 | if (!dst_node) { |
| 2445 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2446 | "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] | 2447 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2448 | goto cleanup; |
| 2449 | } |
| 2450 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2451 | 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] | 2452 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2453 | "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] | 2454 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2455 | goto cleanup; |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | ret = LY_SUCCESS; |
| 2460 | cleanup: |
| 2461 | ly_set_erase(&keys, NULL); |
| 2462 | return ret; |
| 2463 | } |
| 2464 | |
| 2465 | /** |
| 2466 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2467 | * |
| 2468 | * path-arg = absolute-path / relative-path |
| 2469 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2470 | * relative-path = 1*(".." "/") descendant-path |
| 2471 | * |
| 2472 | * @param[in,out] path Path to parse. |
| 2473 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2474 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2475 | * @param[out] name Name of the token. |
| 2476 | * @param[out] nam_len Length of the name. |
| 2477 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2478 | * must not be changed between consecutive calls. -1 if the |
| 2479 | * path is absolute. |
| 2480 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2481 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2482 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2483 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2484 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2485 | int *parent_times, int *has_predicate) |
| 2486 | { |
| 2487 | int par_times = 0; |
| 2488 | |
| 2489 | assert(path && *path); |
| 2490 | assert(parent_times); |
| 2491 | assert(prefix); |
| 2492 | assert(prefix_len); |
| 2493 | assert(name); |
| 2494 | assert(name_len); |
| 2495 | assert(has_predicate); |
| 2496 | |
| 2497 | *prefix = NULL; |
| 2498 | *prefix_len = 0; |
| 2499 | *name = NULL; |
| 2500 | *name_len = 0; |
| 2501 | *has_predicate = 0; |
| 2502 | |
| 2503 | if (!*parent_times) { |
| 2504 | if (!strncmp(*path, "..", 2)) { |
| 2505 | *path += 2; |
| 2506 | ++par_times; |
| 2507 | while (!strncmp(*path, "/..", 3)) { |
| 2508 | *path += 3; |
| 2509 | ++par_times; |
| 2510 | } |
| 2511 | } |
| 2512 | if (par_times) { |
| 2513 | *parent_times = par_times; |
| 2514 | } else { |
| 2515 | *parent_times = -1; |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | if (**path != '/') { |
| 2520 | return LY_EINVAL; |
| 2521 | } |
| 2522 | /* skip '/' */ |
| 2523 | ++(*path); |
| 2524 | |
| 2525 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2526 | 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] | 2527 | |
| 2528 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2529 | /* path continues by another token or this is the last token */ |
| 2530 | return LY_SUCCESS; |
| 2531 | } else if ((*path)[0] != '[') { |
| 2532 | /* unexpected character */ |
| 2533 | return LY_EINVAL; |
| 2534 | } else { |
| 2535 | /* predicate starting with [ */ |
| 2536 | *has_predicate = 1; |
| 2537 | return LY_SUCCESS; |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2542 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2543 | * |
| 2544 | * The set of features used for target must be a subset of features used for the leafref. |
| 2545 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2546 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2547 | * |
| 2548 | * @param[in] refnode The leafref node. |
| 2549 | * @param[in] target Tha target node of the leafref. |
| 2550 | * @return LY_SUCCESS or LY_EVALID; |
| 2551 | */ |
| 2552 | static LY_ERR |
| 2553 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2554 | { |
| 2555 | LY_ERR ret = LY_EVALID; |
| 2556 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2557 | unsigned int u, v, count; |
| 2558 | struct ly_set features = {0}; |
| 2559 | |
| 2560 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2561 | if (iter->iffeatures) { |
| 2562 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2563 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2564 | 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] | 2565 | } |
| 2566 | } |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2571 | * So when adding features applicable to the target into the features set, we should not be |
| 2572 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2573 | * to the leafref itself. */ |
| 2574 | count = features.count; |
| 2575 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2576 | if (iter->iffeatures) { |
| 2577 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2578 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2579 | 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] | 2580 | /* new feature was added (or LY_EMEM) */ |
| 2581 | goto cleanup; |
| 2582 | } |
| 2583 | } |
| 2584 | } |
| 2585 | } |
| 2586 | } |
| 2587 | ret = LY_SUCCESS; |
| 2588 | |
| 2589 | cleanup: |
| 2590 | ly_set_erase(&features, NULL); |
| 2591 | return ret; |
| 2592 | } |
| 2593 | |
| 2594 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2595 | * @brief Validate the leafref path. |
| 2596 | * @param[in] ctx Compile context |
| 2597 | * @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] | 2598 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2599 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2600 | */ |
| 2601 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2602 | 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] | 2603 | { |
| 2604 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2605 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2606 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2607 | const char *id, *prefix, *name; |
| 2608 | size_t prefix_len, name_len; |
| 2609 | int parent_times = 0, has_predicate; |
| 2610 | unsigned int iter, u; |
| 2611 | LY_ERR ret = LY_SUCCESS; |
| 2612 | |
| 2613 | assert(ctx); |
| 2614 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2615 | assert(leafref); |
| 2616 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2617 | ctx->path[0] = '\0'; |
| 2618 | lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 2619 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2620 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2621 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2622 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2623 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2624 | if (!iter) { /* first iteration */ |
| 2625 | /* precess ".." in relative paths */ |
| 2626 | if (parent_times > 0) { |
| 2627 | /* move from the context node */ |
| 2628 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2629 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2630 | * all schema nodes that cannot be instantiated in data tree */ |
| 2631 | 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] | 2632 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2638 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2639 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2640 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2641 | } |
| 2642 | if (!mod) { |
| 2643 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2644 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2645 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2646 | return LY_EVALID; |
| 2647 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2648 | if (!mod->implemented) { |
| 2649 | /* make the module implemented */ |
| 2650 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2651 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2652 | |
| 2653 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2654 | if (!node) { |
| 2655 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2656 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2657 | return LY_EVALID; |
| 2658 | } |
| 2659 | parent = node; |
| 2660 | |
| 2661 | if (has_predicate) { |
| 2662 | /* we have predicate, so the current result must be list */ |
| 2663 | if (node->nodetype != LYS_LIST) { |
| 2664 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2665 | "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] | 2666 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2667 | return LY_EVALID; |
| 2668 | } |
| 2669 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2670 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2671 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | ++iter; |
| 2675 | } |
| 2676 | if (ret) { |
| 2677 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2678 | "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] | 2679 | return LY_EVALID; |
| 2680 | } |
| 2681 | |
| 2682 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2683 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2684 | "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] | 2685 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2686 | return LY_EVALID; |
| 2687 | } |
| 2688 | |
| 2689 | /* check status */ |
| 2690 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2691 | return LY_EVALID; |
| 2692 | } |
| 2693 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2694 | /* check config */ |
| 2695 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2696 | if (node->flags & LYS_CONFIG_R) { |
| 2697 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2698 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2699 | leafref->path); |
| 2700 | return LY_EVALID; |
| 2701 | } |
| 2702 | } |
| 2703 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2704 | /* store the target's type and check for circular chain of leafrefs */ |
| 2705 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2706 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2707 | if (type == (struct lysc_type*)leafref) { |
| 2708 | /* circular chain detected */ |
| 2709 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2710 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2711 | return LY_EVALID; |
| 2712 | } |
| 2713 | } |
| 2714 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2715 | /* check if leafref and its target are under common if-features */ |
| 2716 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2717 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2718 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2719 | leafref->path); |
| 2720 | return LY_EVALID; |
| 2721 | } |
| 2722 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2723 | ctx->path_len = 1; |
| 2724 | ctx->path[1] = '\0'; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2725 | return LY_SUCCESS; |
| 2726 | } |
| 2727 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2728 | 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] | 2729 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2730 | /** |
| 2731 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2732 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2733 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2734 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2735 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2736 | * @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] | 2737 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2738 | * @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] | 2739 | * @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] | 2740 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2741 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2742 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2743 | * @return LY_ERR value. |
| 2744 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2745 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2746 | 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] | 2747 | 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] | 2748 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2749 | { |
| 2750 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2751 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2752 | struct lysc_type_bin *bin; |
| 2753 | struct lysc_type_num *num; |
| 2754 | struct lysc_type_str *str; |
| 2755 | struct lysc_type_bits *bits; |
| 2756 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2757 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2758 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2759 | struct lysc_type_union *un, *un_aux; |
| 2760 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2761 | |
| 2762 | switch (basetype) { |
| 2763 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2764 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2765 | |
| 2766 | /* 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] | 2767 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2768 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2769 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2770 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2771 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | if (tpdfname) { |
| 2776 | type_p->compiled = *type; |
| 2777 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2778 | } |
| 2779 | break; |
| 2780 | case LY_TYPE_BITS: |
| 2781 | /* RFC 7950 9.7 - bits */ |
| 2782 | bits = (struct lysc_type_bits*)(*type); |
| 2783 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2784 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2785 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2786 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2787 | } |
| 2788 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2789 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2790 | /* 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] | 2791 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2792 | 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] | 2793 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2794 | 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] | 2795 | free(*type); |
| 2796 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2797 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2798 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | if (tpdfname) { |
| 2802 | type_p->compiled = *type; |
| 2803 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2804 | } |
| 2805 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2806 | case LY_TYPE_DEC64: |
| 2807 | dec = (struct lysc_type_dec*)(*type); |
| 2808 | |
| 2809 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2810 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2811 | if (!type_p->fraction_digits) { |
| 2812 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2813 | 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] | 2814 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2815 | 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] | 2816 | free(*type); |
| 2817 | *type = NULL; |
| 2818 | } |
| 2819 | return LY_EVALID; |
| 2820 | } |
| 2821 | } else if (type_p->fraction_digits) { |
| 2822 | /* 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] | 2823 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2824 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2825 | "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] | 2826 | tpdfname); |
| 2827 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2828 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2829 | "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] | 2830 | free(*type); |
| 2831 | *type = NULL; |
| 2832 | } |
| 2833 | return LY_EVALID; |
| 2834 | } |
| 2835 | dec->fraction_digits = type_p->fraction_digits; |
| 2836 | |
| 2837 | /* RFC 7950 9.2.4 - range */ |
| 2838 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2839 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2840 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2841 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2842 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2843 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | if (tpdfname) { |
| 2847 | type_p->compiled = *type; |
| 2848 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2849 | } |
| 2850 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2851 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2852 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2853 | |
| 2854 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2855 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2856 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2857 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2858 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2859 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2860 | } |
| 2861 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2862 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2863 | } |
| 2864 | |
| 2865 | /* RFC 7950 9.4.5 - pattern */ |
| 2866 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2867 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2868 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2869 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2870 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2871 | } |
| 2872 | |
| 2873 | if (tpdfname) { |
| 2874 | type_p->compiled = *type; |
| 2875 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2876 | } |
| 2877 | break; |
| 2878 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2879 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2880 | |
| 2881 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2882 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2883 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2884 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2885 | } |
| 2886 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2887 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2888 | /* 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] | 2889 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2890 | 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] | 2891 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2892 | 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] | 2893 | free(*type); |
| 2894 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2895 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2896 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | if (tpdfname) { |
| 2900 | type_p->compiled = *type; |
| 2901 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2902 | } |
| 2903 | break; |
| 2904 | case LY_TYPE_INT8: |
| 2905 | case LY_TYPE_UINT8: |
| 2906 | case LY_TYPE_INT16: |
| 2907 | case LY_TYPE_UINT16: |
| 2908 | case LY_TYPE_INT32: |
| 2909 | case LY_TYPE_UINT32: |
| 2910 | case LY_TYPE_INT64: |
| 2911 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2912 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2913 | |
| 2914 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2915 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2916 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2917 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2918 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2919 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2920 | } |
| 2921 | } |
| 2922 | |
| 2923 | if (tpdfname) { |
| 2924 | type_p->compiled = *type; |
| 2925 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2926 | } |
| 2927 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2928 | case LY_TYPE_IDENT: |
| 2929 | idref = (struct lysc_type_identityref*)(*type); |
| 2930 | |
| 2931 | /* RFC 7950 9.10.2 - base */ |
| 2932 | if (type_p->bases) { |
| 2933 | if (base) { |
| 2934 | /* only the directly derived identityrefs can contain base specification */ |
| 2935 | if (tpdfname) { |
| 2936 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2937 | "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] | 2938 | tpdfname); |
| 2939 | } else { |
| 2940 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2941 | "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] | 2942 | free(*type); |
| 2943 | *type = NULL; |
| 2944 | } |
| 2945 | return LY_EVALID; |
| 2946 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2947 | 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] | 2948 | } |
| 2949 | |
| 2950 | if (!base && !type_p->flags) { |
| 2951 | /* type derived from identityref built-in type must contain at least one base */ |
| 2952 | if (tpdfname) { |
| 2953 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2954 | } else { |
| 2955 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2956 | free(*type); |
| 2957 | *type = NULL; |
| 2958 | } |
| 2959 | return LY_EVALID; |
| 2960 | } |
| 2961 | |
| 2962 | if (tpdfname) { |
| 2963 | type_p->compiled = *type; |
| 2964 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2965 | } |
| 2966 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2967 | case LY_TYPE_LEAFREF: |
| 2968 | /* RFC 7950 9.9.3 - require-instance */ |
| 2969 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2970 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2971 | if (tpdfname) { |
| 2972 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2973 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2974 | } else { |
| 2975 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2976 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2977 | free(*type); |
| 2978 | *type = NULL; |
| 2979 | } |
| 2980 | return LY_EVALID; |
| 2981 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2982 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2983 | } else if (base) { |
| 2984 | /* inherit */ |
| 2985 | ((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] | 2986 | } else { |
| 2987 | /* default is true */ |
| 2988 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2989 | } |
| 2990 | if (type_p->path) { |
| 2991 | 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] | 2992 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2993 | } else if (base) { |
| 2994 | 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] | 2995 | ((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] | 2996 | } else if (tpdfname) { |
| 2997 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2998 | return LY_EVALID; |
| 2999 | } else { |
| 3000 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 3001 | free(*type); |
| 3002 | *type = NULL; |
| 3003 | return LY_EVALID; |
| 3004 | } |
| 3005 | if (tpdfname) { |
| 3006 | type_p->compiled = *type; |
| 3007 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3008 | } |
| 3009 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 3010 | case LY_TYPE_INST: |
| 3011 | /* RFC 7950 9.9.3 - require-instance */ |
| 3012 | if (type_p->flags & LYS_SET_REQINST) { |
| 3013 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 3014 | } else { |
| 3015 | /* default is true */ |
| 3016 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 3017 | } |
| 3018 | |
| 3019 | if (tpdfname) { |
| 3020 | type_p->compiled = *type; |
| 3021 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3022 | } |
| 3023 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3024 | case LY_TYPE_UNION: |
| 3025 | un = (struct lysc_type_union*)(*type); |
| 3026 | |
| 3027 | /* RFC 7950 7.4 - type */ |
| 3028 | if (type_p->types) { |
| 3029 | if (base) { |
| 3030 | /* only the directly derived union can contain types specification */ |
| 3031 | if (tpdfname) { |
| 3032 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3033 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 3034 | tpdfname); |
| 3035 | } else { |
| 3036 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 3037 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 3038 | free(*type); |
| 3039 | *type = NULL; |
| 3040 | } |
| 3041 | return LY_EVALID; |
| 3042 | } |
| 3043 | /* compile the type */ |
| 3044 | additional = 0; |
| 3045 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 3046 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3047 | 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] | 3048 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 3049 | /* add space for additional types from the union subtype */ |
| 3050 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 3051 | 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))); |
| 3052 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3053 | un->types = (void*)((uint32_t*)(p) + 1); |
| 3054 | |
| 3055 | /* copy subtypes of the subtype union */ |
| 3056 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 3057 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 3058 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 3059 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3060 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 3061 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 3062 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 3063 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 3064 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 3065 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 3066 | /* TODO extensions */ |
| 3067 | |
| 3068 | } else { |
| 3069 | un->types[u + additional] = un_aux->types[v]; |
| 3070 | ++un_aux->types[v]->refcount; |
| 3071 | } |
| 3072 | ++additional; |
| 3073 | LY_ARRAY_INCREMENT(un->types); |
| 3074 | } |
| 3075 | /* compensate u increment in main loop */ |
| 3076 | --additional; |
| 3077 | |
| 3078 | /* free the replaced union subtype */ |
| 3079 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 3080 | } else { |
| 3081 | LY_ARRAY_INCREMENT(un->types); |
| 3082 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3083 | } |
| 3084 | } |
| 3085 | |
| 3086 | if (!base && !type_p->flags) { |
| 3087 | /* type derived from union built-in type must contain at least one type */ |
| 3088 | if (tpdfname) { |
| 3089 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 3090 | } else { |
| 3091 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 3092 | free(*type); |
| 3093 | *type = NULL; |
| 3094 | } |
| 3095 | return LY_EVALID; |
| 3096 | } |
| 3097 | |
| 3098 | if (tpdfname) { |
| 3099 | type_p->compiled = *type; |
| 3100 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3101 | } |
| 3102 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3103 | case LY_TYPE_BOOL: |
| 3104 | case LY_TYPE_EMPTY: |
| 3105 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 3106 | break; |
| 3107 | } |
| 3108 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 3109 | done: |
| 3110 | return ret; |
| 3111 | } |
| 3112 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3113 | /** |
| 3114 | * @brief Compile information about the leaf/leaf-list's type. |
| 3115 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3116 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 3117 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3118 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3119 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 3120 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3121 | * @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] | 3122 | * @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] | 3123 | * @return LY_ERR value. |
| 3124 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3125 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3126 | 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] | 3127 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3128 | { |
| 3129 | LY_ERR ret = LY_SUCCESS; |
| 3130 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3131 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3132 | struct type_context { |
| 3133 | const struct lysp_tpdf *tpdf; |
| 3134 | struct lysp_node *node; |
| 3135 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3136 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3137 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3138 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3139 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3140 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3141 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3142 | |
| 3143 | (*type) = NULL; |
| 3144 | |
| 3145 | tctx = calloc(1, sizeof *tctx); |
| 3146 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3147 | 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] | 3148 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 3149 | ret == LY_SUCCESS; |
| 3150 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 3151 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 3152 | if (basetype) { |
| 3153 | break; |
| 3154 | } |
| 3155 | |
| 3156 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3157 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3158 | 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] | 3159 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3160 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3161 | if (units && !*units) { |
| 3162 | /* inherit units */ |
| 3163 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3164 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3165 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3166 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3167 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3168 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3169 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3170 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3171 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3172 | break; |
| 3173 | } |
| 3174 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3175 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3176 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3177 | * 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] | 3178 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3179 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3180 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3181 | dummyloops = 1; |
| 3182 | goto preparenext; |
| 3183 | } else { |
| 3184 | tctx = NULL; |
| 3185 | break; |
| 3186 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3187 | } |
| 3188 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3189 | /* circular typedef reference detection */ |
| 3190 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3191 | /* local part */ |
| 3192 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3193 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3194 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3195 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3196 | free(tctx); |
| 3197 | ret = LY_EVALID; |
| 3198 | goto cleanup; |
| 3199 | } |
| 3200 | } |
| 3201 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3202 | /* global part for unions corner case */ |
| 3203 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3204 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3205 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3206 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3207 | free(tctx); |
| 3208 | ret = LY_EVALID; |
| 3209 | goto cleanup; |
| 3210 | } |
| 3211 | } |
| 3212 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3213 | /* store information for the following processing */ |
| 3214 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3215 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3216 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3217 | /* prepare next loop */ |
| 3218 | tctx_prev = tctx; |
| 3219 | tctx = calloc(1, sizeof *tctx); |
| 3220 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3221 | } |
| 3222 | free(tctx); |
| 3223 | |
| 3224 | /* allocate type according to the basetype */ |
| 3225 | switch (basetype) { |
| 3226 | case LY_TYPE_BINARY: |
| 3227 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3228 | break; |
| 3229 | case LY_TYPE_BITS: |
| 3230 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3231 | break; |
| 3232 | case LY_TYPE_BOOL: |
| 3233 | case LY_TYPE_EMPTY: |
| 3234 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3235 | break; |
| 3236 | case LY_TYPE_DEC64: |
| 3237 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3238 | break; |
| 3239 | case LY_TYPE_ENUM: |
| 3240 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3241 | break; |
| 3242 | case LY_TYPE_IDENT: |
| 3243 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3244 | break; |
| 3245 | case LY_TYPE_INST: |
| 3246 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3247 | break; |
| 3248 | case LY_TYPE_LEAFREF: |
| 3249 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3250 | break; |
| 3251 | case LY_TYPE_STRING: |
| 3252 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3253 | break; |
| 3254 | case LY_TYPE_UNION: |
| 3255 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3256 | break; |
| 3257 | case LY_TYPE_INT8: |
| 3258 | case LY_TYPE_UINT8: |
| 3259 | case LY_TYPE_INT16: |
| 3260 | case LY_TYPE_UINT16: |
| 3261 | case LY_TYPE_INT32: |
| 3262 | case LY_TYPE_UINT32: |
| 3263 | case LY_TYPE_INT64: |
| 3264 | case LY_TYPE_UINT64: |
| 3265 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3266 | break; |
| 3267 | case LY_TYPE_UNKNOWN: |
| 3268 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3269 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3270 | ret = LY_EVALID; |
| 3271 | goto cleanup; |
| 3272 | } |
| 3273 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3274 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3275 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3276 | ly_data_type2str[basetype]); |
| 3277 | free(*type); |
| 3278 | (*type) = NULL; |
| 3279 | ret = LY_EVALID; |
| 3280 | goto cleanup; |
| 3281 | } |
| 3282 | |
| 3283 | /* get restrictions from the referred typedefs */ |
| 3284 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3285 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3286 | |
| 3287 | /* remember the typedef context for circular check */ |
| 3288 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3289 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3290 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3291 | base = tctx->tpdf->type.compiled; |
| 3292 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3293 | } 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] | 3294 | /* no change, just use the type information from the base */ |
| 3295 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3296 | ++base->refcount; |
| 3297 | continue; |
| 3298 | } |
| 3299 | |
| 3300 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3301 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3302 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3303 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3304 | ret = LY_EVALID; |
| 3305 | goto cleanup; |
| 3306 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3307 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3308 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3309 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3310 | ret = LY_EVALID; |
| 3311 | goto cleanup; |
| 3312 | } |
| 3313 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3314 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3315 | /* TODO user type plugins */ |
| 3316 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3317 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3318 | 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] | 3319 | 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] | 3320 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3321 | LY_CHECK_GOTO(ret, cleanup); |
| 3322 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3323 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3324 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3325 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3326 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3327 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3328 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3329 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3330 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3331 | /* TODO user type plugins */ |
| 3332 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3333 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3334 | 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] | 3335 | LY_CHECK_GOTO(ret, cleanup); |
| 3336 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3337 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3338 | free(*type); |
| 3339 | (*type) = base; |
| 3340 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3341 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3342 | if (dflt && !(*type)->dflt) { |
| 3343 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3344 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3345 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3346 | (*type)->dflt->realtype = (*type); |
| 3347 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3348 | 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] | 3349 | 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] | 3350 | if (err) { |
| 3351 | ly_err_print(err); |
| 3352 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3353 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3354 | ly_err_free(err); |
| 3355 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3356 | if (ret == LY_EINCOMPLETE) { |
| 3357 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3358 | 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] | 3359 | |
| 3360 | /* but in general result is so far ok */ |
| 3361 | ret = LY_SUCCESS; |
| 3362 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3363 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3364 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3365 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3366 | COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3367 | |
| 3368 | cleanup: |
| 3369 | ly_set_erase(&tpdf_chain, free); |
| 3370 | return ret; |
| 3371 | } |
| 3372 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3373 | /** |
| 3374 | * @brief Compile status information of the given node. |
| 3375 | * |
| 3376 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3377 | * has the status correctly set during the compilation. |
| 3378 | * |
| 3379 | * @param[in] ctx Compile context |
| 3380 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3381 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3382 | * the compatibility with the parent's status value. |
| 3383 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3384 | * @return LY_ERR value. |
| 3385 | */ |
| 3386 | static LY_ERR |
| 3387 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3388 | { |
| 3389 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3390 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3391 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3392 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3393 | if ((parent_flags & 0x3) != 0x3) { |
| 3394 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3395 | * combination of bits (0x3) which marks the uses_status value */ |
| 3396 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3397 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3398 | } |
| 3399 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3400 | } else { |
| 3401 | (*node_flags) |= LYS_STATUS_CURR; |
| 3402 | } |
| 3403 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3404 | /* check status compatibility with the parent */ |
| 3405 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3406 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3407 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3408 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3409 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3410 | } else { /* LYS_STATUS_DEPRC */ |
| 3411 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3412 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3413 | } |
| 3414 | return LY_EVALID; |
| 3415 | } |
| 3416 | } |
| 3417 | return LY_SUCCESS; |
| 3418 | } |
| 3419 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3420 | /** |
| 3421 | * @brief Check uniqness of the node/action/notification name. |
| 3422 | * |
| 3423 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3424 | * structures, but they share the namespace so we need to check their name collisions. |
| 3425 | * |
| 3426 | * @param[in] ctx Compile context. |
| 3427 | * @param[in] children List (linked list) of data nodes to go through. |
| 3428 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3429 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3430 | * @param[in] name Name of the item to find in the given lists. |
| 3431 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3432 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3433 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3434 | */ |
| 3435 | static LY_ERR |
| 3436 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3437 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3438 | const char *name, void *exclude) |
| 3439 | { |
| 3440 | const struct lysc_node *iter; |
| 3441 | unsigned int u; |
| 3442 | |
| 3443 | LY_LIST_FOR(children, iter) { |
| 3444 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3445 | goto error; |
| 3446 | } |
| 3447 | } |
| 3448 | LY_ARRAY_FOR(actions, u) { |
| 3449 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3450 | goto error; |
| 3451 | } |
| 3452 | } |
| 3453 | LY_ARRAY_FOR(notifs, u) { |
| 3454 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3455 | goto error; |
| 3456 | } |
| 3457 | } |
| 3458 | return LY_SUCCESS; |
| 3459 | error: |
| 3460 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification"); |
| 3461 | return LY_EEXIST; |
| 3462 | } |
| 3463 | |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 3464 | /** |
| 3465 | * @brief Get the XPath context node for the given schema node. |
| 3466 | * @param[in] start The schema node where the XPath expression appears. |
| 3467 | * @return The context node to evaluate XPath expression in given schema node. |
| 3468 | * @return NULL in case the context node is the root node. |
| 3469 | */ |
| 3470 | static struct lysc_node * |
| 3471 | lysc_xpath_context(struct lysc_node *start) |
| 3472 | { |
| 3473 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 3474 | start = start->parent); |
| 3475 | return start; |
| 3476 | } |
| 3477 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3478 | 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] | 3479 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3480 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3481 | * @brief Compile parsed RPC/action schema node information. |
| 3482 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3483 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3484 | * @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] | 3485 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3486 | * @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). |
| 3487 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3488 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3489 | */ |
| 3490 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3491 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3492 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3493 | { |
| 3494 | LY_ERR ret = LY_SUCCESS; |
| 3495 | struct lysp_node *child_p; |
| 3496 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3497 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3498 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3499 | lysc_update_path(ctx, parent, action_p->name); |
| 3500 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3501 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3502 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3503 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3504 | action_p->name, action)) { |
| 3505 | return LY_EVALID; |
| 3506 | } |
| 3507 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3508 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3509 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3510 | "Action \"%s\" is placed inside %s.", action_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3511 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3512 | return LY_EVALID; |
| 3513 | } |
| 3514 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3515 | action->nodetype = LYS_ACTION; |
| 3516 | action->module = ctx->mod; |
| 3517 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3518 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3519 | action->sp = action_p; |
| 3520 | } |
| 3521 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3522 | |
| 3523 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3524 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3525 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3526 | |
| 3527 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3528 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3529 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3530 | COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3531 | COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3532 | |
| 3533 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3534 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 3535 | COMPILE_ARRAY_MUST_GOTO(ctx, action_p->input.musts, action->input.musts, u, action, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3536 | COMPILE_EXTS_GOTO(ctx, action_p->input.exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3537 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3538 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3539 | 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] | 3540 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3541 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3542 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3543 | |
| 3544 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3545 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 3546 | COMPILE_ARRAY_MUST_GOTO(ctx, action_p->output.musts, action->output.musts, u, action, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3547 | COMPILE_EXTS_GOTO(ctx, action_p->output.exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3548 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3549 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3550 | 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] | 3551 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3552 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3553 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3554 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3555 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3556 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3557 | return ret; |
| 3558 | } |
| 3559 | |
| 3560 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3561 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3562 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3563 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3564 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3565 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3566 | * @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] | 3567 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3568 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3569 | */ |
| 3570 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3571 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3572 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3573 | { |
| 3574 | LY_ERR ret = LY_SUCCESS; |
| 3575 | struct lysp_node *child_p; |
| 3576 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3577 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3578 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3579 | lysc_update_path(ctx, parent, notif_p->name); |
| 3580 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3581 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3582 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3583 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3584 | notif_p->name, notif)) { |
| 3585 | return LY_EVALID; |
| 3586 | } |
| 3587 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3588 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3589 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3590 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3591 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3592 | return LY_EVALID; |
| 3593 | } |
| 3594 | |
| 3595 | notif->nodetype = LYS_NOTIF; |
| 3596 | notif->module = ctx->mod; |
| 3597 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3598 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3599 | notif->sp = notif_p; |
| 3600 | } |
| 3601 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3602 | |
| 3603 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3604 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3605 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3606 | |
| 3607 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3608 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3609 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3610 | COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 3611 | COMPILE_ARRAY_MUST_GOTO(ctx, notif_p->musts, notif->musts, u, notif, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3612 | COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3613 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3614 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3615 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3616 | 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] | 3617 | } |
| 3618 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3619 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3620 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3621 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3622 | return ret; |
| 3623 | } |
| 3624 | |
| 3625 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3626 | * @brief Compile parsed container node information. |
| 3627 | * @param[in] ctx Compile context |
| 3628 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3629 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3630 | * is enriched with the container-specific information. |
| 3631 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3632 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3633 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3634 | 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] | 3635 | { |
| 3636 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3637 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3638 | struct lysp_node *child_p; |
| 3639 | unsigned int u; |
| 3640 | LY_ERR ret = LY_SUCCESS; |
| 3641 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3642 | if (cont_p->presence) { |
| 3643 | cont->flags |= LYS_PRESENCE; |
| 3644 | } |
| 3645 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3646 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3647 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3648 | } |
| 3649 | |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 3650 | COMPILE_ARRAY_MUST_GOTO(ctx, cont_p->musts, cont->musts, u, node, ret, done); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3651 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3652 | 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] | 3653 | |
| 3654 | done: |
| 3655 | return ret; |
| 3656 | } |
| 3657 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3658 | /* |
| 3659 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3660 | * @param[in] ctx Compile context. |
| 3661 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3662 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3663 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3664 | * @return LY_ERR value. |
| 3665 | */ |
| 3666 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3667 | 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] | 3668 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3669 | unsigned int u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3670 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3671 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3672 | 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] | 3673 | leaf->units ? NULL : &leaf->units)); |
| 3674 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3675 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3676 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3677 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3678 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3679 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3680 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3681 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3682 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3683 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3684 | } |
| 3685 | } else { |
| 3686 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3687 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3688 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3689 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3690 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3691 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3692 | } |
| 3693 | } |
| 3694 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3695 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3696 | ly_set_add(&ctx->unres, leaf, 0); |
| 3697 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3698 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3699 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3700 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3701 | ly_set_add(&ctx->unres, leaf, 0); |
| 3702 | } |
| 3703 | } |
| 3704 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3705 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3706 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3707 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3708 | return LY_EVALID; |
| 3709 | } |
| 3710 | } |
| 3711 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3712 | return LY_SUCCESS; |
| 3713 | } |
| 3714 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3715 | /** |
| 3716 | * @brief Compile parsed leaf node information. |
| 3717 | * @param[in] ctx Compile context |
| 3718 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3719 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3720 | * is enriched with the leaf-specific information. |
| 3721 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3722 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3723 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3724 | 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] | 3725 | { |
| 3726 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3727 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3728 | unsigned int u; |
| 3729 | LY_ERR ret = LY_SUCCESS; |
| 3730 | |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 3731 | COMPILE_ARRAY_MUST_GOTO(ctx, leaf_p->musts, leaf->musts, u, node, ret, done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3732 | if (leaf_p->units) { |
| 3733 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3734 | leaf->flags |= LYS_SET_UNITS; |
| 3735 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3736 | |
| 3737 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3738 | leaf->dflt = (void*)leaf_p->dflt; |
| 3739 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3740 | if (ret) { |
| 3741 | leaf->dflt = NULL; |
| 3742 | return ret; |
| 3743 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3744 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3745 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3746 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3747 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3748 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3749 | leaf->dflt->realtype = leaf->type; |
| 3750 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3751 | 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] | 3752 | 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] | 3753 | leaf->dflt->realtype->refcount++; |
| 3754 | if (err) { |
| 3755 | ly_err_print(err); |
| 3756 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3757 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3758 | ly_err_free(err); |
| 3759 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3760 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3761 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3762 | LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod)); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3763 | |
| 3764 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3765 | ret = LY_SUCCESS; |
| 3766 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3767 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3768 | leaf->flags |= LYS_SET_DFLT; |
| 3769 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3770 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3771 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3772 | done: |
| 3773 | return ret; |
| 3774 | } |
| 3775 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3776 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3777 | * @brief Compile parsed leaf-list node information. |
| 3778 | * @param[in] ctx Compile context |
| 3779 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3780 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3781 | * is enriched with the leaf-list-specific information. |
| 3782 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3783 | */ |
| 3784 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3785 | 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] | 3786 | { |
| 3787 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3788 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3789 | unsigned int u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3790 | LY_ERR ret = LY_SUCCESS; |
| 3791 | |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 3792 | COMPILE_ARRAY_MUST_GOTO(ctx, llist_p->musts, llist->musts, u, node, ret, done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3793 | if (llist_p->units) { |
| 3794 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3795 | llist->flags |= LYS_SET_UNITS; |
| 3796 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3797 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3798 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3799 | llist->dflts = (void*)llist_p->dflts; |
| 3800 | 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] | 3801 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3802 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3803 | 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] | 3804 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3805 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3806 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3807 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3808 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3809 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3810 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3811 | llist->dflts[u]->realtype = llist->type; |
| 3812 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3813 | 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] | 3814 | 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] | 3815 | llist->dflts[u]->realtype->refcount++; |
| 3816 | if (err) { |
| 3817 | ly_err_print(err); |
| 3818 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3819 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3820 | ly_err_free(err); |
| 3821 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3822 | if (ret == LY_EINCOMPLETE) { |
| 3823 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3824 | 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] | 3825 | |
| 3826 | /* but in general result is so far ok */ |
| 3827 | ret = LY_SUCCESS; |
| 3828 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3829 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3830 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3831 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3832 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3833 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3834 | /* configuration data values must be unique - so check the default values */ |
| 3835 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3836 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3837 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3838 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3839 | 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] | 3840 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3841 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3842 | if (dynamic) { |
| 3843 | free((char*)val); |
| 3844 | } |
| 3845 | return LY_EVALID; |
| 3846 | } |
| 3847 | } |
| 3848 | } |
| 3849 | } |
| 3850 | |
| 3851 | /* 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] | 3852 | |
| 3853 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3854 | if (llist->min) { |
| 3855 | llist->flags |= LYS_MAND_TRUE; |
| 3856 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3857 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3858 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3859 | done: |
| 3860 | return ret; |
| 3861 | } |
| 3862 | |
| 3863 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3864 | * @brief Compile information about list's uniques. |
| 3865 | * @param[in] ctx Compile context. |
| 3866 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3867 | * @param[in] uniques Sized array list of unique statements. |
| 3868 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3869 | * @return LY_ERR value. |
| 3870 | */ |
| 3871 | static LY_ERR |
| 3872 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3873 | { |
| 3874 | LY_ERR ret = LY_SUCCESS; |
| 3875 | struct lysc_node_leaf **key, ***unique; |
| 3876 | const char *keystr, *delim; |
| 3877 | size_t len; |
| 3878 | unsigned int v; |
| 3879 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3880 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3881 | |
| 3882 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3883 | config = -1; |
| 3884 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3885 | keystr = uniques[v]; |
| 3886 | while (keystr) { |
| 3887 | delim = strpbrk(keystr, " \t\n"); |
| 3888 | if (delim) { |
| 3889 | len = delim - keystr; |
| 3890 | while (isspace(*delim)) { |
| 3891 | ++delim; |
| 3892 | } |
| 3893 | } else { |
| 3894 | len = strlen(keystr); |
| 3895 | } |
| 3896 | |
| 3897 | /* unique node must be present */ |
| 3898 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3899 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3900 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3901 | if (ret != LY_SUCCESS) { |
| 3902 | if (ret == LY_EDENIED) { |
| 3903 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3904 | "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] | 3905 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3906 | } |
| 3907 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3908 | } else if (flags) { |
| 3909 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3910 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3911 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
| 3912 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3913 | } |
| 3914 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3915 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3916 | /* all referenced leafs must be of the same config type */ |
| 3917 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3918 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3919 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3920 | return LY_EVALID; |
| 3921 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3922 | config = 1; |
| 3923 | } else { /* LYS_CONFIG_R */ |
| 3924 | config = 0; |
| 3925 | } |
| 3926 | |
| 3927 | /* check status */ |
| 3928 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3929 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3930 | |
| 3931 | /* mark leaf as unique */ |
| 3932 | (*key)->flags |= LYS_UNIQUE; |
| 3933 | |
| 3934 | /* next unique value in line */ |
| 3935 | keystr = delim; |
| 3936 | } |
| 3937 | /* next unique definition */ |
| 3938 | } |
| 3939 | |
| 3940 | return LY_SUCCESS; |
| 3941 | } |
| 3942 | |
| 3943 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3944 | * @brief Compile parsed list node information. |
| 3945 | * @param[in] ctx Compile context |
| 3946 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3947 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3948 | * is enriched with the list-specific information. |
| 3949 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3950 | */ |
| 3951 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3952 | 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] | 3953 | { |
| 3954 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3955 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3956 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3957 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3958 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3959 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3960 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3961 | LY_ERR ret = LY_SUCCESS; |
| 3962 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3963 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3964 | if (list->min) { |
| 3965 | list->flags |= LYS_MAND_TRUE; |
| 3966 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3967 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3968 | |
| 3969 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3970 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3971 | } |
| 3972 | |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 3973 | COMPILE_ARRAY_MUST_GOTO(ctx, list_p->musts, list->musts, u, node, ret, done); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3974 | |
| 3975 | /* keys */ |
| 3976 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3977 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3978 | return LY_EVALID; |
| 3979 | } |
| 3980 | |
| 3981 | /* find all the keys (must be direct children) */ |
| 3982 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3983 | if (!keystr) { |
| 3984 | /* keyless list */ |
| 3985 | list->flags |= LYS_KEYLESS; |
| 3986 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3987 | while (keystr) { |
| 3988 | delim = strpbrk(keystr, " \t\n"); |
| 3989 | if (delim) { |
| 3990 | len = delim - keystr; |
| 3991 | while (isspace(*delim)) { |
| 3992 | ++delim; |
| 3993 | } |
| 3994 | } else { |
| 3995 | len = strlen(keystr); |
| 3996 | } |
| 3997 | |
| 3998 | /* key node must be present */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3999 | key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 4000 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4001 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4002 | "The list's key \"%.*s\" not found.", len, keystr); |
| 4003 | return LY_EVALID; |
| 4004 | } |
| 4005 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4006 | if (key->flags & LYS_KEY) { |
| 4007 | /* the node was already marked as a key */ |
| 4008 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4009 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 4010 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4011 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4012 | |
| 4013 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4014 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4015 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4016 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 4017 | return LY_EVALID; |
| 4018 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4019 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4020 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4021 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4022 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4023 | "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] | 4024 | return LY_EVALID; |
| 4025 | } |
| 4026 | } else { |
| 4027 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4028 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4029 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4030 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4031 | return LY_EVALID; |
| 4032 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4033 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4034 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4035 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4036 | return LY_EVALID; |
| 4037 | } |
| 4038 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4039 | |
| 4040 | /* check status */ |
| 4041 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4042 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4043 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4044 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4045 | if (key->dflt) { |
| 4046 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 4047 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 4048 | free(key->dflt); |
| 4049 | key->dflt = NULL; |
| 4050 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4051 | } |
| 4052 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4053 | key->flags |= LYS_KEY; |
| 4054 | |
| 4055 | /* move it to the correct position */ |
| 4056 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 4057 | /* fix links in closest previous siblings of the key */ |
| 4058 | if (key->next) { |
| 4059 | key->next->prev = key->prev; |
| 4060 | } else { |
| 4061 | /* last child */ |
| 4062 | list->child->prev = key->prev; |
| 4063 | } |
| 4064 | if (key->prev->next) { |
| 4065 | key->prev->next = key->next; |
| 4066 | } |
| 4067 | /* fix links in the key */ |
| 4068 | if (prev_key) { |
| 4069 | key->prev = (struct lysc_node*)prev_key; |
| 4070 | key->next = prev_key->next; |
| 4071 | } else { |
| 4072 | key->prev = list->child->prev; |
| 4073 | key->next = list->child; |
| 4074 | } |
| 4075 | /* fix links in closes future siblings of the key */ |
| 4076 | if (prev_key) { |
| 4077 | if (prev_key->next) { |
| 4078 | prev_key->next->prev = (struct lysc_node*)key; |
| 4079 | } else { |
| 4080 | list->child->prev = (struct lysc_node*)key; |
| 4081 | } |
| 4082 | prev_key->next = (struct lysc_node*)key; |
| 4083 | } else { |
| 4084 | list->child->prev = (struct lysc_node*)key; |
| 4085 | } |
| 4086 | /* fix links in parent */ |
| 4087 | if (!key->prev->next) { |
| 4088 | list->child = (struct lysc_node*)key; |
| 4089 | } |
| 4090 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4091 | |
| 4092 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4093 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4094 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4095 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | /* uniques */ |
| 4099 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4100 | 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] | 4101 | } |
| 4102 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4103 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 4104 | 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] | 4105 | |
| 4106 | done: |
| 4107 | return ret; |
| 4108 | } |
| 4109 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4110 | /** |
| 4111 | * @brief Do some checks and set the default choice's case. |
| 4112 | * |
| 4113 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 4114 | * |
| 4115 | * @param[in] ctx Compile context. |
| 4116 | * @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, |
| 4117 | * not the reference to the imported module. |
| 4118 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 4119 | * @return LY_ERR value. |
| 4120 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4121 | static LY_ERR |
| 4122 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 4123 | { |
| 4124 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 4125 | const char *prefix = NULL, *name; |
| 4126 | size_t prefix_len = 0; |
| 4127 | |
| 4128 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4129 | name = strchr(dflt, ':'); |
| 4130 | if (name) { |
| 4131 | prefix = dflt; |
| 4132 | prefix_len = name - prefix; |
| 4133 | ++name; |
| 4134 | } else { |
| 4135 | name = dflt; |
| 4136 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4137 | 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] | 4138 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 4139 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4140 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 4141 | prefix_len, prefix); |
| 4142 | return LY_EVALID; |
| 4143 | } |
| 4144 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 4145 | if (!ch->dflt) { |
| 4146 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4147 | "Default case \"%s\" not found.", dflt); |
| 4148 | return LY_EVALID; |
| 4149 | } |
| 4150 | /* no mandatory nodes directly under the default case */ |
| 4151 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4152 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 4153 | break; |
| 4154 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4155 | if (iter->flags & LYS_MAND_TRUE) { |
| 4156 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4157 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 4158 | return LY_EVALID; |
| 4159 | } |
| 4160 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4161 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4162 | return LY_SUCCESS; |
| 4163 | } |
| 4164 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4165 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4166 | 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] | 4167 | { |
| 4168 | struct lys_module *mod; |
| 4169 | const char *prefix = NULL, *name; |
| 4170 | size_t prefix_len = 0; |
| 4171 | struct lysc_node_case *cs; |
| 4172 | struct lysc_node *node; |
| 4173 | |
| 4174 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4175 | name = strchr(dflt, ':'); |
| 4176 | if (name) { |
| 4177 | prefix = dflt; |
| 4178 | prefix_len = name - prefix; |
| 4179 | ++name; |
| 4180 | } else { |
| 4181 | name = dflt; |
| 4182 | } |
| 4183 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 4184 | if (prefix) { |
| 4185 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 4186 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4187 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 4188 | "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] | 4189 | return LY_EVALID; |
| 4190 | } |
| 4191 | } else { |
| 4192 | mod = ctx->mod; |
| 4193 | } |
| 4194 | /* get the default case */ |
| 4195 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 4196 | if (!cs) { |
| 4197 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4198 | "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] | 4199 | return LY_EVALID; |
| 4200 | } |
| 4201 | |
| 4202 | /* check that there is no mandatory node */ |
| 4203 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4204 | if (node->parent != (struct lysc_node*)cs) { |
| 4205 | break; |
| 4206 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4207 | if (node->flags & LYS_MAND_TRUE) { |
| 4208 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4209 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4210 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4211 | return LY_EVALID; |
| 4212 | } |
| 4213 | } |
| 4214 | |
| 4215 | /* set the default case in choice */ |
| 4216 | ch->dflt = cs; |
| 4217 | cs->flags |= LYS_SET_DFLT; |
| 4218 | |
| 4219 | return LY_SUCCESS; |
| 4220 | } |
| 4221 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4222 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4223 | * @brief Compile parsed choice node information. |
| 4224 | * @param[in] ctx Compile context |
| 4225 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4226 | * @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] | 4227 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4228 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4229 | */ |
| 4230 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4231 | 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] | 4232 | { |
| 4233 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4234 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4235 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4236 | LY_ERR ret = LY_SUCCESS; |
| 4237 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4238 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4239 | if (child_p->nodetype == LYS_CASE) { |
| 4240 | 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] | 4241 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4242 | } |
| 4243 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4244 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4245 | } |
| 4246 | } |
| 4247 | |
| 4248 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4249 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4250 | 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] | 4251 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4252 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4253 | return ret; |
| 4254 | } |
| 4255 | |
| 4256 | /** |
| 4257 | * @brief Compile parsed anydata or anyxml node information. |
| 4258 | * @param[in] ctx Compile context |
| 4259 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4260 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4261 | * is enriched with the any-specific information. |
| 4262 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4263 | */ |
| 4264 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4265 | 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] | 4266 | { |
| 4267 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4268 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4269 | unsigned int u; |
| 4270 | LY_ERR ret = LY_SUCCESS; |
| 4271 | |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 4272 | COMPILE_ARRAY_MUST_GOTO(ctx, any_p->musts, any->musts, u, node, ret, done); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4273 | |
| 4274 | if (any->flags & LYS_CONFIG_W) { |
| 4275 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4276 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4277 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4278 | done: |
| 4279 | return ret; |
| 4280 | } |
| 4281 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4282 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4283 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4284 | * |
| 4285 | * @param[in] ctx Compile context |
| 4286 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4287 | * the choice itself is expected instead of a specific case node. |
| 4288 | * @param[in] node Schema node to connect into the list. |
| 4289 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 4290 | */ |
| 4291 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4292 | 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] | 4293 | { |
| 4294 | struct lysc_node **children; |
| 4295 | |
| 4296 | if (node->nodetype == LYS_CASE) { |
| 4297 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4298 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4299 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4300 | } |
| 4301 | if (children) { |
| 4302 | if (!(*children)) { |
| 4303 | /* first child */ |
| 4304 | *children = node; |
| 4305 | } else if (*children != node) { |
| 4306 | /* by the condition in previous branch we cover the choice/case children |
| 4307 | * - the children list is shared by the choice and the the first case, in addition |
| 4308 | * the first child of each case must be referenced from the case node. So the node is |
| 4309 | * actually always already inserted in case it is the first children - so here such |
| 4310 | * a situation actually corresponds to the first branch */ |
| 4311 | /* insert at the end of the parent's children list */ |
| 4312 | (*children)->prev->next = node; |
| 4313 | node->prev = (*children)->prev; |
| 4314 | (*children)->prev = node; |
| 4315 | |
| 4316 | /* check the name uniqueness */ |
| 4317 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4318 | lysc_node_notifs(parent), node->name, node)) { |
| 4319 | return LY_EEXIST; |
| 4320 | } |
| 4321 | } |
| 4322 | } |
| 4323 | return LY_SUCCESS; |
| 4324 | } |
| 4325 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4326 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4327 | * @brief Prepare the case structure in choice node for the new data node. |
| 4328 | * |
| 4329 | * 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 |
| 4330 | * created in the choice when the first child was processed. |
| 4331 | * |
| 4332 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4333 | * @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, |
| 4334 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4335 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4336 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4337 | * @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, |
| 4338 | * 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] | 4339 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4340 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4341 | 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] | 4342 | { |
| 4343 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4344 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4345 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4346 | unsigned int u; |
| 4347 | LY_ERR ret; |
| 4348 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4349 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4350 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4351 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4352 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4353 | return NULL; \ |
| 4354 | } \ |
| 4355 | } |
| 4356 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4357 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4358 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4359 | |
| 4360 | /* we have to add an implicit case node into the parent choice */ |
| 4361 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4362 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 4363 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4364 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4365 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4366 | /* the case is already present since the child is not its first children */ |
| 4367 | return (struct lysc_node_case*)ch->cases->prev; |
| 4368 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4369 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4370 | |
| 4371 | /* explicit parent case is not present (this is its first child) */ |
| 4372 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4373 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 4374 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4375 | cs->sp = node_p; |
| 4376 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4377 | /* 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] | 4378 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4379 | |
| 4380 | if (node_p->when) { |
| 4381 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4382 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4383 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4384 | (*when)->context = lysc_xpath_context(ch->parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4385 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4386 | 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] | 4387 | } else { |
| 4388 | LOGINT(ctx->ctx); |
| 4389 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4390 | } |
| 4391 | cs->module = ctx->mod; |
| 4392 | cs->prev = (struct lysc_node*)cs; |
| 4393 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4394 | 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] | 4395 | cs->parent = (struct lysc_node*)ch; |
| 4396 | cs->child = child; |
| 4397 | |
| 4398 | return cs; |
| 4399 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4400 | if (cs) { |
| 4401 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4402 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4403 | return NULL; |
| 4404 | |
| 4405 | #undef UNIQUE_CHECK |
| 4406 | } |
| 4407 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4408 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4409 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4410 | * |
| 4411 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4412 | * @param[in] node Target node where the config is supposed to be changed. |
| 4413 | * @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] | 4414 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4415 | * 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] | 4416 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4417 | * @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] | 4418 | * @return LY_ERR value. |
| 4419 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4420 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4421 | 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] | 4422 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4423 | { |
| 4424 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4425 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4426 | |
| 4427 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4428 | /* nothing to do */ |
| 4429 | return LY_SUCCESS; |
| 4430 | } |
| 4431 | |
| 4432 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4433 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4434 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4435 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4436 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4437 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4438 | return LY_EVALID; |
| 4439 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4440 | node->flags |= LYS_SET_CONFIG; |
| 4441 | } else { |
| 4442 | if (node->flags & LYS_SET_CONFIG) { |
| 4443 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4444 | /* setting config flags, but have node with explicit config true */ |
| 4445 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4446 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4447 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4448 | return LY_EVALID; |
| 4449 | } |
| 4450 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4451 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4452 | return LY_SUCCESS; |
| 4453 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4454 | } |
| 4455 | node->flags &= ~LYS_CONFIG_MASK; |
| 4456 | node->flags |= config; |
| 4457 | |
| 4458 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4459 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4460 | 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] | 4461 | } |
| 4462 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4463 | return LY_SUCCESS; |
| 4464 | } |
| 4465 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4466 | /** |
| 4467 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4468 | * |
| 4469 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4470 | * the flag to such parents from a mandatory children. |
| 4471 | * |
| 4472 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4473 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4474 | * (mandatory children was removed). |
| 4475 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4476 | void |
| 4477 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4478 | { |
| 4479 | struct lysc_node *iter; |
| 4480 | |
| 4481 | if (add) { /* set flag */ |
| 4482 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4483 | parent = parent->parent) { |
| 4484 | parent->flags |= LYS_MAND_TRUE; |
| 4485 | } |
| 4486 | } else { /* unset flag */ |
| 4487 | 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] | 4488 | 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] | 4489 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4490 | /* there is another mandatory node */ |
| 4491 | return; |
| 4492 | } |
| 4493 | } |
| 4494 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4495 | parent->flags &= ~LYS_MAND_TRUE; |
| 4496 | } |
| 4497 | } |
| 4498 | } |
| 4499 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4500 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4501 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4502 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4503 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4504 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4505 | */ |
| 4506 | static void |
| 4507 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4508 | { |
| 4509 | unsigned int v; |
| 4510 | size_t len; |
| 4511 | |
| 4512 | len = strlen(aug_p->nodeid); |
| 4513 | LY_ARRAY_FOR(result, v) { |
| 4514 | if (strlen(result[v]->nodeid) <= len) { |
| 4515 | continue; |
| 4516 | } |
| 4517 | if (v < LY_ARRAY_SIZE(result)) { |
| 4518 | /* move the rest of array */ |
| 4519 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4520 | break; |
| 4521 | } |
| 4522 | } |
| 4523 | result[v] = aug_p; |
| 4524 | LY_ARRAY_INCREMENT(result); |
| 4525 | } |
| 4526 | |
| 4527 | /** |
| 4528 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4529 | * |
| 4530 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4531 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4532 | * |
| 4533 | * @param[in] ctx Compile context. |
| 4534 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4535 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4536 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4537 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4538 | * @return LY_ERR value. |
| 4539 | */ |
| 4540 | LY_ERR |
| 4541 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4542 | { |
| 4543 | struct lysp_augment **result = NULL; |
| 4544 | unsigned int u, v; |
| 4545 | size_t count = 0; |
| 4546 | |
| 4547 | assert(augments); |
| 4548 | |
| 4549 | /* get count of the augments in module and all its submodules */ |
| 4550 | if (aug_p) { |
| 4551 | count += LY_ARRAY_SIZE(aug_p); |
| 4552 | } |
| 4553 | LY_ARRAY_FOR(inc_p, u) { |
| 4554 | if (inc_p[u].submodule->augments) { |
| 4555 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4556 | } |
| 4557 | } |
| 4558 | |
| 4559 | if (!count) { |
| 4560 | *augments = NULL; |
| 4561 | return LY_SUCCESS; |
| 4562 | } |
| 4563 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4564 | |
| 4565 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4566 | * together, so there can be even /z/y betwwen them. */ |
| 4567 | LY_ARRAY_FOR(aug_p, u) { |
| 4568 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4569 | } |
| 4570 | LY_ARRAY_FOR(inc_p, u) { |
| 4571 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4572 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | *augments = result; |
| 4577 | return LY_SUCCESS; |
| 4578 | } |
| 4579 | |
| 4580 | /** |
| 4581 | * @brief Compile the parsed augment connecting it into its target. |
| 4582 | * |
| 4583 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4584 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4585 | * are already implemented and compiled. |
| 4586 | * |
| 4587 | * @param[in] ctx Compile context. |
| 4588 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4589 | * @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 |
| 4590 | * children in case of the augmenting uses data. |
| 4591 | * @return LY_SUCCESS on success. |
| 4592 | * @return LY_EVALID on failure. |
| 4593 | */ |
| 4594 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4595 | 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] | 4596 | { |
| 4597 | LY_ERR ret = LY_SUCCESS; |
| 4598 | struct lysp_node *node_p, *case_node_p; |
| 4599 | struct lysc_node *target; /* target target of the augment */ |
| 4600 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4601 | struct lysc_when **when, *when_shared; |
| 4602 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4603 | uint16_t flags = 0; |
| 4604 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4605 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4606 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4607 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4608 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4609 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4610 | 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] | 4611 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4612 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4613 | if (ret != LY_SUCCESS) { |
| 4614 | if (ret == LY_EDENIED) { |
| 4615 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4616 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4617 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4618 | } |
| 4619 | return LY_EVALID; |
| 4620 | } |
| 4621 | |
| 4622 | /* check for mandatory nodes |
| 4623 | * - new cases augmenting some choice can have mandatory nodes |
| 4624 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4625 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4626 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4627 | allow_mandatory = 1; |
| 4628 | } |
| 4629 | |
| 4630 | when_shared = NULL; |
| 4631 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4632 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4633 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4634 | && !((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] | 4635 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4636 | && !(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] | 4637 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4638 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4639 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4640 | return LY_EVALID; |
| 4641 | } |
| 4642 | |
| 4643 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4644 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4645 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4646 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4647 | } else { |
| 4648 | 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] | 4649 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4650 | } |
| 4651 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4652 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4653 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4654 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4655 | * 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] | 4656 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4657 | /* 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] | 4658 | 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] | 4659 | } else if (target->nodetype == LYS_CHOICE) { |
| 4660 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4661 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4662 | } else { |
| 4663 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4664 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4665 | } |
| 4666 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4667 | 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] | 4668 | node->flags &= ~LYS_MAND_TRUE; |
| 4669 | lys_compile_mandatory_parents(target, 0); |
| 4670 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4671 | "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] | 4672 | return LY_EVALID; |
| 4673 | } |
| 4674 | |
| 4675 | /* pass augment's when to all the children */ |
| 4676 | if (aug_p->when) { |
| 4677 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4678 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4679 | ret = lys_compile_when(ctx, aug_p->when, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4680 | LY_CHECK_GOTO(ret, error); |
| 4681 | (*when)->context = lysc_xpath_context(target); |
| 4682 | when_shared = *when; |
| 4683 | } else { |
| 4684 | ++when_shared->refcount; |
| 4685 | (*when) = when_shared; |
| 4686 | } |
| 4687 | } |
| 4688 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4689 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4690 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4691 | switch (target->nodetype) { |
| 4692 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4693 | 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] | 4694 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4695 | 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] | 4696 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4697 | break; |
| 4698 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4699 | 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] | 4700 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4701 | 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] | 4702 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4703 | break; |
| 4704 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4705 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4706 | if (aug_p->actions) { |
| 4707 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4708 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4709 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4710 | return LY_EVALID; |
| 4711 | } |
| 4712 | if (aug_p->notifs) { |
| 4713 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4714 | "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".", |
| 4715 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4716 | return LY_EVALID; |
| 4717 | } |
| 4718 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4719 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4720 | lysc_update_path(ctx, NULL, NULL); |
| 4721 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4722 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4723 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4724 | return ret; |
| 4725 | } |
| 4726 | |
| 4727 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4728 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4729 | * |
| 4730 | * @param[in] ctx Compile context. |
| 4731 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4732 | * @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] | 4733 | * @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] | 4734 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4735 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4736 | * 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] | 4737 | * @return LY_ERR value. |
| 4738 | */ |
| 4739 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4740 | 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] | 4741 | { |
| 4742 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4744 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4745 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4746 | return LY_EVALID; |
| 4747 | } |
| 4748 | |
| 4749 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4750 | /* check if node has default value */ |
| 4751 | if (node->nodetype & LYS_LEAF) { |
| 4752 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4753 | if (refine_flag) { |
| 4754 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4755 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4756 | return LY_EVALID; |
| 4757 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4758 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4759 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4760 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4761 | |
| 4762 | /* update the list of incomplete default values if needed */ |
| 4763 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4764 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4765 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4766 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4767 | free(leaf->dflt); |
| 4768 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4769 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4770 | } |
| 4771 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4772 | if (refine_flag) { |
| 4773 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4774 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4775 | return LY_EVALID; |
| 4776 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4777 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4778 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4779 | 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] | 4780 | return LY_EVALID; |
| 4781 | } |
| 4782 | |
| 4783 | node->flags &= ~LYS_MAND_FALSE; |
| 4784 | node->flags |= LYS_MAND_TRUE; |
| 4785 | lys_compile_mandatory_parents(node->parent, 1); |
| 4786 | } else { |
| 4787 | /* make mandatory false */ |
| 4788 | node->flags &= ~LYS_MAND_TRUE; |
| 4789 | node->flags |= LYS_MAND_FALSE; |
| 4790 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4791 | 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] | 4792 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4793 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4794 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4795 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4796 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4797 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4798 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4799 | } |
| 4800 | } |
| 4801 | return LY_SUCCESS; |
| 4802 | } |
| 4803 | |
| 4804 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4805 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4806 | * If present, also apply uses's modificators. |
| 4807 | * |
| 4808 | * @param[in] ctx Compile context |
| 4809 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4810 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4811 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4812 | * the compile context. |
| 4813 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4814 | */ |
| 4815 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4816 | 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] | 4817 | { |
| 4818 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4819 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4820 | /* 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] | 4821 | struct lysc_node_container context_node_fake = |
| 4822 | {.nodetype = LYS_CONTAINER, |
| 4823 | .module = ctx->mod, |
| 4824 | .flags = parent ? parent->flags : 0, |
| 4825 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4826 | .prev = (struct lysc_node*)&context_node_fake, |
| 4827 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4828 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4829 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4830 | int found; |
| 4831 | const char *id, *name, *prefix; |
| 4832 | size_t prefix_len, name_len; |
| 4833 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4834 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4835 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4836 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4837 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4838 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4839 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4840 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4841 | unsigned int actions_index, notifs_index; |
| 4842 | struct lysc_notif **notifs = NULL; |
| 4843 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4844 | |
| 4845 | /* search for the grouping definition */ |
| 4846 | found = 0; |
| 4847 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4848 | 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] | 4849 | if (prefix) { |
| 4850 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4851 | if (!mod) { |
| 4852 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4853 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4854 | return LY_EVALID; |
| 4855 | } |
| 4856 | } else { |
| 4857 | mod = ctx->mod_def; |
| 4858 | } |
| 4859 | if (mod == ctx->mod_def) { |
| 4860 | 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] | 4861 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4862 | LY_ARRAY_FOR(grp, u) { |
| 4863 | if (!strcmp(grp[u].name, name)) { |
| 4864 | grp = &grp[u]; |
| 4865 | found = 1; |
| 4866 | break; |
| 4867 | } |
| 4868 | } |
| 4869 | } |
| 4870 | } |
| 4871 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4872 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4873 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4874 | if (grp) { |
| 4875 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4876 | if (!strcmp(grp[u].name, name)) { |
| 4877 | grp = &grp[u]; |
| 4878 | found = 1; |
| 4879 | } |
| 4880 | } |
| 4881 | } |
| 4882 | if (!found && mod->parsed->includes) { |
| 4883 | /* ... and all the submodules */ |
| 4884 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4885 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4886 | if (grp) { |
| 4887 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4888 | if (!strcmp(grp[v].name, name)) { |
| 4889 | grp = &grp[v]; |
| 4890 | found = 1; |
| 4891 | } |
| 4892 | } |
| 4893 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4894 | } |
| 4895 | } |
| 4896 | } |
| 4897 | if (!found) { |
| 4898 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4899 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4900 | return LY_EVALID; |
| 4901 | } |
| 4902 | |
| 4903 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4904 | grp_stack_count = ctx->groupings.count; |
| 4905 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4906 | if (grp_stack_count == ctx->groupings.count) { |
| 4907 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4908 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4909 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4910 | return LY_EVALID; |
| 4911 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4912 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4913 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4914 | grp->flags |= LYS_USED_GRP; |
| 4915 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4916 | |
| 4917 | /* switch context's mod_def */ |
| 4918 | mod_old = ctx->mod_def; |
| 4919 | ctx->mod_def = mod; |
| 4920 | |
| 4921 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4922 | 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] | 4923 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4924 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4925 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4926 | /* 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] | 4927 | 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] | 4928 | |
| 4929 | /* some preparation for applying refines */ |
| 4930 | if (grp->data == node_p) { |
| 4931 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4932 | if (parent) { |
| 4933 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4934 | } else if (ctx->mod->compiled->data) { |
| 4935 | child = ctx->mod->compiled->data; |
| 4936 | } else { |
| 4937 | child = NULL; |
| 4938 | } |
| 4939 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4940 | } |
| 4941 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4942 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4943 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4944 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4945 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4946 | /* 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] | 4947 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4948 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4949 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4950 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4951 | (*when)->context = lysc_xpath_context(parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4952 | when_shared = *when; |
| 4953 | } else { |
| 4954 | ++when_shared->refcount; |
| 4955 | (*when) = when_shared; |
| 4956 | } |
| 4957 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4958 | } |
| 4959 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4960 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4961 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4962 | /* 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] | 4963 | 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] | 4964 | } |
| 4965 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4966 | /* compile actions */ |
| 4967 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4968 | if (actions) { |
| 4969 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4970 | 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] | 4971 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4972 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4973 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4974 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4975 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4976 | } |
| 4977 | } |
| 4978 | |
| 4979 | /* compile notifications */ |
| 4980 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4981 | if (notifs) { |
| 4982 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4983 | 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] | 4984 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4985 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4986 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4987 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4988 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4989 | } |
| 4990 | } |
| 4991 | |
| 4992 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4993 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4994 | 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] | 4995 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4996 | 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] | 4997 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4998 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4999 | /* reload previous context's mod_def */ |
| 5000 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5001 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5002 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5003 | /* apply refine */ |
| 5004 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5005 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 5006 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5007 | 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] | 5008 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5009 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5010 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5011 | |
| 5012 | /* default value */ |
| 5013 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5014 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5015 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5016 | "Invalid refine of default - %s cannot hold %d default values.", |
| 5017 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5018 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5019 | } |
| 5020 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 5021 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5022 | "Invalid refine of default - %s cannot hold default value(s).", |
| 5023 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5024 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5025 | } |
| 5026 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5027 | struct ly_err_item *err = NULL; |
| 5028 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 5029 | if (leaf->dflt) { |
| 5030 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5031 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5032 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5033 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5034 | } else { |
| 5035 | /* prepare a new one */ |
| 5036 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 5037 | leaf->dflt->realtype = leaf->type; |
| 5038 | } |
| 5039 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5040 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5041 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 5042 | 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] | 5043 | 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] | 5044 | leaf->dflt->realtype->refcount++; |
| 5045 | if (err) { |
| 5046 | ly_err_print(err); |
| 5047 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5048 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 5049 | ly_err_free(err); |
| 5050 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5051 | if (rc == LY_EINCOMPLETE) { |
| 5052 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5053 | 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] | 5054 | |
| 5055 | /* but in general result is so far ok */ |
| 5056 | rc = LY_SUCCESS; |
| 5057 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5058 | LY_CHECK_GOTO(rc, cleanup); |
| 5059 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5060 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5061 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 5062 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5063 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5064 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5065 | "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] | 5066 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5067 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5068 | |
| 5069 | /* remove previous set of default values */ |
| 5070 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5071 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5072 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 5073 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 5074 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5075 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5076 | LY_ARRAY_FREE(llist->dflts); |
| 5077 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5078 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5079 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5080 | |
| 5081 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5082 | 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] | 5083 | 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] | 5084 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5085 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5086 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5087 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5088 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5089 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 5090 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5091 | 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] | 5092 | 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] | 5093 | 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] | 5094 | llist->dflts[u]->realtype->refcount++; |
| 5095 | if (err) { |
| 5096 | ly_err_print(err); |
| 5097 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5098 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 5099 | ly_err_free(err); |
| 5100 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5101 | if (rc == LY_EINCOMPLETE) { |
| 5102 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5103 | 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] | 5104 | |
| 5105 | /* but in general result is so far ok */ |
| 5106 | rc = LY_SUCCESS; |
| 5107 | } |
| 5108 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5109 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5110 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5111 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5112 | if (((struct lysc_node_choice*)node)->dflt) { |
| 5113 | /* unset LYS_SET_DFLT from the current default case */ |
| 5114 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 5115 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5116 | 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] | 5117 | } |
| 5118 | } |
| 5119 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5120 | /* description */ |
| 5121 | if (rfn->dsc) { |
| 5122 | FREE_STRING(ctx->ctx, node->dsc); |
| 5123 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 5124 | } |
| 5125 | |
| 5126 | /* reference */ |
| 5127 | if (rfn->ref) { |
| 5128 | FREE_STRING(ctx->ctx, node->ref); |
| 5129 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 5130 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5131 | |
| 5132 | /* config */ |
| 5133 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5134 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5135 | 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] | 5136 | } else { |
| 5137 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 5138 | flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path); |
| 5139 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | /* mandatory */ |
| 5143 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5144 | 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] | 5145 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5146 | |
| 5147 | /* presence */ |
| 5148 | if (rfn->presence) { |
| 5149 | if (node->nodetype != LYS_CONTAINER) { |
| 5150 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5151 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5152 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5153 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5154 | } |
| 5155 | node->flags |= LYS_PRESENCE; |
| 5156 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5157 | |
| 5158 | /* must */ |
| 5159 | if (rfn->musts) { |
| 5160 | switch (node->nodetype) { |
| 5161 | case LYS_LEAF: |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 5162 | COMPILE_ARRAY_MUST_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, u, node, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5163 | break; |
| 5164 | case LYS_LEAFLIST: |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 5165 | COMPILE_ARRAY_MUST_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, u, node, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5166 | break; |
| 5167 | case LYS_LIST: |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 5168 | COMPILE_ARRAY_MUST_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, u, node, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5169 | break; |
| 5170 | case LYS_CONTAINER: |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 5171 | COMPILE_ARRAY_MUST_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, u, node, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5172 | break; |
| 5173 | case LYS_ANYXML: |
| 5174 | case LYS_ANYDATA: |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 5175 | COMPILE_ARRAY_MUST_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, u, node, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5176 | break; |
| 5177 | default: |
| 5178 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5179 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5180 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5181 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5182 | } |
| 5183 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5184 | |
| 5185 | /* min/max-elements */ |
| 5186 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5187 | switch (node->nodetype) { |
| 5188 | case LYS_LEAFLIST: |
| 5189 | if (rfn->flags & LYS_SET_MAX) { |
| 5190 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5191 | } |
| 5192 | if (rfn->flags & LYS_SET_MIN) { |
| 5193 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5194 | if (rfn->min) { |
| 5195 | node->flags |= LYS_MAND_TRUE; |
| 5196 | lys_compile_mandatory_parents(node->parent, 1); |
| 5197 | } else { |
| 5198 | node->flags &= ~LYS_MAND_TRUE; |
| 5199 | lys_compile_mandatory_parents(node->parent, 0); |
| 5200 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5201 | } |
| 5202 | break; |
| 5203 | case LYS_LIST: |
| 5204 | if (rfn->flags & LYS_SET_MAX) { |
| 5205 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5206 | } |
| 5207 | if (rfn->flags & LYS_SET_MIN) { |
| 5208 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5209 | if (rfn->min) { |
| 5210 | node->flags |= LYS_MAND_TRUE; |
| 5211 | lys_compile_mandatory_parents(node->parent, 1); |
| 5212 | } else { |
| 5213 | node->flags &= ~LYS_MAND_TRUE; |
| 5214 | lys_compile_mandatory_parents(node->parent, 0); |
| 5215 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5216 | } |
| 5217 | break; |
| 5218 | default: |
| 5219 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5220 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5221 | (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] | 5222 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5223 | } |
| 5224 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5225 | |
| 5226 | /* if-feature */ |
| 5227 | if (rfn->iffeatures) { |
| 5228 | /* 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] | 5229 | 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] | 5230 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5231 | |
| 5232 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5233 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5234 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5235 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 5236 | for (u = 0; u < refined.count; ++u) { |
| 5237 | node = (struct lysc_node*)refined.objs[u]; |
| 5238 | rfn = &uses_p->refines[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5239 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5240 | |
| 5241 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5242 | if ((node->flags & LYS_MAND_TRUE) && |
| 5243 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5244 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5245 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5246 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5247 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5251 | if (node->nodetype == LYS_LIST) { |
| 5252 | min = ((struct lysc_node_list*)node)->min; |
| 5253 | max = ((struct lysc_node_list*)node)->max; |
| 5254 | } else { |
| 5255 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5256 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5257 | } |
| 5258 | if (min > max) { |
| 5259 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5260 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5261 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5262 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5263 | } |
| 5264 | } |
| 5265 | } |
| 5266 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5267 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5268 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5269 | |
| 5270 | cleanup: |
| 5271 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5272 | if (context_node_fake.child) { |
| 5273 | context_node_fake.child->prev = child; |
| 5274 | } |
| 5275 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5276 | child->parent = parent; |
| 5277 | } |
| 5278 | |
| 5279 | if (uses_p->augments || uses_p->refines) { |
| 5280 | /* 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] | 5281 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5282 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5283 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5284 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5285 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5286 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5287 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5288 | } |
| 5289 | } |
| 5290 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5291 | /* reload previous context's mod_def */ |
| 5292 | ctx->mod_def = mod_old; |
| 5293 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5294 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5295 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5296 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5297 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5298 | |
| 5299 | return ret; |
| 5300 | } |
| 5301 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5302 | static int |
| 5303 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5304 | { |
| 5305 | struct lysp_node *iter; |
| 5306 | int len = 0; |
| 5307 | |
| 5308 | *path = NULL; |
| 5309 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5310 | char *s = *path; |
| 5311 | char *id; |
| 5312 | |
| 5313 | switch (iter->nodetype) { |
| 5314 | case LYS_USES: |
| 5315 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5316 | break; |
| 5317 | case LYS_GROUPING: |
| 5318 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5319 | break; |
| 5320 | case LYS_AUGMENT: |
| 5321 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5322 | break; |
| 5323 | default: |
| 5324 | id = strdup(iter->name); |
| 5325 | break; |
| 5326 | } |
| 5327 | |
| 5328 | if (!iter->parent) { |
| 5329 | /* print prefix */ |
| 5330 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5331 | } else { |
| 5332 | /* prefix is the same as in parent */ |
| 5333 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5334 | } |
| 5335 | free(s); |
| 5336 | free(id); |
| 5337 | } |
| 5338 | |
| 5339 | if (len < 0) { |
| 5340 | free(*path); |
| 5341 | *path = NULL; |
| 5342 | } else if (len == 0) { |
| 5343 | *path = strdup("/"); |
| 5344 | len = 1; |
| 5345 | } |
| 5346 | return len; |
| 5347 | } |
| 5348 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5349 | /** |
| 5350 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5351 | * |
| 5352 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5353 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5354 | */ |
| 5355 | static LY_ERR |
| 5356 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5357 | { |
| 5358 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5359 | char *path; |
| 5360 | int len; |
| 5361 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5362 | struct lysp_node_uses fake_uses = { |
| 5363 | .parent = node_p, |
| 5364 | .nodetype = LYS_USES, |
| 5365 | .flags = 0, .next = NULL, |
| 5366 | .name = grp->name, |
| 5367 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5368 | .refines = NULL, .augments = NULL |
| 5369 | }; |
| 5370 | struct lysc_node_container fake_container = { |
| 5371 | .nodetype = LYS_CONTAINER, |
| 5372 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5373 | .module = ctx->mod, |
| 5374 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5375 | .prev = (struct lysc_node*)&fake_container, |
| 5376 | .name = "fake", |
| 5377 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5378 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5379 | }; |
| 5380 | |
| 5381 | if (grp->parent) { |
| 5382 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5383 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5384 | |
| 5385 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5386 | if (len < 0) { |
| 5387 | LOGMEM(ctx->ctx); |
| 5388 | return LY_EMEM; |
| 5389 | } |
| 5390 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5391 | ctx->path_len = (uint16_t)len; |
| 5392 | free(path); |
| 5393 | |
| 5394 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5395 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5396 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5397 | lysc_update_path(ctx, NULL, NULL); |
| 5398 | lysc_update_path(ctx, NULL, NULL); |
| 5399 | |
| 5400 | ctx->path_len = 1; |
| 5401 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5402 | |
| 5403 | /* cleanup */ |
| 5404 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5405 | |
| 5406 | return ret; |
| 5407 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5408 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5409 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5410 | * @brief Compile parsed schema node information. |
| 5411 | * @param[in] ctx Compile context |
| 5412 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5413 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5414 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5415 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5416 | * @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). |
| 5417 | * 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] | 5418 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5419 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5420 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5421 | 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] | 5422 | { |
| 5423 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5424 | struct lysc_node *node; |
| 5425 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5426 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5427 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5428 | 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] | 5429 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5430 | if (node_p->nodetype != LYS_USES) { |
| 5431 | lysc_update_path(ctx, parent, node_p->name); |
| 5432 | } else { |
| 5433 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5434 | lysc_update_path(ctx, NULL, node_p->name); |
| 5435 | } |
| 5436 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5437 | switch (node_p->nodetype) { |
| 5438 | case LYS_CONTAINER: |
| 5439 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5440 | node_compile_spec = lys_compile_node_container; |
| 5441 | break; |
| 5442 | case LYS_LEAF: |
| 5443 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5444 | node_compile_spec = lys_compile_node_leaf; |
| 5445 | break; |
| 5446 | case LYS_LIST: |
| 5447 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5448 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5449 | break; |
| 5450 | case LYS_LEAFLIST: |
| 5451 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5452 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5453 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5454 | case LYS_CHOICE: |
| 5455 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5456 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5457 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5458 | case LYS_ANYXML: |
| 5459 | case LYS_ANYDATA: |
| 5460 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5461 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5462 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5463 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5464 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5465 | lysc_update_path(ctx, NULL, NULL); |
| 5466 | lysc_update_path(ctx, NULL, NULL); |
| 5467 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5468 | default: |
| 5469 | LOGINT(ctx->ctx); |
| 5470 | return LY_EINT; |
| 5471 | } |
| 5472 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5473 | node->nodetype = node_p->nodetype; |
| 5474 | node->module = ctx->mod; |
| 5475 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5476 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5477 | |
| 5478 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5479 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5480 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5481 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5482 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5483 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5484 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5485 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5486 | node->flags |= LYS_CONFIG_R; |
| 5487 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5488 | /* config not explicitely set, inherit it from parent */ |
| 5489 | if (parent) { |
| 5490 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5491 | } else { |
| 5492 | /* default is config true */ |
| 5493 | node->flags |= LYS_CONFIG_W; |
| 5494 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5495 | } else { |
| 5496 | /* config set explicitely */ |
| 5497 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5498 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5499 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5500 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5501 | "Configuration node cannot be child of any state data node."); |
| 5502 | goto error; |
| 5503 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5504 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5505 | /* *list ordering */ |
| 5506 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5507 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5508 | 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] | 5509 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5510 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5511 | node->flags &= ~LYS_ORDBY_MASK; |
| 5512 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5513 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5514 | /* default ordering is system */ |
| 5515 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5516 | } |
| 5517 | } |
| 5518 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5519 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5520 | * 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] | 5521 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5522 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5523 | * 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] | 5524 | 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] | 5525 | } |
| 5526 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5527 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5528 | node->sp = node_p; |
| 5529 | } |
| 5530 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5531 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5532 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5533 | if (node_p->when) { |
| 5534 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5535 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5536 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 5537 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5538 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5539 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5540 | |
| 5541 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5542 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5543 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5544 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5545 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5546 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5547 | if (node->flags & LYS_MAND_TRUE) { |
| 5548 | lys_compile_mandatory_parents(parent, 1); |
| 5549 | } |
| 5550 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5551 | lysc_update_path(ctx, NULL, NULL); |
| 5552 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5553 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5554 | if (parent) { |
| 5555 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5556 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5557 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5558 | } else { |
| 5559 | lysc_update_path(ctx, parent, node->name); |
| 5560 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5561 | 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] | 5562 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5563 | if (uses_status) { |
| 5564 | |
| 5565 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5566 | /* 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] | 5567 | * it directly gets the same status flags as the choice; |
| 5568 | * 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] | 5569 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5570 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5571 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5572 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5573 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5574 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5575 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5576 | 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] | 5577 | |
| 5578 | if (parent->nodetype == LYS_CHOICE) { |
| 5579 | lysc_update_path(ctx, NULL, NULL); |
| 5580 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5581 | } else { |
| 5582 | /* top-level element */ |
| 5583 | if (!ctx->mod->compiled->data) { |
| 5584 | ctx->mod->compiled->data = node; |
| 5585 | } else { |
| 5586 | /* insert at the end of the module's top-level nodes list */ |
| 5587 | ctx->mod->compiled->data->prev->next = node; |
| 5588 | node->prev = ctx->mod->compiled->data->prev; |
| 5589 | ctx->mod->compiled->data->prev = node; |
| 5590 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5591 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5592 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5593 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5594 | return LY_EVALID; |
| 5595 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5596 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5597 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5598 | |
| 5599 | return LY_SUCCESS; |
| 5600 | |
| 5601 | error: |
| 5602 | lysc_node_free(ctx->ctx, node); |
| 5603 | return ret; |
| 5604 | } |
| 5605 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5606 | static void |
| 5607 | lysc_disconnect(struct lysc_node *node) |
| 5608 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5609 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5610 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5611 | int remove_cs = 0; |
| 5612 | |
| 5613 | parent = node->parent; |
| 5614 | |
| 5615 | /* parent's first child */ |
| 5616 | if (!parent) { |
| 5617 | return; |
| 5618 | } |
| 5619 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5620 | cs = (struct lysc_node_case*)node; |
| 5621 | } else if (parent->nodetype == LYS_CASE) { |
| 5622 | /* disconnecting some node in a case */ |
| 5623 | cs = (struct lysc_node_case*)parent; |
| 5624 | parent = cs->parent; |
| 5625 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5626 | if (child == node) { |
| 5627 | if (cs->child == child) { |
| 5628 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5629 | /* case with a single child -> remove also the case */ |
| 5630 | child->parent = NULL; |
| 5631 | remove_cs = 1; |
| 5632 | } else { |
| 5633 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5634 | } |
| 5635 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5636 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5637 | } |
| 5638 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5639 | if (!remove_cs) { |
| 5640 | cs = NULL; |
| 5641 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5642 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5643 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5644 | } |
| 5645 | |
| 5646 | if (cs) { |
| 5647 | if (remove_cs) { |
| 5648 | /* cs has only one child which is being also removed */ |
| 5649 | lysc_disconnect((struct lysc_node*)cs); |
| 5650 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5651 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5652 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5653 | /* default case removed */ |
| 5654 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5655 | } |
| 5656 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5657 | /* first case removed */ |
| 5658 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5659 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5660 | if (cs->child) { |
| 5661 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5662 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5663 | prev = cs->child->prev; |
| 5664 | } /* else all the children are under a single case */ |
| 5665 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5666 | if (child->parent != (struct lysc_node*)cs) { |
| 5667 | break; |
| 5668 | } |
| 5669 | lysc_node_free(node->module->ctx, child); |
| 5670 | } |
| 5671 | if (prev) { |
| 5672 | if (prev->next) { |
| 5673 | prev->next = child; |
| 5674 | } |
| 5675 | if (child) { |
| 5676 | child->prev = prev; |
| 5677 | } else { |
| 5678 | /* link from the first child under the cases */ |
| 5679 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5680 | } |
| 5681 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5682 | } |
| 5683 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5684 | } |
| 5685 | |
| 5686 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5687 | if (node->prev->next) { |
| 5688 | node->prev->next = node->next; |
| 5689 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5690 | if (node->next) { |
| 5691 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5692 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5693 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5694 | if (child) { |
| 5695 | child->prev = node->prev; |
| 5696 | } |
| 5697 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5698 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5699 | } |
| 5700 | } |
| 5701 | |
| 5702 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5703 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5704 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5705 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5706 | struct ly_set devs_p = {0}; |
| 5707 | struct ly_set targets = {0}; |
| 5708 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5709 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5710 | struct lysc_action *rpcs; |
| 5711 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5712 | struct lysp_deviation *dev; |
| 5713 | struct lysp_deviate *d, **dp_new; |
| 5714 | struct lysp_deviate_add *d_add; |
| 5715 | struct lysp_deviate_del *d_del; |
| 5716 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5717 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5718 | struct lysc_deviation { |
| 5719 | const char *nodeid; |
| 5720 | struct lysc_node *target; /* target node of the deviation */ |
| 5721 | 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] | 5722 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5723 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5724 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5725 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5726 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5727 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5728 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5729 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5730 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5731 | |
| 5732 | /* get all deviations from the module and all its submodules ... */ |
| 5733 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5734 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5735 | } |
| 5736 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5737 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5738 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5739 | } |
| 5740 | } |
| 5741 | if (!devs_p.count) { |
| 5742 | /* nothing to do */ |
| 5743 | return LY_SUCCESS; |
| 5744 | } |
| 5745 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5746 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5747 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5748 | /* ... and group them by the target node */ |
| 5749 | devs = calloc(devs_p.count, sizeof *devs); |
| 5750 | for (u = 0; u < devs_p.count; ++u) { |
| 5751 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5752 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5753 | |
| 5754 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5755 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5756 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5757 | if (target->nodetype == LYS_ACTION) { |
| 5758 | /* move the target pointer to input/output to make them different from the action and |
| 5759 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5760 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5761 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5762 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5763 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5764 | flags |= LYSC_OPT_INTERNAL; |
| 5765 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5766 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5767 | flags |= LYSC_OPT_INTERNAL; |
| 5768 | } |
| 5769 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5770 | /* insert into the set of targets with duplicity detection */ |
| 5771 | i = ly_set_add(&targets, target, 0); |
| 5772 | if (!devs[i]) { |
| 5773 | /* new record */ |
| 5774 | devs[i] = calloc(1, sizeof **devs); |
| 5775 | devs[i]->target = target; |
| 5776 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5777 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5778 | } |
| 5779 | /* add deviates into the deviation's list of deviates */ |
| 5780 | for (d = dev->deviates; d; d = d->next) { |
| 5781 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5782 | *dp_new = d; |
| 5783 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5784 | devs[i]->not_supported = 1; |
| 5785 | } |
| 5786 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5787 | |
| 5788 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5789 | } |
| 5790 | |
| 5791 | /* MACROS for deviates checking */ |
| 5792 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5793 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5794 | 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] | 5795 | goto cleanup; \ |
| 5796 | } |
| 5797 | |
| 5798 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5799 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5800 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \ |
| 5801 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5802 | goto cleanup; \ |
| 5803 | } |
| 5804 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5805 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5806 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5807 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5808 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5809 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5810 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5811 | goto cleanup; \ |
| 5812 | } |
| 5813 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5814 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5815 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5816 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5817 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5818 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5819 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5820 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5821 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5822 | goto cleanup; \ |
| 5823 | } |
| 5824 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5825 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5826 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5827 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5828 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5829 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5830 | goto cleanup; \ |
| 5831 | } |
| 5832 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5833 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5834 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5835 | 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] | 5836 | goto cleanup; \ |
| 5837 | } |
| 5838 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5839 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5840 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5841 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5842 | "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] | 5843 | goto cleanup; \ |
| 5844 | } |
| 5845 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5846 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5847 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5848 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5849 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5850 | 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] | 5851 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5852 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5853 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5854 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5855 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5856 | goto cleanup; \ |
| 5857 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5858 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5859 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5860 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5861 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5862 | (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] | 5863 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5864 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5865 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5866 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | /* apply deviations */ |
| 5870 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5871 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5872 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5873 | struct ly_err_item *err = NULL; |
| 5874 | |
| 5875 | dflt = NULL; |
| 5876 | changed_type = 0; |
| 5877 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5878 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5879 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5880 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5881 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5882 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5883 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5884 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5885 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5886 | } |
| 5887 | } |
| 5888 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5889 | /* not-supported */ |
| 5890 | if (devs[u]->not_supported) { |
| 5891 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 5892 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 5893 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5894 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5895 | |
| 5896 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5897 | if (devs[u]->target->parent) { \ |
| 5898 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5899 | } else { \ |
| 5900 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5901 | } \ |
| 5902 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5903 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5904 | } \ |
| 5905 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5906 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5907 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5908 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5909 | } |
| 5910 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5911 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 5912 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5913 | /* remove RPC's/action's input */ |
| 5914 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5915 | 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] | 5916 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5917 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5918 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5919 | /* remove RPC's/action's output */ |
| 5920 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5921 | 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] | 5922 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5923 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5924 | } else { |
| 5925 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5926 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5927 | } |
| 5928 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5929 | /* remove Notification */ |
| 5930 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5931 | } else { |
| 5932 | /* remove the target node */ |
| 5933 | lysc_disconnect(devs[u]->target); |
| 5934 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5935 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5936 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5937 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5938 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5939 | continue; |
| 5940 | } |
| 5941 | |
| 5942 | /* list of deviates (not-supported is not present in the list) */ |
| 5943 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5944 | d = devs[u]->deviates[v]; |
| 5945 | |
| 5946 | switch (d->mod) { |
| 5947 | case LYS_DEV_ADD: |
| 5948 | d_add = (struct lysp_deviate_add*)d; |
| 5949 | /* [units-stmt] */ |
| 5950 | if (d_add->units) { |
| 5951 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5952 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5953 | |
| 5954 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5955 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5956 | } |
| 5957 | |
| 5958 | /* *must-stmt */ |
| 5959 | if (d_add->musts) { |
| 5960 | switch (devs[u]->target->nodetype) { |
| 5961 | case LYS_CONTAINER: |
| 5962 | case LYS_LIST: |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 5963 | COMPILE_ARRAY_MUST_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 5964 | x, devs[u]->target, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5965 | break; |
| 5966 | case LYS_LEAF: |
| 5967 | case LYS_LEAFLIST: |
| 5968 | case LYS_ANYDATA: |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 5969 | COMPILE_ARRAY_MUST_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 5970 | x, devs[u]->target, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5971 | break; |
| 5972 | case LYS_NOTIF: |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 5973 | COMPILE_ARRAY_MUST_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 5974 | x, devs[u]->target, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5975 | break; |
| 5976 | case LYS_ACTION: |
| 5977 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 5978 | COMPILE_ARRAY_MUST_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 5979 | x, devs[u]->target, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5980 | break; |
| 5981 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame^] | 5982 | COMPILE_ARRAY_MUST_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 5983 | x, devs[u]->target, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5984 | break; |
| 5985 | } |
| 5986 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5987 | default: |
| 5988 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5989 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5990 | goto cleanup; |
| 5991 | } |
| 5992 | } |
| 5993 | |
| 5994 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5995 | if (d_add->uniques) { |
| 5996 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5997 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5998 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5999 | |
| 6000 | /* *default-stmt */ |
| 6001 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6002 | switch (devs[u]->target->nodetype) { |
| 6003 | case LYS_LEAF: |
| 6004 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6005 | 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] | 6006 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6007 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6008 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6009 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6010 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6011 | } else { |
| 6012 | /* prepare new default value storage */ |
| 6013 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6014 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6015 | dflt = d_add->dflts[0]; |
| 6016 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 6017 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6018 | /* mark the new default values as leaf's own */ |
| 6019 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6020 | break; |
| 6021 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6022 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6023 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6024 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6025 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6026 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6027 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6028 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6029 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6030 | LY_ARRAY_FREE(llist->dflts); |
| 6031 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6032 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6033 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6034 | } |
| 6035 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6036 | 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] | 6037 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 6038 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6039 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6040 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 6041 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6042 | LY_ARRAY_INCREMENT(llist->dflts); |
| 6043 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 6044 | llist->dflts[x]->realtype = llist->type; |
| 6045 | 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] | 6046 | 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] | 6047 | (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] | 6048 | llist->dflts[x]->realtype->refcount++; |
| 6049 | if (err) { |
| 6050 | ly_err_print(err); |
| 6051 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6052 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 6053 | d_add->dflts[x - y], err->msg); |
| 6054 | ly_err_free(err); |
| 6055 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6056 | if (rc == LY_EINCOMPLETE) { |
| 6057 | /* postpone default compilation when the tree is complete */ |
| 6058 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6059 | |
| 6060 | /* but in general result is so far ok */ |
| 6061 | rc = LY_SUCCESS; |
| 6062 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6063 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6064 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6065 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6066 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 6067 | break; |
| 6068 | case LYS_CHOICE: |
| 6069 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 6070 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 6071 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 6072 | * 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] | 6073 | 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] | 6074 | goto cleanup; |
| 6075 | } |
| 6076 | break; |
| 6077 | default: |
| 6078 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6079 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6080 | goto cleanup; |
| 6081 | } |
| 6082 | } |
| 6083 | |
| 6084 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6085 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 6086 | 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] | 6087 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6088 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 6089 | goto cleanup; |
| 6090 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6091 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6092 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 6093 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6094 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6095 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 6096 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6097 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 6098 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6099 | goto cleanup; |
| 6100 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6101 | 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] | 6102 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6103 | |
| 6104 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6105 | if (d_add->flags & LYS_MAND_MASK) { |
| 6106 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 6107 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6108 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 6109 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6110 | goto cleanup; |
| 6111 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6112 | 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] | 6113 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6114 | |
| 6115 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6116 | if (d_add->flags & LYS_SET_MIN) { |
| 6117 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6118 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6119 | /* change value */ |
| 6120 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 6121 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6122 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6123 | /* change value */ |
| 6124 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 6125 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6127 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 6128 | goto cleanup; |
| 6129 | } |
| 6130 | if (d_add->min) { |
| 6131 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6132 | } |
| 6133 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6134 | |
| 6135 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6136 | if (d_add->flags & LYS_SET_MAX) { |
| 6137 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6138 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6139 | /* change value */ |
| 6140 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6141 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6142 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6143 | /* change value */ |
| 6144 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6145 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6146 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6147 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6148 | goto cleanup; |
| 6149 | } |
| 6150 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6151 | |
| 6152 | break; |
| 6153 | case LYS_DEV_DELETE: |
| 6154 | d_del = (struct lysp_deviate_del*)d; |
| 6155 | |
| 6156 | /* [units-stmt] */ |
| 6157 | if (d_del->units) { |
| 6158 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6159 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6160 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6161 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6162 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6163 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6164 | goto cleanup; |
| 6165 | } |
| 6166 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6167 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6168 | } |
| 6169 | |
| 6170 | /* *must-stmt */ |
| 6171 | if (d_del->musts) { |
| 6172 | switch (devs[u]->target->nodetype) { |
| 6173 | case LYS_CONTAINER: |
| 6174 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6175 | 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] | 6176 | break; |
| 6177 | case LYS_LEAF: |
| 6178 | case LYS_LEAFLIST: |
| 6179 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6180 | 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] | 6181 | break; |
| 6182 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6183 | 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] | 6184 | break; |
| 6185 | case LYS_ACTION: |
| 6186 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6187 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6188 | break; |
| 6189 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6190 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6191 | break; |
| 6192 | } |
| 6193 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6194 | default: |
| 6195 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6196 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6197 | goto cleanup; |
| 6198 | } |
| 6199 | } |
| 6200 | |
| 6201 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6202 | if (d_del->uniques) { |
| 6203 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6204 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6205 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6206 | LY_ARRAY_FOR(list->uniques, z) { |
| 6207 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6208 | nodeid = strpbrk(name, " \t\n"); |
| 6209 | if (nodeid) { |
| 6210 | if (strncmp(name, list->uniques[z][y]->name, nodeid - name) |
| 6211 | || list->uniques[z][y]->name[nodeid - name] != '\0') { |
| 6212 | break; |
| 6213 | } |
| 6214 | while (isspace(*nodeid)) { |
| 6215 | ++nodeid; |
| 6216 | } |
| 6217 | } else { |
| 6218 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6219 | break; |
| 6220 | } |
| 6221 | } |
| 6222 | } |
| 6223 | if (!name) { |
| 6224 | /* complete match - remove the unique */ |
| 6225 | LY_ARRAY_DECREMENT(list->uniques); |
| 6226 | LY_ARRAY_FREE(list->uniques[z]); |
| 6227 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6228 | --z; |
| 6229 | break; |
| 6230 | } |
| 6231 | } |
| 6232 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6233 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6234 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6235 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6236 | goto cleanup; |
| 6237 | } |
| 6238 | } |
| 6239 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6240 | LY_ARRAY_FREE(list->uniques); |
| 6241 | list->uniques = NULL; |
| 6242 | } |
| 6243 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6244 | |
| 6245 | /* *default-stmt */ |
| 6246 | if (d_del->dflts) { |
| 6247 | switch (devs[u]->target->nodetype) { |
| 6248 | case LYS_LEAF: |
| 6249 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6250 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6251 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6252 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6253 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6254 | 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] | 6255 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6256 | if (i) { |
| 6257 | free((char*)dflt); |
| 6258 | } |
| 6259 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6260 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6261 | d_del->dflts[0], dflt); |
| 6262 | goto cleanup; |
| 6263 | } |
| 6264 | if (i) { |
| 6265 | free((char*)dflt); |
| 6266 | } |
| 6267 | dflt = NULL; |
| 6268 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6269 | /* update the list of incomplete default values if needed */ |
| 6270 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6271 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6272 | /* remove the default specification */ |
| 6273 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6274 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6275 | free(leaf->dflt); |
| 6276 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6277 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6278 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6279 | break; |
| 6280 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6281 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6282 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6283 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6284 | 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] | 6285 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6286 | if (i) { |
| 6287 | free((char*)dflt); |
| 6288 | } |
| 6289 | dflt = NULL; |
| 6290 | break; |
| 6291 | } |
| 6292 | if (i) { |
| 6293 | free((char*)dflt); |
| 6294 | } |
| 6295 | dflt = NULL; |
| 6296 | } |
| 6297 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6298 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6299 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6300 | goto cleanup; |
| 6301 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6302 | |
| 6303 | /* update the list of incomplete default values if needed */ |
| 6304 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6305 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6306 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6307 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6308 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6309 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6310 | free(llist->dflts[y]); |
| 6311 | 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] | 6312 | 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] | 6313 | } |
| 6314 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6315 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6316 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6317 | LY_ARRAY_FREE(llist->dflts); |
| 6318 | llist->dflts = NULL; |
| 6319 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6320 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6321 | break; |
| 6322 | case LYS_CHOICE: |
| 6323 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6324 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6325 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6326 | 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] | 6327 | if (prefix) { |
| 6328 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6329 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6330 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6331 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6332 | "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] | 6333 | goto cleanup; |
| 6334 | } |
| 6335 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6336 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6337 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6338 | "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] | 6339 | goto cleanup; |
| 6340 | } |
| 6341 | } |
| 6342 | /* else { |
| 6343 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6344 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6345 | * the module of the default case, just matching its name */ |
| 6346 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6347 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6348 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6349 | 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] | 6350 | goto cleanup; |
| 6351 | } |
| 6352 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6353 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6354 | break; |
| 6355 | default: |
| 6356 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6357 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6358 | goto cleanup; |
| 6359 | } |
| 6360 | } |
| 6361 | |
| 6362 | break; |
| 6363 | case LYS_DEV_REPLACE: |
| 6364 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6365 | |
| 6366 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6367 | if (d_rpl->type) { |
| 6368 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6369 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6370 | 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] | 6371 | |
| 6372 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6373 | /* the target has default from the previous type - remove it */ |
| 6374 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6375 | /* update the list of incomplete default values if needed */ |
| 6376 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6377 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6378 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6379 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6380 | free(leaf->dflt); |
| 6381 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6382 | leaf->dflt_mod = NULL; |
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 | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6385 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
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 | free(llist->dflts[x]); |
| 6389 | } |
| 6390 | LY_ARRAY_FREE(llist->dflts); |
| 6391 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6392 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6393 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6394 | } |
| 6395 | } |
| 6396 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6397 | /* there is no default value, do not set changed_type after type compilation |
| 6398 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6399 | changed_type = -1; |
| 6400 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6401 | 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] | 6402 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6403 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6404 | |
| 6405 | /* [units-stmt] */ |
| 6406 | if (d_rpl->units) { |
| 6407 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6408 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6409 | units, "replacing", "units", d_rpl->units); |
| 6410 | |
| 6411 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6412 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6413 | } |
| 6414 | |
| 6415 | /* [default-stmt] */ |
| 6416 | if (d_rpl->dflt) { |
| 6417 | switch (devs[u]->target->nodetype) { |
| 6418 | case LYS_LEAF: |
| 6419 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6420 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6421 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6422 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6423 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6424 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6425 | dflt = d_rpl->dflt; |
| 6426 | /* 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] | 6427 | break; |
| 6428 | case LYS_CHOICE: |
| 6429 | 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] | 6430 | 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] | 6431 | goto cleanup; |
| 6432 | } |
| 6433 | break; |
| 6434 | default: |
| 6435 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6436 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6437 | goto cleanup; |
| 6438 | } |
| 6439 | } |
| 6440 | |
| 6441 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6442 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 6443 | 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] | 6444 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6445 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6446 | goto cleanup; |
| 6447 | } |
| 6448 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6449 | 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] | 6450 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6451 | goto cleanup; |
| 6452 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6453 | 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] | 6454 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6455 | |
| 6456 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6457 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6458 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6459 | 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] | 6460 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6461 | goto cleanup; |
| 6462 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6463 | 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] | 6464 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6465 | |
| 6466 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6467 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6468 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6469 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6470 | /* change value */ |
| 6471 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6472 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6473 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6474 | /* change value */ |
| 6475 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6476 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6477 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6478 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6479 | goto cleanup; |
| 6480 | } |
| 6481 | if (d_rpl->min) { |
| 6482 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6483 | } |
| 6484 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6485 | |
| 6486 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6487 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6488 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6489 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6490 | /* change value */ |
| 6491 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6492 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6493 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6494 | /* change value */ |
| 6495 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6496 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6497 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6498 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6499 | goto cleanup; |
| 6500 | } |
| 6501 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6502 | |
| 6503 | break; |
| 6504 | default: |
| 6505 | LOGINT(ctx->ctx); |
| 6506 | goto cleanup; |
| 6507 | } |
| 6508 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6509 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6510 | /* final check when all deviations of a single target node are applied */ |
| 6511 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6512 | /* check min-max compatibility */ |
| 6513 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6514 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6515 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6516 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6517 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6518 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6519 | } else { |
| 6520 | min = max = 0; |
| 6521 | } |
| 6522 | if (min > max) { |
| 6523 | 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] | 6524 | "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] | 6525 | goto cleanup; |
| 6526 | } |
| 6527 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6528 | if (dflt) { |
| 6529 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6530 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6531 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6532 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6533 | 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] | 6534 | 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] | 6535 | leaf->dflt->realtype->refcount++; |
| 6536 | if (err) { |
| 6537 | ly_err_print(err); |
| 6538 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6539 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6540 | ly_err_free(err); |
| 6541 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6542 | if (rc == LY_EINCOMPLETE) { |
| 6543 | /* postpone default compilation when the tree is complete */ |
| 6544 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6545 | |
| 6546 | /* but in general result is so far ok */ |
| 6547 | rc = LY_SUCCESS; |
| 6548 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6549 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6550 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6551 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6552 | int dynamic; |
| 6553 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6554 | 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] | 6555 | |
| 6556 | /* update the list of incomplete default values if needed */ |
| 6557 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6558 | |
| 6559 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6560 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6561 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6562 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6563 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6564 | 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] | 6565 | 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] | 6566 | leaf->dflt->realtype->refcount++; |
| 6567 | if (err) { |
| 6568 | ly_err_print(err); |
| 6569 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6570 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6571 | ly_err_free(err); |
| 6572 | } |
| 6573 | if (dynamic) { |
| 6574 | free((void*)dflt); |
| 6575 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6576 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6577 | if (rc == LY_EINCOMPLETE) { |
| 6578 | /* postpone default compilation when the tree is complete */ |
| 6579 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6580 | |
| 6581 | /* but in general result is so far ok */ |
| 6582 | rc = LY_SUCCESS; |
| 6583 | } |
| 6584 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6585 | } else { /* LYS_LEAFLIST */ |
| 6586 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6587 | 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] | 6588 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6589 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6590 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6591 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6592 | 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] | 6593 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6594 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6595 | llist->dflts[x]->realtype->refcount++; |
| 6596 | if (err) { |
| 6597 | ly_err_print(err); |
| 6598 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6599 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6600 | dflt, err->msg); |
| 6601 | ly_err_free(err); |
| 6602 | } |
| 6603 | if (dynamic) { |
| 6604 | free((void*)dflt); |
| 6605 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6606 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6607 | if (rc == LY_EINCOMPLETE) { |
| 6608 | /* postpone default compilation when the tree is complete */ |
| 6609 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6610 | |
| 6611 | /* but in general result is so far ok */ |
| 6612 | rc = LY_SUCCESS; |
| 6613 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6614 | LY_CHECK_GOTO(rc, cleanup); |
| 6615 | } |
| 6616 | } |
| 6617 | } |
| 6618 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6619 | /* check mandatory - default compatibility */ |
| 6620 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6621 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6622 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6623 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6624 | "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] | 6625 | goto cleanup; |
| 6626 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6627 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6628 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6629 | 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] | 6630 | goto cleanup; |
| 6631 | } |
| 6632 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6633 | /* mandatory node under a default case */ |
| 6634 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6635 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6636 | 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] | 6637 | goto cleanup; |
| 6638 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6639 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6640 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6641 | } |
| 6642 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6643 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6644 | ret = LY_SUCCESS; |
| 6645 | |
| 6646 | cleanup: |
| 6647 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6648 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6649 | free(devs[u]); |
| 6650 | } |
| 6651 | free(devs); |
| 6652 | ly_set_erase(&targets, NULL); |
| 6653 | ly_set_erase(&devs_p, NULL); |
| 6654 | |
| 6655 | return ret; |
| 6656 | } |
| 6657 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6658 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6659 | * @brief Compile the given YANG submodule into the main module. |
| 6660 | * @param[in] ctx Compile context |
| 6661 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6662 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6663 | */ |
| 6664 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6665 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6666 | { |
| 6667 | unsigned int u; |
| 6668 | LY_ERR ret = LY_SUCCESS; |
| 6669 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6670 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6671 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6672 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6673 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6674 | if (!mainmod->mod->off_features) { |
| 6675 | /* features are compiled directly into the compiled module structure, |
| 6676 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6677 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6678 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6679 | LY_CHECK_GOTO(ret, error); |
| 6680 | } |
| 6681 | if (!mainmod->mod->off_extensions) { |
| 6682 | /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */ |
| 6683 | ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6684 | LY_CHECK_GOTO(ret, error); |
| 6685 | } |
| 6686 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6687 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6688 | 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] | 6689 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6690 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6691 | /* data nodes */ |
| 6692 | LY_LIST_FOR(submod->data, node_p) { |
| 6693 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6694 | LY_CHECK_GOTO(ret, error); |
| 6695 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6696 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6697 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6698 | 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] | 6699 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6700 | error: |
| 6701 | return ret; |
| 6702 | } |
| 6703 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6704 | static void * |
| 6705 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6706 | { |
| 6707 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6708 | if (substmts[u].stmt == stmt) { |
| 6709 | return substmts[u].storage; |
| 6710 | } |
| 6711 | } |
| 6712 | return NULL; |
| 6713 | } |
| 6714 | |
| 6715 | LY_ERR |
| 6716 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6717 | { |
| 6718 | LY_ERR ret = LY_EVALID, r; |
| 6719 | unsigned int u; |
| 6720 | struct lysp_stmt *stmt; |
| 6721 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6722 | |
| 6723 | /* check for invalid substatements */ |
| 6724 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6725 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6726 | continue; |
| 6727 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6728 | for (u = 0; substmts[u].stmt; ++u) { |
| 6729 | if (substmts[u].stmt == stmt->kw) { |
| 6730 | break; |
| 6731 | } |
| 6732 | } |
| 6733 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6735 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6736 | goto cleanup; |
| 6737 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6738 | } |
| 6739 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6740 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6741 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6742 | /* keep order of the processing the same as the order in the defined substmts, |
| 6743 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6744 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6745 | int stmt_present = 0; |
| 6746 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6747 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6748 | if (substmts[u].stmt != stmt->kw) { |
| 6749 | continue; |
| 6750 | } |
| 6751 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6752 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6753 | if (substmts[u].storage) { |
| 6754 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6755 | case LY_STMT_STATUS: |
| 6756 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6757 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6758 | break; |
| 6759 | case LY_STMT_UNITS: { |
| 6760 | const char **units; |
| 6761 | |
| 6762 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6763 | /* single item */ |
| 6764 | if (*((const char **)substmts[u].storage)) { |
| 6765 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6766 | goto cleanup; |
| 6767 | } |
| 6768 | units = (const char **)substmts[u].storage; |
| 6769 | } else { |
| 6770 | /* sized array */ |
| 6771 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6772 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6773 | } |
| 6774 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6775 | break; |
| 6776 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6777 | case LY_STMT_TYPE: { |
| 6778 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6779 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6780 | |
| 6781 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6782 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6783 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6784 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6785 | goto cleanup; |
| 6786 | } |
| 6787 | compiled = substmts[u].storage; |
| 6788 | } else { |
| 6789 | /* sized array */ |
| 6790 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6791 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6792 | compiled = (void*)type; |
| 6793 | } |
| 6794 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6795 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6796 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6797 | flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled, |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 6798 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6799 | lysp_type_free(ctx->ctx, parsed); |
| 6800 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6801 | break; |
| 6802 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6803 | case LY_STMT_IF_FEATURE: { |
| 6804 | struct lysc_iffeature *iff = NULL; |
| 6805 | |
| 6806 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6807 | /* single item */ |
| 6808 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6809 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6810 | goto cleanup; |
| 6811 | } |
| 6812 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6813 | } else { |
| 6814 | /* sized array */ |
| 6815 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6816 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6817 | } |
| 6818 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6819 | break; |
| 6820 | } |
| 6821 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6822 | * also note that in many statements their extensions are not taken into account */ |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6823 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6824 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.", |
| 6825 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6826 | goto cleanup; |
| 6827 | } |
| 6828 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6829 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6830 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6831 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6832 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6833 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6834 | goto cleanup; |
| 6835 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6836 | } |
| 6837 | |
| 6838 | ret = LY_SUCCESS; |
| 6839 | |
| 6840 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6841 | return ret; |
| 6842 | } |
| 6843 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6844 | LY_ERR |
| 6845 | lys_compile(struct lys_module *mod, int options) |
| 6846 | { |
| 6847 | struct lysc_ctx ctx = {0}; |
| 6848 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6849 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6850 | struct lysp_module *sp; |
| 6851 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6852 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6853 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6854 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6855 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6856 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6857 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6858 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 6859 | |
| 6860 | if (!mod->implemented) { |
| 6861 | /* just imported modules are not compiled */ |
| 6862 | return LY_SUCCESS; |
| 6863 | } |
| 6864 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6865 | sp = mod->parsed; |
| 6866 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6867 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6868 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6869 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6870 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6871 | ctx.path_len = 1; |
| 6872 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6873 | |
| 6874 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6875 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 6876 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6877 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6878 | 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] | 6879 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6880 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6881 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6882 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6883 | |
| 6884 | /* features */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6885 | if (mod->off_features) { |
| 6886 | /* there is already precompiled array of features */ |
| 6887 | mod_c->features = mod->off_features; |
| 6888 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6889 | } else { |
| 6890 | /* features are compiled directly into the compiled module structure, |
| 6891 | * 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] | 6892 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6893 | LY_CHECK_GOTO(ret, error); |
| 6894 | } |
| 6895 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 6896 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 6897 | * are present. */ |
| 6898 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6899 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6900 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6901 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6902 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6903 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6904 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6905 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6906 | 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] | 6907 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6908 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6909 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6910 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6911 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6912 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6913 | /* extensions */ |
| 6914 | /* 2-steps: a) prepare compiled structures and ... */ |
| 6915 | if (mod->off_extensions) { |
| 6916 | /* there is already precompiled array of extension definitions */ |
| 6917 | mod_c->extensions = mod->off_extensions; |
| 6918 | mod->off_extensions = NULL; |
| 6919 | } else { |
| 6920 | /* extension definitions are compiled directly into the compiled module structure */ |
| 6921 | ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions); |
Radek Krejci | bfb2e14 | 2019-09-09 14:47:44 +0200 | [diff] [blame] | 6922 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6923 | } |
| 6924 | /* ... b) connect the extension definitions with the appropriate extension plugins */ |
| 6925 | lys_compile_extension_plugins(mod_c->extensions); |
| 6926 | |
| 6927 | /* identities */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6928 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6929 | 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] | 6930 | if (sp->identities) { |
| 6931 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 6932 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6933 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6934 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6935 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6936 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 6937 | LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6938 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6939 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6940 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6941 | 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] | 6942 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6943 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 6944 | LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6945 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 6946 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6947 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6948 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6949 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 6950 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6951 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6952 | /* extension instances TODO cover extension instances from submodules */ |
| 6953 | COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6954 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6955 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6956 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 6957 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 6958 | * 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] | 6959 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6960 | 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] | 6961 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6962 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6963 | /* validate the path */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 6964 | LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type), error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6965 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6966 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6967 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6968 | /* validate the path */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 6969 | LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 6970 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]), |
| 6971 | error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6972 | } |
| 6973 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6974 | } |
| 6975 | } |
| 6976 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6977 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6978 | 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] | 6979 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6980 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6981 | /* store pointer to the real type */ |
| 6982 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 6983 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6984 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6985 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6986 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6987 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6988 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6989 | /* store pointer to the real type */ |
| 6990 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 6991 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6992 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6993 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 6994 | } |
| 6995 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6996 | } |
| 6997 | } |
| 6998 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6999 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7000 | /* finish incomplete default values compilation */ |
| 7001 | for (u = 0; u < ctx.dflts.count; ++u) { |
| 7002 | struct ly_err_item *err = NULL; |
| 7003 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[u]; |
| 7004 | ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized), |
| 7005 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7006 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7007 | if (err) { |
| 7008 | ly_err_print(err); |
| 7009 | ctx.path[0] = '\0'; |
| 7010 | lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE); |
| 7011 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 7012 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7013 | ly_err_free(err); |
| 7014 | } |
| 7015 | LY_CHECK_GOTO(ret, error); |
| 7016 | } |
| 7017 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7018 | /* validate non-instantiated groupings from the parsed schema, |
| 7019 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7020 | ctx.options |= LYSC_OPT_GROUPING; |
| 7021 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7022 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7023 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7024 | } |
| 7025 | } |
| 7026 | LY_LIST_FOR(sp->data, node_p) { |
| 7027 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7028 | LY_ARRAY_FOR(grps, u) { |
| 7029 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7030 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7031 | } |
| 7032 | } |
| 7033 | } |
| 7034 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7035 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7036 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7037 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7038 | } |
| 7039 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7040 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7041 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7042 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7043 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7044 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7045 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7046 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7047 | lysp_module_free(mod->parsed); |
| 7048 | ((struct lys_module*)mod)->parsed = NULL; |
| 7049 | } |
| 7050 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7051 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7052 | /* remove flag of the modules implemented by dependency */ |
| 7053 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 7054 | m = ctx.ctx->list.objs[u]; |
| 7055 | if (m->implemented == 2) { |
| 7056 | m->implemented = 1; |
| 7057 | } |
| 7058 | } |
| 7059 | } |
| 7060 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7061 | ((struct lys_module*)mod)->compiled = mod_c; |
| 7062 | return LY_SUCCESS; |
| 7063 | |
| 7064 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7065 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7066 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7067 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7068 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7069 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7070 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7071 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7072 | mod->compiled = NULL; |
| 7073 | |
| 7074 | /* revert compilation of modules implemented by dependency */ |
| 7075 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 7076 | m = ctx.ctx->list.objs[u]; |
| 7077 | if (m->implemented == 2) { |
| 7078 | /* revert features list to the precompiled state */ |
| 7079 | lys_feature_precompile_revert(&ctx, m); |
| 7080 | /* mark module as imported-only / not-implemented */ |
| 7081 | m->implemented = 0; |
| 7082 | /* free the compiled version of the module */ |
| 7083 | lysc_module_free(m->compiled, NULL); |
| 7084 | m->compiled = NULL; |
| 7085 | } |
| 7086 | } |
| 7087 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7088 | return ret; |
| 7089 | } |