Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #include "common.h" |
| 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 24 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 25 | #include "dict.h" |
| 26 | #include "log.h" |
| 27 | #include "set.h" |
| 28 | #include "plugins_types.h" |
| 29 | #include "tree.h" |
| 30 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 31 | #include "tree_schema_internal.h" |
| 32 | #include "xpath.h" |
| 33 | |
| 34 | /** |
| 35 | * @brief Duplicate string into dictionary |
| 36 | * @param[in] CTX libyang context of the dictionary. |
| 37 | * @param[in] ORIG String to duplicate. |
| 38 | * @param[out] DUP Where to store the result. |
| 39 | */ |
| 40 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 41 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 42 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 43 | if (ARRAY_P) { \ |
| 44 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 45 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 46 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 47 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 48 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 49 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 50 | } \ |
| 51 | } |
| 52 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 53 | #define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 54 | if (ARRAY_P) { \ |
| 55 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 56 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 57 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 58 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 59 | RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 60 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 61 | } \ |
| 62 | } |
| 63 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 64 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 65 | if (ARRAY_P) { \ |
| 66 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 67 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 68 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 69 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 70 | RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 71 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 72 | } \ |
| 73 | } |
| 74 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 75 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 76 | if (MEMBER_P) { \ |
| 77 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 78 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 79 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 80 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 81 | } |
| 82 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 83 | #define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 84 | if (MEMBER_P) { \ |
| 85 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
| 86 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 87 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 88 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 89 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 90 | } |
| 91 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 92 | #define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 93 | if (ARRAY) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 94 | for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
| 95 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 96 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 97 | return LY_EVALID; \ |
| 98 | } \ |
| 99 | } \ |
| 100 | } |
| 101 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 102 | static struct lysc_ext_instance * |
| 103 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 104 | { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 105 | /* TODO - extensions */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 106 | (void) ctx; |
| 107 | (void) orig; |
| 108 | return NULL; |
| 109 | } |
| 110 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 111 | /** |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 112 | * @brief Update path in the compile context. |
| 113 | * |
| 114 | * @param[in] ctx Compile context with the path. |
| 115 | * @param[in] parent Parent of the current node to check difference of the node's module. The current module is taken from lysc_ctx::mod. |
| 116 | * @param[in] name Name of the node to update path with. If NULL, the last segment is removed. If the format is `{keyword}`, the following |
| 117 | * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used). |
| 118 | */ |
| 119 | static void |
| 120 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 121 | { |
| 122 | int len; |
| 123 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 124 | |
| 125 | if (!name) { |
| 126 | /* removing last path segment */ |
| 127 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 128 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 129 | if (ctx->path[ctx->path_len] == '=') { |
| 130 | ctx->path[ctx->path_len++] = '}'; |
| 131 | } else { |
| 132 | /* not a top-level special tag, remove also preceiding '/' */ |
| 133 | goto remove_nodelevel; |
| 134 | } |
| 135 | } else { |
| 136 | remove_nodelevel: |
| 137 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 138 | if (ctx->path_len == 0) { |
| 139 | /* top-level (last segment) */ |
| 140 | ++ctx->path_len; |
| 141 | } |
| 142 | } |
| 143 | /* set new terminating NULL-byte */ |
| 144 | ctx->path[ctx->path_len] = '\0'; |
| 145 | } else { |
| 146 | if (ctx->path_len > 1) { |
| 147 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 148 | /* extension of the special tag */ |
| 149 | nextlevel = 2; |
| 150 | --ctx->path_len; |
| 151 | } else { |
| 152 | /* there is already some path, so add next level */ |
| 153 | nextlevel = 1; |
| 154 | } |
| 155 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 156 | |
| 157 | if (nextlevel != 2) { |
| 158 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 159 | /* module not changed, print the name unprefixed */ |
| 160 | len = sprintf(&ctx->path[ctx->path_len], "%s%s", nextlevel ? "/" : "", name); |
| 161 | } else { |
| 162 | len = sprintf(&ctx->path[ctx->path_len], "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name); |
| 163 | } |
| 164 | } else { |
| 165 | len = sprintf(&ctx->path[ctx->path_len], "='%s'}", name); |
| 166 | } |
| 167 | ctx->path_len += len; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 172 | * @brief Duplicate the compiled pattern structure. |
| 173 | * |
| 174 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 175 | * |
| 176 | * @param[in] orig The pattern structure to duplicate. |
| 177 | * @return The duplicated structure to use. |
| 178 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 179 | static struct lysc_pattern* |
| 180 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 181 | { |
| 182 | ++orig->refcount; |
| 183 | return orig; |
| 184 | } |
| 185 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 186 | /** |
| 187 | * @brief Duplicate the array of compiled patterns. |
| 188 | * |
| 189 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 190 | * |
| 191 | * @param[in] ctx Libyang context for logging. |
| 192 | * @param[in] orig The patterns sized array to duplicate. |
| 193 | * @return New sized array as a copy of @p orig. |
| 194 | * @return NULL in case of memory allocation error. |
| 195 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 196 | static struct lysc_pattern** |
| 197 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 198 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 199 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 200 | unsigned int u; |
| 201 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 202 | assert(orig); |
| 203 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 204 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 205 | LY_ARRAY_FOR(orig, u) { |
| 206 | dup[u] = lysc_pattern_dup(orig[u]); |
| 207 | LY_ARRAY_INCREMENT(dup); |
| 208 | } |
| 209 | return dup; |
| 210 | } |
| 211 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 212 | /** |
| 213 | * @brief Duplicate compiled range structure. |
| 214 | * |
| 215 | * @param[in] ctx Libyang context for logging. |
| 216 | * @param[in] orig The range structure to be duplicated. |
| 217 | * @return New compiled range structure as a copy of @p orig. |
| 218 | * @return NULL in case of memory allocation error. |
| 219 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 220 | struct lysc_range* |
| 221 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 222 | { |
| 223 | struct lysc_range *dup; |
| 224 | LY_ERR ret; |
| 225 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 226 | assert(orig); |
| 227 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 228 | dup = calloc(1, sizeof *dup); |
| 229 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 230 | if (orig->parts) { |
| 231 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 232 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 233 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 234 | } |
| 235 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 236 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 237 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 238 | |
| 239 | return dup; |
| 240 | cleanup: |
| 241 | free(dup); |
| 242 | (void) ret; /* set but not used due to the return type */ |
| 243 | return NULL; |
| 244 | } |
| 245 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 246 | /** |
| 247 | * @brief Stack for processing if-feature expressions. |
| 248 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 249 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 250 | int size; /**< number of items in the stack */ |
| 251 | int index; /**< first empty item */ |
| 252 | 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] | 253 | }; |
| 254 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 255 | /** |
| 256 | * @brief Add @ref ifftokens into the stack. |
| 257 | * @param[in] stack The if-feature stack to use. |
| 258 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 259 | * @return LY_EMEM in case of memory allocation error |
| 260 | * @return LY_ESUCCESS if the value successfully stored. |
| 261 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 262 | static LY_ERR |
| 263 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 264 | { |
| 265 | if (stack->index == stack->size) { |
| 266 | stack->size += 4; |
| 267 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 268 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 269 | } |
| 270 | stack->stack[stack->index++] = value; |
| 271 | return LY_SUCCESS; |
| 272 | } |
| 273 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 274 | /** |
| 275 | * @brief Get (and remove) the last item form the stack. |
| 276 | * @param[in] stack The if-feature stack to use. |
| 277 | * @return The value from the top of the stack. |
| 278 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 279 | static uint8_t |
| 280 | iff_stack_pop(struct iff_stack *stack) |
| 281 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 282 | assert(stack && stack->index); |
| 283 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 284 | stack->index--; |
| 285 | return stack->stack[stack->index]; |
| 286 | } |
| 287 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 288 | /** |
| 289 | * @brief Clean up the stack. |
| 290 | * @param[in] stack The if-feature stack to use. |
| 291 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 292 | static void |
| 293 | iff_stack_clean(struct iff_stack *stack) |
| 294 | { |
| 295 | stack->size = 0; |
| 296 | free(stack->stack); |
| 297 | } |
| 298 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 299 | /** |
| 300 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 301 | * (libyang format of the if-feature expression). |
| 302 | * @param[in,out] list The 2bits array to modify. |
| 303 | * @param[in] op The operand (@ref ifftokens) to store. |
| 304 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 305 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 306 | static void |
| 307 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 308 | { |
| 309 | uint8_t *item; |
| 310 | uint8_t mask = 3; |
| 311 | |
| 312 | assert(pos >= 0); |
| 313 | assert(op <= 3); /* max 2 bits */ |
| 314 | |
| 315 | item = &list[pos / 4]; |
| 316 | mask = mask << 2 * (pos % 4); |
| 317 | *item = (*item) & ~mask; |
| 318 | *item = (*item) | (op << 2 * (pos % 4)); |
| 319 | } |
| 320 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 321 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 322 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 323 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 324 | /** |
| 325 | * @brief Find a feature of the given name and referenced in the given module. |
| 326 | * |
| 327 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 328 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 329 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 330 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 331 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 332 | * assigned till that time will be still valid. |
| 333 | * |
| 334 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 335 | * @param[in] name Name of the feature including possible prefix. |
| 336 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 337 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 338 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 339 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 340 | 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] | 341 | { |
| 342 | size_t i; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 343 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 344 | |
| 345 | for (i = 0; i < len; ++i) { |
| 346 | if (name[i] == ':') { |
| 347 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 348 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 349 | LY_CHECK_RET(!mod, NULL); |
| 350 | |
| 351 | name = &name[i + 1]; |
| 352 | len = len - i - 1; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 357 | if (mod->implemented) { |
| 358 | /* module is implemented so there is already the compiled schema */ |
| 359 | flist = mod->compiled->features; |
| 360 | } else { |
| 361 | flist = mod->off_features; |
| 362 | } |
| 363 | LY_ARRAY_FOR(flist, i) { |
| 364 | f = &flist[i]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 365 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 366 | return f; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 374 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 375 | { |
| 376 | const char *name; |
| 377 | unsigned int u; |
| 378 | const struct lys_module *mod; |
| 379 | struct lysp_ext *edef = NULL; |
| 380 | |
| 381 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 382 | ext->insubstmt = ext_p->insubstmt; |
| 383 | ext->insubstmt_index = ext_p->insubstmt_index; |
| 384 | |
| 385 | /* get module where the extension definition should be placed */ |
| 386 | for (u = 0; ext_p->name[u] != ':'; ++u); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 387 | mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 388 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 389 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 390 | LY_EVALID); |
| 391 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 392 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 393 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 394 | ext_p->name, mod->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 395 | LY_EVALID); |
| 396 | name = &ext_p->name[u + 1]; |
| 397 | /* find the extension definition there */ |
| 398 | for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) { |
| 399 | if (!strcmp(name, mod->parsed->extensions[u].name)) { |
| 400 | edef = &mod->parsed->extensions[u]; |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 405 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
| 406 | LY_EVALID); |
| 407 | /* TODO plugins */ |
| 408 | |
| 409 | return LY_SUCCESS; |
| 410 | } |
| 411 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 412 | /** |
| 413 | * @brief Compile information from the if-feature statement |
| 414 | * @param[in] ctx Compile context. |
| 415 | * @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] | 416 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 417 | * @return LY_ERR value. |
| 418 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 419 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 420 | 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] | 421 | { |
| 422 | const char *c = *value; |
| 423 | int r, rc = EXIT_FAILURE; |
| 424 | int i, j, last_not, checkversion = 0; |
| 425 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 426 | uint8_t op; |
| 427 | struct iff_stack stack = {0, 0, NULL}; |
| 428 | struct lysc_feature *f; |
| 429 | |
| 430 | assert(c); |
| 431 | |
| 432 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 433 | for (i = j = last_not = 0; c[i]; i++) { |
| 434 | if (c[i] == '(') { |
| 435 | j++; |
| 436 | checkversion = 1; |
| 437 | continue; |
| 438 | } else if (c[i] == ')') { |
| 439 | j--; |
| 440 | continue; |
| 441 | } else if (isspace(c[i])) { |
| 442 | checkversion = 1; |
| 443 | continue; |
| 444 | } |
| 445 | |
| 446 | 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] | 447 | int sp; |
| 448 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 449 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 450 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 451 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 452 | return LY_EVALID; |
| 453 | } else if (!isspace(c[i + r])) { |
| 454 | /* feature name starting with the not/and/or */ |
| 455 | last_not = 0; |
| 456 | f_size++; |
| 457 | } else if (c[i] == 'n') { /* not operation */ |
| 458 | if (last_not) { |
| 459 | /* double not */ |
| 460 | expr_size = expr_size - 2; |
| 461 | last_not = 0; |
| 462 | } else { |
| 463 | last_not = 1; |
| 464 | } |
| 465 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 466 | if (f_exp != f_size) { |
| 467 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 468 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 469 | return LY_EVALID; |
| 470 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 471 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 472 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 473 | /* not a not operation */ |
| 474 | last_not = 0; |
| 475 | } |
| 476 | i += r; |
| 477 | } else { |
| 478 | f_size++; |
| 479 | last_not = 0; |
| 480 | } |
| 481 | expr_size++; |
| 482 | |
| 483 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 484 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 485 | i--; |
| 486 | break; |
| 487 | } |
| 488 | i++; |
| 489 | } |
| 490 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 491 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 492 | /* not matching count of ( and ) */ |
| 493 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 494 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 495 | return LY_EVALID; |
| 496 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 497 | if (f_exp != f_size) { |
| 498 | /* features do not match the needed arguments for the logical operations */ |
| 499 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 500 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 501 | "the required number of operands for the operations.", *value); |
| 502 | return LY_EVALID; |
| 503 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 504 | |
| 505 | if (checkversion || expr_size > 1) { |
| 506 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 507 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 508 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 509 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 510 | return LY_EVALID; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /* allocate the memory */ |
| 515 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 516 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 517 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 518 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 519 | |
| 520 | stack.size = expr_size; |
| 521 | f_size--; expr_size--; /* used as indexes from now */ |
| 522 | |
| 523 | for (i--; i >= 0; i--) { |
| 524 | if (c[i] == ')') { |
| 525 | /* push it on stack */ |
| 526 | iff_stack_push(&stack, LYS_IFF_RP); |
| 527 | continue; |
| 528 | } else if (c[i] == '(') { |
| 529 | /* pop from the stack into result all operators until ) */ |
| 530 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 531 | iff_setop(iff->expr, op, expr_size--); |
| 532 | } |
| 533 | continue; |
| 534 | } else if (isspace(c[i])) { |
| 535 | continue; |
| 536 | } |
| 537 | |
| 538 | /* end of operator or operand -> find beginning and get what is it */ |
| 539 | j = i + 1; |
| 540 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 541 | i--; |
| 542 | } |
| 543 | i++; /* go back by one step */ |
| 544 | |
| 545 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 546 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 547 | /* double not */ |
| 548 | iff_stack_pop(&stack); |
| 549 | } else { |
| 550 | /* not has the highest priority, so do not pop from the stack |
| 551 | * as in case of AND and OR */ |
| 552 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 553 | } |
| 554 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 555 | /* as for OR - pop from the stack all operators with the same or higher |
| 556 | * priority and store them to the result, then push the AND to the stack */ |
| 557 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 558 | op = iff_stack_pop(&stack); |
| 559 | iff_setop(iff->expr, op, expr_size--); |
| 560 | } |
| 561 | iff_stack_push(&stack, LYS_IFF_AND); |
| 562 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 563 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 564 | op = iff_stack_pop(&stack); |
| 565 | iff_setop(iff->expr, op, expr_size--); |
| 566 | } |
| 567 | iff_stack_push(&stack, LYS_IFF_OR); |
| 568 | } else { |
| 569 | /* feature name, length is j - i */ |
| 570 | |
| 571 | /* add it to the expression */ |
| 572 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 573 | |
| 574 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 575 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 576 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 577 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 578 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 579 | iff->features[f_size] = f; |
| 580 | LY_ARRAY_INCREMENT(iff->features); |
| 581 | f_size--; |
| 582 | } |
| 583 | } |
| 584 | while (stack.index) { |
| 585 | op = iff_stack_pop(&stack); |
| 586 | iff_setop(iff->expr, op, expr_size--); |
| 587 | } |
| 588 | |
| 589 | if (++expr_size || ++f_size) { |
| 590 | /* not all expected operators and operands found */ |
| 591 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 592 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 593 | rc = LY_EINT; |
| 594 | } else { |
| 595 | rc = LY_SUCCESS; |
| 596 | } |
| 597 | |
| 598 | error: |
| 599 | /* cleanup */ |
| 600 | iff_stack_clean(&stack); |
| 601 | |
| 602 | return rc; |
| 603 | } |
| 604 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 605 | /** |
| 606 | * @brief Compile information from the when statement |
| 607 | * @param[in] ctx Compile context. |
| 608 | * @param[in] when_p The parsed when statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 609 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 610 | * @return LY_ERR value. |
| 611 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 612 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 613 | 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] | 614 | { |
| 615 | unsigned int u; |
| 616 | LY_ERR ret = LY_SUCCESS; |
| 617 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 618 | *when = calloc(1, sizeof **when); |
| 619 | (*when)->refcount = 1; |
| 620 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
| 621 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 622 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 623 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 624 | COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 625 | |
| 626 | done: |
| 627 | return ret; |
| 628 | } |
| 629 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 630 | /** |
| 631 | * @brief Compile information from the must statement |
| 632 | * @param[in] ctx Compile context. |
| 633 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 634 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 635 | * @return LY_ERR value. |
| 636 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 637 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 638 | 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] | 639 | { |
| 640 | unsigned int u; |
| 641 | LY_ERR ret = LY_SUCCESS; |
| 642 | |
| 643 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 644 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
| 645 | |
| 646 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 647 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 648 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 649 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 650 | COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 651 | |
| 652 | done: |
| 653 | return ret; |
| 654 | } |
| 655 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 656 | /** |
| 657 | * @brief Compile information from the import statement |
| 658 | * @param[in] ctx Compile context. |
| 659 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 660 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 661 | * @return LY_ERR value. |
| 662 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 663 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 664 | 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] | 665 | { |
| 666 | unsigned int u; |
| 667 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 668 | LY_ERR ret = LY_SUCCESS; |
| 669 | |
| 670 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 671 | COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 672 | imp->module = imp_p->module; |
| 673 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 674 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 675 | * 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] | 676 | * 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] | 677 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 678 | /* try to use filepath if present */ |
| 679 | if (imp->module->filepath) { |
| 680 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 681 | !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] | 682 | if (mod != imp->module) { |
| 683 | 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] | 684 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 685 | mod = NULL; |
| 686 | } |
| 687 | } |
| 688 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 689 | 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] | 690 | 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] | 691 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 692 | return LY_ENOTFOUND; |
| 693 | } |
| 694 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | done: |
| 698 | return ret; |
| 699 | } |
| 700 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 701 | /** |
| 702 | * @brief Compile information from the identity statement |
| 703 | * |
| 704 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 705 | * |
| 706 | * @param[in] ctx Compile context. |
| 707 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 708 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 709 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 710 | * @return LY_ERR value. |
| 711 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 712 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 713 | 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] | 714 | { |
| 715 | unsigned int u; |
| 716 | LY_ERR ret = LY_SUCCESS; |
| 717 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 718 | lysc_update_path(ctx, NULL, ident_p->name); |
| 719 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 720 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 721 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 722 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 723 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 724 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 725 | 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] | 726 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 727 | COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 728 | ident->flags = ident_p->flags; |
| 729 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 730 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 731 | done: |
| 732 | return ret; |
| 733 | } |
| 734 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 735 | /** |
| 736 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 737 | * |
| 738 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 739 | * |
| 740 | * @param[in] ctx Compile context for logging. |
| 741 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 742 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 743 | * @return LY_SUCCESS if everything is ok. |
| 744 | * @return LY_EVALID if the identity is derived from itself. |
| 745 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 746 | static LY_ERR |
| 747 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 748 | { |
| 749 | LY_ERR ret = LY_EVALID; |
| 750 | unsigned int u, v; |
| 751 | struct ly_set recursion = {0}; |
| 752 | struct lysc_ident *drv; |
| 753 | |
| 754 | if (!derived) { |
| 755 | return LY_SUCCESS; |
| 756 | } |
| 757 | |
| 758 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 759 | if (ident == derived[u]) { |
| 760 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 761 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 762 | goto cleanup; |
| 763 | } |
| 764 | ly_set_add(&recursion, derived[u], 0); |
| 765 | } |
| 766 | |
| 767 | for (v = 0; v < recursion.count; ++v) { |
| 768 | drv = recursion.objs[v]; |
| 769 | if (!drv->derived) { |
| 770 | continue; |
| 771 | } |
| 772 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 773 | if (ident == drv->derived[u]) { |
| 774 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 775 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 776 | goto cleanup; |
| 777 | } |
| 778 | ly_set_add(&recursion, drv->derived[u], 0); |
| 779 | } |
| 780 | } |
| 781 | ret = LY_SUCCESS; |
| 782 | |
| 783 | cleanup: |
| 784 | ly_set_erase(&recursion, NULL); |
| 785 | return ret; |
| 786 | } |
| 787 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 788 | /** |
| 789 | * @brief Find and process the referenced base identities from another identity or identityref |
| 790 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 791 | * 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] | 792 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 793 | * to distinguish these two use cases. |
| 794 | * |
| 795 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 796 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 797 | * @param[in] ident Referencing identity to work with. |
| 798 | * @param[in] bases Array of bases of identityref to fill in. |
| 799 | * @return LY_ERR value. |
| 800 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 801 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 802 | 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] | 803 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 804 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 805 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 806 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 807 | struct lysc_ident **idref; |
| 808 | |
| 809 | assert(ident || bases); |
| 810 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 811 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 812 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 813 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 814 | return LY_EVALID; |
| 815 | } |
| 816 | |
| 817 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 818 | s = strchr(bases_p[u], ':'); |
| 819 | if (s) { |
| 820 | /* prefixed identity */ |
| 821 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 822 | 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] | 823 | } else { |
| 824 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 825 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 826 | } |
| 827 | if (!mod) { |
| 828 | if (ident) { |
| 829 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 830 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 831 | } else { |
| 832 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 833 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 834 | } |
| 835 | return LY_EVALID; |
| 836 | } |
| 837 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 838 | if (mod->compiled && mod->compiled->identities) { |
| 839 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 840 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 841 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 842 | if (ident == &mod->compiled->identities[v]) { |
| 843 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 844 | "Identity \"%s\" is derived from itself.", ident->name); |
| 845 | return LY_EVALID; |
| 846 | } |
| 847 | 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] | 848 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 849 | 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] | 850 | *idref = ident; |
| 851 | } else { |
| 852 | /* we have match! store the found identity */ |
| 853 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 854 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 855 | } |
| 856 | break; |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | if (!idref || !(*idref)) { |
| 861 | if (ident) { |
| 862 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 863 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 864 | } else { |
| 865 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 866 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 867 | } |
| 868 | return LY_EVALID; |
| 869 | } |
| 870 | } |
| 871 | return LY_SUCCESS; |
| 872 | } |
| 873 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 874 | /** |
| 875 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 876 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 877 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 878 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 879 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 880 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 881 | static LY_ERR |
| 882 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 883 | { |
| 884 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 885 | |
| 886 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 887 | if (!idents_p[i].bases) { |
| 888 | continue; |
| 889 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 890 | lysc_update_path(ctx, NULL, idents[i].name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 891 | 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] | 892 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 893 | } |
| 894 | return LY_SUCCESS; |
| 895 | } |
| 896 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 897 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 898 | 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] | 899 | { |
| 900 | unsigned int offset = 0, u; |
| 901 | struct lysc_ctx context = {0}; |
| 902 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 903 | assert(ctx_sc || ctx); |
| 904 | |
| 905 | if (!ctx_sc) { |
| 906 | context.ctx = ctx; |
| 907 | context.mod = module; |
| 908 | context.path_len = 1; |
| 909 | context.path[0] = '/'; |
| 910 | ctx_sc = &context; |
| 911 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 912 | |
| 913 | if (!features_p) { |
| 914 | return LY_SUCCESS; |
| 915 | } |
| 916 | if (*features) { |
| 917 | offset = LY_ARRAY_SIZE(*features); |
| 918 | } |
| 919 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 920 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 921 | 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] | 922 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 923 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 924 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 925 | LY_ARRAY_INCREMENT(*features); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 926 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 927 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 928 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 929 | 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] | 930 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 931 | (*features)[offset + u].module = ctx_sc->mod; |
| 932 | |
| 933 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 934 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 935 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 936 | |
| 937 | return LY_SUCCESS; |
| 938 | } |
| 939 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 940 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 941 | * @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] | 942 | * |
| 943 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 944 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 945 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 946 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 947 | * being currently processed). |
| 948 | * @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] | 949 | * @return LY_SUCCESS if everything is ok. |
| 950 | * @return LY_EVALID if the feature references indirectly itself. |
| 951 | */ |
| 952 | static LY_ERR |
| 953 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 954 | { |
| 955 | LY_ERR ret = LY_EVALID; |
| 956 | unsigned int u, v; |
| 957 | struct ly_set recursion = {0}; |
| 958 | struct lysc_feature *drv; |
| 959 | |
| 960 | if (!depfeatures) { |
| 961 | return LY_SUCCESS; |
| 962 | } |
| 963 | |
| 964 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 965 | if (feature == depfeatures[u]) { |
| 966 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 967 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 968 | goto cleanup; |
| 969 | } |
| 970 | ly_set_add(&recursion, depfeatures[u], 0); |
| 971 | } |
| 972 | |
| 973 | for (v = 0; v < recursion.count; ++v) { |
| 974 | drv = recursion.objs[v]; |
| 975 | if (!drv->depfeatures) { |
| 976 | continue; |
| 977 | } |
| 978 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 979 | if (feature == drv->depfeatures[u]) { |
| 980 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 981 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 982 | goto cleanup; |
| 983 | } |
| 984 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 985 | } |
| 986 | } |
| 987 | ret = LY_SUCCESS; |
| 988 | |
| 989 | cleanup: |
| 990 | ly_set_erase(&recursion, NULL); |
| 991 | return ret; |
| 992 | } |
| 993 | |
| 994 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 995 | * @brief Create pre-compiled features array. |
| 996 | * |
| 997 | * See lys_feature_precompile() for more details. |
| 998 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 999 | * @param[in] ctx Compile context. |
| 1000 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1001 | * @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] | 1002 | * @return LY_ERR value. |
| 1003 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1004 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1005 | 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] | 1006 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1007 | unsigned int u, v, x; |
| 1008 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1009 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1010 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1011 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1012 | /* find the preprecompiled feature */ |
| 1013 | LY_ARRAY_FOR(features, x) { |
| 1014 | if (strcmp(features[x].name, feature_p->name)) { |
| 1015 | continue; |
| 1016 | } |
| 1017 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1018 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1019 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1020 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1021 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1022 | COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, u, lys_compile_ext, ret, done); |
| 1023 | 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] | 1024 | if (feature->iffeatures) { |
| 1025 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1026 | if (feature->iffeatures[u].features) { |
| 1027 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1028 | /* check for circular dependency - direct reference first,... */ |
| 1029 | if (feature == feature->iffeatures[u].features[v]) { |
| 1030 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1031 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1032 | return LY_EVALID; |
| 1033 | } |
| 1034 | /* ... and indirect circular reference */ |
| 1035 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1036 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1037 | /* add itself into the dependants list */ |
| 1038 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1039 | *df = feature; |
| 1040 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1041 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1042 | } |
| 1043 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1044 | lysc_update_path(ctx, NULL, NULL); |
| 1045 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1046 | done: |
| 1047 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1048 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1049 | |
| 1050 | LOGINT(ctx->ctx); |
| 1051 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1052 | } |
| 1053 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1054 | /** |
| 1055 | * @brief Revert compiled list of features back to the precompiled state. |
| 1056 | * |
| 1057 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1058 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1059 | * |
| 1060 | * @param[in] ctx Compilation context. |
| 1061 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1062 | * and supposed to hold the off_features list. |
| 1063 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1064 | static void |
| 1065 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1066 | { |
| 1067 | unsigned int u, v; |
| 1068 | |
| 1069 | /* keep the off_features list until the complete lys_module is freed */ |
| 1070 | mod->off_features = mod->compiled->features; |
| 1071 | mod->compiled->features = NULL; |
| 1072 | |
| 1073 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1074 | * which may points into the data being freed here */ |
| 1075 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1076 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1077 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1078 | } |
| 1079 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1080 | mod->off_features[u].iffeatures = NULL; |
| 1081 | |
| 1082 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1083 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1084 | } |
| 1085 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1086 | mod->off_features[u].exts = NULL; |
| 1087 | } |
| 1088 | } |
| 1089 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1090 | /** |
| 1091 | * @brief Validate and normalize numeric value from a range definition. |
| 1092 | * @param[in] ctx Compile context. |
| 1093 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1094 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1095 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1096 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1097 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1098 | * @param[in] value String value of the range boundary. |
| 1099 | * @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. |
| 1100 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1101 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1102 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1103 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1104 | 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] | 1105 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1106 | size_t fraction = 0, size; |
| 1107 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1108 | *len = 0; |
| 1109 | |
| 1110 | assert(value); |
| 1111 | /* parse value */ |
| 1112 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1113 | return LY_EVALID; |
| 1114 | } |
| 1115 | |
| 1116 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1117 | ++(*len); |
| 1118 | } |
| 1119 | |
| 1120 | while (isdigit(value[*len])) { |
| 1121 | ++(*len); |
| 1122 | } |
| 1123 | |
| 1124 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1125 | if (basetype == LY_TYPE_DEC64) { |
| 1126 | goto decimal; |
| 1127 | } else { |
| 1128 | *valcopy = strndup(value, *len); |
| 1129 | return LY_SUCCESS; |
| 1130 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1131 | } |
| 1132 | fraction = *len; |
| 1133 | |
| 1134 | ++(*len); |
| 1135 | while (isdigit(value[*len])) { |
| 1136 | ++(*len); |
| 1137 | } |
| 1138 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1139 | if (basetype == LY_TYPE_DEC64) { |
| 1140 | decimal: |
| 1141 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1142 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1143 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1144 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1145 | *len, value, frdigits); |
| 1146 | return LY_EINVAL; |
| 1147 | } |
| 1148 | if (fraction) { |
| 1149 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1150 | } else { |
| 1151 | size = (*len) + frdigits + 1; |
| 1152 | } |
| 1153 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1154 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1155 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1156 | (*valcopy)[size - 1] = '\0'; |
| 1157 | if (fraction) { |
| 1158 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1159 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1160 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1161 | } else { |
| 1162 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1163 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1164 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1165 | } |
| 1166 | return LY_SUCCESS; |
| 1167 | } |
| 1168 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1169 | /** |
| 1170 | * @brief Check that values in range are in ascendant order. |
| 1171 | * @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] | 1172 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1173 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1174 | * @param[in] value Current value to check. |
| 1175 | * @param[in] prev_value The last seen value. |
| 1176 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1177 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1178 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1179 | 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] | 1180 | { |
| 1181 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1182 | 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] | 1183 | return LY_EEXIST; |
| 1184 | } |
| 1185 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1186 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1187 | return LY_EEXIST; |
| 1188 | } |
| 1189 | } |
| 1190 | return LY_SUCCESS; |
| 1191 | } |
| 1192 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1193 | /** |
| 1194 | * @brief Set min/max value of the range part. |
| 1195 | * @param[in] ctx Compile context. |
| 1196 | * @param[in] part Range part structure to fill. |
| 1197 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1198 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1199 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1200 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1201 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1202 | * @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] | 1203 | * @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] | 1204 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1205 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1206 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1207 | * frdigits value), LY_EMEM. |
| 1208 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1209 | static LY_ERR |
| 1210 | 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] | 1211 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1212 | { |
| 1213 | LY_ERR ret = LY_SUCCESS; |
| 1214 | char *valcopy = NULL; |
| 1215 | size_t len; |
| 1216 | |
| 1217 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1218 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1219 | LY_CHECK_GOTO(ret, finalize); |
| 1220 | } |
| 1221 | if (!valcopy && base_range) { |
| 1222 | if (max) { |
| 1223 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1224 | } else { |
| 1225 | part->min_64 = base_range->parts[0].min_64; |
| 1226 | } |
| 1227 | if (!first) { |
| 1228 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1229 | } |
| 1230 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1234 | case LY_TYPE_INT8: /* range */ |
| 1235 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1236 | 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] | 1237 | } else if (max) { |
| 1238 | part->max_64 = INT64_C(127); |
| 1239 | } else { |
| 1240 | part->min_64 = INT64_C(-128); |
| 1241 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1242 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1243 | 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] | 1244 | } |
| 1245 | break; |
| 1246 | case LY_TYPE_INT16: /* range */ |
| 1247 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1248 | 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] | 1249 | } else if (max) { |
| 1250 | part->max_64 = INT64_C(32767); |
| 1251 | } else { |
| 1252 | part->min_64 = INT64_C(-32768); |
| 1253 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1254 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1255 | 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] | 1256 | } |
| 1257 | break; |
| 1258 | case LY_TYPE_INT32: /* range */ |
| 1259 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1260 | 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] | 1261 | } else if (max) { |
| 1262 | part->max_64 = INT64_C(2147483647); |
| 1263 | } else { |
| 1264 | part->min_64 = INT64_C(-2147483648); |
| 1265 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1266 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1267 | 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] | 1268 | } |
| 1269 | break; |
| 1270 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1271 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1272 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1273 | 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] | 1274 | max ? &part->max_64 : &part->min_64); |
| 1275 | } else if (max) { |
| 1276 | part->max_64 = INT64_C(9223372036854775807); |
| 1277 | } else { |
| 1278 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1279 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1280 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1281 | 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] | 1282 | } |
| 1283 | break; |
| 1284 | case LY_TYPE_UINT8: /* range */ |
| 1285 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1286 | 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] | 1287 | } else if (max) { |
| 1288 | part->max_u64 = UINT64_C(255); |
| 1289 | } else { |
| 1290 | part->min_u64 = UINT64_C(0); |
| 1291 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1292 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1293 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1294 | } |
| 1295 | break; |
| 1296 | case LY_TYPE_UINT16: /* range */ |
| 1297 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1298 | 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] | 1299 | } else if (max) { |
| 1300 | part->max_u64 = UINT64_C(65535); |
| 1301 | } else { |
| 1302 | part->min_u64 = UINT64_C(0); |
| 1303 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1304 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1305 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1306 | } |
| 1307 | break; |
| 1308 | case LY_TYPE_UINT32: /* range */ |
| 1309 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1310 | 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] | 1311 | } else if (max) { |
| 1312 | part->max_u64 = UINT64_C(4294967295); |
| 1313 | } else { |
| 1314 | part->min_u64 = UINT64_C(0); |
| 1315 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1316 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1317 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1318 | } |
| 1319 | break; |
| 1320 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1321 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1322 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1323 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1324 | 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] | 1325 | } else if (max) { |
| 1326 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1327 | } else { |
| 1328 | part->min_u64 = UINT64_C(0); |
| 1329 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1330 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1331 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1332 | } |
| 1333 | break; |
| 1334 | default: |
| 1335 | LOGINT(ctx->ctx); |
| 1336 | ret = LY_EINT; |
| 1337 | } |
| 1338 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1339 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1340 | if (ret == LY_EDENIED) { |
| 1341 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1342 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1343 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1344 | } else if (ret == LY_EVALID) { |
| 1345 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1346 | "Invalid %s restriction - invalid value \"%s\".", |
| 1347 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1348 | } else if (ret == LY_EEXIST) { |
| 1349 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1350 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1351 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1352 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1353 | } else if (!ret && value) { |
| 1354 | *value = *value + len; |
| 1355 | } |
| 1356 | free(valcopy); |
| 1357 | return ret; |
| 1358 | } |
| 1359 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1360 | /** |
| 1361 | * @brief Compile the parsed range restriction. |
| 1362 | * @param[in] ctx Compile context. |
| 1363 | * @param[in] range_p Parsed range structure to compile. |
| 1364 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1365 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1366 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1367 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1368 | * range restriction must be more restrictive than the base_range. |
| 1369 | * @param[in,out] range Pointer to the created current range structure. |
| 1370 | * @return LY_ERR value. |
| 1371 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1372 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1373 | 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] | 1374 | struct lysc_range *base_range, struct lysc_range **range) |
| 1375 | { |
| 1376 | LY_ERR ret = LY_EVALID; |
| 1377 | const char *expr; |
| 1378 | struct lysc_range_part *parts = NULL, *part; |
| 1379 | int range_expected = 0, uns; |
| 1380 | unsigned int parts_done = 0, u, v; |
| 1381 | |
| 1382 | assert(range); |
| 1383 | assert(range_p); |
| 1384 | |
| 1385 | expr = range_p->arg; |
| 1386 | while(1) { |
| 1387 | if (isspace(*expr)) { |
| 1388 | ++expr; |
| 1389 | } else if (*expr == '\0') { |
| 1390 | if (range_expected) { |
| 1391 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1392 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1393 | length_restr ? "length" : "range", range_p->arg); |
| 1394 | goto cleanup; |
| 1395 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1396 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1397 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1398 | length_restr ? "length" : "range", range_p->arg); |
| 1399 | goto cleanup; |
| 1400 | } |
| 1401 | parts_done++; |
| 1402 | break; |
| 1403 | } else if (!strncmp(expr, "min", 3)) { |
| 1404 | if (parts) { |
| 1405 | /* min cannot be used elsewhere than in the first part */ |
| 1406 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1407 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1408 | expr - range_p->arg, range_p->arg); |
| 1409 | goto cleanup; |
| 1410 | } |
| 1411 | expr += 3; |
| 1412 | |
| 1413 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1414 | 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] | 1415 | part->max_64 = part->min_64; |
| 1416 | } else if (*expr == '|') { |
| 1417 | if (!parts || range_expected) { |
| 1418 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1419 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1420 | goto cleanup; |
| 1421 | } |
| 1422 | expr++; |
| 1423 | parts_done++; |
| 1424 | /* process next part of the expression */ |
| 1425 | } else if (!strncmp(expr, "..", 2)) { |
| 1426 | expr += 2; |
| 1427 | while (isspace(*expr)) { |
| 1428 | expr++; |
| 1429 | } |
| 1430 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1431 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1432 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1433 | goto cleanup; |
| 1434 | } |
| 1435 | /* continue expecting the upper boundary */ |
| 1436 | range_expected = 1; |
| 1437 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1438 | /* number */ |
| 1439 | if (range_expected) { |
| 1440 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1441 | 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] | 1442 | range_expected = 0; |
| 1443 | } else { |
| 1444 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1445 | 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] | 1446 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1447 | part->max_64 = part->min_64; |
| 1448 | } |
| 1449 | |
| 1450 | /* continue with possible another expression part */ |
| 1451 | } else if (!strncmp(expr, "max", 3)) { |
| 1452 | expr += 3; |
| 1453 | while (isspace(*expr)) { |
| 1454 | expr++; |
| 1455 | } |
| 1456 | if (*expr != '\0') { |
| 1457 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1458 | length_restr ? "length" : "range", expr); |
| 1459 | goto cleanup; |
| 1460 | } |
| 1461 | if (range_expected) { |
| 1462 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1463 | 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] | 1464 | range_expected = 0; |
| 1465 | } else { |
| 1466 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1467 | 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] | 1468 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1469 | part->min_64 = part->max_64; |
| 1470 | } |
| 1471 | } else { |
| 1472 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1473 | length_restr ? "length" : "range", expr); |
| 1474 | goto cleanup; |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | /* check with the previous range/length restriction */ |
| 1479 | if (base_range) { |
| 1480 | switch (basetype) { |
| 1481 | case LY_TYPE_BINARY: |
| 1482 | case LY_TYPE_UINT8: |
| 1483 | case LY_TYPE_UINT16: |
| 1484 | case LY_TYPE_UINT32: |
| 1485 | case LY_TYPE_UINT64: |
| 1486 | case LY_TYPE_STRING: |
| 1487 | uns = 1; |
| 1488 | break; |
| 1489 | case LY_TYPE_DEC64: |
| 1490 | case LY_TYPE_INT8: |
| 1491 | case LY_TYPE_INT16: |
| 1492 | case LY_TYPE_INT32: |
| 1493 | case LY_TYPE_INT64: |
| 1494 | uns = 0; |
| 1495 | break; |
| 1496 | default: |
| 1497 | LOGINT(ctx->ctx); |
| 1498 | ret = LY_EINT; |
| 1499 | goto cleanup; |
| 1500 | } |
| 1501 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1502 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1503 | goto baseerror; |
| 1504 | } |
| 1505 | /* current lower bound is not lower than the base */ |
| 1506 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1507 | /* base has single value */ |
| 1508 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1509 | /* both lower bounds are the same */ |
| 1510 | if (parts[u].min_64 != parts[u].max_64) { |
| 1511 | /* current continues with a range */ |
| 1512 | goto baseerror; |
| 1513 | } else { |
| 1514 | /* equal single values, move both forward */ |
| 1515 | ++v; |
| 1516 | continue; |
| 1517 | } |
| 1518 | } else { |
| 1519 | /* base is single value lower than current range, so the |
| 1520 | * value from base range is removed in the current, |
| 1521 | * move only base and repeat checking */ |
| 1522 | ++v; |
| 1523 | --u; |
| 1524 | continue; |
| 1525 | } |
| 1526 | } else { |
| 1527 | /* base is the range */ |
| 1528 | if (parts[u].min_64 == parts[u].max_64) { |
| 1529 | /* current is a single value */ |
| 1530 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1531 | /* current is behind the base range, so base range is omitted, |
| 1532 | * move the base and keep the current for further check */ |
| 1533 | ++v; |
| 1534 | --u; |
| 1535 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1536 | continue; |
| 1537 | } else { |
| 1538 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1539 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1540 | /* higher bound is higher than the current higher bound */ |
| 1541 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1542 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1543 | * continue with the same current, but move the base */ |
| 1544 | --u; |
| 1545 | ++v; |
| 1546 | continue; |
| 1547 | } |
| 1548 | /* current range starts within the base range but end behind it */ |
| 1549 | goto baseerror; |
| 1550 | } else { |
| 1551 | /* current range is smaller than the base, |
| 1552 | * move current, but stay with the base */ |
| 1553 | continue; |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | if (u != parts_done) { |
| 1559 | baseerror: |
| 1560 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1561 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1562 | length_restr ? "length" : "range", range_p->arg); |
| 1563 | goto cleanup; |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | if (!(*range)) { |
| 1568 | *range = calloc(1, sizeof **range); |
| 1569 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1570 | } |
| 1571 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1572 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1573 | if (range_p->eapptag) { |
| 1574 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1575 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1576 | } |
| 1577 | if (range_p->emsg) { |
| 1578 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1579 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1580 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1581 | if (range_p->dsc) { |
| 1582 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1583 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1584 | } |
| 1585 | if (range_p->ref) { |
| 1586 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1587 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1588 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1589 | /* extensions are taken only from the last range by the caller */ |
| 1590 | |
| 1591 | (*range)->parts = parts; |
| 1592 | parts = NULL; |
| 1593 | ret = LY_SUCCESS; |
| 1594 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1595 | LY_ARRAY_FREE(parts); |
| 1596 | |
| 1597 | return ret; |
| 1598 | } |
| 1599 | |
| 1600 | /** |
| 1601 | * @brief Checks pattern syntax. |
| 1602 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1603 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1604 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1605 | * @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] | 1606 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1607 | */ |
| 1608 | static LY_ERR |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1609 | 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] | 1610 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1611 | int idx, idx2, start, end, count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1612 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1613 | int err_code; |
| 1614 | const char *orig_ptr; |
| 1615 | PCRE2_SIZE err_offset; |
| 1616 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1617 | #define URANGE_LEN 19 |
| 1618 | char *ublock2urange[][2] = { |
| 1619 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1620 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1621 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1622 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1623 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1624 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1625 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1626 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1627 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1628 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1629 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1630 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1631 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1632 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1633 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1634 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1635 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1636 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1637 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1638 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1639 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1640 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1641 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1642 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1643 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1644 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1645 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1646 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1647 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1648 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1649 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1650 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1651 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1652 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1653 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1654 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1655 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1656 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1657 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1658 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1659 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1660 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1661 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1662 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1663 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1664 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1665 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1666 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1667 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1668 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1669 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1670 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1671 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1672 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1673 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1674 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1675 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1676 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1677 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1678 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1679 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1680 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1681 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1682 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1683 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1684 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1685 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1686 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1687 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1688 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1689 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1690 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1691 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1692 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1693 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1694 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1695 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1696 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1697 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1698 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1699 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1700 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1701 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1702 | {NULL, NULL} |
| 1703 | }; |
| 1704 | |
| 1705 | /* adjust the expression to a Perl equivalent |
| 1706 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1707 | |
| 1708 | /* we need to replace all "$" with "\$", count them now */ |
| 1709 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1710 | |
| 1711 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1712 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1713 | perl_regex[0] = '\0'; |
| 1714 | |
| 1715 | ptr = perl_regex; |
| 1716 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1717 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1718 | if (orig_ptr[0] == '$') { |
| 1719 | ptr += sprintf(ptr, "\\$"); |
| 1720 | } else if (orig_ptr[0] == '^') { |
| 1721 | ptr += sprintf(ptr, "\\^"); |
| 1722 | } else { |
| 1723 | ptr[0] = orig_ptr[0]; |
| 1724 | ++ptr; |
| 1725 | } |
| 1726 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1727 | ptr[0] = '\0'; |
| 1728 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1729 | |
| 1730 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1731 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1732 | start = ptr - perl_regex; |
| 1733 | |
| 1734 | ptr = strchr(ptr, '}'); |
| 1735 | if (!ptr) { |
| 1736 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1737 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1738 | free(perl_regex); |
| 1739 | return LY_EVALID; |
| 1740 | } |
| 1741 | end = (ptr - perl_regex) + 1; |
| 1742 | |
| 1743 | /* need more space */ |
| 1744 | if (end - start < URANGE_LEN) { |
| 1745 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1746 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1747 | } |
| 1748 | |
| 1749 | /* find our range */ |
| 1750 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1751 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1752 | break; |
| 1753 | } |
| 1754 | } |
| 1755 | if (!ublock2urange[idx][0]) { |
| 1756 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1757 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1758 | free(perl_regex); |
| 1759 | return LY_EVALID; |
| 1760 | } |
| 1761 | |
| 1762 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1763 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1764 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1765 | ++count; |
| 1766 | } |
| 1767 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1768 | --count; |
| 1769 | } |
| 1770 | } |
| 1771 | if (count) { |
| 1772 | /* skip brackets */ |
| 1773 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1774 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1775 | } else { |
| 1776 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1777 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1782 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 1783 | 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] | 1784 | &err_code, &err_offset, NULL); |
| 1785 | if (!code_local) { |
| 1786 | PCRE2_UCHAR err_msg[256] = {0}; |
| 1787 | pcre2_get_error_message(err_code, err_msg, 256); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1788 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1789 | free(perl_regex); |
| 1790 | return LY_EVALID; |
| 1791 | } |
| 1792 | free(perl_regex); |
| 1793 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1794 | if (code) { |
| 1795 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1796 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1797 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | return LY_SUCCESS; |
| 1801 | |
| 1802 | #undef URANGE_LEN |
| 1803 | } |
| 1804 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1805 | /** |
| 1806 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1807 | * @param[in] ctx Compile context. |
| 1808 | * @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] | 1809 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1810 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1811 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1812 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1813 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1814 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1815 | 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] | 1816 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1817 | { |
| 1818 | struct lysc_pattern **pattern; |
| 1819 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1820 | LY_ERR ret = LY_SUCCESS; |
| 1821 | |
| 1822 | /* first, copy the patterns from the base type */ |
| 1823 | if (base_patterns) { |
| 1824 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1825 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1826 | } |
| 1827 | |
| 1828 | LY_ARRAY_FOR(patterns_p, u) { |
| 1829 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1830 | *pattern = calloc(1, sizeof **pattern); |
| 1831 | ++(*pattern)->refcount; |
| 1832 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1833 | 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] | 1834 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1835 | |
| 1836 | if (patterns_p[u].arg[0] == 0x15) { |
| 1837 | (*pattern)->inverted = 1; |
| 1838 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1839 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1840 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1841 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1842 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 1843 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1844 | COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, v, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1845 | } |
| 1846 | done: |
| 1847 | return ret; |
| 1848 | } |
| 1849 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1850 | /** |
| 1851 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1852 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1853 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1854 | 0 /* LY_TYPE_UNKNOWN */, |
| 1855 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1856 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1857 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1858 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1859 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 1860 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1861 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1862 | 0 /* LY_TYPE_BOOL */, |
| 1863 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 1864 | 0 /* LY_TYPE_EMPTY */, |
| 1865 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 1866 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 1867 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 1868 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1869 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 1870 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1871 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1872 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1873 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 1874 | }; |
| 1875 | |
| 1876 | /** |
| 1877 | * @brief stringification of the YANG built-in data types |
| 1878 | */ |
| 1879 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 1880 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 1881 | "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] | 1882 | }; |
| 1883 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1884 | /** |
| 1885 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 1886 | * @param[in] ctx Compile context. |
| 1887 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 1888 | * @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] | 1889 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 1890 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 1891 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1892 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1893 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1894 | 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] | 1895 | 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] | 1896 | { |
| 1897 | LY_ERR ret = LY_SUCCESS; |
| 1898 | unsigned int u, v, match; |
| 1899 | int32_t value = 0; |
| 1900 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1901 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1902 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1903 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1904 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 1905 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 1906 | return LY_EVALID; |
| 1907 | } |
| 1908 | |
| 1909 | LY_ARRAY_FOR(enums_p, u) { |
| 1910 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 1911 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1912 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 1913 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1914 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1915 | if (base_enums) { |
| 1916 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 1917 | LY_ARRAY_FOR(base_enums, v) { |
| 1918 | if (!strcmp(e->name, base_enums[v].name)) { |
| 1919 | break; |
| 1920 | } |
| 1921 | } |
| 1922 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 1923 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1924 | "Invalid %s - derived type adds new item \"%s\".", |
| 1925 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 1926 | return LY_EVALID; |
| 1927 | } |
| 1928 | match = v; |
| 1929 | } |
| 1930 | |
| 1931 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1932 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1933 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1934 | e->value = (int32_t)enums_p[u].value; |
| 1935 | if (!u || e->value >= value) { |
| 1936 | value = e->value + 1; |
| 1937 | } |
| 1938 | /* check collision with other values */ |
| 1939 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1940 | if (e->value == (*enums)[v].value) { |
| 1941 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1942 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 1943 | e->value, e->name, (*enums)[v].name); |
| 1944 | return LY_EVALID; |
| 1945 | } |
| 1946 | } |
| 1947 | } else if (base_enums) { |
| 1948 | /* inherit the assigned value */ |
| 1949 | e->value = base_enums[match].value; |
| 1950 | if (!u || e->value >= value) { |
| 1951 | value = e->value + 1; |
| 1952 | } |
| 1953 | } else { |
| 1954 | /* assign value automatically */ |
| 1955 | if (u && value == INT32_MIN) { |
| 1956 | /* counter overflow */ |
| 1957 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1958 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 1959 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 1960 | return LY_EVALID; |
| 1961 | } |
| 1962 | e->value = value++; |
| 1963 | } |
| 1964 | } else { /* LY_TYPE_BITS */ |
| 1965 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1966 | e->value = (int32_t)enums_p[u].value; |
| 1967 | if (!u || (uint32_t)e->value >= position) { |
| 1968 | position = (uint32_t)e->value + 1; |
| 1969 | } |
| 1970 | /* check collision with other values */ |
| 1971 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1972 | if (e->value == (*enums)[v].value) { |
| 1973 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1974 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 1975 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 1976 | return LY_EVALID; |
| 1977 | } |
| 1978 | } |
| 1979 | } else if (base_enums) { |
| 1980 | /* inherit the assigned value */ |
| 1981 | e->value = base_enums[match].value; |
| 1982 | if (!u || (uint32_t)e->value >= position) { |
| 1983 | position = (uint32_t)e->value + 1; |
| 1984 | } |
| 1985 | } else { |
| 1986 | /* assign value automatically */ |
| 1987 | if (u && position == 0) { |
| 1988 | /* counter overflow */ |
| 1989 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1990 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 1991 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 1992 | return LY_EVALID; |
| 1993 | } |
| 1994 | e->value = position++; |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | if (base_enums) { |
| 1999 | /* the assigned values must not change from the derived type */ |
| 2000 | if (e->value != base_enums[match].value) { |
| 2001 | if (basetype == LY_TYPE_ENUM) { |
| 2002 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2003 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2004 | e->name, base_enums[match].value, e->value); |
| 2005 | } else { |
| 2006 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2007 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2008 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2009 | } |
| 2010 | return LY_EVALID; |
| 2011 | } |
| 2012 | } |
| 2013 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2014 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done); |
| 2015 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, v, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2016 | |
| 2017 | if (basetype == LY_TYPE_BITS) { |
| 2018 | /* keep bits ordered by position */ |
| 2019 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2020 | if (v != u) { |
| 2021 | memcpy(&storage, e, sizeof *e); |
| 2022 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2023 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2024 | } |
| 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | done: |
| 2029 | return ret; |
| 2030 | } |
| 2031 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2032 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2033 | for ((NODE) = (NODE)->parent; \ |
| 2034 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 2035 | (NODE) = (NODE)->parent); \ |
| 2036 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2037 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2038 | TERM; \ |
| 2039 | } |
| 2040 | |
| 2041 | /** |
| 2042 | * @brief Validate the predicate(s) from the leafref path. |
| 2043 | * @param[in] ctx Compile context |
| 2044 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2045 | * 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] | 2046 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2047 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2048 | * @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] | 2049 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2050 | */ |
| 2051 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2052 | 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] | 2053 | 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] | 2054 | { |
| 2055 | LY_ERR ret = LY_EVALID; |
| 2056 | const struct lys_module *mod; |
| 2057 | const struct lysc_node *src_node, *dst_node; |
| 2058 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2059 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2060 | unsigned int dest_parent_times, c, u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2061 | const char *start, *end, *pke_end; |
| 2062 | struct ly_set keys = {0}; |
| 2063 | int i; |
| 2064 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2065 | assert(path_context); |
| 2066 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2067 | while (**predicate == '[') { |
| 2068 | start = (*predicate)++; |
| 2069 | |
| 2070 | while (isspace(**predicate)) { |
| 2071 | ++(*predicate); |
| 2072 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2073 | 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] | 2074 | while (isspace(**predicate)) { |
| 2075 | ++(*predicate); |
| 2076 | } |
| 2077 | if (**predicate != '=') { |
| 2078 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2079 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2080 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2081 | goto cleanup; |
| 2082 | } |
| 2083 | ++(*predicate); |
| 2084 | while (isspace(**predicate)) { |
| 2085 | ++(*predicate); |
| 2086 | } |
| 2087 | |
| 2088 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2089 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2090 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2091 | goto cleanup; |
| 2092 | } |
| 2093 | --pke_end; |
| 2094 | while (isspace(*pke_end)) { |
| 2095 | --pke_end; |
| 2096 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2097 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2098 | /* localize path-key-expr */ |
| 2099 | pke_start = path_key_expr = *predicate; |
| 2100 | /* move after the current predicate */ |
| 2101 | *predicate = end + 1; |
| 2102 | |
| 2103 | /* source (must be leaf or leaf-list) */ |
| 2104 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2105 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2106 | if (!mod) { |
| 2107 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2108 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2109 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2110 | goto cleanup; |
| 2111 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2112 | if (!mod->implemented) { |
| 2113 | /* make the module implemented */ |
| 2114 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2115 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2116 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2117 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2118 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2119 | src_node = NULL; |
| 2120 | if (context_node->keys) { |
| 2121 | for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) { |
| 2122 | if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') { |
| 2123 | src_node = (const struct lysc_node*)context_node->keys[u]; |
| 2124 | break; |
| 2125 | } |
| 2126 | } |
| 2127 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2128 | if (!src_node) { |
| 2129 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2130 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2131 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2132 | goto cleanup; |
| 2133 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2134 | |
| 2135 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2136 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2137 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2138 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2139 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2140 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2141 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2142 | *predicate - start, start, src_node->name); |
| 2143 | goto cleanup; |
| 2144 | } |
| 2145 | |
| 2146 | /* destination */ |
| 2147 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2148 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2149 | |
| 2150 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2151 | if (strncmp(path_key_expr, "current", 7)) { |
| 2152 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2153 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2154 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2155 | *predicate - start, start); |
| 2156 | goto cleanup; |
| 2157 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2158 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2159 | if (*path_key_expr != '(') { |
| 2160 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2161 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2162 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2163 | if (*path_key_expr != ')') { |
| 2164 | goto error_current_function_invocation; |
| 2165 | } |
| 2166 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2167 | |
| 2168 | if (*path_key_expr != '/') { |
| 2169 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2170 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2171 | *predicate - start, start); |
| 2172 | goto cleanup; |
| 2173 | } |
| 2174 | ++path_key_expr; |
| 2175 | while (isspace(*path_key_expr)) { |
| 2176 | ++path_key_expr; |
| 2177 | } |
| 2178 | |
| 2179 | /* rel-path-keyexpr: |
| 2180 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2181 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2182 | ++dest_parent_times; |
| 2183 | path_key_expr += 2; |
| 2184 | while (isspace(*path_key_expr)) { |
| 2185 | ++path_key_expr; |
| 2186 | } |
| 2187 | if (*path_key_expr != '/') { |
| 2188 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2189 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2190 | *predicate - start, start); |
| 2191 | goto cleanup; |
| 2192 | } |
| 2193 | ++path_key_expr; |
| 2194 | while (isspace(*path_key_expr)) { |
| 2195 | ++path_key_expr; |
| 2196 | } |
| 2197 | |
| 2198 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2199 | * all schema nodes that cannot be instantiated in data tree */ |
| 2200 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2201 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2202 | *predicate - start, start); |
| 2203 | } |
| 2204 | if (!dest_parent_times) { |
| 2205 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2206 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2207 | *predicate - start, start); |
| 2208 | goto cleanup; |
| 2209 | } |
| 2210 | if (path_key_expr == pke_end) { |
| 2211 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2212 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2213 | *predicate - start, start); |
| 2214 | goto cleanup; |
| 2215 | } |
| 2216 | |
| 2217 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2218 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2219 | 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] | 2220 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2221 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2222 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2223 | goto cleanup; |
| 2224 | } |
| 2225 | |
| 2226 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2227 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2228 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2229 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2230 | } |
| 2231 | if (!mod) { |
| 2232 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2233 | "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] | 2234 | *predicate - start, start, dst_len, dst); |
| 2235 | goto cleanup; |
| 2236 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2237 | if (!mod->implemented) { |
| 2238 | /* make the module implemented */ |
| 2239 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2240 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2241 | |
| 2242 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2243 | if (!dst_node) { |
| 2244 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2245 | "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] | 2246 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2247 | goto cleanup; |
| 2248 | } |
| 2249 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2250 | 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] | 2251 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2252 | "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] | 2253 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2254 | goto cleanup; |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | ret = LY_SUCCESS; |
| 2259 | cleanup: |
| 2260 | ly_set_erase(&keys, NULL); |
| 2261 | return ret; |
| 2262 | } |
| 2263 | |
| 2264 | /** |
| 2265 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2266 | * |
| 2267 | * path-arg = absolute-path / relative-path |
| 2268 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2269 | * relative-path = 1*(".." "/") descendant-path |
| 2270 | * |
| 2271 | * @param[in,out] path Path to parse. |
| 2272 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2273 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2274 | * @param[out] name Name of the token. |
| 2275 | * @param[out] nam_len Length of the name. |
| 2276 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2277 | * must not be changed between consecutive calls. -1 if the |
| 2278 | * path is absolute. |
| 2279 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2280 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2281 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2282 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2283 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2284 | int *parent_times, int *has_predicate) |
| 2285 | { |
| 2286 | int par_times = 0; |
| 2287 | |
| 2288 | assert(path && *path); |
| 2289 | assert(parent_times); |
| 2290 | assert(prefix); |
| 2291 | assert(prefix_len); |
| 2292 | assert(name); |
| 2293 | assert(name_len); |
| 2294 | assert(has_predicate); |
| 2295 | |
| 2296 | *prefix = NULL; |
| 2297 | *prefix_len = 0; |
| 2298 | *name = NULL; |
| 2299 | *name_len = 0; |
| 2300 | *has_predicate = 0; |
| 2301 | |
| 2302 | if (!*parent_times) { |
| 2303 | if (!strncmp(*path, "..", 2)) { |
| 2304 | *path += 2; |
| 2305 | ++par_times; |
| 2306 | while (!strncmp(*path, "/..", 3)) { |
| 2307 | *path += 3; |
| 2308 | ++par_times; |
| 2309 | } |
| 2310 | } |
| 2311 | if (par_times) { |
| 2312 | *parent_times = par_times; |
| 2313 | } else { |
| 2314 | *parent_times = -1; |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | if (**path != '/') { |
| 2319 | return LY_EINVAL; |
| 2320 | } |
| 2321 | /* skip '/' */ |
| 2322 | ++(*path); |
| 2323 | |
| 2324 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2325 | 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] | 2326 | |
| 2327 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2328 | /* path continues by another token or this is the last token */ |
| 2329 | return LY_SUCCESS; |
| 2330 | } else if ((*path)[0] != '[') { |
| 2331 | /* unexpected character */ |
| 2332 | return LY_EINVAL; |
| 2333 | } else { |
| 2334 | /* predicate starting with [ */ |
| 2335 | *has_predicate = 1; |
| 2336 | return LY_SUCCESS; |
| 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2341 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2342 | * |
| 2343 | * The set of features used for target must be a subset of features used for the leafref. |
| 2344 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2345 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2346 | * |
| 2347 | * @param[in] refnode The leafref node. |
| 2348 | * @param[in] target Tha target node of the leafref. |
| 2349 | * @return LY_SUCCESS or LY_EVALID; |
| 2350 | */ |
| 2351 | static LY_ERR |
| 2352 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2353 | { |
| 2354 | LY_ERR ret = LY_EVALID; |
| 2355 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2356 | unsigned int u, v, count; |
| 2357 | struct ly_set features = {0}; |
| 2358 | |
| 2359 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2360 | if (iter->iffeatures) { |
| 2361 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2362 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2363 | 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] | 2364 | } |
| 2365 | } |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2370 | * So when adding features applicable to the target into the features set, we should not be |
| 2371 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2372 | * to the leafref itself. */ |
| 2373 | count = features.count; |
| 2374 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2375 | if (iter->iffeatures) { |
| 2376 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2377 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2378 | 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] | 2379 | /* new feature was added (or LY_EMEM) */ |
| 2380 | goto cleanup; |
| 2381 | } |
| 2382 | } |
| 2383 | } |
| 2384 | } |
| 2385 | } |
| 2386 | ret = LY_SUCCESS; |
| 2387 | |
| 2388 | cleanup: |
| 2389 | ly_set_erase(&features, NULL); |
| 2390 | return ret; |
| 2391 | } |
| 2392 | |
| 2393 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2394 | * @brief Validate the leafref path. |
| 2395 | * @param[in] ctx Compile context |
| 2396 | * @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] | 2397 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2398 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2399 | */ |
| 2400 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2401 | 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] | 2402 | { |
| 2403 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2404 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2405 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2406 | const char *id, *prefix, *name; |
| 2407 | size_t prefix_len, name_len; |
| 2408 | int parent_times = 0, has_predicate; |
| 2409 | unsigned int iter, u; |
| 2410 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2411 | char *logpath; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2412 | |
| 2413 | assert(ctx); |
| 2414 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2415 | assert(leafref); |
| 2416 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2417 | logpath = lysc_path(startnode, LY_PATH_LOG); |
| 2418 | strncpy(ctx->path, logpath, LYSC_CTX_BUFSIZE - 1); |
| 2419 | ctx->path_len = strlen(logpath); |
| 2420 | free(logpath); |
| 2421 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2422 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2423 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2424 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2425 | if (!iter) { /* first iteration */ |
| 2426 | /* precess ".." in relative paths */ |
| 2427 | if (parent_times > 0) { |
| 2428 | /* move from the context node */ |
| 2429 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2430 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2431 | * all schema nodes that cannot be instantiated in data tree */ |
| 2432 | 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] | 2433 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2439 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2440 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2441 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2442 | } |
| 2443 | if (!mod) { |
| 2444 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2445 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2446 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2447 | return LY_EVALID; |
| 2448 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2449 | if (!mod->implemented) { |
| 2450 | /* make the module implemented */ |
| 2451 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2452 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2453 | |
| 2454 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2455 | if (!node) { |
| 2456 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2457 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2458 | return LY_EVALID; |
| 2459 | } |
| 2460 | parent = node; |
| 2461 | |
| 2462 | if (has_predicate) { |
| 2463 | /* we have predicate, so the current result must be list */ |
| 2464 | if (node->nodetype != LYS_LIST) { |
| 2465 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2466 | "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] | 2467 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2468 | return LY_EVALID; |
| 2469 | } |
| 2470 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2471 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2472 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | ++iter; |
| 2476 | } |
| 2477 | if (ret) { |
| 2478 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2479 | "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] | 2480 | return LY_EVALID; |
| 2481 | } |
| 2482 | |
| 2483 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2484 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2485 | "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] | 2486 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2487 | return LY_EVALID; |
| 2488 | } |
| 2489 | |
| 2490 | /* check status */ |
| 2491 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2492 | return LY_EVALID; |
| 2493 | } |
| 2494 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2495 | /* check config */ |
| 2496 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2497 | if (node->flags & LYS_CONFIG_R) { |
| 2498 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2499 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2500 | leafref->path); |
| 2501 | return LY_EVALID; |
| 2502 | } |
| 2503 | } |
| 2504 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2505 | /* store the target's type and check for circular chain of leafrefs */ |
| 2506 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2507 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2508 | if (type == (struct lysc_type*)leafref) { |
| 2509 | /* circular chain detected */ |
| 2510 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2511 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2512 | return LY_EVALID; |
| 2513 | } |
| 2514 | } |
| 2515 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2516 | /* check if leafref and its target are under common if-features */ |
| 2517 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2518 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2519 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2520 | leafref->path); |
| 2521 | return LY_EVALID; |
| 2522 | } |
| 2523 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2524 | ctx->path_len = 1; |
| 2525 | ctx->path[1] = '\0'; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2526 | return LY_SUCCESS; |
| 2527 | } |
| 2528 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2529 | 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] | 2530 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2531 | /** |
| 2532 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2533 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2534 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2535 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2536 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2537 | * @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] | 2538 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2539 | * @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] | 2540 | * @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] | 2541 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2542 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2543 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2544 | * @return LY_ERR value. |
| 2545 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2546 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2547 | 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] | 2548 | 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] | 2549 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2550 | { |
| 2551 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2552 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2553 | struct lysc_type_bin *bin; |
| 2554 | struct lysc_type_num *num; |
| 2555 | struct lysc_type_str *str; |
| 2556 | struct lysc_type_bits *bits; |
| 2557 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2558 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2559 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2560 | struct lysc_type_union *un, *un_aux; |
| 2561 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2562 | |
| 2563 | switch (basetype) { |
| 2564 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2565 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2566 | |
| 2567 | /* 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] | 2568 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2569 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2570 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2571 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2572 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2573 | } |
| 2574 | } |
| 2575 | |
| 2576 | if (tpdfname) { |
| 2577 | type_p->compiled = *type; |
| 2578 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2579 | } |
| 2580 | break; |
| 2581 | case LY_TYPE_BITS: |
| 2582 | /* RFC 7950 9.7 - bits */ |
| 2583 | bits = (struct lysc_type_bits*)(*type); |
| 2584 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2585 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2586 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2587 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2588 | } |
| 2589 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2590 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2591 | /* 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] | 2592 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2593 | 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] | 2594 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2595 | 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] | 2596 | free(*type); |
| 2597 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2598 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2599 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | if (tpdfname) { |
| 2603 | type_p->compiled = *type; |
| 2604 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2605 | } |
| 2606 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2607 | case LY_TYPE_DEC64: |
| 2608 | dec = (struct lysc_type_dec*)(*type); |
| 2609 | |
| 2610 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2611 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2612 | if (!type_p->fraction_digits) { |
| 2613 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2614 | 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] | 2615 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2616 | 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] | 2617 | free(*type); |
| 2618 | *type = NULL; |
| 2619 | } |
| 2620 | return LY_EVALID; |
| 2621 | } |
| 2622 | } else if (type_p->fraction_digits) { |
| 2623 | /* 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] | 2624 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2625 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2626 | "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] | 2627 | tpdfname); |
| 2628 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2629 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2630 | "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] | 2631 | free(*type); |
| 2632 | *type = NULL; |
| 2633 | } |
| 2634 | return LY_EVALID; |
| 2635 | } |
| 2636 | dec->fraction_digits = type_p->fraction_digits; |
| 2637 | |
| 2638 | /* RFC 7950 9.2.4 - range */ |
| 2639 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2640 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2641 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2642 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2643 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2644 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | if (tpdfname) { |
| 2648 | type_p->compiled = *type; |
| 2649 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2650 | } |
| 2651 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2652 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2653 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2654 | |
| 2655 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2656 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2657 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2658 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2659 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2660 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2661 | } |
| 2662 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2663 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2664 | } |
| 2665 | |
| 2666 | /* RFC 7950 9.4.5 - pattern */ |
| 2667 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2668 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2669 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2670 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2671 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2672 | } |
| 2673 | |
| 2674 | if (tpdfname) { |
| 2675 | type_p->compiled = *type; |
| 2676 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2677 | } |
| 2678 | break; |
| 2679 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2680 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2681 | |
| 2682 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2683 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2684 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2685 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2686 | } |
| 2687 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2688 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2689 | /* 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] | 2690 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2691 | 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] | 2692 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2693 | 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] | 2694 | free(*type); |
| 2695 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2696 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2697 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | if (tpdfname) { |
| 2701 | type_p->compiled = *type; |
| 2702 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2703 | } |
| 2704 | break; |
| 2705 | case LY_TYPE_INT8: |
| 2706 | case LY_TYPE_UINT8: |
| 2707 | case LY_TYPE_INT16: |
| 2708 | case LY_TYPE_UINT16: |
| 2709 | case LY_TYPE_INT32: |
| 2710 | case LY_TYPE_UINT32: |
| 2711 | case LY_TYPE_INT64: |
| 2712 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2713 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2714 | |
| 2715 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2716 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2717 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2718 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2719 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2720 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | if (tpdfname) { |
| 2725 | type_p->compiled = *type; |
| 2726 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2727 | } |
| 2728 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2729 | case LY_TYPE_IDENT: |
| 2730 | idref = (struct lysc_type_identityref*)(*type); |
| 2731 | |
| 2732 | /* RFC 7950 9.10.2 - base */ |
| 2733 | if (type_p->bases) { |
| 2734 | if (base) { |
| 2735 | /* only the directly derived identityrefs can contain base specification */ |
| 2736 | if (tpdfname) { |
| 2737 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2738 | "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] | 2739 | tpdfname); |
| 2740 | } else { |
| 2741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2742 | "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] | 2743 | free(*type); |
| 2744 | *type = NULL; |
| 2745 | } |
| 2746 | return LY_EVALID; |
| 2747 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2748 | 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] | 2749 | } |
| 2750 | |
| 2751 | if (!base && !type_p->flags) { |
| 2752 | /* type derived from identityref built-in type must contain at least one base */ |
| 2753 | if (tpdfname) { |
| 2754 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2755 | } else { |
| 2756 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2757 | free(*type); |
| 2758 | *type = NULL; |
| 2759 | } |
| 2760 | return LY_EVALID; |
| 2761 | } |
| 2762 | |
| 2763 | if (tpdfname) { |
| 2764 | type_p->compiled = *type; |
| 2765 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2766 | } |
| 2767 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2768 | case LY_TYPE_LEAFREF: |
| 2769 | /* RFC 7950 9.9.3 - require-instance */ |
| 2770 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2771 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2772 | if (tpdfname) { |
| 2773 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2774 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2775 | } else { |
| 2776 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2777 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2778 | free(*type); |
| 2779 | *type = NULL; |
| 2780 | } |
| 2781 | return LY_EVALID; |
| 2782 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2783 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2784 | } else if (base) { |
| 2785 | /* inherit */ |
| 2786 | ((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] | 2787 | } else { |
| 2788 | /* default is true */ |
| 2789 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2790 | } |
| 2791 | if (type_p->path) { |
| 2792 | 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] | 2793 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2794 | } else if (base) { |
| 2795 | 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] | 2796 | ((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] | 2797 | } else if (tpdfname) { |
| 2798 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2799 | return LY_EVALID; |
| 2800 | } else { |
| 2801 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2802 | free(*type); |
| 2803 | *type = NULL; |
| 2804 | return LY_EVALID; |
| 2805 | } |
| 2806 | if (tpdfname) { |
| 2807 | type_p->compiled = *type; |
| 2808 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2809 | } |
| 2810 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2811 | case LY_TYPE_INST: |
| 2812 | /* RFC 7950 9.9.3 - require-instance */ |
| 2813 | if (type_p->flags & LYS_SET_REQINST) { |
| 2814 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2815 | } else { |
| 2816 | /* default is true */ |
| 2817 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2818 | } |
| 2819 | |
| 2820 | if (tpdfname) { |
| 2821 | type_p->compiled = *type; |
| 2822 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2823 | } |
| 2824 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2825 | case LY_TYPE_UNION: |
| 2826 | un = (struct lysc_type_union*)(*type); |
| 2827 | |
| 2828 | /* RFC 7950 7.4 - type */ |
| 2829 | if (type_p->types) { |
| 2830 | if (base) { |
| 2831 | /* only the directly derived union can contain types specification */ |
| 2832 | if (tpdfname) { |
| 2833 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2834 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2835 | tpdfname); |
| 2836 | } else { |
| 2837 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2838 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2839 | free(*type); |
| 2840 | *type = NULL; |
| 2841 | } |
| 2842 | return LY_EVALID; |
| 2843 | } |
| 2844 | /* compile the type */ |
| 2845 | additional = 0; |
| 2846 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 2847 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2848 | 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] | 2849 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2850 | /* add space for additional types from the union subtype */ |
| 2851 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 2852 | 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))); |
| 2853 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2854 | un->types = (void*)((uint32_t*)(p) + 1); |
| 2855 | |
| 2856 | /* copy subtypes of the subtype union */ |
| 2857 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 2858 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2859 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2860 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2861 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2862 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 2863 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 2864 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 2865 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2866 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 2867 | /* TODO extensions */ |
| 2868 | |
| 2869 | } else { |
| 2870 | un->types[u + additional] = un_aux->types[v]; |
| 2871 | ++un_aux->types[v]->refcount; |
| 2872 | } |
| 2873 | ++additional; |
| 2874 | LY_ARRAY_INCREMENT(un->types); |
| 2875 | } |
| 2876 | /* compensate u increment in main loop */ |
| 2877 | --additional; |
| 2878 | |
| 2879 | /* free the replaced union subtype */ |
| 2880 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2881 | } else { |
| 2882 | LY_ARRAY_INCREMENT(un->types); |
| 2883 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | if (!base && !type_p->flags) { |
| 2888 | /* type derived from union built-in type must contain at least one type */ |
| 2889 | if (tpdfname) { |
| 2890 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2891 | } else { |
| 2892 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2893 | free(*type); |
| 2894 | *type = NULL; |
| 2895 | } |
| 2896 | return LY_EVALID; |
| 2897 | } |
| 2898 | |
| 2899 | if (tpdfname) { |
| 2900 | type_p->compiled = *type; |
| 2901 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2902 | } |
| 2903 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2904 | case LY_TYPE_BOOL: |
| 2905 | case LY_TYPE_EMPTY: |
| 2906 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2907 | break; |
| 2908 | } |
| 2909 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2910 | done: |
| 2911 | return ret; |
| 2912 | } |
| 2913 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2914 | /** |
| 2915 | * @brief Compile information about the leaf/leaf-list's type. |
| 2916 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2917 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2918 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2919 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2920 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2921 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2922 | * @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] | 2923 | * @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] | 2924 | * @return LY_ERR value. |
| 2925 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2926 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2927 | 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] | 2928 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2929 | { |
| 2930 | LY_ERR ret = LY_SUCCESS; |
| 2931 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2932 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2933 | struct type_context { |
| 2934 | const struct lysp_tpdf *tpdf; |
| 2935 | struct lysp_node *node; |
| 2936 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2937 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2938 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2939 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2940 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2941 | const char *dflt = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2942 | |
| 2943 | (*type) = NULL; |
| 2944 | |
| 2945 | tctx = calloc(1, sizeof *tctx); |
| 2946 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2947 | 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] | 2948 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2949 | ret == LY_SUCCESS; |
| 2950 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2951 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2952 | if (basetype) { |
| 2953 | break; |
| 2954 | } |
| 2955 | |
| 2956 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2957 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2958 | 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] | 2959 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2960 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2961 | if (units && !*units) { |
| 2962 | /* inherit units */ |
| 2963 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2964 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2965 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2966 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2967 | dflt = tctx->tpdf->dflt; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2968 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2969 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2970 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2971 | break; |
| 2972 | } |
| 2973 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2974 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2975 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2976 | * 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] | 2977 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2978 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2979 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2980 | dummyloops = 1; |
| 2981 | goto preparenext; |
| 2982 | } else { |
| 2983 | tctx = NULL; |
| 2984 | break; |
| 2985 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2986 | } |
| 2987 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2988 | /* circular typedef reference detection */ |
| 2989 | for (u = 0; u < tpdf_chain.count; u++) { |
| 2990 | /* local part */ |
| 2991 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 2992 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2993 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2994 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2995 | free(tctx); |
| 2996 | ret = LY_EVALID; |
| 2997 | goto cleanup; |
| 2998 | } |
| 2999 | } |
| 3000 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3001 | /* global part for unions corner case */ |
| 3002 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3003 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3004 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3005 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3006 | free(tctx); |
| 3007 | ret = LY_EVALID; |
| 3008 | goto cleanup; |
| 3009 | } |
| 3010 | } |
| 3011 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3012 | /* store information for the following processing */ |
| 3013 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3014 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3015 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3016 | /* prepare next loop */ |
| 3017 | tctx_prev = tctx; |
| 3018 | tctx = calloc(1, sizeof *tctx); |
| 3019 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3020 | } |
| 3021 | free(tctx); |
| 3022 | |
| 3023 | /* allocate type according to the basetype */ |
| 3024 | switch (basetype) { |
| 3025 | case LY_TYPE_BINARY: |
| 3026 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3027 | break; |
| 3028 | case LY_TYPE_BITS: |
| 3029 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3030 | break; |
| 3031 | case LY_TYPE_BOOL: |
| 3032 | case LY_TYPE_EMPTY: |
| 3033 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3034 | break; |
| 3035 | case LY_TYPE_DEC64: |
| 3036 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3037 | break; |
| 3038 | case LY_TYPE_ENUM: |
| 3039 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3040 | break; |
| 3041 | case LY_TYPE_IDENT: |
| 3042 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3043 | break; |
| 3044 | case LY_TYPE_INST: |
| 3045 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3046 | break; |
| 3047 | case LY_TYPE_LEAFREF: |
| 3048 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3049 | break; |
| 3050 | case LY_TYPE_STRING: |
| 3051 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3052 | break; |
| 3053 | case LY_TYPE_UNION: |
| 3054 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3055 | break; |
| 3056 | case LY_TYPE_INT8: |
| 3057 | case LY_TYPE_UINT8: |
| 3058 | case LY_TYPE_INT16: |
| 3059 | case LY_TYPE_UINT16: |
| 3060 | case LY_TYPE_INT32: |
| 3061 | case LY_TYPE_UINT32: |
| 3062 | case LY_TYPE_INT64: |
| 3063 | case LY_TYPE_UINT64: |
| 3064 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3065 | break; |
| 3066 | case LY_TYPE_UNKNOWN: |
| 3067 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3068 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3069 | ret = LY_EVALID; |
| 3070 | goto cleanup; |
| 3071 | } |
| 3072 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3073 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3074 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3075 | ly_data_type2str[basetype]); |
| 3076 | free(*type); |
| 3077 | (*type) = NULL; |
| 3078 | ret = LY_EVALID; |
| 3079 | goto cleanup; |
| 3080 | } |
| 3081 | |
| 3082 | /* get restrictions from the referred typedefs */ |
| 3083 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3084 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3085 | |
| 3086 | /* remember the typedef context for circular check */ |
| 3087 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3088 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3089 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3090 | base = tctx->tpdf->type.compiled; |
| 3091 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3092 | } 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] | 3093 | /* no change, just use the type information from the base */ |
| 3094 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3095 | ++base->refcount; |
| 3096 | continue; |
| 3097 | } |
| 3098 | |
| 3099 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3100 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3101 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3102 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3103 | ret = LY_EVALID; |
| 3104 | goto cleanup; |
| 3105 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3106 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3107 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3108 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3109 | ret = LY_EVALID; |
| 3110 | goto cleanup; |
| 3111 | } |
| 3112 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3113 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3114 | /* TODO user type plugins */ |
| 3115 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3116 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3117 | 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] | 3118 | 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] | 3119 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3120 | LY_CHECK_GOTO(ret, cleanup); |
| 3121 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3122 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3123 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3124 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3125 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3126 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3127 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3128 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3129 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3130 | /* TODO user type plugins */ |
| 3131 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3132 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3133 | 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] | 3134 | LY_CHECK_GOTO(ret, cleanup); |
| 3135 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3136 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3137 | free(*type); |
| 3138 | (*type) = base; |
| 3139 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3140 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3141 | if (!(*type)->dflt) { |
| 3142 | DUP_STRING(ctx->ctx, dflt, (*type)->dflt); |
| 3143 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3144 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3145 | COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3146 | |
| 3147 | cleanup: |
| 3148 | ly_set_erase(&tpdf_chain, free); |
| 3149 | return ret; |
| 3150 | } |
| 3151 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3152 | /** |
| 3153 | * @brief Compile status information of the given node. |
| 3154 | * |
| 3155 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3156 | * has the status correctly set during the compilation. |
| 3157 | * |
| 3158 | * @param[in] ctx Compile context |
| 3159 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3160 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3161 | * the compatibility with the parent's status value. |
| 3162 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3163 | * @return LY_ERR value. |
| 3164 | */ |
| 3165 | static LY_ERR |
| 3166 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3167 | { |
| 3168 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3169 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3170 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3171 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3172 | if ((parent_flags & 0x3) != 0x3) { |
| 3173 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3174 | * combination of bits (0x3) which marks the uses_status value */ |
| 3175 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3176 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3177 | } |
| 3178 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3179 | } else { |
| 3180 | (*node_flags) |= LYS_STATUS_CURR; |
| 3181 | } |
| 3182 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3183 | /* check status compatibility with the parent */ |
| 3184 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3185 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3186 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3187 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3188 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3189 | } else { /* LYS_STATUS_DEPRC */ |
| 3190 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3191 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3192 | } |
| 3193 | return LY_EVALID; |
| 3194 | } |
| 3195 | } |
| 3196 | return LY_SUCCESS; |
| 3197 | } |
| 3198 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3199 | /** |
| 3200 | * @brief Check uniqness of the node/action/notification name. |
| 3201 | * |
| 3202 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3203 | * structures, but they share the namespace so we need to check their name collisions. |
| 3204 | * |
| 3205 | * @param[in] ctx Compile context. |
| 3206 | * @param[in] children List (linked list) of data nodes to go through. |
| 3207 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3208 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3209 | * @param[in] name Name of the item to find in the given lists. |
| 3210 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3211 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3212 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3213 | */ |
| 3214 | static LY_ERR |
| 3215 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3216 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3217 | const char *name, void *exclude) |
| 3218 | { |
| 3219 | const struct lysc_node *iter; |
| 3220 | unsigned int u; |
| 3221 | |
| 3222 | LY_LIST_FOR(children, iter) { |
| 3223 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3224 | goto error; |
| 3225 | } |
| 3226 | } |
| 3227 | LY_ARRAY_FOR(actions, u) { |
| 3228 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3229 | goto error; |
| 3230 | } |
| 3231 | } |
| 3232 | LY_ARRAY_FOR(notifs, u) { |
| 3233 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3234 | goto error; |
| 3235 | } |
| 3236 | } |
| 3237 | return LY_SUCCESS; |
| 3238 | error: |
| 3239 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification"); |
| 3240 | return LY_EEXIST; |
| 3241 | } |
| 3242 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3243 | 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] | 3244 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3245 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3246 | * @brief Compile parsed RPC/action schema node information. |
| 3247 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3248 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3249 | * @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] | 3250 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3251 | * @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). |
| 3252 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3253 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3254 | */ |
| 3255 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3256 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3257 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3258 | { |
| 3259 | LY_ERR ret = LY_SUCCESS; |
| 3260 | struct lysp_node *child_p; |
| 3261 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3262 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3263 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3264 | lysc_update_path(ctx, parent, action_p->name); |
| 3265 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3266 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3267 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3268 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3269 | action_p->name, action)) { |
| 3270 | return LY_EVALID; |
| 3271 | } |
| 3272 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3273 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3274 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3275 | "Action \"%s\" is placed inside %s.", action_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3276 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3277 | return LY_EVALID; |
| 3278 | } |
| 3279 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3280 | action->nodetype = LYS_ACTION; |
| 3281 | action->module = ctx->mod; |
| 3282 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3283 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3284 | action->sp = action_p; |
| 3285 | } |
| 3286 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3287 | |
| 3288 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3289 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3290 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3291 | |
| 3292 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3293 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3294 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3295 | COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
| 3296 | COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3297 | |
| 3298 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3299 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3300 | COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup); |
| 3301 | COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup); |
| 3302 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3303 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3304 | 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] | 3305 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3306 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3307 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3308 | |
| 3309 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3310 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3311 | COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup); |
| 3312 | COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup); |
| 3313 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3314 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3315 | 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] | 3316 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3317 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3318 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3319 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3320 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3321 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3322 | return ret; |
| 3323 | } |
| 3324 | |
| 3325 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3326 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3327 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3328 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3329 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3330 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3331 | * @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] | 3332 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3333 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3334 | */ |
| 3335 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3336 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3337 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3338 | { |
| 3339 | LY_ERR ret = LY_SUCCESS; |
| 3340 | struct lysp_node *child_p; |
| 3341 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3342 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3343 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3344 | lysc_update_path(ctx, parent, notif_p->name); |
| 3345 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3346 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3347 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3348 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3349 | notif_p->name, notif)) { |
| 3350 | return LY_EVALID; |
| 3351 | } |
| 3352 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3353 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3354 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3355 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3356 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3357 | return LY_EVALID; |
| 3358 | } |
| 3359 | |
| 3360 | notif->nodetype = LYS_NOTIF; |
| 3361 | notif->module = ctx->mod; |
| 3362 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3363 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3364 | notif->sp = notif_p; |
| 3365 | } |
| 3366 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3367 | |
| 3368 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3369 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3370 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3371 | |
| 3372 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3373 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3374 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3375 | COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
| 3376 | COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup); |
| 3377 | COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3378 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3379 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3380 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3381 | 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] | 3382 | } |
| 3383 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3384 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3385 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3386 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3387 | return ret; |
| 3388 | } |
| 3389 | |
| 3390 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3391 | * @brief Compile parsed container node information. |
| 3392 | * @param[in] ctx Compile context |
| 3393 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3394 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3395 | * is enriched with the container-specific information. |
| 3396 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3397 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3398 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3399 | 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] | 3400 | { |
| 3401 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3402 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3403 | struct lysp_node *child_p; |
| 3404 | unsigned int u; |
| 3405 | LY_ERR ret = LY_SUCCESS; |
| 3406 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3407 | if (cont_p->presence) { |
| 3408 | cont->flags |= LYS_PRESENCE; |
| 3409 | } |
| 3410 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3411 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3412 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3413 | } |
| 3414 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3415 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done); |
| 3416 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3417 | 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] | 3418 | |
| 3419 | done: |
| 3420 | return ret; |
| 3421 | } |
| 3422 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3423 | /* |
| 3424 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3425 | * @param[in] ctx Compile context. |
| 3426 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3427 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3428 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3429 | * @return LY_ERR value. |
| 3430 | */ |
| 3431 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3432 | 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] | 3433 | { |
| 3434 | unsigned int u, v; |
| 3435 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3436 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3437 | 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] | 3438 | leaf->units ? NULL : &leaf->units)); |
| 3439 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3440 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
| 3441 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
| 3442 | DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3443 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3444 | } |
| 3445 | } else { |
| 3446 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
| 3447 | DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3448 | } |
| 3449 | } |
| 3450 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3451 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3452 | ly_set_add(&ctx->unres, leaf, 0); |
| 3453 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3454 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3455 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3456 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3457 | ly_set_add(&ctx->unres, leaf, 0); |
| 3458 | } |
| 3459 | } |
| 3460 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
| 3461 | if (leaf->flags & LYS_SET_DFLT) { |
| 3462 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).", |
| 3463 | leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt); |
| 3464 | return LY_EVALID; |
| 3465 | } |
| 3466 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3467 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3468 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3469 | return LY_EVALID; |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3474 | /* configuration data values must be unique - so check the default values */ |
| 3475 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3476 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3477 | if (!strcmp(llist->dflts[u], llist->dflts[v])) { |
| 3478 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3479 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]); |
| 3480 | return LY_EVALID; |
| 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | } |
| 3485 | |
| 3486 | /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */ |
| 3487 | |
| 3488 | return LY_SUCCESS; |
| 3489 | } |
| 3490 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3491 | /** |
| 3492 | * @brief Compile parsed leaf node information. |
| 3493 | * @param[in] ctx Compile context |
| 3494 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3495 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3496 | * is enriched with the leaf-specific information. |
| 3497 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3498 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3499 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3500 | 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] | 3501 | { |
| 3502 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3503 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3504 | unsigned int u; |
| 3505 | LY_ERR ret = LY_SUCCESS; |
| 3506 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3507 | COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3508 | if (leaf_p->units) { |
| 3509 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3510 | leaf->flags |= LYS_SET_UNITS; |
| 3511 | } |
| 3512 | if (leaf_p->dflt) { |
| 3513 | leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3514 | leaf->flags |= LYS_SET_DFLT; |
| 3515 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3516 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3517 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3518 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3519 | done: |
| 3520 | return ret; |
| 3521 | } |
| 3522 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3523 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3524 | * @brief Compile parsed leaf-list node information. |
| 3525 | * @param[in] ctx Compile context |
| 3526 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3527 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3528 | * is enriched with the leaf-list-specific information. |
| 3529 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3530 | */ |
| 3531 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3532 | 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] | 3533 | { |
| 3534 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3535 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3536 | unsigned int u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3537 | LY_ERR ret = LY_SUCCESS; |
| 3538 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3539 | COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3540 | if (llist_p->units) { |
| 3541 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3542 | llist->flags |= LYS_SET_UNITS; |
| 3543 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3544 | |
| 3545 | if (llist_p->dflts) { |
| 3546 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3547 | LY_ARRAY_FOR(llist_p->dflts, u) { |
| 3548 | DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]); |
| 3549 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3550 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3551 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3555 | if (llist->min) { |
| 3556 | llist->flags |= LYS_MAND_TRUE; |
| 3557 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3558 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3559 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3560 | 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] | 3561 | |
| 3562 | done: |
| 3563 | return ret; |
| 3564 | } |
| 3565 | |
| 3566 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3567 | * @brief Compile information about list's uniques. |
| 3568 | * @param[in] ctx Compile context. |
| 3569 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3570 | * @param[in] uniques Sized array list of unique statements. |
| 3571 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3572 | * @return LY_ERR value. |
| 3573 | */ |
| 3574 | static LY_ERR |
| 3575 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3576 | { |
| 3577 | LY_ERR ret = LY_SUCCESS; |
| 3578 | struct lysc_node_leaf **key, ***unique; |
| 3579 | const char *keystr, *delim; |
| 3580 | size_t len; |
| 3581 | unsigned int v; |
| 3582 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3583 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3584 | |
| 3585 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3586 | config = -1; |
| 3587 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3588 | keystr = uniques[v]; |
| 3589 | while (keystr) { |
| 3590 | delim = strpbrk(keystr, " \t\n"); |
| 3591 | if (delim) { |
| 3592 | len = delim - keystr; |
| 3593 | while (isspace(*delim)) { |
| 3594 | ++delim; |
| 3595 | } |
| 3596 | } else { |
| 3597 | len = strlen(keystr); |
| 3598 | } |
| 3599 | |
| 3600 | /* unique node must be present */ |
| 3601 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3602 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3603 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3604 | if (ret != LY_SUCCESS) { |
| 3605 | if (ret == LY_EDENIED) { |
| 3606 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3607 | "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] | 3608 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3609 | } |
| 3610 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3611 | } else if (flags) { |
| 3612 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3613 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3614 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
| 3615 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3616 | } |
| 3617 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3618 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3619 | /* all referenced leafs must be of the same config type */ |
| 3620 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3621 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3622 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3623 | return LY_EVALID; |
| 3624 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3625 | config = 1; |
| 3626 | } else { /* LYS_CONFIG_R */ |
| 3627 | config = 0; |
| 3628 | } |
| 3629 | |
| 3630 | /* check status */ |
| 3631 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3632 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3633 | |
| 3634 | /* mark leaf as unique */ |
| 3635 | (*key)->flags |= LYS_UNIQUE; |
| 3636 | |
| 3637 | /* next unique value in line */ |
| 3638 | keystr = delim; |
| 3639 | } |
| 3640 | /* next unique definition */ |
| 3641 | } |
| 3642 | |
| 3643 | return LY_SUCCESS; |
| 3644 | } |
| 3645 | |
| 3646 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3647 | * @brief Compile parsed list node information. |
| 3648 | * @param[in] ctx Compile context |
| 3649 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3650 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3651 | * is enriched with the list-specific information. |
| 3652 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3653 | */ |
| 3654 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3655 | 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] | 3656 | { |
| 3657 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3658 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3659 | struct lysp_node *child_p; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3660 | struct lysc_node_leaf **key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3661 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3662 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3663 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3664 | LY_ERR ret = LY_SUCCESS; |
| 3665 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3666 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3667 | if (list->min) { |
| 3668 | list->flags |= LYS_MAND_TRUE; |
| 3669 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3670 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3671 | |
| 3672 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3673 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3674 | } |
| 3675 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3676 | COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3677 | |
| 3678 | /* keys */ |
| 3679 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3680 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3681 | return LY_EVALID; |
| 3682 | } |
| 3683 | |
| 3684 | /* find all the keys (must be direct children) */ |
| 3685 | keystr = list_p->key; |
| 3686 | while (keystr) { |
| 3687 | delim = strpbrk(keystr, " \t\n"); |
| 3688 | if (delim) { |
| 3689 | len = delim - keystr; |
| 3690 | while (isspace(*delim)) { |
| 3691 | ++delim; |
| 3692 | } |
| 3693 | } else { |
| 3694 | len = strlen(keystr); |
| 3695 | } |
| 3696 | |
| 3697 | /* key node must be present */ |
| 3698 | LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM); |
| 3699 | *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 3700 | if (!(*key)) { |
| 3701 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3702 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3703 | return LY_EVALID; |
| 3704 | } |
| 3705 | /* keys must be unique */ |
| 3706 | for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) { |
| 3707 | if (*key == list->keys[u]) { |
| 3708 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3709 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3710 | return LY_EVALID; |
| 3711 | } |
| 3712 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3713 | lysc_update_path(ctx, (struct lysc_node*)list, (*key)->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3714 | /* key must have the same config flag as the list itself */ |
| 3715 | if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) { |
| 3716 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3717 | return LY_EVALID; |
| 3718 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3719 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3720 | /* YANG 1.0 denies key to be of empty type */ |
| 3721 | if ((*key)->type->basetype == LY_TYPE_EMPTY) { |
| 3722 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3723 | "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] | 3724 | return LY_EVALID; |
| 3725 | } |
| 3726 | } else { |
| 3727 | /* when and if-feature are illegal on list keys */ |
| 3728 | if ((*key)->when) { |
| 3729 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3730 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3731 | return LY_EVALID; |
| 3732 | } |
| 3733 | if ((*key)->iffeatures) { |
| 3734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3735 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3736 | return LY_EVALID; |
| 3737 | } |
| 3738 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3739 | |
| 3740 | /* check status */ |
| 3741 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3742 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3743 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3744 | /* ignore default values of the key */ |
| 3745 | if ((*key)->dflt) { |
| 3746 | lydict_remove(ctx->ctx, (*key)->dflt); |
| 3747 | (*key)->dflt = NULL; |
| 3748 | } |
| 3749 | /* mark leaf as key */ |
| 3750 | (*key)->flags |= LYS_KEY; |
| 3751 | |
| 3752 | /* next key value */ |
| 3753 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3754 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | /* uniques */ |
| 3758 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3759 | 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] | 3760 | } |
| 3761 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3762 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3763 | 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] | 3764 | |
| 3765 | done: |
| 3766 | return ret; |
| 3767 | } |
| 3768 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3769 | /** |
| 3770 | * @brief Do some checks and set the default choice's case. |
| 3771 | * |
| 3772 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3773 | * |
| 3774 | * @param[in] ctx Compile context. |
| 3775 | * @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, |
| 3776 | * not the reference to the imported module. |
| 3777 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3778 | * @return LY_ERR value. |
| 3779 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3780 | static LY_ERR |
| 3781 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3782 | { |
| 3783 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3784 | const char *prefix = NULL, *name; |
| 3785 | size_t prefix_len = 0; |
| 3786 | |
| 3787 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3788 | name = strchr(dflt, ':'); |
| 3789 | if (name) { |
| 3790 | prefix = dflt; |
| 3791 | prefix_len = name - prefix; |
| 3792 | ++name; |
| 3793 | } else { |
| 3794 | name = dflt; |
| 3795 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3796 | 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] | 3797 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3798 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3799 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3800 | prefix_len, prefix); |
| 3801 | return LY_EVALID; |
| 3802 | } |
| 3803 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3804 | if (!ch->dflt) { |
| 3805 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3806 | "Default case \"%s\" not found.", dflt); |
| 3807 | return LY_EVALID; |
| 3808 | } |
| 3809 | /* no mandatory nodes directly under the default case */ |
| 3810 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3811 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3812 | break; |
| 3813 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3814 | if (iter->flags & LYS_MAND_TRUE) { |
| 3815 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3816 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3817 | return LY_EVALID; |
| 3818 | } |
| 3819 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3820 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3821 | return LY_SUCCESS; |
| 3822 | } |
| 3823 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3824 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3825 | 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] | 3826 | { |
| 3827 | struct lys_module *mod; |
| 3828 | const char *prefix = NULL, *name; |
| 3829 | size_t prefix_len = 0; |
| 3830 | struct lysc_node_case *cs; |
| 3831 | struct lysc_node *node; |
| 3832 | |
| 3833 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3834 | name = strchr(dflt, ':'); |
| 3835 | if (name) { |
| 3836 | prefix = dflt; |
| 3837 | prefix_len = name - prefix; |
| 3838 | ++name; |
| 3839 | } else { |
| 3840 | name = dflt; |
| 3841 | } |
| 3842 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3843 | if (prefix) { |
| 3844 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3845 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3846 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3847 | "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] | 3848 | return LY_EVALID; |
| 3849 | } |
| 3850 | } else { |
| 3851 | mod = ctx->mod; |
| 3852 | } |
| 3853 | /* get the default case */ |
| 3854 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3855 | if (!cs) { |
| 3856 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3857 | "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] | 3858 | return LY_EVALID; |
| 3859 | } |
| 3860 | |
| 3861 | /* check that there is no mandatory node */ |
| 3862 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3863 | if (node->parent != (struct lysc_node*)cs) { |
| 3864 | break; |
| 3865 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3866 | if (node->flags & LYS_MAND_TRUE) { |
| 3867 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3868 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3869 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3870 | return LY_EVALID; |
| 3871 | } |
| 3872 | } |
| 3873 | |
| 3874 | /* set the default case in choice */ |
| 3875 | ch->dflt = cs; |
| 3876 | cs->flags |= LYS_SET_DFLT; |
| 3877 | |
| 3878 | return LY_SUCCESS; |
| 3879 | } |
| 3880 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3881 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3882 | * @brief Compile parsed choice node information. |
| 3883 | * @param[in] ctx Compile context |
| 3884 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3885 | * @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] | 3886 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3887 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3888 | */ |
| 3889 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3890 | 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] | 3891 | { |
| 3892 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 3893 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 3894 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3895 | LY_ERR ret = LY_SUCCESS; |
| 3896 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3897 | LY_LIST_FOR(ch_p->child, child_p) { |
| 3898 | if (child_p->nodetype == LYS_CASE) { |
| 3899 | 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] | 3900 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3901 | } |
| 3902 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3903 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3904 | } |
| 3905 | } |
| 3906 | |
| 3907 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3908 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3909 | 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] | 3910 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3911 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3912 | return ret; |
| 3913 | } |
| 3914 | |
| 3915 | /** |
| 3916 | * @brief Compile parsed anydata or anyxml node information. |
| 3917 | * @param[in] ctx Compile context |
| 3918 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3919 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3920 | * is enriched with the any-specific information. |
| 3921 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3922 | */ |
| 3923 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3924 | 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] | 3925 | { |
| 3926 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 3927 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 3928 | unsigned int u; |
| 3929 | LY_ERR ret = LY_SUCCESS; |
| 3930 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3931 | COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3932 | |
| 3933 | if (any->flags & LYS_CONFIG_W) { |
| 3934 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
| 3935 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML)); |
| 3936 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3937 | done: |
| 3938 | return ret; |
| 3939 | } |
| 3940 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3941 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3942 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 3943 | * |
| 3944 | * @param[in] ctx Compile context |
| 3945 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 3946 | * the choice itself is expected instead of a specific case node. |
| 3947 | * @param[in] node Schema node to connect into the list. |
| 3948 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 3949 | */ |
| 3950 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3951 | 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] | 3952 | { |
| 3953 | struct lysc_node **children; |
| 3954 | |
| 3955 | if (node->nodetype == LYS_CASE) { |
| 3956 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 3957 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3958 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3959 | } |
| 3960 | if (children) { |
| 3961 | if (!(*children)) { |
| 3962 | /* first child */ |
| 3963 | *children = node; |
| 3964 | } else if (*children != node) { |
| 3965 | /* by the condition in previous branch we cover the choice/case children |
| 3966 | * - the children list is shared by the choice and the the first case, in addition |
| 3967 | * the first child of each case must be referenced from the case node. So the node is |
| 3968 | * actually always already inserted in case it is the first children - so here such |
| 3969 | * a situation actually corresponds to the first branch */ |
| 3970 | /* insert at the end of the parent's children list */ |
| 3971 | (*children)->prev->next = node; |
| 3972 | node->prev = (*children)->prev; |
| 3973 | (*children)->prev = node; |
| 3974 | |
| 3975 | /* check the name uniqueness */ |
| 3976 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 3977 | lysc_node_notifs(parent), node->name, node)) { |
| 3978 | return LY_EEXIST; |
| 3979 | } |
| 3980 | } |
| 3981 | } |
| 3982 | return LY_SUCCESS; |
| 3983 | } |
| 3984 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3985 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3986 | * @brief Get the XPath context node for the given schema node. |
| 3987 | * @param[in] start The schema node where the XPath expression appears. |
| 3988 | * @return The context node to evaluate XPath expression in given schema node. |
| 3989 | * @return NULL in case the context node is the root node. |
| 3990 | */ |
| 3991 | static struct lysc_node * |
| 3992 | lysc_xpath_context(struct lysc_node *start) |
| 3993 | { |
| 3994 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 3995 | start = start->parent); |
| 3996 | return start; |
| 3997 | } |
| 3998 | |
| 3999 | /** |
| 4000 | * @brief Prepare the case structure in choice node for the new data node. |
| 4001 | * |
| 4002 | * 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 |
| 4003 | * created in the choice when the first child was processed. |
| 4004 | * |
| 4005 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4006 | * @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, |
| 4007 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4008 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4009 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4010 | * @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, |
| 4011 | * 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] | 4012 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4013 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4014 | 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] | 4015 | { |
| 4016 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4017 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4018 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4019 | unsigned int u; |
| 4020 | LY_ERR ret; |
| 4021 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4022 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4023 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4024 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4025 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4026 | return NULL; \ |
| 4027 | } \ |
| 4028 | } |
| 4029 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4030 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4031 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4032 | |
| 4033 | /* we have to add an implicit case node into the parent choice */ |
| 4034 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4035 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 4036 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4037 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4038 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4039 | /* the case is already present since the child is not its first children */ |
| 4040 | return (struct lysc_node_case*)ch->cases->prev; |
| 4041 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4042 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4043 | |
| 4044 | /* explicit parent case is not present (this is its first child) */ |
| 4045 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4046 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 4047 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4048 | cs->sp = node_p; |
| 4049 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4050 | /* 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] | 4051 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4052 | |
| 4053 | if (node_p->when) { |
| 4054 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4055 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4056 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4057 | (*when)->context = lysc_xpath_context(ch->parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4058 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4059 | 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] | 4060 | } else { |
| 4061 | LOGINT(ctx->ctx); |
| 4062 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4063 | } |
| 4064 | cs->module = ctx->mod; |
| 4065 | cs->prev = (struct lysc_node*)cs; |
| 4066 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4067 | 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] | 4068 | cs->parent = (struct lysc_node*)ch; |
| 4069 | cs->child = child; |
| 4070 | |
| 4071 | return cs; |
| 4072 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4073 | if (cs) { |
| 4074 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4075 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4076 | return NULL; |
| 4077 | |
| 4078 | #undef UNIQUE_CHECK |
| 4079 | } |
| 4080 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4081 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4082 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4083 | * |
| 4084 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4085 | * @param[in] node Target node where the config is supposed to be changed. |
| 4086 | * @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] | 4087 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4088 | * 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] | 4089 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4090 | * @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] | 4091 | * @return LY_ERR value. |
| 4092 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4093 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4094 | 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] | 4095 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4096 | { |
| 4097 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4098 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4099 | |
| 4100 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4101 | /* nothing to do */ |
| 4102 | return LY_SUCCESS; |
| 4103 | } |
| 4104 | |
| 4105 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4106 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4107 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4108 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4109 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4110 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4111 | return LY_EVALID; |
| 4112 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4113 | node->flags |= LYS_SET_CONFIG; |
| 4114 | } else { |
| 4115 | if (node->flags & LYS_SET_CONFIG) { |
| 4116 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4117 | /* setting config flags, but have node with explicit config true */ |
| 4118 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4119 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4120 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4121 | return LY_EVALID; |
| 4122 | } |
| 4123 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4124 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4125 | return LY_SUCCESS; |
| 4126 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4127 | } |
| 4128 | node->flags &= ~LYS_CONFIG_MASK; |
| 4129 | node->flags |= config; |
| 4130 | |
| 4131 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4132 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4133 | 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] | 4134 | } |
| 4135 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4136 | return LY_SUCCESS; |
| 4137 | } |
| 4138 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4139 | /** |
| 4140 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4141 | * |
| 4142 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4143 | * the flag to such parents from a mandatory children. |
| 4144 | * |
| 4145 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4146 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4147 | * (mandatory children was removed). |
| 4148 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4149 | void |
| 4150 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4151 | { |
| 4152 | struct lysc_node *iter; |
| 4153 | |
| 4154 | if (add) { /* set flag */ |
| 4155 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4156 | parent = parent->parent) { |
| 4157 | parent->flags |= LYS_MAND_TRUE; |
| 4158 | } |
| 4159 | } else { /* unset flag */ |
| 4160 | 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] | 4161 | 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] | 4162 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4163 | /* there is another mandatory node */ |
| 4164 | return; |
| 4165 | } |
| 4166 | } |
| 4167 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4168 | parent->flags &= ~LYS_MAND_TRUE; |
| 4169 | } |
| 4170 | } |
| 4171 | } |
| 4172 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4173 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4174 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4175 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4176 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4177 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4178 | */ |
| 4179 | static void |
| 4180 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4181 | { |
| 4182 | unsigned int v; |
| 4183 | size_t len; |
| 4184 | |
| 4185 | len = strlen(aug_p->nodeid); |
| 4186 | LY_ARRAY_FOR(result, v) { |
| 4187 | if (strlen(result[v]->nodeid) <= len) { |
| 4188 | continue; |
| 4189 | } |
| 4190 | if (v < LY_ARRAY_SIZE(result)) { |
| 4191 | /* move the rest of array */ |
| 4192 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4193 | break; |
| 4194 | } |
| 4195 | } |
| 4196 | result[v] = aug_p; |
| 4197 | LY_ARRAY_INCREMENT(result); |
| 4198 | } |
| 4199 | |
| 4200 | /** |
| 4201 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4202 | * |
| 4203 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4204 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4205 | * |
| 4206 | * @param[in] ctx Compile context. |
| 4207 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4208 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4209 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4210 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4211 | * @return LY_ERR value. |
| 4212 | */ |
| 4213 | LY_ERR |
| 4214 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4215 | { |
| 4216 | struct lysp_augment **result = NULL; |
| 4217 | unsigned int u, v; |
| 4218 | size_t count = 0; |
| 4219 | |
| 4220 | assert(augments); |
| 4221 | |
| 4222 | /* get count of the augments in module and all its submodules */ |
| 4223 | if (aug_p) { |
| 4224 | count += LY_ARRAY_SIZE(aug_p); |
| 4225 | } |
| 4226 | LY_ARRAY_FOR(inc_p, u) { |
| 4227 | if (inc_p[u].submodule->augments) { |
| 4228 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4229 | } |
| 4230 | } |
| 4231 | |
| 4232 | if (!count) { |
| 4233 | *augments = NULL; |
| 4234 | return LY_SUCCESS; |
| 4235 | } |
| 4236 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4237 | |
| 4238 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4239 | * together, so there can be even /z/y betwwen them. */ |
| 4240 | LY_ARRAY_FOR(aug_p, u) { |
| 4241 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4242 | } |
| 4243 | LY_ARRAY_FOR(inc_p, u) { |
| 4244 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4245 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4246 | } |
| 4247 | } |
| 4248 | |
| 4249 | *augments = result; |
| 4250 | return LY_SUCCESS; |
| 4251 | } |
| 4252 | |
| 4253 | /** |
| 4254 | * @brief Compile the parsed augment connecting it into its target. |
| 4255 | * |
| 4256 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4257 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4258 | * are already implemented and compiled. |
| 4259 | * |
| 4260 | * @param[in] ctx Compile context. |
| 4261 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4262 | * @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 |
| 4263 | * children in case of the augmenting uses data. |
| 4264 | * @return LY_SUCCESS on success. |
| 4265 | * @return LY_EVALID on failure. |
| 4266 | */ |
| 4267 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4268 | 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] | 4269 | { |
| 4270 | LY_ERR ret = LY_SUCCESS; |
| 4271 | struct lysp_node *node_p, *case_node_p; |
| 4272 | struct lysc_node *target; /* target target of the augment */ |
| 4273 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4274 | struct lysc_when **when, *when_shared; |
| 4275 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4276 | uint16_t flags = 0; |
| 4277 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4278 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4279 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4280 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4281 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4282 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4283 | 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] | 4284 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4285 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4286 | if (ret != LY_SUCCESS) { |
| 4287 | if (ret == LY_EDENIED) { |
| 4288 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4289 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4290 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4291 | } |
| 4292 | return LY_EVALID; |
| 4293 | } |
| 4294 | |
| 4295 | /* check for mandatory nodes |
| 4296 | * - new cases augmenting some choice can have mandatory nodes |
| 4297 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4298 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4299 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4300 | allow_mandatory = 1; |
| 4301 | } |
| 4302 | |
| 4303 | when_shared = NULL; |
| 4304 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4305 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4306 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4307 | && !((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] | 4308 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4309 | && !(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] | 4310 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4311 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4312 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4313 | return LY_EVALID; |
| 4314 | } |
| 4315 | |
| 4316 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4317 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4318 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4319 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4320 | } else { |
| 4321 | 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] | 4322 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4323 | } |
| 4324 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4325 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4326 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4327 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4328 | * 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] | 4329 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4330 | /* 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] | 4331 | 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] | 4332 | } else if (target->nodetype == LYS_CHOICE) { |
| 4333 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4334 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4335 | } else { |
| 4336 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4337 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4338 | } |
| 4339 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4340 | 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] | 4341 | node->flags &= ~LYS_MAND_TRUE; |
| 4342 | lys_compile_mandatory_parents(target, 0); |
| 4343 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4344 | "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] | 4345 | return LY_EVALID; |
| 4346 | } |
| 4347 | |
| 4348 | /* pass augment's when to all the children */ |
| 4349 | if (aug_p->when) { |
| 4350 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4351 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4352 | ret = lys_compile_when(ctx, aug_p->when, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4353 | LY_CHECK_GOTO(ret, error); |
| 4354 | (*when)->context = lysc_xpath_context(target); |
| 4355 | when_shared = *when; |
| 4356 | } else { |
| 4357 | ++when_shared->refcount; |
| 4358 | (*when) = when_shared; |
| 4359 | } |
| 4360 | } |
| 4361 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4362 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4363 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4364 | switch (target->nodetype) { |
| 4365 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4366 | 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] | 4367 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4368 | 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] | 4369 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4370 | break; |
| 4371 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4372 | 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] | 4373 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4374 | 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] | 4375 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4376 | break; |
| 4377 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4378 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4379 | if (aug_p->actions) { |
| 4380 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4381 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4382 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4383 | return LY_EVALID; |
| 4384 | } |
| 4385 | if (aug_p->notifs) { |
| 4386 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4387 | "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".", |
| 4388 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4389 | return LY_EVALID; |
| 4390 | } |
| 4391 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4392 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4393 | lysc_update_path(ctx, NULL, NULL); |
| 4394 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4395 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4396 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4397 | return ret; |
| 4398 | } |
| 4399 | |
| 4400 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4401 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4402 | * |
| 4403 | * @param[in] ctx Compile context. |
| 4404 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4405 | * @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] | 4406 | * @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] | 4407 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4408 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4409 | * 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] | 4410 | * @return LY_ERR value. |
| 4411 | */ |
| 4412 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4413 | 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] | 4414 | { |
| 4415 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4416 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4417 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4418 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4419 | return LY_EVALID; |
| 4420 | } |
| 4421 | |
| 4422 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4423 | /* check if node has default value */ |
| 4424 | if (node->nodetype & LYS_LEAF) { |
| 4425 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4426 | if (refine_flag) { |
| 4427 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4428 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4429 | return LY_EVALID; |
| 4430 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4431 | } else { |
| 4432 | /* remove the default value taken from the leaf's type */ |
| 4433 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt); |
| 4434 | ((struct lysc_node_leaf*)node)->dflt = NULL; |
| 4435 | } |
| 4436 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4437 | if (refine_flag) { |
| 4438 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4439 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4440 | return LY_EVALID; |
| 4441 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4442 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4443 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4444 | 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] | 4445 | return LY_EVALID; |
| 4446 | } |
| 4447 | |
| 4448 | node->flags &= ~LYS_MAND_FALSE; |
| 4449 | node->flags |= LYS_MAND_TRUE; |
| 4450 | lys_compile_mandatory_parents(node->parent, 1); |
| 4451 | } else { |
| 4452 | /* make mandatory false */ |
| 4453 | node->flags &= ~LYS_MAND_TRUE; |
| 4454 | node->flags |= LYS_MAND_FALSE; |
| 4455 | lys_compile_mandatory_parents(node->parent, 0); |
| 4456 | if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) { |
| 4457 | /* get the type's default value if any */ |
| 4458 | DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt); |
| 4459 | } |
| 4460 | } |
| 4461 | return LY_SUCCESS; |
| 4462 | } |
| 4463 | |
| 4464 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4465 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4466 | * If present, also apply uses's modificators. |
| 4467 | * |
| 4468 | * @param[in] ctx Compile context |
| 4469 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4470 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4471 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4472 | * the compile context. |
| 4473 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4474 | */ |
| 4475 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4476 | 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] | 4477 | { |
| 4478 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4479 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4480 | /* 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] | 4481 | struct lysc_node_container context_node_fake = |
| 4482 | {.nodetype = LYS_CONTAINER, |
| 4483 | .module = ctx->mod, |
| 4484 | .flags = parent ? parent->flags : 0, |
| 4485 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4486 | .prev = (struct lysc_node*)&context_node_fake, |
| 4487 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4488 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4489 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4490 | int found; |
| 4491 | const char *id, *name, *prefix; |
| 4492 | size_t prefix_len, name_len; |
| 4493 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4494 | struct lysp_refine *rfn; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4495 | LY_ERR ret = LY_EVALID; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4496 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4497 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4498 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4499 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4500 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4501 | unsigned int actions_index, notifs_index; |
| 4502 | struct lysc_notif **notifs = NULL; |
| 4503 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4504 | |
| 4505 | /* search for the grouping definition */ |
| 4506 | found = 0; |
| 4507 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4508 | 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] | 4509 | if (prefix) { |
| 4510 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4511 | if (!mod) { |
| 4512 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4513 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4514 | return LY_EVALID; |
| 4515 | } |
| 4516 | } else { |
| 4517 | mod = ctx->mod_def; |
| 4518 | } |
| 4519 | if (mod == ctx->mod_def) { |
| 4520 | 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] | 4521 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4522 | LY_ARRAY_FOR(grp, u) { |
| 4523 | if (!strcmp(grp[u].name, name)) { |
| 4524 | grp = &grp[u]; |
| 4525 | found = 1; |
| 4526 | break; |
| 4527 | } |
| 4528 | } |
| 4529 | } |
| 4530 | } |
| 4531 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4532 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4533 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4534 | if (grp) { |
| 4535 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4536 | if (!strcmp(grp[u].name, name)) { |
| 4537 | grp = &grp[u]; |
| 4538 | found = 1; |
| 4539 | } |
| 4540 | } |
| 4541 | } |
| 4542 | if (!found && mod->parsed->includes) { |
| 4543 | /* ... and all the submodules */ |
| 4544 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4545 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4546 | if (grp) { |
| 4547 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4548 | if (!strcmp(grp[v].name, name)) { |
| 4549 | grp = &grp[v]; |
| 4550 | found = 1; |
| 4551 | } |
| 4552 | } |
| 4553 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4554 | } |
| 4555 | } |
| 4556 | } |
| 4557 | if (!found) { |
| 4558 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4559 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4560 | return LY_EVALID; |
| 4561 | } |
| 4562 | |
| 4563 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4564 | grp_stack_count = ctx->groupings.count; |
| 4565 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4566 | if (grp_stack_count == ctx->groupings.count) { |
| 4567 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4568 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4569 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4570 | return LY_EVALID; |
| 4571 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4572 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4573 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4574 | grp->flags |= LYS_USED_GRP; |
| 4575 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4576 | |
| 4577 | /* switch context's mod_def */ |
| 4578 | mod_old = ctx->mod_def; |
| 4579 | ctx->mod_def = mod; |
| 4580 | |
| 4581 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4582 | 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] | 4583 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4584 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4585 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4586 | /* 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] | 4587 | 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] | 4588 | |
| 4589 | /* some preparation for applying refines */ |
| 4590 | if (grp->data == node_p) { |
| 4591 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4592 | if (parent) { |
| 4593 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4594 | } else if (ctx->mod->compiled->data) { |
| 4595 | child = ctx->mod->compiled->data; |
| 4596 | } else { |
| 4597 | child = NULL; |
| 4598 | } |
| 4599 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4600 | } |
| 4601 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4602 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4603 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4604 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4605 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4606 | /* 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] | 4607 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4608 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4609 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4610 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4611 | (*when)->context = lysc_xpath_context(parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4612 | when_shared = *when; |
| 4613 | } else { |
| 4614 | ++when_shared->refcount; |
| 4615 | (*when) = when_shared; |
| 4616 | } |
| 4617 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4618 | } |
| 4619 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4620 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4621 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4622 | /* 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] | 4623 | 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] | 4624 | } |
| 4625 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4626 | /* compile actions */ |
| 4627 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4628 | if (actions) { |
| 4629 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4630 | 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] | 4631 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4632 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4633 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4634 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4635 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4636 | } |
| 4637 | } |
| 4638 | |
| 4639 | /* compile notifications */ |
| 4640 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4641 | if (notifs) { |
| 4642 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4643 | 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] | 4644 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4645 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4646 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4647 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4648 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4649 | } |
| 4650 | } |
| 4651 | |
| 4652 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4653 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4654 | 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] | 4655 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4656 | 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] | 4657 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4658 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4659 | /* reload previous context's mod_def */ |
| 4660 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4661 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4662 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4663 | /* apply refine */ |
| 4664 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4665 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4666 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4667 | 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] | 4668 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4669 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4670 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4671 | |
| 4672 | /* default value */ |
| 4673 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4674 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4675 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4676 | "Invalid refine of default - %s cannot hold %d default values.", |
| 4677 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4678 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4679 | } |
| 4680 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4682 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4683 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4684 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4685 | } |
| 4686 | if (node->nodetype == LYS_LEAF) { |
| 4687 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt); |
| 4688 | DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4689 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4690 | /* TODO check the default value according to type */ |
| 4691 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4692 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4693 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4694 | "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] | 4695 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4696 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4697 | LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) { |
| 4698 | lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]); |
| 4699 | } |
| 4700 | LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4701 | ((struct lysc_node_leaflist*)node)->dflts = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4702 | LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4703 | LY_ARRAY_FOR(rfn->dflts, u) { |
| 4704 | LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts); |
| 4705 | DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]); |
| 4706 | } |
| 4707 | /* TODO check the default values according to type */ |
| 4708 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4709 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4710 | /* unset LYS_SET_DFLT from the current default case */ |
| 4711 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4712 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4713 | 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] | 4714 | } |
| 4715 | } |
| 4716 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4717 | /* description */ |
| 4718 | if (rfn->dsc) { |
| 4719 | FREE_STRING(ctx->ctx, node->dsc); |
| 4720 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4721 | } |
| 4722 | |
| 4723 | /* reference */ |
| 4724 | if (rfn->ref) { |
| 4725 | FREE_STRING(ctx->ctx, node->ref); |
| 4726 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4727 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4728 | |
| 4729 | /* config */ |
| 4730 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4731 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4732 | 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] | 4733 | } else { |
| 4734 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 4735 | flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path); |
| 4736 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | /* mandatory */ |
| 4740 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4741 | 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] | 4742 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4743 | |
| 4744 | /* presence */ |
| 4745 | if (rfn->presence) { |
| 4746 | if (node->nodetype != LYS_CONTAINER) { |
| 4747 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4748 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4749 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4750 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4751 | } |
| 4752 | node->flags |= LYS_PRESENCE; |
| 4753 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4754 | |
| 4755 | /* must */ |
| 4756 | if (rfn->musts) { |
| 4757 | switch (node->nodetype) { |
| 4758 | case LYS_LEAF: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4759 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4760 | break; |
| 4761 | case LYS_LEAFLIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4762 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4763 | break; |
| 4764 | case LYS_LIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4765 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4766 | break; |
| 4767 | case LYS_CONTAINER: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4768 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4769 | break; |
| 4770 | case LYS_ANYXML: |
| 4771 | case LYS_ANYDATA: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4772 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4773 | break; |
| 4774 | default: |
| 4775 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4776 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 4777 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4778 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4779 | } |
| 4780 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4781 | |
| 4782 | /* min/max-elements */ |
| 4783 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4784 | switch (node->nodetype) { |
| 4785 | case LYS_LEAFLIST: |
| 4786 | if (rfn->flags & LYS_SET_MAX) { |
| 4787 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4788 | } |
| 4789 | if (rfn->flags & LYS_SET_MIN) { |
| 4790 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4791 | if (rfn->min) { |
| 4792 | node->flags |= LYS_MAND_TRUE; |
| 4793 | lys_compile_mandatory_parents(node->parent, 1); |
| 4794 | } else { |
| 4795 | node->flags &= ~LYS_MAND_TRUE; |
| 4796 | lys_compile_mandatory_parents(node->parent, 0); |
| 4797 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4798 | } |
| 4799 | break; |
| 4800 | case LYS_LIST: |
| 4801 | if (rfn->flags & LYS_SET_MAX) { |
| 4802 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4803 | } |
| 4804 | if (rfn->flags & LYS_SET_MIN) { |
| 4805 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4806 | if (rfn->min) { |
| 4807 | node->flags |= LYS_MAND_TRUE; |
| 4808 | lys_compile_mandatory_parents(node->parent, 1); |
| 4809 | } else { |
| 4810 | node->flags &= ~LYS_MAND_TRUE; |
| 4811 | lys_compile_mandatory_parents(node->parent, 0); |
| 4812 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4813 | } |
| 4814 | break; |
| 4815 | default: |
| 4816 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4817 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 4818 | (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] | 4819 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4820 | } |
| 4821 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4822 | |
| 4823 | /* if-feature */ |
| 4824 | if (rfn->iffeatures) { |
| 4825 | /* 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] | 4826 | 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] | 4827 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4828 | |
| 4829 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4830 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4831 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4832 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 4833 | for (u = 0; u < refined.count; ++u) { |
| 4834 | node = (struct lysc_node*)refined.objs[u]; |
| 4835 | rfn = &uses_p->refines[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4836 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4837 | |
| 4838 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 4839 | if ((node->flags & LYS_MAND_TRUE) && |
| 4840 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 4841 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 4842 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4843 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4844 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4848 | if (node->nodetype == LYS_LIST) { |
| 4849 | min = ((struct lysc_node_list*)node)->min; |
| 4850 | max = ((struct lysc_node_list*)node)->max; |
| 4851 | } else { |
| 4852 | min = ((struct lysc_node_leaflist*)node)->min; |
| 4853 | max = ((struct lysc_node_leaflist*)node)->max; |
| 4854 | } |
| 4855 | if (min > max) { |
| 4856 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4857 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 4858 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4859 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4860 | } |
| 4861 | } |
| 4862 | } |
| 4863 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4864 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4865 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4866 | |
| 4867 | cleanup: |
| 4868 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 4869 | if (context_node_fake.child) { |
| 4870 | context_node_fake.child->prev = child; |
| 4871 | } |
| 4872 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4873 | child->parent = parent; |
| 4874 | } |
| 4875 | |
| 4876 | if (uses_p->augments || uses_p->refines) { |
| 4877 | /* 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] | 4878 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4879 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4880 | LY_ARRAY_FREE(context_node_fake.actions); |
| 4881 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 4882 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4883 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4884 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 4885 | } |
| 4886 | } |
| 4887 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4888 | /* reload previous context's mod_def */ |
| 4889 | ctx->mod_def = mod_old; |
| 4890 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 4891 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 4892 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4893 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4894 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4895 | |
| 4896 | return ret; |
| 4897 | } |
| 4898 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4899 | static int |
| 4900 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 4901 | { |
| 4902 | struct lysp_node *iter; |
| 4903 | int len = 0; |
| 4904 | |
| 4905 | *path = NULL; |
| 4906 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 4907 | char *s = *path; |
| 4908 | char *id; |
| 4909 | |
| 4910 | switch (iter->nodetype) { |
| 4911 | case LYS_USES: |
| 4912 | asprintf(&id, "{uses='%s'}", iter->name); |
| 4913 | break; |
| 4914 | case LYS_GROUPING: |
| 4915 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 4916 | break; |
| 4917 | case LYS_AUGMENT: |
| 4918 | asprintf(&id, "{augment='%s'}", iter->name); |
| 4919 | break; |
| 4920 | default: |
| 4921 | id = strdup(iter->name); |
| 4922 | break; |
| 4923 | } |
| 4924 | |
| 4925 | if (!iter->parent) { |
| 4926 | /* print prefix */ |
| 4927 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 4928 | } else { |
| 4929 | /* prefix is the same as in parent */ |
| 4930 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 4931 | } |
| 4932 | free(s); |
| 4933 | free(id); |
| 4934 | } |
| 4935 | |
| 4936 | if (len < 0) { |
| 4937 | free(*path); |
| 4938 | *path = NULL; |
| 4939 | } else if (len == 0) { |
| 4940 | *path = strdup("/"); |
| 4941 | len = 1; |
| 4942 | } |
| 4943 | return len; |
| 4944 | } |
| 4945 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4946 | /** |
| 4947 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 4948 | * |
| 4949 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 4950 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 4951 | */ |
| 4952 | static LY_ERR |
| 4953 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 4954 | { |
| 4955 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4956 | char *path; |
| 4957 | int len; |
| 4958 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4959 | struct lysp_node_uses fake_uses = { |
| 4960 | .parent = node_p, |
| 4961 | .nodetype = LYS_USES, |
| 4962 | .flags = 0, .next = NULL, |
| 4963 | .name = grp->name, |
| 4964 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 4965 | .refines = NULL, .augments = NULL |
| 4966 | }; |
| 4967 | struct lysc_node_container fake_container = { |
| 4968 | .nodetype = LYS_CONTAINER, |
| 4969 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 4970 | .module = ctx->mod, |
| 4971 | .sp = NULL, .parent = NULL, .next = NULL, |
| 4972 | .prev = (struct lysc_node*)&fake_container, |
| 4973 | .name = "fake", |
| 4974 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 4975 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 4976 | }; |
| 4977 | |
| 4978 | if (grp->parent) { |
| 4979 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 4980 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4981 | |
| 4982 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 4983 | if (len < 0) { |
| 4984 | LOGMEM(ctx->ctx); |
| 4985 | return LY_EMEM; |
| 4986 | } |
| 4987 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 4988 | ctx->path_len = (uint16_t)len; |
| 4989 | free(path); |
| 4990 | |
| 4991 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 4992 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4993 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4994 | lysc_update_path(ctx, NULL, NULL); |
| 4995 | lysc_update_path(ctx, NULL, NULL); |
| 4996 | |
| 4997 | ctx->path_len = 1; |
| 4998 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4999 | |
| 5000 | /* cleanup */ |
| 5001 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5002 | |
| 5003 | return ret; |
| 5004 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5005 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5006 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5007 | * @brief Compile parsed schema node information. |
| 5008 | * @param[in] ctx Compile context |
| 5009 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5010 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5011 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5012 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5013 | * @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). |
| 5014 | * 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] | 5015 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5016 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5017 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5018 | 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] | 5019 | { |
| 5020 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5021 | struct lysc_node *node; |
| 5022 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5023 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5024 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5025 | 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] | 5026 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5027 | if (node_p->nodetype != LYS_USES) { |
| 5028 | lysc_update_path(ctx, parent, node_p->name); |
| 5029 | } else { |
| 5030 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5031 | lysc_update_path(ctx, NULL, node_p->name); |
| 5032 | } |
| 5033 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5034 | switch (node_p->nodetype) { |
| 5035 | case LYS_CONTAINER: |
| 5036 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5037 | node_compile_spec = lys_compile_node_container; |
| 5038 | break; |
| 5039 | case LYS_LEAF: |
| 5040 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5041 | node_compile_spec = lys_compile_node_leaf; |
| 5042 | break; |
| 5043 | case LYS_LIST: |
| 5044 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5045 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5046 | break; |
| 5047 | case LYS_LEAFLIST: |
| 5048 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5049 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5050 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5051 | case LYS_CHOICE: |
| 5052 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5053 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5054 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5055 | case LYS_ANYXML: |
| 5056 | case LYS_ANYDATA: |
| 5057 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5058 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5059 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5060 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5061 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5062 | lysc_update_path(ctx, NULL, NULL); |
| 5063 | lysc_update_path(ctx, NULL, NULL); |
| 5064 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5065 | default: |
| 5066 | LOGINT(ctx->ctx); |
| 5067 | return LY_EINT; |
| 5068 | } |
| 5069 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5070 | node->nodetype = node_p->nodetype; |
| 5071 | node->module = ctx->mod; |
| 5072 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5073 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5074 | |
| 5075 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5076 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5077 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5078 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5079 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5080 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5081 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5082 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5083 | node->flags |= LYS_CONFIG_R; |
| 5084 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5085 | /* config not explicitely set, inherit it from parent */ |
| 5086 | if (parent) { |
| 5087 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5088 | } else { |
| 5089 | /* default is config true */ |
| 5090 | node->flags |= LYS_CONFIG_W; |
| 5091 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5092 | } else { |
| 5093 | /* config set explicitely */ |
| 5094 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5095 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5096 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5097 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5098 | "Configuration node cannot be child of any state data node."); |
| 5099 | goto error; |
| 5100 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5101 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5102 | /* *list ordering */ |
| 5103 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5104 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5105 | 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] | 5106 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5107 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5108 | node->flags &= ~LYS_ORDBY_MASK; |
| 5109 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5110 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5111 | /* default ordering is system */ |
| 5112 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5113 | } |
| 5114 | } |
| 5115 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5116 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5117 | * 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] | 5118 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5119 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5120 | * 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] | 5121 | 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] | 5122 | } |
| 5123 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5124 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5125 | node->sp = node_p; |
| 5126 | } |
| 5127 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5128 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5129 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5130 | if (node_p->when) { |
| 5131 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5132 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5133 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 5134 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5135 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5136 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error); |
| 5137 | COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5138 | |
| 5139 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5140 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5141 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5142 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5143 | if (node->flags & LYS_MAND_TRUE) { |
| 5144 | lys_compile_mandatory_parents(parent, 1); |
| 5145 | } |
| 5146 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5147 | lysc_update_path(ctx, NULL, NULL); |
| 5148 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5149 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5150 | if (parent) { |
| 5151 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5152 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5153 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5154 | } else { |
| 5155 | lysc_update_path(ctx, parent, node->name); |
| 5156 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5157 | 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] | 5158 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5159 | if (uses_status) { |
| 5160 | |
| 5161 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5162 | /* 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] | 5163 | * it directly gets the same status flags as the choice; |
| 5164 | * 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] | 5165 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5166 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5167 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5168 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5169 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5170 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5171 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5172 | 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] | 5173 | |
| 5174 | if (parent->nodetype == LYS_CHOICE) { |
| 5175 | lysc_update_path(ctx, NULL, NULL); |
| 5176 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5177 | } else { |
| 5178 | /* top-level element */ |
| 5179 | if (!ctx->mod->compiled->data) { |
| 5180 | ctx->mod->compiled->data = node; |
| 5181 | } else { |
| 5182 | /* insert at the end of the module's top-level nodes list */ |
| 5183 | ctx->mod->compiled->data->prev->next = node; |
| 5184 | node->prev = ctx->mod->compiled->data->prev; |
| 5185 | ctx->mod->compiled->data->prev = node; |
| 5186 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5187 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5188 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5189 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5190 | return LY_EVALID; |
| 5191 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5192 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5193 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5194 | |
| 5195 | return LY_SUCCESS; |
| 5196 | |
| 5197 | error: |
| 5198 | lysc_node_free(ctx->ctx, node); |
| 5199 | return ret; |
| 5200 | } |
| 5201 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5202 | static void |
| 5203 | lysc_disconnect(struct lysc_node *node) |
| 5204 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5205 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5206 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5207 | int remove_cs = 0; |
| 5208 | |
| 5209 | parent = node->parent; |
| 5210 | |
| 5211 | /* parent's first child */ |
| 5212 | if (!parent) { |
| 5213 | return; |
| 5214 | } |
| 5215 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5216 | cs = (struct lysc_node_case*)node; |
| 5217 | } else if (parent->nodetype == LYS_CASE) { |
| 5218 | /* disconnecting some node in a case */ |
| 5219 | cs = (struct lysc_node_case*)parent; |
| 5220 | parent = cs->parent; |
| 5221 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5222 | if (child == node) { |
| 5223 | if (cs->child == child) { |
| 5224 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5225 | /* case with a single child -> remove also the case */ |
| 5226 | child->parent = NULL; |
| 5227 | remove_cs = 1; |
| 5228 | } else { |
| 5229 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5230 | } |
| 5231 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5232 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5233 | } |
| 5234 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5235 | if (!remove_cs) { |
| 5236 | cs = NULL; |
| 5237 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5238 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5239 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5240 | } |
| 5241 | |
| 5242 | if (cs) { |
| 5243 | if (remove_cs) { |
| 5244 | /* cs has only one child which is being also removed */ |
| 5245 | lysc_disconnect((struct lysc_node*)cs); |
| 5246 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5247 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5248 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5249 | /* default case removed */ |
| 5250 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5251 | } |
| 5252 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5253 | /* first case removed */ |
| 5254 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5255 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5256 | if (cs->child) { |
| 5257 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5258 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5259 | prev = cs->child->prev; |
| 5260 | } /* else all the children are under a single case */ |
| 5261 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5262 | if (child->parent != (struct lysc_node*)cs) { |
| 5263 | break; |
| 5264 | } |
| 5265 | lysc_node_free(node->module->ctx, child); |
| 5266 | } |
| 5267 | if (prev) { |
| 5268 | if (prev->next) { |
| 5269 | prev->next = child; |
| 5270 | } |
| 5271 | if (child) { |
| 5272 | child->prev = prev; |
| 5273 | } else { |
| 5274 | /* link from the first child under the cases */ |
| 5275 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5276 | } |
| 5277 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5278 | } |
| 5279 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5280 | } |
| 5281 | |
| 5282 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5283 | if (node->prev->next) { |
| 5284 | node->prev->next = node->next; |
| 5285 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5286 | if (node->next) { |
| 5287 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5288 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5289 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5290 | if (child) { |
| 5291 | child->prev = node->prev; |
| 5292 | } |
| 5293 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5294 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5295 | } |
| 5296 | } |
| 5297 | |
| 5298 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5299 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5300 | { |
| 5301 | LY_ERR ret = LY_EVALID; |
| 5302 | struct ly_set devs_p = {0}; |
| 5303 | struct ly_set targets = {0}; |
| 5304 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5305 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5306 | struct lysc_action *rpcs; |
| 5307 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5308 | struct lysp_deviation *dev; |
| 5309 | struct lysp_deviate *d, **dp_new; |
| 5310 | struct lysp_deviate_add *d_add; |
| 5311 | struct lysp_deviate_del *d_del; |
| 5312 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5313 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5314 | struct lysc_deviation { |
| 5315 | const char *nodeid; |
| 5316 | struct lysc_node *target; /* target node of the deviation */ |
| 5317 | 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] | 5318 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5319 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5320 | } **devs = NULL; |
| 5321 | int i; |
| 5322 | size_t prefix_len, name_len; |
| 5323 | const char *prefix, *name, *nodeid; |
| 5324 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5325 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5326 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5327 | |
| 5328 | /* get all deviations from the module and all its submodules ... */ |
| 5329 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5330 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5331 | } |
| 5332 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5333 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5334 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5335 | } |
| 5336 | } |
| 5337 | if (!devs_p.count) { |
| 5338 | /* nothing to do */ |
| 5339 | return LY_SUCCESS; |
| 5340 | } |
| 5341 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5342 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5343 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5344 | /* ... and group them by the target node */ |
| 5345 | devs = calloc(devs_p.count, sizeof *devs); |
| 5346 | for (u = 0; u < devs_p.count; ++u) { |
| 5347 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5348 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5349 | |
| 5350 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5351 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5352 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5353 | if (target->nodetype == LYS_ACTION) { |
| 5354 | /* move the target pointer to input/output to make them different from the action and |
| 5355 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5356 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5357 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5358 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5359 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5360 | flags |= LYSC_OPT_INTERNAL; |
| 5361 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5362 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5363 | flags |= LYSC_OPT_INTERNAL; |
| 5364 | } |
| 5365 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5366 | /* insert into the set of targets with duplicity detection */ |
| 5367 | i = ly_set_add(&targets, target, 0); |
| 5368 | if (!devs[i]) { |
| 5369 | /* new record */ |
| 5370 | devs[i] = calloc(1, sizeof **devs); |
| 5371 | devs[i]->target = target; |
| 5372 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5373 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5374 | } |
| 5375 | /* add deviates into the deviation's list of deviates */ |
| 5376 | for (d = dev->deviates; d; d = d->next) { |
| 5377 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5378 | *dp_new = d; |
| 5379 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5380 | devs[i]->not_supported = 1; |
| 5381 | } |
| 5382 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5383 | |
| 5384 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5385 | } |
| 5386 | |
| 5387 | /* MACROS for deviates checking */ |
| 5388 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5389 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5390 | 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] | 5391 | goto cleanup; \ |
| 5392 | } |
| 5393 | |
| 5394 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5395 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5396 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \ |
| 5397 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5398 | goto cleanup; \ |
| 5399 | } |
| 5400 | |
| 5401 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5402 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5403 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5404 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5405 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5406 | goto cleanup; \ |
| 5407 | } |
| 5408 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5409 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5410 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5411 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5412 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5413 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5414 | goto cleanup; \ |
| 5415 | } |
| 5416 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5417 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5418 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5419 | 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] | 5420 | goto cleanup; \ |
| 5421 | } |
| 5422 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5423 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5424 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5425 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5426 | "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] | 5427 | goto cleanup; \ |
| 5428 | } |
| 5429 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5430 | #define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \ |
| 5431 | DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \ |
| 5432 | if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \ |
| 5433 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5434 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \ |
| 5435 | PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5436 | goto cleanup; \ |
| 5437 | } \ |
| 5438 | DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \ |
| 5439 | ((TYPE)devs[u]->target)->MEMBER_TRG = NULL; |
| 5440 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5441 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5442 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5443 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5444 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5445 | 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] | 5446 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5447 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5448 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5449 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5450 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5451 | goto cleanup; \ |
| 5452 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5453 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5454 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5455 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5456 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5457 | (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] | 5458 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5459 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5460 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5461 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5462 | } |
| 5463 | |
| 5464 | /* apply deviations */ |
| 5465 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5466 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5467 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5468 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5469 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5470 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5471 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5472 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5473 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5474 | } |
| 5475 | } |
| 5476 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5477 | /* not-supported */ |
| 5478 | if (devs[u]->not_supported) { |
| 5479 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 5480 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 5481 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5482 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5483 | |
| 5484 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5485 | if (devs[u]->target->parent) { \ |
| 5486 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5487 | } else { \ |
| 5488 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5489 | } \ |
| 5490 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5491 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5492 | } \ |
| 5493 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5494 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5495 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5496 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5497 | } |
| 5498 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5499 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 5500 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5501 | /* remove RPC's/action's input */ |
| 5502 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5503 | 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] | 5504 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5505 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5506 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5507 | /* remove RPC's/action's output */ |
| 5508 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5509 | 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] | 5510 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5511 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5512 | } else { |
| 5513 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5514 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5515 | } |
| 5516 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5517 | /* remove Notification */ |
| 5518 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5519 | } else { |
| 5520 | /* remove the target node */ |
| 5521 | lysc_disconnect(devs[u]->target); |
| 5522 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5523 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5524 | |
| 5525 | continue; |
| 5526 | } |
| 5527 | |
| 5528 | /* list of deviates (not-supported is not present in the list) */ |
| 5529 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5530 | d = devs[u]->deviates[v]; |
| 5531 | |
| 5532 | switch (d->mod) { |
| 5533 | case LYS_DEV_ADD: |
| 5534 | d_add = (struct lysp_deviate_add*)d; |
| 5535 | /* [units-stmt] */ |
| 5536 | if (d_add->units) { |
| 5537 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5538 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5539 | |
| 5540 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5541 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5542 | } |
| 5543 | |
| 5544 | /* *must-stmt */ |
| 5545 | if (d_add->musts) { |
| 5546 | switch (devs[u]->target->nodetype) { |
| 5547 | case LYS_CONTAINER: |
| 5548 | case LYS_LIST: |
| 5549 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5550 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5551 | break; |
| 5552 | case LYS_LEAF: |
| 5553 | case LYS_LEAFLIST: |
| 5554 | case LYS_ANYDATA: |
| 5555 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5556 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5557 | break; |
| 5558 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5559 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5560 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5561 | break; |
| 5562 | case LYS_ACTION: |
| 5563 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5564 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5565 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5566 | break; |
| 5567 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5568 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5569 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5570 | break; |
| 5571 | } |
| 5572 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5573 | default: |
| 5574 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5575 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5576 | goto cleanup; |
| 5577 | } |
| 5578 | } |
| 5579 | |
| 5580 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5581 | if (d_add->uniques) { |
| 5582 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5583 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5584 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5585 | |
| 5586 | /* *default-stmt */ |
| 5587 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5588 | switch (devs[u]->target->nodetype) { |
| 5589 | case LYS_LEAF: |
| 5590 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5591 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt); |
| 5592 | if (((struct lysc_node_leaf*)devs[u]->target)->dflt) { |
| 5593 | /* first, remove the default value taken from the type */ |
| 5594 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 5595 | ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL; |
| 5596 | } |
| 5597 | DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5598 | /* mark the new default values as leaf's own */ |
| 5599 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5600 | break; |
| 5601 | case LYS_LEAFLIST: |
| 5602 | if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 5603 | /* first, remove the default value taken from the type */ |
| 5604 | LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) { |
| 5605 | lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]); |
| 5606 | } |
| 5607 | LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 5608 | ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL; |
| 5609 | } |
| 5610 | /* add new default value(s) */ |
| 5611 | LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts, |
| 5612 | LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 5613 | for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 5614 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
| 5615 | DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]); |
| 5616 | LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 5617 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5618 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5619 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5620 | break; |
| 5621 | case LYS_CHOICE: |
| 5622 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5623 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5624 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5625 | * 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] | 5626 | 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] | 5627 | goto cleanup; |
| 5628 | } |
| 5629 | break; |
| 5630 | default: |
| 5631 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5632 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5633 | goto cleanup; |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5638 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 5639 | 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] | 5640 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5641 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5642 | goto cleanup; |
| 5643 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5644 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5645 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 5646 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5647 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5648 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5649 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5650 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5651 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5652 | goto cleanup; |
| 5653 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5654 | 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] | 5655 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5656 | |
| 5657 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5658 | if (d_add->flags & LYS_MAND_MASK) { |
| 5659 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5660 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5661 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5662 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5663 | goto cleanup; |
| 5664 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5665 | 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] | 5666 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5667 | |
| 5668 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5669 | if (d_add->flags & LYS_SET_MIN) { |
| 5670 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5671 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5672 | /* change value */ |
| 5673 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5674 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5675 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5676 | /* change value */ |
| 5677 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5678 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5679 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5680 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5681 | goto cleanup; |
| 5682 | } |
| 5683 | if (d_add->min) { |
| 5684 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5685 | } |
| 5686 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5687 | |
| 5688 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5689 | if (d_add->flags & LYS_SET_MAX) { |
| 5690 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5691 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5692 | /* change value */ |
| 5693 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5694 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5695 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5696 | /* change value */ |
| 5697 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5698 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5699 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5700 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 5701 | goto cleanup; |
| 5702 | } |
| 5703 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5704 | |
| 5705 | break; |
| 5706 | case LYS_DEV_DELETE: |
| 5707 | d_del = (struct lysp_deviate_del*)d; |
| 5708 | |
| 5709 | /* [units-stmt] */ |
| 5710 | if (d_del->units) { |
| 5711 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
| 5712 | DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units"); |
| 5713 | } |
| 5714 | |
| 5715 | /* *must-stmt */ |
| 5716 | if (d_del->musts) { |
| 5717 | switch (devs[u]->target->nodetype) { |
| 5718 | case LYS_CONTAINER: |
| 5719 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5720 | 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] | 5721 | break; |
| 5722 | case LYS_LEAF: |
| 5723 | case LYS_LEAFLIST: |
| 5724 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5725 | 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] | 5726 | break; |
| 5727 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5728 | 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] | 5729 | break; |
| 5730 | case LYS_ACTION: |
| 5731 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5732 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5733 | break; |
| 5734 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5735 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5736 | break; |
| 5737 | } |
| 5738 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5739 | default: |
| 5740 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5741 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5742 | goto cleanup; |
| 5743 | } |
| 5744 | } |
| 5745 | |
| 5746 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5747 | if (d_del->uniques) { |
| 5748 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5749 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 5750 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 5751 | LY_ARRAY_FOR(list->uniques, z) { |
| 5752 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 5753 | nodeid = strpbrk(name, " \t\n"); |
| 5754 | if (nodeid) { |
| 5755 | if (strncmp(name, list->uniques[z][y]->name, nodeid - name) |
| 5756 | || list->uniques[z][y]->name[nodeid - name] != '\0') { |
| 5757 | break; |
| 5758 | } |
| 5759 | while (isspace(*nodeid)) { |
| 5760 | ++nodeid; |
| 5761 | } |
| 5762 | } else { |
| 5763 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 5764 | break; |
| 5765 | } |
| 5766 | } |
| 5767 | } |
| 5768 | if (!name) { |
| 5769 | /* complete match - remove the unique */ |
| 5770 | LY_ARRAY_DECREMENT(list->uniques); |
| 5771 | LY_ARRAY_FREE(list->uniques[z]); |
| 5772 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 5773 | --z; |
| 5774 | break; |
| 5775 | } |
| 5776 | } |
| 5777 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 5778 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5779 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 5780 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5781 | goto cleanup; |
| 5782 | } |
| 5783 | } |
| 5784 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 5785 | LY_ARRAY_FREE(list->uniques); |
| 5786 | list->uniques = NULL; |
| 5787 | } |
| 5788 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5789 | |
| 5790 | /* *default-stmt */ |
| 5791 | if (d_del->dflts) { |
| 5792 | switch (devs[u]->target->nodetype) { |
| 5793 | case LYS_LEAF: |
| 5794 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 5795 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 5796 | dflt, "deleting", "default", d_del->dflts[0]); |
| 5797 | |
| 5798 | DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default"); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5799 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5800 | break; |
| 5801 | case LYS_LEAFLIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5802 | DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default"); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5803 | if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) { |
| 5804 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
| 5805 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5806 | break; |
| 5807 | case LYS_CHOICE: |
| 5808 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 5809 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 5810 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 5811 | 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] | 5812 | if (prefix) { |
| 5813 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 5814 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 5815 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5816 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 5817 | "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] | 5818 | goto cleanup; |
| 5819 | } |
| 5820 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 5821 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5822 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 5823 | "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] | 5824 | goto cleanup; |
| 5825 | } |
| 5826 | } |
| 5827 | /* else { |
| 5828 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 5829 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 5830 | * the module of the default case, just matching its name */ |
| 5831 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 5832 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5833 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 5834 | 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] | 5835 | goto cleanup; |
| 5836 | } |
| 5837 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 5838 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 5839 | break; |
| 5840 | default: |
| 5841 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5842 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5843 | goto cleanup; |
| 5844 | } |
| 5845 | } |
| 5846 | |
| 5847 | break; |
| 5848 | case LYS_DEV_REPLACE: |
| 5849 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 5850 | |
| 5851 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5852 | if (d_rpl->type) { |
| 5853 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 5854 | /* type is mandatory, so checking for its presence is not necessary */ |
| 5855 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5856 | LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, (struct lysc_node_leaf*)devs[u]->target), cleanup); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5857 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5858 | |
| 5859 | /* [units-stmt] */ |
| 5860 | if (d_rpl->units) { |
| 5861 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 5862 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 5863 | units, "replacing", "units", d_rpl->units); |
| 5864 | |
| 5865 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5866 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5867 | } |
| 5868 | |
| 5869 | /* [default-stmt] */ |
| 5870 | if (d_rpl->dflt) { |
| 5871 | switch (devs[u]->target->nodetype) { |
| 5872 | case LYS_LEAF: |
| 5873 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 5874 | dflt, "replacing", "default", d_rpl->dflt); |
| 5875 | |
| 5876 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 5877 | DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 5878 | break; |
| 5879 | case LYS_CHOICE: |
| 5880 | 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] | 5881 | 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] | 5882 | goto cleanup; |
| 5883 | } |
| 5884 | break; |
| 5885 | default: |
| 5886 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5887 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5888 | goto cleanup; |
| 5889 | } |
| 5890 | } |
| 5891 | |
| 5892 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5893 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 5894 | 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] | 5895 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5896 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 5897 | goto cleanup; |
| 5898 | } |
| 5899 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5900 | 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] | 5901 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 5902 | goto cleanup; |
| 5903 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5904 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5905 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5906 | |
| 5907 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5908 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 5909 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5910 | 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] | 5911 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 5912 | goto cleanup; |
| 5913 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5914 | 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] | 5915 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5916 | |
| 5917 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5918 | if (d_rpl->flags & LYS_SET_MIN) { |
| 5919 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5920 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5921 | /* change value */ |
| 5922 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 5923 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5924 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5925 | /* change value */ |
| 5926 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 5927 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5928 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5929 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 5930 | goto cleanup; |
| 5931 | } |
| 5932 | if (d_rpl->min) { |
| 5933 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5934 | } |
| 5935 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5936 | |
| 5937 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5938 | if (d_rpl->flags & LYS_SET_MAX) { |
| 5939 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5940 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5941 | /* change value */ |
| 5942 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 5943 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5944 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5945 | /* change value */ |
| 5946 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 5947 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5948 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5949 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 5950 | goto cleanup; |
| 5951 | } |
| 5952 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5953 | |
| 5954 | break; |
| 5955 | default: |
| 5956 | LOGINT(ctx->ctx); |
| 5957 | goto cleanup; |
| 5958 | } |
| 5959 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5960 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5961 | /* final check when all deviations of a single target node are applied */ |
| 5962 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5963 | /* check min-max compatibility */ |
| 5964 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5965 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 5966 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 5967 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5968 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 5969 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 5970 | } else { |
| 5971 | min = max = 0; |
| 5972 | } |
| 5973 | if (min > max) { |
| 5974 | 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] | 5975 | "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] | 5976 | goto cleanup; |
| 5977 | } |
| 5978 | |
| 5979 | /* check mandatory - default compatibility */ |
| 5980 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 5981 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 5982 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 5983 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5984 | "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] | 5985 | goto cleanup; |
| 5986 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 5987 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 5988 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5989 | 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] | 5990 | goto cleanup; |
| 5991 | } |
| 5992 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 5993 | /* mandatory node under a default case */ |
| 5994 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5995 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 5996 | 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] | 5997 | goto cleanup; |
| 5998 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5999 | |
| 6000 | /* TODO check default value(s) according to the type */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6001 | |
| 6002 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6003 | } |
| 6004 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6005 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6006 | ret = LY_SUCCESS; |
| 6007 | |
| 6008 | cleanup: |
| 6009 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6010 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6011 | free(devs[u]); |
| 6012 | } |
| 6013 | free(devs); |
| 6014 | ly_set_erase(&targets, NULL); |
| 6015 | ly_set_erase(&devs_p, NULL); |
| 6016 | |
| 6017 | return ret; |
| 6018 | } |
| 6019 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6020 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6021 | * @brief Compile the given YANG submodule into the main module. |
| 6022 | * @param[in] ctx Compile context |
| 6023 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6024 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6025 | */ |
| 6026 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6027 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6028 | { |
| 6029 | unsigned int u; |
| 6030 | LY_ERR ret = LY_SUCCESS; |
| 6031 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6032 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6033 | struct lysc_module *mainmod = ctx->mod->compiled; |
| 6034 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6035 | if (!mainmod->mod->off_features) { |
| 6036 | /* features are compiled directly into the compiled module structure, |
| 6037 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6038 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6039 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6040 | mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features); |
| 6041 | LY_CHECK_GOTO(ret, error); |
| 6042 | } |
| 6043 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6044 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6045 | 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] | 6046 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6047 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6048 | /* TODO data nodes */ |
| 6049 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6050 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6051 | 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] | 6052 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6053 | error: |
| 6054 | return ret; |
| 6055 | } |
| 6056 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6057 | LY_ERR |
| 6058 | lys_compile(struct lys_module *mod, int options) |
| 6059 | { |
| 6060 | struct lysc_ctx ctx = {0}; |
| 6061 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6062 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6063 | struct lysp_module *sp; |
| 6064 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6065 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6066 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6067 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6068 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6069 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6070 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6071 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 6072 | |
| 6073 | if (!mod->implemented) { |
| 6074 | /* just imported modules are not compiled */ |
| 6075 | return LY_SUCCESS; |
| 6076 | } |
| 6077 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6078 | sp = mod->parsed; |
| 6079 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6080 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6081 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6082 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6083 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6084 | ctx.path_len = 1; |
| 6085 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6086 | |
| 6087 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6088 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 6089 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6090 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6091 | 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] | 6092 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6093 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6094 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6095 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6096 | if (mod->off_features) { |
| 6097 | /* there is already precompiled array of features */ |
| 6098 | mod_c->features = mod->off_features; |
| 6099 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6100 | } else { |
| 6101 | /* features are compiled directly into the compiled module structure, |
| 6102 | * 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] | 6103 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6104 | LY_CHECK_GOTO(ret, error); |
| 6105 | } |
| 6106 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 6107 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 6108 | * are present. */ |
| 6109 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6110 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6111 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6112 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6113 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6114 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6115 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6116 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6117 | 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] | 6118 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6119 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6120 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6121 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6122 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6123 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6124 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6125 | 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] | 6126 | if (sp->identities) { |
| 6127 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 6128 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6129 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6130 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6131 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6132 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6133 | ret = lys_compile_node(&ctx, node_p, NULL, 0); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6134 | LY_CHECK_GOTO(ret, error); |
| 6135 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6136 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6137 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6138 | 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] | 6139 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6140 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 6141 | ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6142 | LY_CHECK_GOTO(ret, error); |
| 6143 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6144 | ret = lys_compile_augment(&ctx, augments[u], NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6145 | LY_CHECK_GOTO(ret, error); |
| 6146 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6147 | |
| 6148 | /* deviations */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6149 | ret = lys_compile_deviations(&ctx, sp); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6150 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6151 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6152 | COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6153 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6154 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6155 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 6156 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 6157 | * 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] | 6158 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6159 | 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] | 6160 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6161 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6162 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6163 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6164 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6165 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6166 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6167 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6168 | /* validate the path */ |
| 6169 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 6170 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 6171 | LY_CHECK_GOTO(ret, error); |
| 6172 | } |
| 6173 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6174 | } |
| 6175 | } |
| 6176 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6177 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6178 | 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] | 6179 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6180 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6181 | /* store pointer to the real type */ |
| 6182 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 6183 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6184 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6185 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6186 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6187 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6188 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6189 | /* store pointer to the real type */ |
| 6190 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 6191 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6192 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6193 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 6194 | } |
| 6195 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6196 | } |
| 6197 | } |
| 6198 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6199 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6200 | /* validate non-instantiated groupings from the parsed schema, |
| 6201 | * without it we would accept even the schemas with invalid grouping specification */ |
| 6202 | ctx.options |= LYSC_OPT_GROUPING; |
| 6203 | LY_ARRAY_FOR(sp->groupings, u) { |
| 6204 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
| 6205 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error); |
| 6206 | } |
| 6207 | } |
| 6208 | LY_LIST_FOR(sp->data, node_p) { |
| 6209 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 6210 | LY_ARRAY_FOR(grps, u) { |
| 6211 | if (!(grps[u].flags & LYS_USED_GRP)) { |
| 6212 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error); |
| 6213 | } |
| 6214 | } |
| 6215 | } |
| 6216 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6217 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6218 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6219 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6220 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6221 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6222 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6223 | lysp_module_free(mod->parsed); |
| 6224 | ((struct lys_module*)mod)->parsed = NULL; |
| 6225 | } |
| 6226 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6227 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6228 | /* remove flag of the modules implemented by dependency */ |
| 6229 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6230 | m = ctx.ctx->list.objs[u]; |
| 6231 | if (m->implemented == 2) { |
| 6232 | m->implemented = 1; |
| 6233 | } |
| 6234 | } |
| 6235 | } |
| 6236 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6237 | ((struct lys_module*)mod)->compiled = mod_c; |
| 6238 | return LY_SUCCESS; |
| 6239 | |
| 6240 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6241 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6242 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6243 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6244 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6245 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6246 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6247 | mod->compiled = NULL; |
| 6248 | |
| 6249 | /* revert compilation of modules implemented by dependency */ |
| 6250 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6251 | m = ctx.ctx->list.objs[u]; |
| 6252 | if (m->implemented == 2) { |
| 6253 | /* revert features list to the precompiled state */ |
| 6254 | lys_feature_precompile_revert(&ctx, m); |
| 6255 | /* mark module as imported-only / not-implemented */ |
| 6256 | m->implemented = 0; |
| 6257 | /* free the compiled version of the module */ |
| 6258 | lysc_module_free(m->compiled, NULL); |
| 6259 | m->compiled = NULL; |
| 6260 | } |
| 6261 | } |
| 6262 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6263 | return ret; |
| 6264 | } |