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 | /** |
| 112 | * @brief Duplicate the compiled pattern structure. |
| 113 | * |
| 114 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 115 | * |
| 116 | * @param[in] orig The pattern structure to duplicate. |
| 117 | * @return The duplicated structure to use. |
| 118 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 119 | static struct lysc_pattern* |
| 120 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 121 | { |
| 122 | ++orig->refcount; |
| 123 | return orig; |
| 124 | } |
| 125 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 126 | /** |
| 127 | * @brief Duplicate the array of compiled patterns. |
| 128 | * |
| 129 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 130 | * |
| 131 | * @param[in] ctx Libyang context for logging. |
| 132 | * @param[in] orig The patterns sized array to duplicate. |
| 133 | * @return New sized array as a copy of @p orig. |
| 134 | * @return NULL in case of memory allocation error. |
| 135 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 136 | static struct lysc_pattern** |
| 137 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 138 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 139 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 140 | unsigned int u; |
| 141 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 142 | assert(orig); |
| 143 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 144 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 145 | LY_ARRAY_FOR(orig, u) { |
| 146 | dup[u] = lysc_pattern_dup(orig[u]); |
| 147 | LY_ARRAY_INCREMENT(dup); |
| 148 | } |
| 149 | return dup; |
| 150 | } |
| 151 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 152 | /** |
| 153 | * @brief Duplicate compiled range structure. |
| 154 | * |
| 155 | * @param[in] ctx Libyang context for logging. |
| 156 | * @param[in] orig The range structure to be duplicated. |
| 157 | * @return New compiled range structure as a copy of @p orig. |
| 158 | * @return NULL in case of memory allocation error. |
| 159 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 160 | struct lysc_range* |
| 161 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 162 | { |
| 163 | struct lysc_range *dup; |
| 164 | LY_ERR ret; |
| 165 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 166 | assert(orig); |
| 167 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 168 | dup = calloc(1, sizeof *dup); |
| 169 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 170 | if (orig->parts) { |
| 171 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 172 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 173 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 174 | } |
| 175 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 176 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 177 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 178 | |
| 179 | return dup; |
| 180 | cleanup: |
| 181 | free(dup); |
| 182 | (void) ret; /* set but not used due to the return type */ |
| 183 | return NULL; |
| 184 | } |
| 185 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 186 | /** |
| 187 | * @brief Stack for processing if-feature expressions. |
| 188 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 189 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 190 | int size; /**< number of items in the stack */ |
| 191 | int index; /**< first empty item */ |
| 192 | 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] | 193 | }; |
| 194 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 195 | /** |
| 196 | * @brief Add @ref ifftokens into the stack. |
| 197 | * @param[in] stack The if-feature stack to use. |
| 198 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 199 | * @return LY_EMEM in case of memory allocation error |
| 200 | * @return LY_ESUCCESS if the value successfully stored. |
| 201 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 202 | static LY_ERR |
| 203 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 204 | { |
| 205 | if (stack->index == stack->size) { |
| 206 | stack->size += 4; |
| 207 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 208 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 209 | } |
| 210 | stack->stack[stack->index++] = value; |
| 211 | return LY_SUCCESS; |
| 212 | } |
| 213 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 214 | /** |
| 215 | * @brief Get (and remove) the last item form the stack. |
| 216 | * @param[in] stack The if-feature stack to use. |
| 217 | * @return The value from the top of the stack. |
| 218 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 219 | static uint8_t |
| 220 | iff_stack_pop(struct iff_stack *stack) |
| 221 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 222 | assert(stack && stack->index); |
| 223 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 224 | stack->index--; |
| 225 | return stack->stack[stack->index]; |
| 226 | } |
| 227 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 228 | /** |
| 229 | * @brief Clean up the stack. |
| 230 | * @param[in] stack The if-feature stack to use. |
| 231 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 232 | static void |
| 233 | iff_stack_clean(struct iff_stack *stack) |
| 234 | { |
| 235 | stack->size = 0; |
| 236 | free(stack->stack); |
| 237 | } |
| 238 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 239 | /** |
| 240 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 241 | * (libyang format of the if-feature expression). |
| 242 | * @param[in,out] list The 2bits array to modify. |
| 243 | * @param[in] op The operand (@ref ifftokens) to store. |
| 244 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 245 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 246 | static void |
| 247 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 248 | { |
| 249 | uint8_t *item; |
| 250 | uint8_t mask = 3; |
| 251 | |
| 252 | assert(pos >= 0); |
| 253 | assert(op <= 3); /* max 2 bits */ |
| 254 | |
| 255 | item = &list[pos / 4]; |
| 256 | mask = mask << 2 * (pos % 4); |
| 257 | *item = (*item) & ~mask; |
| 258 | *item = (*item) | (op << 2 * (pos % 4)); |
| 259 | } |
| 260 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 261 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 262 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 263 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 264 | /** |
| 265 | * @brief Find a feature of the given name and referenced in the given module. |
| 266 | * |
| 267 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 268 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 269 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 270 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 271 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 272 | * assigned till that time will be still valid. |
| 273 | * |
| 274 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 275 | * @param[in] name Name of the feature including possible prefix. |
| 276 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 277 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 278 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 279 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 280 | 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] | 281 | { |
| 282 | size_t i; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 283 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 284 | |
| 285 | for (i = 0; i < len; ++i) { |
| 286 | if (name[i] == ':') { |
| 287 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 288 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 289 | LY_CHECK_RET(!mod, NULL); |
| 290 | |
| 291 | name = &name[i + 1]; |
| 292 | len = len - i - 1; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 297 | if (mod->implemented) { |
| 298 | /* module is implemented so there is already the compiled schema */ |
| 299 | flist = mod->compiled->features; |
| 300 | } else { |
| 301 | flist = mod->off_features; |
| 302 | } |
| 303 | LY_ARRAY_FOR(flist, i) { |
| 304 | f = &flist[i]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 305 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 306 | return f; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 314 | 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] | 315 | { |
| 316 | const char *name; |
| 317 | unsigned int u; |
| 318 | const struct lys_module *mod; |
| 319 | struct lysp_ext *edef = NULL; |
| 320 | |
| 321 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 322 | ext->insubstmt = ext_p->insubstmt; |
| 323 | ext->insubstmt_index = ext_p->insubstmt_index; |
| 324 | |
| 325 | /* get module where the extension definition should be placed */ |
| 326 | for (u = 0; ext_p->name[u] != ':'; ++u); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 327 | mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 328 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 329 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 330 | LY_EVALID); |
| 331 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 332 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 333 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 334 | ext_p->name, mod->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 335 | LY_EVALID); |
| 336 | name = &ext_p->name[u + 1]; |
| 337 | /* find the extension definition there */ |
| 338 | for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) { |
| 339 | if (!strcmp(name, mod->parsed->extensions[u].name)) { |
| 340 | edef = &mod->parsed->extensions[u]; |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 345 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
| 346 | LY_EVALID); |
| 347 | /* TODO plugins */ |
| 348 | |
| 349 | return LY_SUCCESS; |
| 350 | } |
| 351 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 352 | /** |
| 353 | * @brief Compile information from the if-feature statement |
| 354 | * @param[in] ctx Compile context. |
| 355 | * @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] | 356 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 357 | * @return LY_ERR value. |
| 358 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 359 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 360 | 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] | 361 | { |
| 362 | const char *c = *value; |
| 363 | int r, rc = EXIT_FAILURE; |
| 364 | int i, j, last_not, checkversion = 0; |
| 365 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 366 | uint8_t op; |
| 367 | struct iff_stack stack = {0, 0, NULL}; |
| 368 | struct lysc_feature *f; |
| 369 | |
| 370 | assert(c); |
| 371 | |
| 372 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 373 | for (i = j = last_not = 0; c[i]; i++) { |
| 374 | if (c[i] == '(') { |
| 375 | j++; |
| 376 | checkversion = 1; |
| 377 | continue; |
| 378 | } else if (c[i] == ')') { |
| 379 | j--; |
| 380 | continue; |
| 381 | } else if (isspace(c[i])) { |
| 382 | checkversion = 1; |
| 383 | continue; |
| 384 | } |
| 385 | |
| 386 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 387 | if (c[i + r] == '\0') { |
| 388 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 389 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 390 | return LY_EVALID; |
| 391 | } else if (!isspace(c[i + r])) { |
| 392 | /* feature name starting with the not/and/or */ |
| 393 | last_not = 0; |
| 394 | f_size++; |
| 395 | } else if (c[i] == 'n') { /* not operation */ |
| 396 | if (last_not) { |
| 397 | /* double not */ |
| 398 | expr_size = expr_size - 2; |
| 399 | last_not = 0; |
| 400 | } else { |
| 401 | last_not = 1; |
| 402 | } |
| 403 | } else { /* and, or */ |
| 404 | f_exp++; |
| 405 | /* not a not operation */ |
| 406 | last_not = 0; |
| 407 | } |
| 408 | i += r; |
| 409 | } else { |
| 410 | f_size++; |
| 411 | last_not = 0; |
| 412 | } |
| 413 | expr_size++; |
| 414 | |
| 415 | while (!isspace(c[i])) { |
| 416 | if (!c[i] || c[i] == ')') { |
| 417 | i--; |
| 418 | break; |
| 419 | } |
| 420 | i++; |
| 421 | } |
| 422 | } |
| 423 | if (j || f_exp != f_size) { |
| 424 | /* not matching count of ( and ) */ |
| 425 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 426 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 427 | return LY_EVALID; |
| 428 | } |
| 429 | |
| 430 | if (checkversion || expr_size > 1) { |
| 431 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 432 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 433 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 434 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 435 | return LY_EVALID; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /* allocate the memory */ |
| 440 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 441 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 442 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 443 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 444 | |
| 445 | stack.size = expr_size; |
| 446 | f_size--; expr_size--; /* used as indexes from now */ |
| 447 | |
| 448 | for (i--; i >= 0; i--) { |
| 449 | if (c[i] == ')') { |
| 450 | /* push it on stack */ |
| 451 | iff_stack_push(&stack, LYS_IFF_RP); |
| 452 | continue; |
| 453 | } else if (c[i] == '(') { |
| 454 | /* pop from the stack into result all operators until ) */ |
| 455 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 456 | iff_setop(iff->expr, op, expr_size--); |
| 457 | } |
| 458 | continue; |
| 459 | } else if (isspace(c[i])) { |
| 460 | continue; |
| 461 | } |
| 462 | |
| 463 | /* end of operator or operand -> find beginning and get what is it */ |
| 464 | j = i + 1; |
| 465 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 466 | i--; |
| 467 | } |
| 468 | i++; /* go back by one step */ |
| 469 | |
| 470 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 471 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 472 | /* double not */ |
| 473 | iff_stack_pop(&stack); |
| 474 | } else { |
| 475 | /* not has the highest priority, so do not pop from the stack |
| 476 | * as in case of AND and OR */ |
| 477 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 478 | } |
| 479 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 480 | /* as for OR - pop from the stack all operators with the same or higher |
| 481 | * priority and store them to the result, then push the AND to the stack */ |
| 482 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 483 | op = iff_stack_pop(&stack); |
| 484 | iff_setop(iff->expr, op, expr_size--); |
| 485 | } |
| 486 | iff_stack_push(&stack, LYS_IFF_AND); |
| 487 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 488 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 489 | op = iff_stack_pop(&stack); |
| 490 | iff_setop(iff->expr, op, expr_size--); |
| 491 | } |
| 492 | iff_stack_push(&stack, LYS_IFF_OR); |
| 493 | } else { |
| 494 | /* feature name, length is j - i */ |
| 495 | |
| 496 | /* add it to the expression */ |
| 497 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 498 | |
| 499 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 500 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 501 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 502 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 503 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 504 | iff->features[f_size] = f; |
| 505 | LY_ARRAY_INCREMENT(iff->features); |
| 506 | f_size--; |
| 507 | } |
| 508 | } |
| 509 | while (stack.index) { |
| 510 | op = iff_stack_pop(&stack); |
| 511 | iff_setop(iff->expr, op, expr_size--); |
| 512 | } |
| 513 | |
| 514 | if (++expr_size || ++f_size) { |
| 515 | /* not all expected operators and operands found */ |
| 516 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 517 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 518 | rc = LY_EINT; |
| 519 | } else { |
| 520 | rc = LY_SUCCESS; |
| 521 | } |
| 522 | |
| 523 | error: |
| 524 | /* cleanup */ |
| 525 | iff_stack_clean(&stack); |
| 526 | |
| 527 | return rc; |
| 528 | } |
| 529 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 530 | /** |
| 531 | * @brief Compile information from the when statement |
| 532 | * @param[in] ctx Compile context. |
| 533 | * @param[in] when_p The parsed when statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 534 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 535 | * @return LY_ERR value. |
| 536 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 537 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 538 | 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] | 539 | { |
| 540 | unsigned int u; |
| 541 | LY_ERR ret = LY_SUCCESS; |
| 542 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 543 | *when = calloc(1, sizeof **when); |
| 544 | (*when)->refcount = 1; |
| 545 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
| 546 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 547 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 548 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 549 | 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] | 550 | |
| 551 | done: |
| 552 | return ret; |
| 553 | } |
| 554 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 555 | /** |
| 556 | * @brief Compile information from the must statement |
| 557 | * @param[in] ctx Compile context. |
| 558 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 559 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 560 | * @return LY_ERR value. |
| 561 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 562 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 563 | 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] | 564 | { |
| 565 | unsigned int u; |
| 566 | LY_ERR ret = LY_SUCCESS; |
| 567 | |
| 568 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 569 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
| 570 | |
| 571 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 572 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 573 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 574 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 575 | 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] | 576 | |
| 577 | done: |
| 578 | return ret; |
| 579 | } |
| 580 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 581 | /** |
| 582 | * @brief Compile information from the import statement |
| 583 | * @param[in] ctx Compile context. |
| 584 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 585 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 586 | * @return LY_ERR value. |
| 587 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 588 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 589 | 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] | 590 | { |
| 591 | unsigned int u; |
| 592 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 593 | LY_ERR ret = LY_SUCCESS; |
| 594 | |
| 595 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 596 | 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] | 597 | imp->module = imp_p->module; |
| 598 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 599 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 600 | * 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] | 601 | * 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] | 602 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 603 | /* try to use filepath if present */ |
| 604 | if (imp->module->filepath) { |
| 605 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 606 | !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] | 607 | if (mod != imp->module) { |
| 608 | 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] | 609 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 610 | mod = NULL; |
| 611 | } |
| 612 | } |
| 613 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 614 | 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] | 615 | 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] | 616 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 617 | return LY_ENOTFOUND; |
| 618 | } |
| 619 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | done: |
| 623 | return ret; |
| 624 | } |
| 625 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 626 | /** |
| 627 | * @brief Compile information from the identity statement |
| 628 | * |
| 629 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 630 | * |
| 631 | * @param[in] ctx Compile context. |
| 632 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 633 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 634 | * @param[in,out] ident Prepared (empty) compiled identity 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_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] | 639 | { |
| 640 | unsigned int u; |
| 641 | LY_ERR ret = LY_SUCCESS; |
| 642 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 643 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 644 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 645 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 646 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 647 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 648 | 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] | 649 | /* 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] | 650 | 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] | 651 | ident->flags = ident_p->flags; |
| 652 | |
| 653 | done: |
| 654 | return ret; |
| 655 | } |
| 656 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 657 | /** |
| 658 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 659 | * |
| 660 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 661 | * |
| 662 | * @param[in] ctx Compile context for logging. |
| 663 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 664 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 665 | * @return LY_SUCCESS if everything is ok. |
| 666 | * @return LY_EVALID if the identity is derived from itself. |
| 667 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 668 | static LY_ERR |
| 669 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 670 | { |
| 671 | LY_ERR ret = LY_EVALID; |
| 672 | unsigned int u, v; |
| 673 | struct ly_set recursion = {0}; |
| 674 | struct lysc_ident *drv; |
| 675 | |
| 676 | if (!derived) { |
| 677 | return LY_SUCCESS; |
| 678 | } |
| 679 | |
| 680 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 681 | if (ident == derived[u]) { |
| 682 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 683 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 684 | goto cleanup; |
| 685 | } |
| 686 | ly_set_add(&recursion, derived[u], 0); |
| 687 | } |
| 688 | |
| 689 | for (v = 0; v < recursion.count; ++v) { |
| 690 | drv = recursion.objs[v]; |
| 691 | if (!drv->derived) { |
| 692 | continue; |
| 693 | } |
| 694 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 695 | if (ident == drv->derived[u]) { |
| 696 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 697 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 698 | goto cleanup; |
| 699 | } |
| 700 | ly_set_add(&recursion, drv->derived[u], 0); |
| 701 | } |
| 702 | } |
| 703 | ret = LY_SUCCESS; |
| 704 | |
| 705 | cleanup: |
| 706 | ly_set_erase(&recursion, NULL); |
| 707 | return ret; |
| 708 | } |
| 709 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 710 | /** |
| 711 | * @brief Find and process the referenced base identities from another identity or identityref |
| 712 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 713 | * 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] | 714 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 715 | * to distinguish these two use cases. |
| 716 | * |
| 717 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 718 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 719 | * @param[in] ident Referencing identity to work with. |
| 720 | * @param[in] bases Array of bases of identityref to fill in. |
| 721 | * @return LY_ERR value. |
| 722 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 723 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 724 | 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] | 725 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 726 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 727 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 728 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 729 | struct lysc_ident **idref; |
| 730 | |
| 731 | assert(ident || bases); |
| 732 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 733 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 735 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 736 | return LY_EVALID; |
| 737 | } |
| 738 | |
| 739 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 740 | s = strchr(bases_p[u], ':'); |
| 741 | if (s) { |
| 742 | /* prefixed identity */ |
| 743 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 744 | 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] | 745 | } else { |
| 746 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 747 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 748 | } |
| 749 | if (!mod) { |
| 750 | if (ident) { |
| 751 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 752 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 753 | } else { |
| 754 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 755 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 756 | } |
| 757 | return LY_EVALID; |
| 758 | } |
| 759 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 760 | if (mod->compiled && mod->compiled->identities) { |
| 761 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 762 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 763 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 764 | if (ident == &mod->compiled->identities[v]) { |
| 765 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 766 | "Identity \"%s\" is derived from itself.", ident->name); |
| 767 | return LY_EVALID; |
| 768 | } |
| 769 | 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] | 770 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 771 | 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] | 772 | *idref = ident; |
| 773 | } else { |
| 774 | /* we have match! store the found identity */ |
| 775 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 776 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 777 | } |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | if (!idref || !(*idref)) { |
| 783 | if (ident) { |
| 784 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 785 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 786 | } else { |
| 787 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 788 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 789 | } |
| 790 | return LY_EVALID; |
| 791 | } |
| 792 | } |
| 793 | return LY_SUCCESS; |
| 794 | } |
| 795 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 796 | /** |
| 797 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 798 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 799 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 800 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 801 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 802 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 803 | static LY_ERR |
| 804 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 805 | { |
| 806 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 807 | |
| 808 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 809 | if (!idents_p[i].bases) { |
| 810 | continue; |
| 811 | } |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 812 | LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 813 | } |
| 814 | return LY_SUCCESS; |
| 815 | } |
| 816 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 817 | LY_ERR |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 818 | lys_feature_precompile(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] | 819 | { |
| 820 | unsigned int offset = 0, u; |
| 821 | struct lysc_ctx context = {0}; |
| 822 | |
| 823 | assert(ctx); |
| 824 | context.ctx = ctx; |
| 825 | |
| 826 | if (!features_p) { |
| 827 | return LY_SUCCESS; |
| 828 | } |
| 829 | if (*features) { |
| 830 | offset = LY_ARRAY_SIZE(*features); |
| 831 | } |
| 832 | |
| 833 | LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM); |
| 834 | LY_ARRAY_FOR(features_p, u) { |
| 835 | LY_ARRAY_INCREMENT(*features); |
| 836 | COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 837 | DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name); |
| 838 | DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 839 | DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref); |
| 840 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 841 | (*features)[offset + u].module = module; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | return LY_SUCCESS; |
| 845 | } |
| 846 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 847 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 848 | * @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] | 849 | * |
| 850 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 851 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 852 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 853 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 854 | * being currently processed). |
| 855 | * @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] | 856 | * @return LY_SUCCESS if everything is ok. |
| 857 | * @return LY_EVALID if the feature references indirectly itself. |
| 858 | */ |
| 859 | static LY_ERR |
| 860 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 861 | { |
| 862 | LY_ERR ret = LY_EVALID; |
| 863 | unsigned int u, v; |
| 864 | struct ly_set recursion = {0}; |
| 865 | struct lysc_feature *drv; |
| 866 | |
| 867 | if (!depfeatures) { |
| 868 | return LY_SUCCESS; |
| 869 | } |
| 870 | |
| 871 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 872 | if (feature == depfeatures[u]) { |
| 873 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 874 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 875 | goto cleanup; |
| 876 | } |
| 877 | ly_set_add(&recursion, depfeatures[u], 0); |
| 878 | } |
| 879 | |
| 880 | for (v = 0; v < recursion.count; ++v) { |
| 881 | drv = recursion.objs[v]; |
| 882 | if (!drv->depfeatures) { |
| 883 | continue; |
| 884 | } |
| 885 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 886 | if (feature == drv->depfeatures[u]) { |
| 887 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 888 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 889 | goto cleanup; |
| 890 | } |
| 891 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 892 | } |
| 893 | } |
| 894 | ret = LY_SUCCESS; |
| 895 | |
| 896 | cleanup: |
| 897 | ly_set_erase(&recursion, NULL); |
| 898 | return ret; |
| 899 | } |
| 900 | |
| 901 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 902 | * @brief Create pre-compiled features array. |
| 903 | * |
| 904 | * See lys_feature_precompile() for more details. |
| 905 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 906 | * @param[in] ctx Compile context. |
| 907 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 908 | * @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] | 909 | * @return LY_ERR value. |
| 910 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 911 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 912 | 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] | 913 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 914 | unsigned int u, v, x; |
| 915 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 916 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 917 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 918 | /* find the preprecompiled feature */ |
| 919 | LY_ARRAY_FOR(features, x) { |
| 920 | if (strcmp(features[x].name, feature_p->name)) { |
| 921 | continue; |
| 922 | } |
| 923 | feature = &features[x]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 924 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 925 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 926 | COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, u, lys_compile_ext, ret, done); |
| 927 | 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] | 928 | if (feature->iffeatures) { |
| 929 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 930 | if (feature->iffeatures[u].features) { |
| 931 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 932 | /* check for circular dependency - direct reference first,... */ |
| 933 | if (feature == feature->iffeatures[u].features[v]) { |
| 934 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 935 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 936 | return LY_EVALID; |
| 937 | } |
| 938 | /* ... and indirect circular reference */ |
| 939 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 940 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 941 | /* add itself into the dependants list */ |
| 942 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 943 | *df = feature; |
| 944 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 945 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 946 | } |
| 947 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 948 | done: |
| 949 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 950 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 951 | |
| 952 | LOGINT(ctx->ctx); |
| 953 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 954 | } |
| 955 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 956 | /** |
| 957 | * @brief Revert compiled list of features back to the precompiled state. |
| 958 | * |
| 959 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 960 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 961 | * |
| 962 | * @param[in] ctx Compilation context. |
| 963 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 964 | * and supposed to hold the off_features list. |
| 965 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 966 | static void |
| 967 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 968 | { |
| 969 | unsigned int u, v; |
| 970 | |
| 971 | /* keep the off_features list until the complete lys_module is freed */ |
| 972 | mod->off_features = mod->compiled->features; |
| 973 | mod->compiled->features = NULL; |
| 974 | |
| 975 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 976 | * which may points into the data being freed here */ |
| 977 | LY_ARRAY_FOR(mod->off_features, u) { |
| 978 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 979 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 980 | } |
| 981 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 982 | mod->off_features[u].iffeatures = NULL; |
| 983 | |
| 984 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 985 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 986 | } |
| 987 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 988 | mod->off_features[u].exts = NULL; |
| 989 | } |
| 990 | } |
| 991 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 992 | /** |
| 993 | * @brief Validate and normalize numeric value from a range definition. |
| 994 | * @param[in] ctx Compile context. |
| 995 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 996 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 997 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 998 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 999 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1000 | * @param[in] value String value of the range boundary. |
| 1001 | * @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. |
| 1002 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1003 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1004 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1005 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1006 | 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] | 1007 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1008 | size_t fraction = 0, size; |
| 1009 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1010 | *len = 0; |
| 1011 | |
| 1012 | assert(value); |
| 1013 | /* parse value */ |
| 1014 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1015 | return LY_EVALID; |
| 1016 | } |
| 1017 | |
| 1018 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1019 | ++(*len); |
| 1020 | } |
| 1021 | |
| 1022 | while (isdigit(value[*len])) { |
| 1023 | ++(*len); |
| 1024 | } |
| 1025 | |
| 1026 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1027 | if (basetype == LY_TYPE_DEC64) { |
| 1028 | goto decimal; |
| 1029 | } else { |
| 1030 | *valcopy = strndup(value, *len); |
| 1031 | return LY_SUCCESS; |
| 1032 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1033 | } |
| 1034 | fraction = *len; |
| 1035 | |
| 1036 | ++(*len); |
| 1037 | while (isdigit(value[*len])) { |
| 1038 | ++(*len); |
| 1039 | } |
| 1040 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1041 | if (basetype == LY_TYPE_DEC64) { |
| 1042 | decimal: |
| 1043 | assert(frdigits); |
| 1044 | if (*len - 1 - fraction > frdigits) { |
| 1045 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1046 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1047 | *len, value, frdigits); |
| 1048 | return LY_EINVAL; |
| 1049 | } |
| 1050 | if (fraction) { |
| 1051 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1052 | } else { |
| 1053 | size = (*len) + frdigits + 1; |
| 1054 | } |
| 1055 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1056 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1057 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1058 | (*valcopy)[size - 1] = '\0'; |
| 1059 | if (fraction) { |
| 1060 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1061 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1062 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1063 | } else { |
| 1064 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1065 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1066 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1067 | } |
| 1068 | return LY_SUCCESS; |
| 1069 | } |
| 1070 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1071 | /** |
| 1072 | * @brief Check that values in range are in ascendant order. |
| 1073 | * @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] | 1074 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1075 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1076 | * @param[in] value Current value to check. |
| 1077 | * @param[in] prev_value The last seen value. |
| 1078 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1079 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1080 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1081 | 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] | 1082 | { |
| 1083 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1084 | 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] | 1085 | return LY_EEXIST; |
| 1086 | } |
| 1087 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1088 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1089 | return LY_EEXIST; |
| 1090 | } |
| 1091 | } |
| 1092 | return LY_SUCCESS; |
| 1093 | } |
| 1094 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1095 | /** |
| 1096 | * @brief Set min/max value of the range part. |
| 1097 | * @param[in] ctx Compile context. |
| 1098 | * @param[in] part Range part structure to fill. |
| 1099 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1100 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1101 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1102 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1103 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1104 | * @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] | 1105 | * @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] | 1106 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1107 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1108 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1109 | * frdigits value), LY_EMEM. |
| 1110 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1111 | static LY_ERR |
| 1112 | 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] | 1113 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1114 | { |
| 1115 | LY_ERR ret = LY_SUCCESS; |
| 1116 | char *valcopy = NULL; |
| 1117 | size_t len; |
| 1118 | |
| 1119 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1120 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1121 | LY_CHECK_GOTO(ret, finalize); |
| 1122 | } |
| 1123 | if (!valcopy && base_range) { |
| 1124 | if (max) { |
| 1125 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1126 | } else { |
| 1127 | part->min_64 = base_range->parts[0].min_64; |
| 1128 | } |
| 1129 | if (!first) { |
| 1130 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1131 | } |
| 1132 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1136 | case LY_TYPE_INT8: /* range */ |
| 1137 | if (valcopy) { |
| 1138 | ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64); |
| 1139 | } else if (max) { |
| 1140 | part->max_64 = INT64_C(127); |
| 1141 | } else { |
| 1142 | part->min_64 = INT64_C(-128); |
| 1143 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1144 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1145 | 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] | 1146 | } |
| 1147 | break; |
| 1148 | case LY_TYPE_INT16: /* range */ |
| 1149 | if (valcopy) { |
| 1150 | ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
| 1151 | } else if (max) { |
| 1152 | part->max_64 = INT64_C(32767); |
| 1153 | } else { |
| 1154 | part->min_64 = INT64_C(-32768); |
| 1155 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1156 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1157 | 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] | 1158 | } |
| 1159 | break; |
| 1160 | case LY_TYPE_INT32: /* range */ |
| 1161 | if (valcopy) { |
| 1162 | ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
| 1163 | } else if (max) { |
| 1164 | part->max_64 = INT64_C(2147483647); |
| 1165 | } else { |
| 1166 | part->min_64 = INT64_C(-2147483648); |
| 1167 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1168 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1169 | 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] | 1170 | } |
| 1171 | break; |
| 1172 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1173 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1174 | if (valcopy) { |
| 1175 | ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
| 1176 | max ? &part->max_64 : &part->min_64); |
| 1177 | } else if (max) { |
| 1178 | part->max_64 = INT64_C(9223372036854775807); |
| 1179 | } else { |
| 1180 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1181 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1182 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1183 | 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] | 1184 | } |
| 1185 | break; |
| 1186 | case LY_TYPE_UINT8: /* range */ |
| 1187 | if (valcopy) { |
| 1188 | ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64); |
| 1189 | } else if (max) { |
| 1190 | part->max_u64 = UINT64_C(255); |
| 1191 | } else { |
| 1192 | part->min_u64 = UINT64_C(0); |
| 1193 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1194 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1195 | 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] | 1196 | } |
| 1197 | break; |
| 1198 | case LY_TYPE_UINT16: /* range */ |
| 1199 | if (valcopy) { |
| 1200 | ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
| 1201 | } else if (max) { |
| 1202 | part->max_u64 = UINT64_C(65535); |
| 1203 | } else { |
| 1204 | part->min_u64 = UINT64_C(0); |
| 1205 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1206 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1207 | 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] | 1208 | } |
| 1209 | break; |
| 1210 | case LY_TYPE_UINT32: /* range */ |
| 1211 | if (valcopy) { |
| 1212 | ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64); |
| 1213 | } else if (max) { |
| 1214 | part->max_u64 = UINT64_C(4294967295); |
| 1215 | } else { |
| 1216 | part->min_u64 = UINT64_C(0); |
| 1217 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1218 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1219 | 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] | 1220 | } |
| 1221 | break; |
| 1222 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1223 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1224 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1225 | if (valcopy) { |
| 1226 | ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
| 1227 | } else if (max) { |
| 1228 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1229 | } else { |
| 1230 | part->min_u64 = UINT64_C(0); |
| 1231 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1232 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1233 | 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] | 1234 | } |
| 1235 | break; |
| 1236 | default: |
| 1237 | LOGINT(ctx->ctx); |
| 1238 | ret = LY_EINT; |
| 1239 | } |
| 1240 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1241 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1242 | if (ret == LY_EDENIED) { |
| 1243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1244 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1245 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1246 | } else if (ret == LY_EVALID) { |
| 1247 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1248 | "Invalid %s restriction - invalid value \"%s\".", |
| 1249 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1250 | } else if (ret == LY_EEXIST) { |
| 1251 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1252 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1253 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1254 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1255 | } else if (!ret && value) { |
| 1256 | *value = *value + len; |
| 1257 | } |
| 1258 | free(valcopy); |
| 1259 | return ret; |
| 1260 | } |
| 1261 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1262 | /** |
| 1263 | * @brief Compile the parsed range restriction. |
| 1264 | * @param[in] ctx Compile context. |
| 1265 | * @param[in] range_p Parsed range structure to compile. |
| 1266 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1267 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1268 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1269 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1270 | * range restriction must be more restrictive than the base_range. |
| 1271 | * @param[in,out] range Pointer to the created current range structure. |
| 1272 | * @return LY_ERR value. |
| 1273 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1274 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1275 | 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] | 1276 | struct lysc_range *base_range, struct lysc_range **range) |
| 1277 | { |
| 1278 | LY_ERR ret = LY_EVALID; |
| 1279 | const char *expr; |
| 1280 | struct lysc_range_part *parts = NULL, *part; |
| 1281 | int range_expected = 0, uns; |
| 1282 | unsigned int parts_done = 0, u, v; |
| 1283 | |
| 1284 | assert(range); |
| 1285 | assert(range_p); |
| 1286 | |
| 1287 | expr = range_p->arg; |
| 1288 | while(1) { |
| 1289 | if (isspace(*expr)) { |
| 1290 | ++expr; |
| 1291 | } else if (*expr == '\0') { |
| 1292 | if (range_expected) { |
| 1293 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1294 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1295 | length_restr ? "length" : "range", range_p->arg); |
| 1296 | goto cleanup; |
| 1297 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1298 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1299 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1300 | length_restr ? "length" : "range", range_p->arg); |
| 1301 | goto cleanup; |
| 1302 | } |
| 1303 | parts_done++; |
| 1304 | break; |
| 1305 | } else if (!strncmp(expr, "min", 3)) { |
| 1306 | if (parts) { |
| 1307 | /* min cannot be used elsewhere than in the first part */ |
| 1308 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1309 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1310 | expr - range_p->arg, range_p->arg); |
| 1311 | goto cleanup; |
| 1312 | } |
| 1313 | expr += 3; |
| 1314 | |
| 1315 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1316 | 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] | 1317 | part->max_64 = part->min_64; |
| 1318 | } else if (*expr == '|') { |
| 1319 | if (!parts || range_expected) { |
| 1320 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1321 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1322 | goto cleanup; |
| 1323 | } |
| 1324 | expr++; |
| 1325 | parts_done++; |
| 1326 | /* process next part of the expression */ |
| 1327 | } else if (!strncmp(expr, "..", 2)) { |
| 1328 | expr += 2; |
| 1329 | while (isspace(*expr)) { |
| 1330 | expr++; |
| 1331 | } |
| 1332 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1333 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1334 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1335 | goto cleanup; |
| 1336 | } |
| 1337 | /* continue expecting the upper boundary */ |
| 1338 | range_expected = 1; |
| 1339 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1340 | /* number */ |
| 1341 | if (range_expected) { |
| 1342 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1343 | 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] | 1344 | range_expected = 0; |
| 1345 | } else { |
| 1346 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1347 | 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] | 1348 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1349 | part->max_64 = part->min_64; |
| 1350 | } |
| 1351 | |
| 1352 | /* continue with possible another expression part */ |
| 1353 | } else if (!strncmp(expr, "max", 3)) { |
| 1354 | expr += 3; |
| 1355 | while (isspace(*expr)) { |
| 1356 | expr++; |
| 1357 | } |
| 1358 | if (*expr != '\0') { |
| 1359 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1360 | length_restr ? "length" : "range", expr); |
| 1361 | goto cleanup; |
| 1362 | } |
| 1363 | if (range_expected) { |
| 1364 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1365 | 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] | 1366 | range_expected = 0; |
| 1367 | } else { |
| 1368 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1369 | 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] | 1370 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1371 | part->min_64 = part->max_64; |
| 1372 | } |
| 1373 | } else { |
| 1374 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1375 | length_restr ? "length" : "range", expr); |
| 1376 | goto cleanup; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | /* check with the previous range/length restriction */ |
| 1381 | if (base_range) { |
| 1382 | switch (basetype) { |
| 1383 | case LY_TYPE_BINARY: |
| 1384 | case LY_TYPE_UINT8: |
| 1385 | case LY_TYPE_UINT16: |
| 1386 | case LY_TYPE_UINT32: |
| 1387 | case LY_TYPE_UINT64: |
| 1388 | case LY_TYPE_STRING: |
| 1389 | uns = 1; |
| 1390 | break; |
| 1391 | case LY_TYPE_DEC64: |
| 1392 | case LY_TYPE_INT8: |
| 1393 | case LY_TYPE_INT16: |
| 1394 | case LY_TYPE_INT32: |
| 1395 | case LY_TYPE_INT64: |
| 1396 | uns = 0; |
| 1397 | break; |
| 1398 | default: |
| 1399 | LOGINT(ctx->ctx); |
| 1400 | ret = LY_EINT; |
| 1401 | goto cleanup; |
| 1402 | } |
| 1403 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1404 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1405 | goto baseerror; |
| 1406 | } |
| 1407 | /* current lower bound is not lower than the base */ |
| 1408 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1409 | /* base has single value */ |
| 1410 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1411 | /* both lower bounds are the same */ |
| 1412 | if (parts[u].min_64 != parts[u].max_64) { |
| 1413 | /* current continues with a range */ |
| 1414 | goto baseerror; |
| 1415 | } else { |
| 1416 | /* equal single values, move both forward */ |
| 1417 | ++v; |
| 1418 | continue; |
| 1419 | } |
| 1420 | } else { |
| 1421 | /* base is single value lower than current range, so the |
| 1422 | * value from base range is removed in the current, |
| 1423 | * move only base and repeat checking */ |
| 1424 | ++v; |
| 1425 | --u; |
| 1426 | continue; |
| 1427 | } |
| 1428 | } else { |
| 1429 | /* base is the range */ |
| 1430 | if (parts[u].min_64 == parts[u].max_64) { |
| 1431 | /* current is a single value */ |
| 1432 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1433 | /* current is behind the base range, so base range is omitted, |
| 1434 | * move the base and keep the current for further check */ |
| 1435 | ++v; |
| 1436 | --u; |
| 1437 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1438 | continue; |
| 1439 | } else { |
| 1440 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1441 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1442 | /* higher bound is higher than the current higher bound */ |
| 1443 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1444 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1445 | * continue with the same current, but move the base */ |
| 1446 | --u; |
| 1447 | ++v; |
| 1448 | continue; |
| 1449 | } |
| 1450 | /* current range starts within the base range but end behind it */ |
| 1451 | goto baseerror; |
| 1452 | } else { |
| 1453 | /* current range is smaller than the base, |
| 1454 | * move current, but stay with the base */ |
| 1455 | continue; |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | if (u != parts_done) { |
| 1461 | baseerror: |
| 1462 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1463 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1464 | length_restr ? "length" : "range", range_p->arg); |
| 1465 | goto cleanup; |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | if (!(*range)) { |
| 1470 | *range = calloc(1, sizeof **range); |
| 1471 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1472 | } |
| 1473 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1474 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1475 | if (range_p->eapptag) { |
| 1476 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1477 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1478 | } |
| 1479 | if (range_p->emsg) { |
| 1480 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1481 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1482 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1483 | if (range_p->dsc) { |
| 1484 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1485 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1486 | } |
| 1487 | if (range_p->ref) { |
| 1488 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1489 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1490 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1491 | /* extensions are taken only from the last range by the caller */ |
| 1492 | |
| 1493 | (*range)->parts = parts; |
| 1494 | parts = NULL; |
| 1495 | ret = LY_SUCCESS; |
| 1496 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1497 | LY_ARRAY_FREE(parts); |
| 1498 | |
| 1499 | return ret; |
| 1500 | } |
| 1501 | |
| 1502 | /** |
| 1503 | * @brief Checks pattern syntax. |
| 1504 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1505 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1506 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1507 | * @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] | 1508 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1509 | */ |
| 1510 | static LY_ERR |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1511 | 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] | 1512 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1513 | int idx, idx2, start, end, count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1514 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1515 | int err_code; |
| 1516 | const char *orig_ptr; |
| 1517 | PCRE2_SIZE err_offset; |
| 1518 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1519 | #define URANGE_LEN 19 |
| 1520 | char *ublock2urange[][2] = { |
| 1521 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1522 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1523 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1524 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1525 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1526 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1527 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1528 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1529 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1530 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1531 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1532 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1533 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1534 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1535 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1536 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1537 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1538 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1539 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1540 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1541 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1542 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1543 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1544 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1545 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1546 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1547 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1548 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1549 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1550 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1551 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1552 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1553 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1554 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1555 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1556 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1557 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1558 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1559 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1560 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1561 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1562 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1563 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1564 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1565 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1566 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1567 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1568 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1569 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1570 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1571 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1572 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1573 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1574 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1575 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1576 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1577 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1578 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1579 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1580 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1581 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1582 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1583 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1584 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1585 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1586 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1587 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1588 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1589 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1590 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1591 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1592 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1593 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1594 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1595 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1596 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1597 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1598 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1599 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1600 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1601 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1602 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1603 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1604 | {NULL, NULL} |
| 1605 | }; |
| 1606 | |
| 1607 | /* adjust the expression to a Perl equivalent |
| 1608 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1609 | |
| 1610 | /* we need to replace all "$" with "\$", count them now */ |
| 1611 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1612 | |
| 1613 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1614 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1615 | perl_regex[0] = '\0'; |
| 1616 | |
| 1617 | ptr = perl_regex; |
| 1618 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1619 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1620 | if (orig_ptr[0] == '$') { |
| 1621 | ptr += sprintf(ptr, "\\$"); |
| 1622 | } else if (orig_ptr[0] == '^') { |
| 1623 | ptr += sprintf(ptr, "\\^"); |
| 1624 | } else { |
| 1625 | ptr[0] = orig_ptr[0]; |
| 1626 | ++ptr; |
| 1627 | } |
| 1628 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1629 | ptr[0] = '\0'; |
| 1630 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1631 | |
| 1632 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1633 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1634 | start = ptr - perl_regex; |
| 1635 | |
| 1636 | ptr = strchr(ptr, '}'); |
| 1637 | if (!ptr) { |
| 1638 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1639 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1640 | free(perl_regex); |
| 1641 | return LY_EVALID; |
| 1642 | } |
| 1643 | end = (ptr - perl_regex) + 1; |
| 1644 | |
| 1645 | /* need more space */ |
| 1646 | if (end - start < URANGE_LEN) { |
| 1647 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1648 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1649 | } |
| 1650 | |
| 1651 | /* find our range */ |
| 1652 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1653 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1654 | break; |
| 1655 | } |
| 1656 | } |
| 1657 | if (!ublock2urange[idx][0]) { |
| 1658 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1659 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1660 | free(perl_regex); |
| 1661 | return LY_EVALID; |
| 1662 | } |
| 1663 | |
| 1664 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1665 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1666 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1667 | ++count; |
| 1668 | } |
| 1669 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1670 | --count; |
| 1671 | } |
| 1672 | } |
| 1673 | if (count) { |
| 1674 | /* skip brackets */ |
| 1675 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1676 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1677 | } else { |
| 1678 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1679 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1684 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 1685 | 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] | 1686 | &err_code, &err_offset, NULL); |
| 1687 | if (!code_local) { |
| 1688 | PCRE2_UCHAR err_msg[256] = {0}; |
| 1689 | pcre2_get_error_message(err_code, err_msg, 256); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1690 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1691 | free(perl_regex); |
| 1692 | return LY_EVALID; |
| 1693 | } |
| 1694 | free(perl_regex); |
| 1695 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1696 | if (code) { |
| 1697 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1698 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1699 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | return LY_SUCCESS; |
| 1703 | |
| 1704 | #undef URANGE_LEN |
| 1705 | } |
| 1706 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1707 | /** |
| 1708 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1709 | * @param[in] ctx Compile context. |
| 1710 | * @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] | 1711 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1712 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1713 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1714 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1715 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1716 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1717 | 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] | 1718 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1719 | { |
| 1720 | struct lysc_pattern **pattern; |
| 1721 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1722 | LY_ERR ret = LY_SUCCESS; |
| 1723 | |
| 1724 | /* first, copy the patterns from the base type */ |
| 1725 | if (base_patterns) { |
| 1726 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1727 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1728 | } |
| 1729 | |
| 1730 | LY_ARRAY_FOR(patterns_p, u) { |
| 1731 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1732 | *pattern = calloc(1, sizeof **pattern); |
| 1733 | ++(*pattern)->refcount; |
| 1734 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1735 | 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] | 1736 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1737 | |
| 1738 | if (patterns_p[u].arg[0] == 0x15) { |
| 1739 | (*pattern)->inverted = 1; |
| 1740 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1741 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1742 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1743 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1744 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 1745 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1746 | 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] | 1747 | } |
| 1748 | done: |
| 1749 | return ret; |
| 1750 | } |
| 1751 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1752 | /** |
| 1753 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1754 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1755 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1756 | 0 /* LY_TYPE_UNKNOWN */, |
| 1757 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1758 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1759 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1760 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1761 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 1762 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1763 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1764 | 0 /* LY_TYPE_BOOL */, |
| 1765 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 1766 | 0 /* LY_TYPE_EMPTY */, |
| 1767 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 1768 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 1769 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 1770 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1771 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 1772 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1773 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1774 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1775 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 1776 | }; |
| 1777 | |
| 1778 | /** |
| 1779 | * @brief stringification of the YANG built-in data types |
| 1780 | */ |
| 1781 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 1782 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 1783 | "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] | 1784 | }; |
| 1785 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1786 | /** |
| 1787 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 1788 | * @param[in] ctx Compile context. |
| 1789 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 1790 | * @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] | 1791 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 1792 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 1793 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1794 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1795 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1796 | 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] | 1797 | 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] | 1798 | { |
| 1799 | LY_ERR ret = LY_SUCCESS; |
| 1800 | unsigned int u, v, match; |
| 1801 | int32_t value = 0; |
| 1802 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1803 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1804 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1805 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1806 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 1807 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 1808 | return LY_EVALID; |
| 1809 | } |
| 1810 | |
| 1811 | LY_ARRAY_FOR(enums_p, u) { |
| 1812 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 1813 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1814 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 1815 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1816 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1817 | if (base_enums) { |
| 1818 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 1819 | LY_ARRAY_FOR(base_enums, v) { |
| 1820 | if (!strcmp(e->name, base_enums[v].name)) { |
| 1821 | break; |
| 1822 | } |
| 1823 | } |
| 1824 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 1825 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1826 | "Invalid %s - derived type adds new item \"%s\".", |
| 1827 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 1828 | return LY_EVALID; |
| 1829 | } |
| 1830 | match = v; |
| 1831 | } |
| 1832 | |
| 1833 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1834 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1835 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1836 | e->value = (int32_t)enums_p[u].value; |
| 1837 | if (!u || e->value >= value) { |
| 1838 | value = e->value + 1; |
| 1839 | } |
| 1840 | /* check collision with other values */ |
| 1841 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1842 | if (e->value == (*enums)[v].value) { |
| 1843 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1844 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 1845 | e->value, e->name, (*enums)[v].name); |
| 1846 | return LY_EVALID; |
| 1847 | } |
| 1848 | } |
| 1849 | } else if (base_enums) { |
| 1850 | /* inherit the assigned value */ |
| 1851 | e->value = base_enums[match].value; |
| 1852 | if (!u || e->value >= value) { |
| 1853 | value = e->value + 1; |
| 1854 | } |
| 1855 | } else { |
| 1856 | /* assign value automatically */ |
| 1857 | if (u && value == INT32_MIN) { |
| 1858 | /* counter overflow */ |
| 1859 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1860 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 1861 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 1862 | return LY_EVALID; |
| 1863 | } |
| 1864 | e->value = value++; |
| 1865 | } |
| 1866 | } else { /* LY_TYPE_BITS */ |
| 1867 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1868 | e->value = (int32_t)enums_p[u].value; |
| 1869 | if (!u || (uint32_t)e->value >= position) { |
| 1870 | position = (uint32_t)e->value + 1; |
| 1871 | } |
| 1872 | /* check collision with other values */ |
| 1873 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1874 | if (e->value == (*enums)[v].value) { |
| 1875 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1876 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 1877 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 1878 | return LY_EVALID; |
| 1879 | } |
| 1880 | } |
| 1881 | } else if (base_enums) { |
| 1882 | /* inherit the assigned value */ |
| 1883 | e->value = base_enums[match].value; |
| 1884 | if (!u || (uint32_t)e->value >= position) { |
| 1885 | position = (uint32_t)e->value + 1; |
| 1886 | } |
| 1887 | } else { |
| 1888 | /* assign value automatically */ |
| 1889 | if (u && position == 0) { |
| 1890 | /* counter overflow */ |
| 1891 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1892 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 1893 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 1894 | return LY_EVALID; |
| 1895 | } |
| 1896 | e->value = position++; |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | if (base_enums) { |
| 1901 | /* the assigned values must not change from the derived type */ |
| 1902 | if (e->value != base_enums[match].value) { |
| 1903 | if (basetype == LY_TYPE_ENUM) { |
| 1904 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1905 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 1906 | e->name, base_enums[match].value, e->value); |
| 1907 | } else { |
| 1908 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1909 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 1910 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 1911 | } |
| 1912 | return LY_EVALID; |
| 1913 | } |
| 1914 | } |
| 1915 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1916 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done); |
| 1917 | 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] | 1918 | |
| 1919 | if (basetype == LY_TYPE_BITS) { |
| 1920 | /* keep bits ordered by position */ |
| 1921 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 1922 | if (v != u) { |
| 1923 | memcpy(&storage, e, sizeof *e); |
| 1924 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 1925 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 1926 | } |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | done: |
| 1931 | return ret; |
| 1932 | } |
| 1933 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1934 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 1935 | for ((NODE) = (NODE)->parent; \ |
| 1936 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 1937 | (NODE) = (NODE)->parent); \ |
| 1938 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 1939 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 1940 | TERM; \ |
| 1941 | } |
| 1942 | |
| 1943 | /** |
| 1944 | * @brief Validate the predicate(s) from the leafref path. |
| 1945 | * @param[in] ctx Compile context |
| 1946 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 1947 | * 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] | 1948 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1949 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1950 | * @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] | 1951 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1952 | */ |
| 1953 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1954 | 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] | 1955 | 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] | 1956 | { |
| 1957 | LY_ERR ret = LY_EVALID; |
| 1958 | const struct lys_module *mod; |
| 1959 | const struct lysc_node *src_node, *dst_node; |
| 1960 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 1961 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1962 | unsigned int dest_parent_times, c, u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1963 | const char *start, *end, *pke_end; |
| 1964 | struct ly_set keys = {0}; |
| 1965 | int i; |
| 1966 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1967 | assert(path_context); |
| 1968 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1969 | while (**predicate == '[') { |
| 1970 | start = (*predicate)++; |
| 1971 | |
| 1972 | while (isspace(**predicate)) { |
| 1973 | ++(*predicate); |
| 1974 | } |
| 1975 | LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup); |
| 1976 | while (isspace(**predicate)) { |
| 1977 | ++(*predicate); |
| 1978 | } |
| 1979 | if (**predicate != '=') { |
| 1980 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1981 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 1982 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1983 | goto cleanup; |
| 1984 | } |
| 1985 | ++(*predicate); |
| 1986 | while (isspace(**predicate)) { |
| 1987 | ++(*predicate); |
| 1988 | } |
| 1989 | |
| 1990 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 1991 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1992 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 1993 | goto cleanup; |
| 1994 | } |
| 1995 | --pke_end; |
| 1996 | while (isspace(*pke_end)) { |
| 1997 | --pke_end; |
| 1998 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 1999 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2000 | /* localize path-key-expr */ |
| 2001 | pke_start = path_key_expr = *predicate; |
| 2002 | /* move after the current predicate */ |
| 2003 | *predicate = end + 1; |
| 2004 | |
| 2005 | /* source (must be leaf or leaf-list) */ |
| 2006 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2007 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2008 | if (!mod) { |
| 2009 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2010 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2011 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2012 | goto cleanup; |
| 2013 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2014 | if (!mod->implemented) { |
| 2015 | /* make the module implemented */ |
| 2016 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2017 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2018 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2019 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2020 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2021 | src_node = NULL; |
| 2022 | if (context_node->keys) { |
| 2023 | for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) { |
| 2024 | if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') { |
| 2025 | src_node = (const struct lysc_node*)context_node->keys[u]; |
| 2026 | break; |
| 2027 | } |
| 2028 | } |
| 2029 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2030 | if (!src_node) { |
| 2031 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2032 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2033 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2034 | goto cleanup; |
| 2035 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2036 | |
| 2037 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2038 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2039 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2040 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2041 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2042 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2043 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2044 | *predicate - start, start, src_node->name); |
| 2045 | goto cleanup; |
| 2046 | } |
| 2047 | |
| 2048 | /* destination */ |
| 2049 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2050 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2051 | |
| 2052 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 2053 | if (strncmp(path_key_expr, "current()", 9)) { |
| 2054 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2055 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2056 | *predicate - start, start); |
| 2057 | goto cleanup; |
| 2058 | } |
| 2059 | path_key_expr += 9; |
| 2060 | while (isspace(*path_key_expr)) { |
| 2061 | ++path_key_expr; |
| 2062 | } |
| 2063 | |
| 2064 | if (*path_key_expr != '/') { |
| 2065 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2066 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2067 | *predicate - start, start); |
| 2068 | goto cleanup; |
| 2069 | } |
| 2070 | ++path_key_expr; |
| 2071 | while (isspace(*path_key_expr)) { |
| 2072 | ++path_key_expr; |
| 2073 | } |
| 2074 | |
| 2075 | /* rel-path-keyexpr: |
| 2076 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2077 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2078 | ++dest_parent_times; |
| 2079 | path_key_expr += 2; |
| 2080 | while (isspace(*path_key_expr)) { |
| 2081 | ++path_key_expr; |
| 2082 | } |
| 2083 | if (*path_key_expr != '/') { |
| 2084 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2085 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2086 | *predicate - start, start); |
| 2087 | goto cleanup; |
| 2088 | } |
| 2089 | ++path_key_expr; |
| 2090 | while (isspace(*path_key_expr)) { |
| 2091 | ++path_key_expr; |
| 2092 | } |
| 2093 | |
| 2094 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2095 | * all schema nodes that cannot be instantiated in data tree */ |
| 2096 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2097 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2098 | *predicate - start, start); |
| 2099 | } |
| 2100 | if (!dest_parent_times) { |
| 2101 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2102 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2103 | *predicate - start, start); |
| 2104 | goto cleanup; |
| 2105 | } |
| 2106 | if (path_key_expr == pke_end) { |
| 2107 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2108 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2109 | *predicate - start, start); |
| 2110 | goto cleanup; |
| 2111 | } |
| 2112 | |
| 2113 | while(path_key_expr != pke_end) { |
| 2114 | if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) { |
| 2115 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2116 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2117 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2118 | goto cleanup; |
| 2119 | } |
| 2120 | |
| 2121 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2122 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2123 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2124 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2125 | } |
| 2126 | if (!mod) { |
| 2127 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2128 | "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] | 2129 | *predicate - start, start, dst_len, dst); |
| 2130 | goto cleanup; |
| 2131 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2132 | if (!mod->implemented) { |
| 2133 | /* make the module implemented */ |
| 2134 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2135 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2136 | |
| 2137 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2138 | if (!dst_node) { |
| 2139 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2140 | "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] | 2141 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2142 | goto cleanup; |
| 2143 | } |
| 2144 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2145 | 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] | 2146 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2147 | "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] | 2148 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2149 | goto cleanup; |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | ret = LY_SUCCESS; |
| 2154 | cleanup: |
| 2155 | ly_set_erase(&keys, NULL); |
| 2156 | return ret; |
| 2157 | } |
| 2158 | |
| 2159 | /** |
| 2160 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2161 | * |
| 2162 | * path-arg = absolute-path / relative-path |
| 2163 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2164 | * relative-path = 1*(".." "/") descendant-path |
| 2165 | * |
| 2166 | * @param[in,out] path Path to parse. |
| 2167 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2168 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2169 | * @param[out] name Name of the token. |
| 2170 | * @param[out] nam_len Length of the name. |
| 2171 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2172 | * must not be changed between consecutive calls. -1 if the |
| 2173 | * path is absolute. |
| 2174 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2175 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2176 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2177 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2178 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2179 | int *parent_times, int *has_predicate) |
| 2180 | { |
| 2181 | int par_times = 0; |
| 2182 | |
| 2183 | assert(path && *path); |
| 2184 | assert(parent_times); |
| 2185 | assert(prefix); |
| 2186 | assert(prefix_len); |
| 2187 | assert(name); |
| 2188 | assert(name_len); |
| 2189 | assert(has_predicate); |
| 2190 | |
| 2191 | *prefix = NULL; |
| 2192 | *prefix_len = 0; |
| 2193 | *name = NULL; |
| 2194 | *name_len = 0; |
| 2195 | *has_predicate = 0; |
| 2196 | |
| 2197 | if (!*parent_times) { |
| 2198 | if (!strncmp(*path, "..", 2)) { |
| 2199 | *path += 2; |
| 2200 | ++par_times; |
| 2201 | while (!strncmp(*path, "/..", 3)) { |
| 2202 | *path += 3; |
| 2203 | ++par_times; |
| 2204 | } |
| 2205 | } |
| 2206 | if (par_times) { |
| 2207 | *parent_times = par_times; |
| 2208 | } else { |
| 2209 | *parent_times = -1; |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | if (**path != '/') { |
| 2214 | return LY_EINVAL; |
| 2215 | } |
| 2216 | /* skip '/' */ |
| 2217 | ++(*path); |
| 2218 | |
| 2219 | /* node-identifier ([prefix:]name) */ |
| 2220 | LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len)); |
| 2221 | |
| 2222 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2223 | /* path continues by another token or this is the last token */ |
| 2224 | return LY_SUCCESS; |
| 2225 | } else if ((*path)[0] != '[') { |
| 2226 | /* unexpected character */ |
| 2227 | return LY_EINVAL; |
| 2228 | } else { |
| 2229 | /* predicate starting with [ */ |
| 2230 | *has_predicate = 1; |
| 2231 | return LY_SUCCESS; |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2236 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2237 | * |
| 2238 | * The set of features used for target must be a subset of features used for the leafref. |
| 2239 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2240 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2241 | * |
| 2242 | * @param[in] refnode The leafref node. |
| 2243 | * @param[in] target Tha target node of the leafref. |
| 2244 | * @return LY_SUCCESS or LY_EVALID; |
| 2245 | */ |
| 2246 | static LY_ERR |
| 2247 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2248 | { |
| 2249 | LY_ERR ret = LY_EVALID; |
| 2250 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2251 | unsigned int u, v, count; |
| 2252 | struct ly_set features = {0}; |
| 2253 | |
| 2254 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2255 | if (iter->iffeatures) { |
| 2256 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2257 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2258 | 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] | 2259 | } |
| 2260 | } |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2265 | * So when adding features applicable to the target into the features set, we should not be |
| 2266 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2267 | * to the leafref itself. */ |
| 2268 | count = features.count; |
| 2269 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2270 | if (iter->iffeatures) { |
| 2271 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2272 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2273 | 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] | 2274 | /* new feature was added (or LY_EMEM) */ |
| 2275 | goto cleanup; |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | ret = LY_SUCCESS; |
| 2282 | |
| 2283 | cleanup: |
| 2284 | ly_set_erase(&features, NULL); |
| 2285 | return ret; |
| 2286 | } |
| 2287 | |
| 2288 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2289 | * @brief Validate the leafref path. |
| 2290 | * @param[in] ctx Compile context |
| 2291 | * @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] | 2292 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2293 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2294 | */ |
| 2295 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2296 | 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] | 2297 | { |
| 2298 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2299 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2300 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2301 | const char *id, *prefix, *name; |
| 2302 | size_t prefix_len, name_len; |
| 2303 | int parent_times = 0, has_predicate; |
| 2304 | unsigned int iter, u; |
| 2305 | LY_ERR ret = LY_SUCCESS; |
| 2306 | |
| 2307 | assert(ctx); |
| 2308 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2309 | assert(leafref); |
| 2310 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2311 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2312 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2313 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2314 | if (!iter) { /* first iteration */ |
| 2315 | /* precess ".." in relative paths */ |
| 2316 | if (parent_times > 0) { |
| 2317 | /* move from the context node */ |
| 2318 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2319 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2320 | * all schema nodes that cannot be instantiated in data tree */ |
| 2321 | 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] | 2322 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2323 | } |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2328 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2329 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2330 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2331 | } |
| 2332 | if (!mod) { |
| 2333 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2334 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2335 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2336 | return LY_EVALID; |
| 2337 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2338 | if (!mod->implemented) { |
| 2339 | /* make the module implemented */ |
| 2340 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2341 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2342 | |
| 2343 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2344 | if (!node) { |
| 2345 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2346 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2347 | return LY_EVALID; |
| 2348 | } |
| 2349 | parent = node; |
| 2350 | |
| 2351 | if (has_predicate) { |
| 2352 | /* we have predicate, so the current result must be list */ |
| 2353 | if (node->nodetype != LYS_LIST) { |
| 2354 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2355 | "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] | 2356 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2357 | return LY_EVALID; |
| 2358 | } |
| 2359 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2360 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2361 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | ++iter; |
| 2365 | } |
| 2366 | if (ret) { |
| 2367 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2368 | "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] | 2369 | return LY_EVALID; |
| 2370 | } |
| 2371 | |
| 2372 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2373 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2374 | "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] | 2375 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2376 | return LY_EVALID; |
| 2377 | } |
| 2378 | |
| 2379 | /* check status */ |
| 2380 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2381 | return LY_EVALID; |
| 2382 | } |
| 2383 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2384 | /* check config */ |
| 2385 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2386 | if (node->flags & LYS_CONFIG_R) { |
| 2387 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2388 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2389 | leafref->path); |
| 2390 | return LY_EVALID; |
| 2391 | } |
| 2392 | } |
| 2393 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2394 | /* store the target's type and check for circular chain of leafrefs */ |
| 2395 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2396 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2397 | if (type == (struct lysc_type*)leafref) { |
| 2398 | /* circular chain detected */ |
| 2399 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2400 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2401 | return LY_EVALID; |
| 2402 | } |
| 2403 | } |
| 2404 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2405 | /* check if leafref and its target are under common if-features */ |
| 2406 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2407 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2408 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2409 | leafref->path); |
| 2410 | return LY_EVALID; |
| 2411 | } |
| 2412 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2413 | return LY_SUCCESS; |
| 2414 | } |
| 2415 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2416 | 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] | 2417 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2418 | /** |
| 2419 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2420 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2421 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2422 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2423 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2424 | * @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] | 2425 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2426 | * @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] | 2427 | * @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] | 2428 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2429 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2430 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2431 | * @return LY_ERR value. |
| 2432 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2433 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2434 | 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] | 2435 | 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] | 2436 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2437 | { |
| 2438 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2439 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2440 | struct lysc_type_bin *bin; |
| 2441 | struct lysc_type_num *num; |
| 2442 | struct lysc_type_str *str; |
| 2443 | struct lysc_type_bits *bits; |
| 2444 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2445 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2446 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2447 | struct lysc_type_union *un, *un_aux; |
| 2448 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2449 | |
| 2450 | switch (basetype) { |
| 2451 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2452 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2453 | |
| 2454 | /* 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] | 2455 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2456 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2457 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2458 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2459 | 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] | 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | if (tpdfname) { |
| 2464 | type_p->compiled = *type; |
| 2465 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2466 | } |
| 2467 | break; |
| 2468 | case LY_TYPE_BITS: |
| 2469 | /* RFC 7950 9.7 - bits */ |
| 2470 | bits = (struct lysc_type_bits*)(*type); |
| 2471 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2472 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2473 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2474 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2475 | } |
| 2476 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2477 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2478 | /* 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] | 2479 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2480 | 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] | 2481 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2482 | 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] | 2483 | free(*type); |
| 2484 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2485 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2486 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | if (tpdfname) { |
| 2490 | type_p->compiled = *type; |
| 2491 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2492 | } |
| 2493 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2494 | case LY_TYPE_DEC64: |
| 2495 | dec = (struct lysc_type_dec*)(*type); |
| 2496 | |
| 2497 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2498 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2499 | if (!type_p->fraction_digits) { |
| 2500 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2501 | 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] | 2502 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2503 | 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] | 2504 | free(*type); |
| 2505 | *type = NULL; |
| 2506 | } |
| 2507 | return LY_EVALID; |
| 2508 | } |
| 2509 | } else if (type_p->fraction_digits) { |
| 2510 | /* 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] | 2511 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2512 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2513 | "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] | 2514 | tpdfname); |
| 2515 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2516 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2517 | "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] | 2518 | free(*type); |
| 2519 | *type = NULL; |
| 2520 | } |
| 2521 | return LY_EVALID; |
| 2522 | } |
| 2523 | dec->fraction_digits = type_p->fraction_digits; |
| 2524 | |
| 2525 | /* RFC 7950 9.2.4 - range */ |
| 2526 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2527 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2528 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2529 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2530 | 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] | 2531 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | if (tpdfname) { |
| 2535 | type_p->compiled = *type; |
| 2536 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2537 | } |
| 2538 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2539 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2540 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2541 | |
| 2542 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2543 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2544 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2545 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2546 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2547 | 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] | 2548 | } |
| 2549 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2550 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2551 | } |
| 2552 | |
| 2553 | /* RFC 7950 9.4.5 - pattern */ |
| 2554 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2555 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2556 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2557 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2558 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2559 | } |
| 2560 | |
| 2561 | if (tpdfname) { |
| 2562 | type_p->compiled = *type; |
| 2563 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2564 | } |
| 2565 | break; |
| 2566 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2567 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2568 | |
| 2569 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2570 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2571 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2572 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2573 | } |
| 2574 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2575 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2576 | /* 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] | 2577 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2578 | 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] | 2579 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2580 | 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] | 2581 | free(*type); |
| 2582 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2583 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2584 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | if (tpdfname) { |
| 2588 | type_p->compiled = *type; |
| 2589 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2590 | } |
| 2591 | break; |
| 2592 | case LY_TYPE_INT8: |
| 2593 | case LY_TYPE_UINT8: |
| 2594 | case LY_TYPE_INT16: |
| 2595 | case LY_TYPE_UINT16: |
| 2596 | case LY_TYPE_INT32: |
| 2597 | case LY_TYPE_UINT32: |
| 2598 | case LY_TYPE_INT64: |
| 2599 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2600 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2601 | |
| 2602 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2603 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2604 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2605 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2606 | if (!tpdfname) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2607 | 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] | 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | if (tpdfname) { |
| 2612 | type_p->compiled = *type; |
| 2613 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2614 | } |
| 2615 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2616 | case LY_TYPE_IDENT: |
| 2617 | idref = (struct lysc_type_identityref*)(*type); |
| 2618 | |
| 2619 | /* RFC 7950 9.10.2 - base */ |
| 2620 | if (type_p->bases) { |
| 2621 | if (base) { |
| 2622 | /* only the directly derived identityrefs can contain base specification */ |
| 2623 | if (tpdfname) { |
| 2624 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2625 | "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] | 2626 | tpdfname); |
| 2627 | } else { |
| 2628 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2629 | "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] | 2630 | free(*type); |
| 2631 | *type = NULL; |
| 2632 | } |
| 2633 | return LY_EVALID; |
| 2634 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2635 | 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] | 2636 | } |
| 2637 | |
| 2638 | if (!base && !type_p->flags) { |
| 2639 | /* type derived from identityref built-in type must contain at least one base */ |
| 2640 | if (tpdfname) { |
| 2641 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2642 | } else { |
| 2643 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2644 | free(*type); |
| 2645 | *type = NULL; |
| 2646 | } |
| 2647 | return LY_EVALID; |
| 2648 | } |
| 2649 | |
| 2650 | if (tpdfname) { |
| 2651 | type_p->compiled = *type; |
| 2652 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2653 | } |
| 2654 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2655 | case LY_TYPE_LEAFREF: |
| 2656 | /* RFC 7950 9.9.3 - require-instance */ |
| 2657 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2658 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2659 | if (tpdfname) { |
| 2660 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2661 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2662 | } else { |
| 2663 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2664 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2665 | free(*type); |
| 2666 | *type = NULL; |
| 2667 | } |
| 2668 | return LY_EVALID; |
| 2669 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2670 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2671 | } else if (base) { |
| 2672 | /* inherit */ |
| 2673 | ((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] | 2674 | } else { |
| 2675 | /* default is true */ |
| 2676 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2677 | } |
| 2678 | if (type_p->path) { |
| 2679 | 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] | 2680 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2681 | } else if (base) { |
| 2682 | 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] | 2683 | ((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] | 2684 | } else if (tpdfname) { |
| 2685 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2686 | return LY_EVALID; |
| 2687 | } else { |
| 2688 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2689 | free(*type); |
| 2690 | *type = NULL; |
| 2691 | return LY_EVALID; |
| 2692 | } |
| 2693 | if (tpdfname) { |
| 2694 | type_p->compiled = *type; |
| 2695 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2696 | } |
| 2697 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2698 | case LY_TYPE_INST: |
| 2699 | /* RFC 7950 9.9.3 - require-instance */ |
| 2700 | if (type_p->flags & LYS_SET_REQINST) { |
| 2701 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2702 | } else { |
| 2703 | /* default is true */ |
| 2704 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2705 | } |
| 2706 | |
| 2707 | if (tpdfname) { |
| 2708 | type_p->compiled = *type; |
| 2709 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2710 | } |
| 2711 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2712 | case LY_TYPE_UNION: |
| 2713 | un = (struct lysc_type_union*)(*type); |
| 2714 | |
| 2715 | /* RFC 7950 7.4 - type */ |
| 2716 | if (type_p->types) { |
| 2717 | if (base) { |
| 2718 | /* only the directly derived union can contain types specification */ |
| 2719 | if (tpdfname) { |
| 2720 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2721 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2722 | tpdfname); |
| 2723 | } else { |
| 2724 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2725 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2726 | free(*type); |
| 2727 | *type = NULL; |
| 2728 | } |
| 2729 | return LY_EVALID; |
| 2730 | } |
| 2731 | /* compile the type */ |
| 2732 | additional = 0; |
| 2733 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 2734 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2735 | 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] | 2736 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2737 | /* add space for additional types from the union subtype */ |
| 2738 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 2739 | 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))); |
| 2740 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2741 | un->types = (void*)((uint32_t*)(p) + 1); |
| 2742 | |
| 2743 | /* copy subtypes of the subtype union */ |
| 2744 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 2745 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2746 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2747 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2748 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2749 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 2750 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 2751 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 2752 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2753 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 2754 | /* TODO extensions */ |
| 2755 | |
| 2756 | } else { |
| 2757 | un->types[u + additional] = un_aux->types[v]; |
| 2758 | ++un_aux->types[v]->refcount; |
| 2759 | } |
| 2760 | ++additional; |
| 2761 | LY_ARRAY_INCREMENT(un->types); |
| 2762 | } |
| 2763 | /* compensate u increment in main loop */ |
| 2764 | --additional; |
| 2765 | |
| 2766 | /* free the replaced union subtype */ |
| 2767 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2768 | } else { |
| 2769 | LY_ARRAY_INCREMENT(un->types); |
| 2770 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | if (!base && !type_p->flags) { |
| 2775 | /* type derived from union built-in type must contain at least one type */ |
| 2776 | if (tpdfname) { |
| 2777 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2778 | } else { |
| 2779 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2780 | free(*type); |
| 2781 | *type = NULL; |
| 2782 | } |
| 2783 | return LY_EVALID; |
| 2784 | } |
| 2785 | |
| 2786 | if (tpdfname) { |
| 2787 | type_p->compiled = *type; |
| 2788 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2789 | } |
| 2790 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2791 | case LY_TYPE_BOOL: |
| 2792 | case LY_TYPE_EMPTY: |
| 2793 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2794 | break; |
| 2795 | } |
| 2796 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2797 | done: |
| 2798 | return ret; |
| 2799 | } |
| 2800 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2801 | /** |
| 2802 | * @brief Compile information about the leaf/leaf-list's type. |
| 2803 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2804 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2805 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2806 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2807 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2808 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2809 | * @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] | 2810 | * @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] | 2811 | * @return LY_ERR value. |
| 2812 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2813 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2814 | 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] | 2815 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2816 | { |
| 2817 | LY_ERR ret = LY_SUCCESS; |
| 2818 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2819 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2820 | struct type_context { |
| 2821 | const struct lysp_tpdf *tpdf; |
| 2822 | struct lysp_node *node; |
| 2823 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2824 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2825 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2826 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2827 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2828 | const char *dflt = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2829 | |
| 2830 | (*type) = NULL; |
| 2831 | |
| 2832 | tctx = calloc(1, sizeof *tctx); |
| 2833 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2834 | 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] | 2835 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2836 | ret == LY_SUCCESS; |
| 2837 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2838 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2839 | if (basetype) { |
| 2840 | break; |
| 2841 | } |
| 2842 | |
| 2843 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2844 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2845 | 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] | 2846 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2847 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2848 | if (units && !*units) { |
| 2849 | /* inherit units */ |
| 2850 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2851 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2852 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2853 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2854 | dflt = tctx->tpdf->dflt; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2855 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2856 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2857 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2858 | break; |
| 2859 | } |
| 2860 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2861 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2862 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2863 | * 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] | 2864 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2865 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2866 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2867 | dummyloops = 1; |
| 2868 | goto preparenext; |
| 2869 | } else { |
| 2870 | tctx = NULL; |
| 2871 | break; |
| 2872 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2873 | } |
| 2874 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2875 | /* circular typedef reference detection */ |
| 2876 | for (u = 0; u < tpdf_chain.count; u++) { |
| 2877 | /* local part */ |
| 2878 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 2879 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2880 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2881 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2882 | free(tctx); |
| 2883 | ret = LY_EVALID; |
| 2884 | goto cleanup; |
| 2885 | } |
| 2886 | } |
| 2887 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 2888 | /* global part for unions corner case */ |
| 2889 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 2890 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2891 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2892 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2893 | free(tctx); |
| 2894 | ret = LY_EVALID; |
| 2895 | goto cleanup; |
| 2896 | } |
| 2897 | } |
| 2898 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2899 | /* store information for the following processing */ |
| 2900 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2901 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2902 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2903 | /* prepare next loop */ |
| 2904 | tctx_prev = tctx; |
| 2905 | tctx = calloc(1, sizeof *tctx); |
| 2906 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2907 | } |
| 2908 | free(tctx); |
| 2909 | |
| 2910 | /* allocate type according to the basetype */ |
| 2911 | switch (basetype) { |
| 2912 | case LY_TYPE_BINARY: |
| 2913 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2914 | break; |
| 2915 | case LY_TYPE_BITS: |
| 2916 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2917 | break; |
| 2918 | case LY_TYPE_BOOL: |
| 2919 | case LY_TYPE_EMPTY: |
| 2920 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2921 | break; |
| 2922 | case LY_TYPE_DEC64: |
| 2923 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2924 | break; |
| 2925 | case LY_TYPE_ENUM: |
| 2926 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2927 | break; |
| 2928 | case LY_TYPE_IDENT: |
| 2929 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2930 | break; |
| 2931 | case LY_TYPE_INST: |
| 2932 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2933 | break; |
| 2934 | case LY_TYPE_LEAFREF: |
| 2935 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2936 | break; |
| 2937 | case LY_TYPE_STRING: |
| 2938 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2939 | break; |
| 2940 | case LY_TYPE_UNION: |
| 2941 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2942 | break; |
| 2943 | case LY_TYPE_INT8: |
| 2944 | case LY_TYPE_UINT8: |
| 2945 | case LY_TYPE_INT16: |
| 2946 | case LY_TYPE_UINT16: |
| 2947 | case LY_TYPE_INT32: |
| 2948 | case LY_TYPE_UINT32: |
| 2949 | case LY_TYPE_INT64: |
| 2950 | case LY_TYPE_UINT64: |
| 2951 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2952 | break; |
| 2953 | case LY_TYPE_UNKNOWN: |
| 2954 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2955 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 2956 | ret = LY_EVALID; |
| 2957 | goto cleanup; |
| 2958 | } |
| 2959 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2960 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2961 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 2962 | ly_data_type2str[basetype]); |
| 2963 | free(*type); |
| 2964 | (*type) = NULL; |
| 2965 | ret = LY_EVALID; |
| 2966 | goto cleanup; |
| 2967 | } |
| 2968 | |
| 2969 | /* get restrictions from the referred typedefs */ |
| 2970 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 2971 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2972 | |
| 2973 | /* remember the typedef context for circular check */ |
| 2974 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2975 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2976 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2977 | base = tctx->tpdf->type.compiled; |
| 2978 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2979 | } 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] | 2980 | /* no change, just use the type information from the base */ |
| 2981 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 2982 | ++base->refcount; |
| 2983 | continue; |
| 2984 | } |
| 2985 | |
| 2986 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2987 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 2988 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 2989 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 2990 | ret = LY_EVALID; |
| 2991 | goto cleanup; |
| 2992 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 2993 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2994 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 2995 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 2996 | ret = LY_EVALID; |
| 2997 | goto cleanup; |
| 2998 | } |
| 2999 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3000 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3001 | /* TODO user type plugins */ |
| 3002 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3003 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3004 | 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] | 3005 | 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] | 3006 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3007 | LY_CHECK_GOTO(ret, cleanup); |
| 3008 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3009 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3010 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3011 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3012 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3013 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3014 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3015 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3016 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3017 | /* TODO user type plugins */ |
| 3018 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3019 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3020 | 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] | 3021 | LY_CHECK_GOTO(ret, cleanup); |
| 3022 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3023 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3024 | free(*type); |
| 3025 | (*type) = base; |
| 3026 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3027 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3028 | if (!(*type)->dflt) { |
| 3029 | DUP_STRING(ctx->ctx, dflt, (*type)->dflt); |
| 3030 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3031 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3032 | 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] | 3033 | |
| 3034 | cleanup: |
| 3035 | ly_set_erase(&tpdf_chain, free); |
| 3036 | return ret; |
| 3037 | } |
| 3038 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3039 | /** |
| 3040 | * @brief Compile status information of the given node. |
| 3041 | * |
| 3042 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3043 | * has the status correctly set during the compilation. |
| 3044 | * |
| 3045 | * @param[in] ctx Compile context |
| 3046 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3047 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3048 | * the compatibility with the parent's status value. |
| 3049 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3050 | * @return LY_ERR value. |
| 3051 | */ |
| 3052 | static LY_ERR |
| 3053 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3054 | { |
| 3055 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3056 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3057 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3058 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3059 | if ((parent_flags & 0x3) != 0x3) { |
| 3060 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3061 | * combination of bits (0x3) which marks the uses_status value */ |
| 3062 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3063 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3064 | } |
| 3065 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3066 | } else { |
| 3067 | (*node_flags) |= LYS_STATUS_CURR; |
| 3068 | } |
| 3069 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3070 | /* check status compatibility with the parent */ |
| 3071 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3072 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3073 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3074 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3075 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3076 | } else { /* LYS_STATUS_DEPRC */ |
| 3077 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3078 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3079 | } |
| 3080 | return LY_EVALID; |
| 3081 | } |
| 3082 | } |
| 3083 | return LY_SUCCESS; |
| 3084 | } |
| 3085 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3086 | /** |
| 3087 | * @brief Check uniqness of the node/action/notification name. |
| 3088 | * |
| 3089 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3090 | * structures, but they share the namespace so we need to check their name collisions. |
| 3091 | * |
| 3092 | * @param[in] ctx Compile context. |
| 3093 | * @param[in] children List (linked list) of data nodes to go through. |
| 3094 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3095 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3096 | * @param[in] name Name of the item to find in the given lists. |
| 3097 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3098 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3099 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3100 | */ |
| 3101 | static LY_ERR |
| 3102 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3103 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3104 | const char *name, void *exclude) |
| 3105 | { |
| 3106 | const struct lysc_node *iter; |
| 3107 | unsigned int u; |
| 3108 | |
| 3109 | LY_LIST_FOR(children, iter) { |
| 3110 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3111 | goto error; |
| 3112 | } |
| 3113 | } |
| 3114 | LY_ARRAY_FOR(actions, u) { |
| 3115 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3116 | goto error; |
| 3117 | } |
| 3118 | } |
| 3119 | LY_ARRAY_FOR(notifs, u) { |
| 3120 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3121 | goto error; |
| 3122 | } |
| 3123 | } |
| 3124 | return LY_SUCCESS; |
| 3125 | error: |
| 3126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification"); |
| 3127 | return LY_EEXIST; |
| 3128 | } |
| 3129 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3130 | 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] | 3131 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3132 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3133 | * @brief Compile parsed RPC/action schema node information. |
| 3134 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3135 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3136 | * @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] | 3137 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3138 | * @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). |
| 3139 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3140 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3141 | */ |
| 3142 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3143 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3144 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3145 | { |
| 3146 | LY_ERR ret = LY_SUCCESS; |
| 3147 | struct lysp_node *child_p; |
| 3148 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3149 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3150 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3151 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3152 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3153 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3154 | action_p->name, action)) { |
| 3155 | return LY_EVALID; |
| 3156 | } |
| 3157 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3158 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3159 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3160 | "Action \"%s\" is placed inside %s.", action_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3161 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3162 | return LY_EVALID; |
| 3163 | } |
| 3164 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3165 | action->nodetype = LYS_ACTION; |
| 3166 | action->module = ctx->mod; |
| 3167 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3168 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3169 | action->sp = action_p; |
| 3170 | } |
| 3171 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3172 | |
| 3173 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3174 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3175 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3176 | |
| 3177 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3178 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3179 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3180 | COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
| 3181 | 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] | 3182 | |
| 3183 | /* input */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3184 | COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup); |
| 3185 | COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup); |
| 3186 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3187 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3188 | 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] | 3189 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3190 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3191 | |
| 3192 | /* output */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3193 | COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup); |
| 3194 | COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup); |
| 3195 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3196 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3197 | 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] | 3198 | } |
| 3199 | |
| 3200 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3201 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3202 | return ret; |
| 3203 | } |
| 3204 | |
| 3205 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3206 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3207 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3208 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3209 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3210 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3211 | * @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] | 3212 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3213 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3214 | */ |
| 3215 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3216 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3217 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3218 | { |
| 3219 | LY_ERR ret = LY_SUCCESS; |
| 3220 | struct lysp_node *child_p; |
| 3221 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3222 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3223 | |
| 3224 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3225 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3226 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3227 | notif_p->name, notif)) { |
| 3228 | return LY_EVALID; |
| 3229 | } |
| 3230 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3231 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3232 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3233 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3234 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3235 | return LY_EVALID; |
| 3236 | } |
| 3237 | |
| 3238 | notif->nodetype = LYS_NOTIF; |
| 3239 | notif->module = ctx->mod; |
| 3240 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3241 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3242 | notif->sp = notif_p; |
| 3243 | } |
| 3244 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3245 | |
| 3246 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3247 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3248 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3249 | |
| 3250 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3251 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3252 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3253 | COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
| 3254 | COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup); |
| 3255 | 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] | 3256 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3257 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3258 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3259 | 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] | 3260 | } |
| 3261 | |
| 3262 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3263 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3264 | return ret; |
| 3265 | } |
| 3266 | |
| 3267 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3268 | * @brief Compile parsed container node information. |
| 3269 | * @param[in] ctx Compile context |
| 3270 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3271 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3272 | * is enriched with the container-specific information. |
| 3273 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3274 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3275 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3276 | 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] | 3277 | { |
| 3278 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3279 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3280 | struct lysp_node *child_p; |
| 3281 | unsigned int u; |
| 3282 | LY_ERR ret = LY_SUCCESS; |
| 3283 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3284 | if (cont_p->presence) { |
| 3285 | cont->flags |= LYS_PRESENCE; |
| 3286 | } |
| 3287 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3288 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3289 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3290 | } |
| 3291 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3292 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done); |
| 3293 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3294 | 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] | 3295 | |
| 3296 | done: |
| 3297 | return ret; |
| 3298 | } |
| 3299 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3300 | /* |
| 3301 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3302 | * @param[in] ctx Compile context. |
| 3303 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3304 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3305 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3306 | * @return LY_ERR value. |
| 3307 | */ |
| 3308 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3309 | 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] | 3310 | { |
| 3311 | unsigned int u, v; |
| 3312 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3313 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3314 | 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] | 3315 | leaf->units ? NULL : &leaf->units)); |
| 3316 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3317 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
| 3318 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
| 3319 | DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3320 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3321 | } |
| 3322 | } else { |
| 3323 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
| 3324 | DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3325 | } |
| 3326 | } |
| 3327 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3328 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3329 | ly_set_add(&ctx->unres, leaf, 0); |
| 3330 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3331 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3332 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3333 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3334 | ly_set_add(&ctx->unres, leaf, 0); |
| 3335 | } |
| 3336 | } |
| 3337 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
| 3338 | if (leaf->flags & LYS_SET_DFLT) { |
| 3339 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).", |
| 3340 | leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt); |
| 3341 | return LY_EVALID; |
| 3342 | } |
| 3343 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3344 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3345 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3346 | return LY_EVALID; |
| 3347 | } |
| 3348 | } |
| 3349 | |
| 3350 | if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3351 | /* configuration data values must be unique - so check the default values */ |
| 3352 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3353 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3354 | if (!strcmp(llist->dflts[u], llist->dflts[v])) { |
| 3355 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3356 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]); |
| 3357 | return LY_EVALID; |
| 3358 | } |
| 3359 | } |
| 3360 | } |
| 3361 | } |
| 3362 | |
| 3363 | /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */ |
| 3364 | |
| 3365 | return LY_SUCCESS; |
| 3366 | } |
| 3367 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3368 | /** |
| 3369 | * @brief Compile parsed leaf node information. |
| 3370 | * @param[in] ctx Compile context |
| 3371 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3372 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3373 | * is enriched with the leaf-specific information. |
| 3374 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3375 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3376 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3377 | 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] | 3378 | { |
| 3379 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3380 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3381 | unsigned int u; |
| 3382 | LY_ERR ret = LY_SUCCESS; |
| 3383 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3384 | 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] | 3385 | if (leaf_p->units) { |
| 3386 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3387 | leaf->flags |= LYS_SET_UNITS; |
| 3388 | } |
| 3389 | if (leaf_p->dflt) { |
| 3390 | leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3391 | leaf->flags |= LYS_SET_DFLT; |
| 3392 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3393 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3394 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3395 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3396 | done: |
| 3397 | return ret; |
| 3398 | } |
| 3399 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3400 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3401 | * @brief Compile parsed leaf-list node information. |
| 3402 | * @param[in] ctx Compile context |
| 3403 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3404 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3405 | * is enriched with the leaf-list-specific information. |
| 3406 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3407 | */ |
| 3408 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3409 | 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] | 3410 | { |
| 3411 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3412 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3413 | unsigned int u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3414 | LY_ERR ret = LY_SUCCESS; |
| 3415 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3416 | 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] | 3417 | if (llist_p->units) { |
| 3418 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3419 | llist->flags |= LYS_SET_UNITS; |
| 3420 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3421 | |
| 3422 | if (llist_p->dflts) { |
| 3423 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3424 | LY_ARRAY_FOR(llist_p->dflts, u) { |
| 3425 | DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]); |
| 3426 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3427 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3428 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3432 | if (llist->min) { |
| 3433 | llist->flags |= LYS_MAND_TRUE; |
| 3434 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3435 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3436 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3437 | 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] | 3438 | |
| 3439 | done: |
| 3440 | return ret; |
| 3441 | } |
| 3442 | |
| 3443 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3444 | * @brief Compile information about list's uniques. |
| 3445 | * @param[in] ctx Compile context. |
| 3446 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3447 | * @param[in] uniques Sized array list of unique statements. |
| 3448 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3449 | * @return LY_ERR value. |
| 3450 | */ |
| 3451 | static LY_ERR |
| 3452 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3453 | { |
| 3454 | LY_ERR ret = LY_SUCCESS; |
| 3455 | struct lysc_node_leaf **key, ***unique; |
| 3456 | const char *keystr, *delim; |
| 3457 | size_t len; |
| 3458 | unsigned int v; |
| 3459 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3460 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3461 | |
| 3462 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3463 | config = -1; |
| 3464 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3465 | keystr = uniques[v]; |
| 3466 | while (keystr) { |
| 3467 | delim = strpbrk(keystr, " \t\n"); |
| 3468 | if (delim) { |
| 3469 | len = delim - keystr; |
| 3470 | while (isspace(*delim)) { |
| 3471 | ++delim; |
| 3472 | } |
| 3473 | } else { |
| 3474 | len = strlen(keystr); |
| 3475 | } |
| 3476 | |
| 3477 | /* unique node must be present */ |
| 3478 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3479 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3480 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3481 | if (ret != LY_SUCCESS) { |
| 3482 | if (ret == LY_EDENIED) { |
| 3483 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3484 | "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] | 3485 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3486 | } |
| 3487 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3488 | } else if (flags) { |
| 3489 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3490 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3491 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
| 3492 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3493 | } |
| 3494 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3495 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3496 | /* all referenced leafs must be of the same config type */ |
| 3497 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3498 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3499 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3500 | return LY_EVALID; |
| 3501 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3502 | config = 1; |
| 3503 | } else { /* LYS_CONFIG_R */ |
| 3504 | config = 0; |
| 3505 | } |
| 3506 | |
| 3507 | /* check status */ |
| 3508 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3509 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3510 | |
| 3511 | /* mark leaf as unique */ |
| 3512 | (*key)->flags |= LYS_UNIQUE; |
| 3513 | |
| 3514 | /* next unique value in line */ |
| 3515 | keystr = delim; |
| 3516 | } |
| 3517 | /* next unique definition */ |
| 3518 | } |
| 3519 | |
| 3520 | return LY_SUCCESS; |
| 3521 | } |
| 3522 | |
| 3523 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3524 | * @brief Compile parsed list node information. |
| 3525 | * @param[in] ctx Compile context |
| 3526 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +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 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_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] | 3533 | { |
| 3534 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3535 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3536 | struct lysp_node *child_p; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3537 | struct lysc_node_leaf **key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3538 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3539 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3540 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3541 | LY_ERR ret = LY_SUCCESS; |
| 3542 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3543 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3544 | if (list->min) { |
| 3545 | list->flags |= LYS_MAND_TRUE; |
| 3546 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3547 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3548 | |
| 3549 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3550 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3551 | } |
| 3552 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3553 | 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] | 3554 | |
| 3555 | /* keys */ |
| 3556 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3557 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3558 | return LY_EVALID; |
| 3559 | } |
| 3560 | |
| 3561 | /* find all the keys (must be direct children) */ |
| 3562 | keystr = list_p->key; |
| 3563 | while (keystr) { |
| 3564 | delim = strpbrk(keystr, " \t\n"); |
| 3565 | if (delim) { |
| 3566 | len = delim - keystr; |
| 3567 | while (isspace(*delim)) { |
| 3568 | ++delim; |
| 3569 | } |
| 3570 | } else { |
| 3571 | len = strlen(keystr); |
| 3572 | } |
| 3573 | |
| 3574 | /* key node must be present */ |
| 3575 | LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM); |
| 3576 | *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 3577 | if (!(*key)) { |
| 3578 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3579 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3580 | return LY_EVALID; |
| 3581 | } |
| 3582 | /* keys must be unique */ |
| 3583 | for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) { |
| 3584 | if (*key == list->keys[u]) { |
| 3585 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3586 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3587 | return LY_EVALID; |
| 3588 | } |
| 3589 | } |
| 3590 | /* key must have the same config flag as the list itself */ |
| 3591 | if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) { |
| 3592 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3593 | return LY_EVALID; |
| 3594 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3595 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3596 | /* YANG 1.0 denies key to be of empty type */ |
| 3597 | if ((*key)->type->basetype == LY_TYPE_EMPTY) { |
| 3598 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3599 | "Key of a list can be of type \"empty\" only in YANG 1.1 modules."); |
| 3600 | return LY_EVALID; |
| 3601 | } |
| 3602 | } else { |
| 3603 | /* when and if-feature are illegal on list keys */ |
| 3604 | if ((*key)->when) { |
| 3605 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3606 | "List's key \"%s\" must not have any \"when\" statement.", (*key)->name); |
| 3607 | return LY_EVALID; |
| 3608 | } |
| 3609 | if ((*key)->iffeatures) { |
| 3610 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3611 | "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name); |
| 3612 | return LY_EVALID; |
| 3613 | } |
| 3614 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3615 | |
| 3616 | /* check status */ |
| 3617 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3618 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3619 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3620 | /* ignore default values of the key */ |
| 3621 | if ((*key)->dflt) { |
| 3622 | lydict_remove(ctx->ctx, (*key)->dflt); |
| 3623 | (*key)->dflt = NULL; |
| 3624 | } |
| 3625 | /* mark leaf as key */ |
| 3626 | (*key)->flags |= LYS_KEY; |
| 3627 | |
| 3628 | /* next key value */ |
| 3629 | keystr = delim; |
| 3630 | } |
| 3631 | |
| 3632 | /* uniques */ |
| 3633 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3634 | 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] | 3635 | } |
| 3636 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3637 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3638 | 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] | 3639 | |
| 3640 | done: |
| 3641 | return ret; |
| 3642 | } |
| 3643 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3644 | /** |
| 3645 | * @brief Do some checks and set the default choice's case. |
| 3646 | * |
| 3647 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3648 | * |
| 3649 | * @param[in] ctx Compile context. |
| 3650 | * @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, |
| 3651 | * not the reference to the imported module. |
| 3652 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3653 | * @return LY_ERR value. |
| 3654 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3655 | static LY_ERR |
| 3656 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3657 | { |
| 3658 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3659 | const char *prefix = NULL, *name; |
| 3660 | size_t prefix_len = 0; |
| 3661 | |
| 3662 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3663 | name = strchr(dflt, ':'); |
| 3664 | if (name) { |
| 3665 | prefix = dflt; |
| 3666 | prefix_len = name - prefix; |
| 3667 | ++name; |
| 3668 | } else { |
| 3669 | name = dflt; |
| 3670 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3671 | 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] | 3672 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3673 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3674 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3675 | prefix_len, prefix); |
| 3676 | return LY_EVALID; |
| 3677 | } |
| 3678 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3679 | if (!ch->dflt) { |
| 3680 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3681 | "Default case \"%s\" not found.", dflt); |
| 3682 | return LY_EVALID; |
| 3683 | } |
| 3684 | /* no mandatory nodes directly under the default case */ |
| 3685 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3686 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3687 | break; |
| 3688 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3689 | if (iter->flags & LYS_MAND_TRUE) { |
| 3690 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3691 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3692 | return LY_EVALID; |
| 3693 | } |
| 3694 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3695 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3696 | return LY_SUCCESS; |
| 3697 | } |
| 3698 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3699 | static LY_ERR |
| 3700 | lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch) |
| 3701 | { |
| 3702 | struct lys_module *mod; |
| 3703 | const char *prefix = NULL, *name; |
| 3704 | size_t prefix_len = 0; |
| 3705 | struct lysc_node_case *cs; |
| 3706 | struct lysc_node *node; |
| 3707 | |
| 3708 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3709 | name = strchr(dflt, ':'); |
| 3710 | if (name) { |
| 3711 | prefix = dflt; |
| 3712 | prefix_len = name - prefix; |
| 3713 | ++name; |
| 3714 | } else { |
| 3715 | name = dflt; |
| 3716 | } |
| 3717 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3718 | if (prefix) { |
| 3719 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3720 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3721 | "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. " |
| 3722 | "The prefix does not match any imported module of the deviation module.", |
| 3723 | devnodeid, dflt); |
| 3724 | return LY_EVALID; |
| 3725 | } |
| 3726 | } else { |
| 3727 | mod = ctx->mod; |
| 3728 | } |
| 3729 | /* get the default case */ |
| 3730 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3731 | if (!cs) { |
| 3732 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3733 | "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.", |
| 3734 | devnodeid, dflt); |
| 3735 | return LY_EVALID; |
| 3736 | } |
| 3737 | |
| 3738 | /* check that there is no mandatory node */ |
| 3739 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3740 | if (node->parent != (struct lysc_node*)cs) { |
| 3741 | break; |
| 3742 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3743 | if (node->flags & LYS_MAND_TRUE) { |
| 3744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3745 | "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - " |
| 3746 | "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name); |
| 3747 | return LY_EVALID; |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | /* set the default case in choice */ |
| 3752 | ch->dflt = cs; |
| 3753 | cs->flags |= LYS_SET_DFLT; |
| 3754 | |
| 3755 | return LY_SUCCESS; |
| 3756 | } |
| 3757 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3758 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3759 | * @brief Compile parsed choice node information. |
| 3760 | * @param[in] ctx Compile context |
| 3761 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3762 | * @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] | 3763 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3764 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3765 | */ |
| 3766 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3767 | 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] | 3768 | { |
| 3769 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 3770 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 3771 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3772 | LY_ERR ret = LY_SUCCESS; |
| 3773 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3774 | LY_LIST_FOR(ch_p->child, child_p) { |
| 3775 | if (child_p->nodetype == LYS_CASE) { |
| 3776 | 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] | 3777 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3778 | } |
| 3779 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3780 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3781 | } |
| 3782 | } |
| 3783 | |
| 3784 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3785 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3786 | 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] | 3787 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3788 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3789 | return ret; |
| 3790 | } |
| 3791 | |
| 3792 | /** |
| 3793 | * @brief Compile parsed anydata or anyxml node information. |
| 3794 | * @param[in] ctx Compile context |
| 3795 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3796 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3797 | * is enriched with the any-specific information. |
| 3798 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3799 | */ |
| 3800 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3801 | 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] | 3802 | { |
| 3803 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 3804 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 3805 | unsigned int u; |
| 3806 | LY_ERR ret = LY_SUCCESS; |
| 3807 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3808 | 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] | 3809 | |
| 3810 | if (any->flags & LYS_CONFIG_W) { |
| 3811 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
| 3812 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML)); |
| 3813 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3814 | done: |
| 3815 | return ret; |
| 3816 | } |
| 3817 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3818 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3819 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 3820 | * |
| 3821 | * @param[in] ctx Compile context |
| 3822 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 3823 | * the choice itself is expected instead of a specific case node. |
| 3824 | * @param[in] node Schema node to connect into the list. |
| 3825 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 3826 | */ |
| 3827 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3828 | 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] | 3829 | { |
| 3830 | struct lysc_node **children; |
| 3831 | |
| 3832 | if (node->nodetype == LYS_CASE) { |
| 3833 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 3834 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3835 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3836 | } |
| 3837 | if (children) { |
| 3838 | if (!(*children)) { |
| 3839 | /* first child */ |
| 3840 | *children = node; |
| 3841 | } else if (*children != node) { |
| 3842 | /* by the condition in previous branch we cover the choice/case children |
| 3843 | * - the children list is shared by the choice and the the first case, in addition |
| 3844 | * the first child of each case must be referenced from the case node. So the node is |
| 3845 | * actually always already inserted in case it is the first children - so here such |
| 3846 | * a situation actually corresponds to the first branch */ |
| 3847 | /* insert at the end of the parent's children list */ |
| 3848 | (*children)->prev->next = node; |
| 3849 | node->prev = (*children)->prev; |
| 3850 | (*children)->prev = node; |
| 3851 | |
| 3852 | /* check the name uniqueness */ |
| 3853 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 3854 | lysc_node_notifs(parent), node->name, node)) { |
| 3855 | return LY_EEXIST; |
| 3856 | } |
| 3857 | } |
| 3858 | } |
| 3859 | return LY_SUCCESS; |
| 3860 | } |
| 3861 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3862 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3863 | * @brief Get the XPath context node for the given schema node. |
| 3864 | * @param[in] start The schema node where the XPath expression appears. |
| 3865 | * @return The context node to evaluate XPath expression in given schema node. |
| 3866 | * @return NULL in case the context node is the root node. |
| 3867 | */ |
| 3868 | static struct lysc_node * |
| 3869 | lysc_xpath_context(struct lysc_node *start) |
| 3870 | { |
| 3871 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 3872 | start = start->parent); |
| 3873 | return start; |
| 3874 | } |
| 3875 | |
| 3876 | /** |
| 3877 | * @brief Prepare the case structure in choice node for the new data node. |
| 3878 | * |
| 3879 | * 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 |
| 3880 | * created in the choice when the first child was processed. |
| 3881 | * |
| 3882 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3883 | * @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, |
| 3884 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3885 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 3886 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 3887 | * @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, |
| 3888 | * 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] | 3889 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3890 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3891 | 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] | 3892 | { |
| 3893 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 3894 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 3895 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3896 | unsigned int u; |
| 3897 | LY_ERR ret; |
| 3898 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3899 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3900 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3901 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3902 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 3903 | return NULL; \ |
| 3904 | } \ |
| 3905 | } |
| 3906 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3907 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 3908 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3909 | |
| 3910 | /* we have to add an implicit case node into the parent choice */ |
| 3911 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 3912 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 3913 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3914 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3915 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 3916 | /* the case is already present since the child is not its first children */ |
| 3917 | return (struct lysc_node_case*)ch->cases->prev; |
| 3918 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3919 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3920 | |
| 3921 | /* explicit parent case is not present (this is its first child) */ |
| 3922 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 3923 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 3924 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 3925 | cs->sp = node_p; |
| 3926 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3927 | /* 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] | 3928 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 3929 | |
| 3930 | if (node_p->when) { |
| 3931 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3932 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 3933 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3934 | (*when)->context = lysc_xpath_context(ch->parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 3935 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3936 | 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] | 3937 | } else { |
| 3938 | LOGINT(ctx->ctx); |
| 3939 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3940 | } |
| 3941 | cs->module = ctx->mod; |
| 3942 | cs->prev = (struct lysc_node*)cs; |
| 3943 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3944 | 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] | 3945 | cs->parent = (struct lysc_node*)ch; |
| 3946 | cs->child = child; |
| 3947 | |
| 3948 | return cs; |
| 3949 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 3950 | if (cs) { |
| 3951 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 3952 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3953 | return NULL; |
| 3954 | |
| 3955 | #undef UNIQUE_CHECK |
| 3956 | } |
| 3957 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3958 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3959 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3960 | * |
| 3961 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3962 | * @param[in] node Target node where the config is supposed to be changed. |
| 3963 | * @param[in] config_flag Node's config flag to be applied to the @p node. |
| 3964 | * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging). |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3965 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 3966 | * 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] | 3967 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 3968 | * @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] | 3969 | * @return LY_ERR value. |
| 3970 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3971 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3972 | lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag, |
| 3973 | const char *nodeid, int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3974 | { |
| 3975 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3976 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3977 | |
| 3978 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 3979 | /* nothing to do */ |
| 3980 | return LY_SUCCESS; |
| 3981 | } |
| 3982 | |
| 3983 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3984 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3985 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 3986 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3987 | "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.", |
| 3988 | refine_flag ? "refine" : "deviation", nodeid); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3989 | return LY_EVALID; |
| 3990 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3991 | node->flags |= LYS_SET_CONFIG; |
| 3992 | } else { |
| 3993 | if (node->flags & LYS_SET_CONFIG) { |
| 3994 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 3995 | /* setting config flags, but have node with explicit config true */ |
| 3996 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3997 | "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.", |
| 3998 | refine_flag ? "refine" : "deviation", nodeid); |
| 3999 | return LY_EVALID; |
| 4000 | } |
| 4001 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4002 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4003 | return LY_SUCCESS; |
| 4004 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4005 | } |
| 4006 | node->flags &= ~LYS_CONFIG_MASK; |
| 4007 | node->flags |= config; |
| 4008 | |
| 4009 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4010 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4011 | LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, nodeid, 1, refine_flag)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4012 | } |
| 4013 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4014 | return LY_SUCCESS; |
| 4015 | } |
| 4016 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4017 | /** |
| 4018 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4019 | * |
| 4020 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4021 | * the flag to such parents from a mandatory children. |
| 4022 | * |
| 4023 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4024 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4025 | * (mandatory children was removed). |
| 4026 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4027 | void |
| 4028 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4029 | { |
| 4030 | struct lysc_node *iter; |
| 4031 | |
| 4032 | if (add) { /* set flag */ |
| 4033 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4034 | parent = parent->parent) { |
| 4035 | parent->flags |= LYS_MAND_TRUE; |
| 4036 | } |
| 4037 | } else { /* unset flag */ |
| 4038 | 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] | 4039 | 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] | 4040 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4041 | /* there is another mandatory node */ |
| 4042 | return; |
| 4043 | } |
| 4044 | } |
| 4045 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4046 | parent->flags &= ~LYS_MAND_TRUE; |
| 4047 | } |
| 4048 | } |
| 4049 | } |
| 4050 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4051 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4052 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4053 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4054 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4055 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4056 | */ |
| 4057 | static void |
| 4058 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4059 | { |
| 4060 | unsigned int v; |
| 4061 | size_t len; |
| 4062 | |
| 4063 | len = strlen(aug_p->nodeid); |
| 4064 | LY_ARRAY_FOR(result, v) { |
| 4065 | if (strlen(result[v]->nodeid) <= len) { |
| 4066 | continue; |
| 4067 | } |
| 4068 | if (v < LY_ARRAY_SIZE(result)) { |
| 4069 | /* move the rest of array */ |
| 4070 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4071 | break; |
| 4072 | } |
| 4073 | } |
| 4074 | result[v] = aug_p; |
| 4075 | LY_ARRAY_INCREMENT(result); |
| 4076 | } |
| 4077 | |
| 4078 | /** |
| 4079 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4080 | * |
| 4081 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4082 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4083 | * |
| 4084 | * @param[in] ctx Compile context. |
| 4085 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4086 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4087 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4088 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4089 | * @return LY_ERR value. |
| 4090 | */ |
| 4091 | LY_ERR |
| 4092 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4093 | { |
| 4094 | struct lysp_augment **result = NULL; |
| 4095 | unsigned int u, v; |
| 4096 | size_t count = 0; |
| 4097 | |
| 4098 | assert(augments); |
| 4099 | |
| 4100 | /* get count of the augments in module and all its submodules */ |
| 4101 | if (aug_p) { |
| 4102 | count += LY_ARRAY_SIZE(aug_p); |
| 4103 | } |
| 4104 | LY_ARRAY_FOR(inc_p, u) { |
| 4105 | if (inc_p[u].submodule->augments) { |
| 4106 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4107 | } |
| 4108 | } |
| 4109 | |
| 4110 | if (!count) { |
| 4111 | *augments = NULL; |
| 4112 | return LY_SUCCESS; |
| 4113 | } |
| 4114 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4115 | |
| 4116 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4117 | * together, so there can be even /z/y betwwen them. */ |
| 4118 | LY_ARRAY_FOR(aug_p, u) { |
| 4119 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4120 | } |
| 4121 | LY_ARRAY_FOR(inc_p, u) { |
| 4122 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4123 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4124 | } |
| 4125 | } |
| 4126 | |
| 4127 | *augments = result; |
| 4128 | return LY_SUCCESS; |
| 4129 | } |
| 4130 | |
| 4131 | /** |
| 4132 | * @brief Compile the parsed augment connecting it into its target. |
| 4133 | * |
| 4134 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4135 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4136 | * are already implemented and compiled. |
| 4137 | * |
| 4138 | * @param[in] ctx Compile context. |
| 4139 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4140 | * @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 |
| 4141 | * children in case of the augmenting uses data. |
| 4142 | * @return LY_SUCCESS on success. |
| 4143 | * @return LY_EVALID on failure. |
| 4144 | */ |
| 4145 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4146 | 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] | 4147 | { |
| 4148 | LY_ERR ret = LY_SUCCESS; |
| 4149 | struct lysp_node *node_p, *case_node_p; |
| 4150 | struct lysc_node *target; /* target target of the augment */ |
| 4151 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4152 | struct lysc_when **when, *when_shared; |
| 4153 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4154 | uint16_t flags = 0; |
| 4155 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4156 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4157 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4158 | 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] | 4159 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4160 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4161 | if (ret != LY_SUCCESS) { |
| 4162 | if (ret == LY_EDENIED) { |
| 4163 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4164 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4165 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4166 | } |
| 4167 | return LY_EVALID; |
| 4168 | } |
| 4169 | |
| 4170 | /* check for mandatory nodes |
| 4171 | * - new cases augmenting some choice can have mandatory nodes |
| 4172 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4173 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4174 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4175 | allow_mandatory = 1; |
| 4176 | } |
| 4177 | |
| 4178 | when_shared = NULL; |
| 4179 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4180 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4181 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4182 | && !((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] | 4183 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4184 | && !(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] | 4185 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4186 | "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".", |
| 4187 | aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
| 4188 | return LY_EVALID; |
| 4189 | } |
| 4190 | |
| 4191 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4192 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4193 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4194 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4195 | } else { |
| 4196 | 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] | 4197 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4198 | } |
| 4199 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4200 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4201 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4202 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4203 | * 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] | 4204 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4205 | /* 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] | 4206 | 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] | 4207 | } else if (target->nodetype == LYS_CHOICE) { |
| 4208 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4209 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4210 | } else { |
| 4211 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4212 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4213 | } |
| 4214 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4215 | 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] | 4216 | node->flags &= ~LYS_MAND_TRUE; |
| 4217 | lys_compile_mandatory_parents(target, 0); |
| 4218 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4219 | "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.", |
| 4220 | aug_p->nodeid, node->name); |
| 4221 | return LY_EVALID; |
| 4222 | } |
| 4223 | |
| 4224 | /* pass augment's when to all the children */ |
| 4225 | if (aug_p->when) { |
| 4226 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4227 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4228 | ret = lys_compile_when(ctx, aug_p->when, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4229 | LY_CHECK_GOTO(ret, error); |
| 4230 | (*when)->context = lysc_xpath_context(target); |
| 4231 | when_shared = *when; |
| 4232 | } else { |
| 4233 | ++when_shared->refcount; |
| 4234 | (*when) = when_shared; |
| 4235 | } |
| 4236 | } |
| 4237 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4238 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4239 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4240 | switch (target->nodetype) { |
| 4241 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4242 | 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] | 4243 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4244 | 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] | 4245 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4246 | break; |
| 4247 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4248 | 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] | 4249 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4250 | 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] | 4251 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4252 | break; |
| 4253 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4254 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4255 | if (aug_p->actions) { |
| 4256 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4257 | "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4258 | aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
| 4259 | return LY_EVALID; |
| 4260 | } |
| 4261 | if (aug_p->notifs) { |
| 4262 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4263 | "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".", |
| 4264 | aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
| 4265 | return LY_EVALID; |
| 4266 | } |
| 4267 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4268 | |
| 4269 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4270 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4271 | return ret; |
| 4272 | } |
| 4273 | |
| 4274 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4275 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4276 | * |
| 4277 | * @param[in] ctx Compile context. |
| 4278 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4279 | * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node. |
| 4280 | * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging). |
| 4281 | * @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] | 4282 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4283 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4284 | * 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] | 4285 | * @return LY_ERR value. |
| 4286 | */ |
| 4287 | static LY_ERR |
| 4288 | lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag) |
| 4289 | { |
| 4290 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4291 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4292 | "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.", |
| 4293 | refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype)); |
| 4294 | return LY_EVALID; |
| 4295 | } |
| 4296 | |
| 4297 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4298 | /* check if node has default value */ |
| 4299 | if (node->nodetype & LYS_LEAF) { |
| 4300 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4301 | if (refine_flag) { |
| 4302 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4303 | "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid); |
| 4304 | return LY_EVALID; |
| 4305 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4306 | } else { |
| 4307 | /* remove the default value taken from the leaf's type */ |
| 4308 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt); |
| 4309 | ((struct lysc_node_leaf*)node)->dflt = NULL; |
| 4310 | } |
| 4311 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4312 | if (refine_flag) { |
| 4313 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4314 | "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid); |
| 4315 | return LY_EVALID; |
| 4316 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4317 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4318 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4319 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4320 | "Invalid refine of mandatory in \"%s\" under the default case.", nodeid); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4321 | return LY_EVALID; |
| 4322 | } |
| 4323 | |
| 4324 | node->flags &= ~LYS_MAND_FALSE; |
| 4325 | node->flags |= LYS_MAND_TRUE; |
| 4326 | lys_compile_mandatory_parents(node->parent, 1); |
| 4327 | } else { |
| 4328 | /* make mandatory false */ |
| 4329 | node->flags &= ~LYS_MAND_TRUE; |
| 4330 | node->flags |= LYS_MAND_FALSE; |
| 4331 | lys_compile_mandatory_parents(node->parent, 0); |
| 4332 | if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) { |
| 4333 | /* get the type's default value if any */ |
| 4334 | DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt); |
| 4335 | } |
| 4336 | } |
| 4337 | return LY_SUCCESS; |
| 4338 | } |
| 4339 | |
| 4340 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4341 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4342 | * If present, also apply uses's modificators. |
| 4343 | * |
| 4344 | * @param[in] ctx Compile context |
| 4345 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4346 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4347 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4348 | * the compile context. |
| 4349 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4350 | */ |
| 4351 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4352 | 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] | 4353 | { |
| 4354 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4355 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4356 | /* 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] | 4357 | struct lysc_node_container context_node_fake = |
| 4358 | {.nodetype = LYS_CONTAINER, |
| 4359 | .module = ctx->mod, |
| 4360 | .flags = parent ? parent->flags : 0, |
| 4361 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4362 | .prev = (struct lysc_node*)&context_node_fake, |
| 4363 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4364 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4365 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4366 | int found; |
| 4367 | const char *id, *name, *prefix; |
| 4368 | size_t prefix_len, name_len; |
| 4369 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4370 | struct lysp_refine *rfn; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4371 | LY_ERR ret = LY_EVALID; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4372 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4373 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4374 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4375 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4376 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4377 | unsigned int actions_index, notifs_index; |
| 4378 | struct lysc_notif **notifs = NULL; |
| 4379 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4380 | |
| 4381 | /* search for the grouping definition */ |
| 4382 | found = 0; |
| 4383 | id = uses_p->name; |
Radek Krejci | 15e99fc | 2019-04-08 14:29:39 +0200 | [diff] [blame] | 4384 | LY_CHECK_RET(lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4385 | if (prefix) { |
| 4386 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4387 | if (!mod) { |
| 4388 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4389 | "Invalid prefix used for grouping reference (%s).", uses_p->name); |
| 4390 | return LY_EVALID; |
| 4391 | } |
| 4392 | } else { |
| 4393 | mod = ctx->mod_def; |
| 4394 | } |
| 4395 | if (mod == ctx->mod_def) { |
| 4396 | 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] | 4397 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4398 | LY_ARRAY_FOR(grp, u) { |
| 4399 | if (!strcmp(grp[u].name, name)) { |
| 4400 | grp = &grp[u]; |
| 4401 | found = 1; |
| 4402 | break; |
| 4403 | } |
| 4404 | } |
| 4405 | } |
| 4406 | } |
| 4407 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4408 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4409 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4410 | if (grp) { |
| 4411 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4412 | if (!strcmp(grp[u].name, name)) { |
| 4413 | grp = &grp[u]; |
| 4414 | found = 1; |
| 4415 | } |
| 4416 | } |
| 4417 | } |
| 4418 | if (!found && mod->parsed->includes) { |
| 4419 | /* ... and all the submodules */ |
| 4420 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4421 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4422 | if (grp) { |
| 4423 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4424 | if (!strcmp(grp[v].name, name)) { |
| 4425 | grp = &grp[v]; |
| 4426 | found = 1; |
| 4427 | } |
| 4428 | } |
| 4429 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4430 | } |
| 4431 | } |
| 4432 | } |
| 4433 | if (!found) { |
| 4434 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4435 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4436 | return LY_EVALID; |
| 4437 | } |
| 4438 | |
| 4439 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4440 | grp_stack_count = ctx->groupings.count; |
| 4441 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4442 | if (grp_stack_count == ctx->groupings.count) { |
| 4443 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4444 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4445 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4446 | return LY_EVALID; |
| 4447 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4448 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4449 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4450 | grp->flags |= LYS_USED_GRP; |
| 4451 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4452 | |
| 4453 | /* switch context's mod_def */ |
| 4454 | mod_old = ctx->mod_def; |
| 4455 | ctx->mod_def = mod; |
| 4456 | |
| 4457 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4458 | 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] | 4459 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4460 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4461 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4462 | /* 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] | 4463 | 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] | 4464 | |
| 4465 | /* some preparation for applying refines */ |
| 4466 | if (grp->data == node_p) { |
| 4467 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4468 | if (parent) { |
| 4469 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4470 | } else if (ctx->mod->compiled->data) { |
| 4471 | child = ctx->mod->compiled->data; |
| 4472 | } else { |
| 4473 | child = NULL; |
| 4474 | } |
| 4475 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4476 | } |
| 4477 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4478 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4479 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4480 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4481 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4482 | /* 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] | 4483 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4484 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4485 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4486 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4487 | (*when)->context = lysc_xpath_context(parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4488 | when_shared = *when; |
| 4489 | } else { |
| 4490 | ++when_shared->refcount; |
| 4491 | (*when) = when_shared; |
| 4492 | } |
| 4493 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4494 | } |
| 4495 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4496 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4497 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4498 | /* 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] | 4499 | 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] | 4500 | } |
| 4501 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4502 | /* compile actions */ |
| 4503 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4504 | if (actions) { |
| 4505 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4506 | 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] | 4507 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4508 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4509 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4510 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4511 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | /* compile notifications */ |
| 4516 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4517 | if (notifs) { |
| 4518 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4519 | 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] | 4520 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4521 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4522 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4523 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4524 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4525 | } |
| 4526 | } |
| 4527 | |
| 4528 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4529 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4530 | 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] | 4531 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4532 | 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] | 4533 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4534 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4535 | /* reload previous context's mod_def */ |
| 4536 | ctx->mod_def = mod_old; |
| 4537 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4538 | /* apply refine */ |
| 4539 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4540 | 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] | 4541 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4542 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4543 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4544 | |
| 4545 | /* default value */ |
| 4546 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4547 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4548 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4549 | "Invalid refine of default in \"%s\" - %s cannot hold %d default values.", |
| 4550 | rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4551 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4552 | } |
| 4553 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4554 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4555 | "Invalid refine of default in \"%s\" - %s cannot hold default value(s).", |
| 4556 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4557 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4558 | } |
| 4559 | if (node->nodetype == LYS_LEAF) { |
| 4560 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt); |
| 4561 | 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] | 4562 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4563 | /* TODO check the default value according to type */ |
| 4564 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4565 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4566 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4567 | "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] | 4568 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4569 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4570 | LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) { |
| 4571 | lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]); |
| 4572 | } |
| 4573 | LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4574 | ((struct lysc_node_leaflist*)node)->dflts = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4575 | 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] | 4576 | LY_ARRAY_FOR(rfn->dflts, u) { |
| 4577 | LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts); |
| 4578 | DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]); |
| 4579 | } |
| 4580 | /* TODO check the default values according to type */ |
| 4581 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4582 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4583 | /* unset LYS_SET_DFLT from the current default case */ |
| 4584 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4585 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4586 | 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] | 4587 | } |
| 4588 | } |
| 4589 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4590 | /* description */ |
| 4591 | if (rfn->dsc) { |
| 4592 | FREE_STRING(ctx->ctx, node->dsc); |
| 4593 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4594 | } |
| 4595 | |
| 4596 | /* reference */ |
| 4597 | if (rfn->ref) { |
| 4598 | FREE_STRING(ctx->ctx, node->ref); |
| 4599 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4600 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4601 | |
| 4602 | /* config */ |
| 4603 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4604 | if (!flags) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4605 | LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4606 | } else { |
| 4607 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 4608 | flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path); |
| 4609 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4610 | } |
| 4611 | |
| 4612 | /* mandatory */ |
| 4613 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4614 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4615 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4616 | |
| 4617 | /* presence */ |
| 4618 | if (rfn->presence) { |
| 4619 | if (node->nodetype != LYS_CONTAINER) { |
| 4620 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4621 | "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.", |
| 4622 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4623 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4624 | } |
| 4625 | node->flags |= LYS_PRESENCE; |
| 4626 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4627 | |
| 4628 | /* must */ |
| 4629 | if (rfn->musts) { |
| 4630 | switch (node->nodetype) { |
| 4631 | case LYS_LEAF: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4632 | 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] | 4633 | break; |
| 4634 | case LYS_LEAFLIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4635 | 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] | 4636 | break; |
| 4637 | case LYS_LIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4638 | 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] | 4639 | break; |
| 4640 | case LYS_CONTAINER: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4641 | 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] | 4642 | break; |
| 4643 | case LYS_ANYXML: |
| 4644 | case LYS_ANYDATA: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4645 | 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] | 4646 | break; |
| 4647 | default: |
| 4648 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4649 | "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.", |
| 4650 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4651 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4652 | } |
| 4653 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4654 | |
| 4655 | /* min/max-elements */ |
| 4656 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4657 | switch (node->nodetype) { |
| 4658 | case LYS_LEAFLIST: |
| 4659 | if (rfn->flags & LYS_SET_MAX) { |
| 4660 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4661 | } |
| 4662 | if (rfn->flags & LYS_SET_MIN) { |
| 4663 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4664 | if (rfn->min) { |
| 4665 | node->flags |= LYS_MAND_TRUE; |
| 4666 | lys_compile_mandatory_parents(node->parent, 1); |
| 4667 | } else { |
| 4668 | node->flags &= ~LYS_MAND_TRUE; |
| 4669 | lys_compile_mandatory_parents(node->parent, 0); |
| 4670 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4671 | } |
| 4672 | break; |
| 4673 | case LYS_LIST: |
| 4674 | if (rfn->flags & LYS_SET_MAX) { |
| 4675 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4676 | } |
| 4677 | if (rfn->flags & LYS_SET_MIN) { |
| 4678 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4679 | if (rfn->min) { |
| 4680 | node->flags |= LYS_MAND_TRUE; |
| 4681 | lys_compile_mandatory_parents(node->parent, 1); |
| 4682 | } else { |
| 4683 | node->flags &= ~LYS_MAND_TRUE; |
| 4684 | lys_compile_mandatory_parents(node->parent, 0); |
| 4685 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4686 | } |
| 4687 | break; |
| 4688 | default: |
| 4689 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4690 | "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.", |
| 4691 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4692 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4693 | } |
| 4694 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4695 | |
| 4696 | /* if-feature */ |
| 4697 | if (rfn->iffeatures) { |
| 4698 | /* 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] | 4699 | 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] | 4700 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4701 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4702 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4703 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 4704 | for (u = 0; u < refined.count; ++u) { |
| 4705 | node = (struct lysc_node*)refined.objs[u]; |
| 4706 | rfn = &uses_p->refines[u]; |
| 4707 | |
| 4708 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 4709 | if ((node->flags & LYS_MAND_TRUE) && |
| 4710 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 4711 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 4712 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4713 | "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4714 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4718 | if (node->nodetype == LYS_LIST) { |
| 4719 | min = ((struct lysc_node_list*)node)->min; |
| 4720 | max = ((struct lysc_node_list*)node)->max; |
| 4721 | } else { |
| 4722 | min = ((struct lysc_node_leaflist*)node)->min; |
| 4723 | max = ((struct lysc_node_leaflist*)node)->max; |
| 4724 | } |
| 4725 | if (min > max) { |
| 4726 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4727 | "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".", |
| 4728 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4729 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4730 | } |
| 4731 | } |
| 4732 | } |
| 4733 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4734 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4735 | |
| 4736 | cleanup: |
| 4737 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 4738 | if (context_node_fake.child) { |
| 4739 | context_node_fake.child->prev = child; |
| 4740 | } |
| 4741 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4742 | child->parent = parent; |
| 4743 | } |
| 4744 | |
| 4745 | if (uses_p->augments || uses_p->refines) { |
| 4746 | /* 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] | 4747 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4748 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4749 | LY_ARRAY_FREE(context_node_fake.actions); |
| 4750 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 4751 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4752 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4753 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 4754 | } |
| 4755 | } |
| 4756 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4757 | /* reload previous context's mod_def */ |
| 4758 | ctx->mod_def = mod_old; |
| 4759 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 4760 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 4761 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4762 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4763 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4764 | |
| 4765 | return ret; |
| 4766 | } |
| 4767 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4768 | /** |
| 4769 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 4770 | * |
| 4771 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 4772 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 4773 | */ |
| 4774 | static LY_ERR |
| 4775 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 4776 | { |
| 4777 | LY_ERR ret; |
| 4778 | struct lysp_node_uses fake_uses = { |
| 4779 | .parent = node_p, |
| 4780 | .nodetype = LYS_USES, |
| 4781 | .flags = 0, .next = NULL, |
| 4782 | .name = grp->name, |
| 4783 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 4784 | .refines = NULL, .augments = NULL |
| 4785 | }; |
| 4786 | struct lysc_node_container fake_container = { |
| 4787 | .nodetype = LYS_CONTAINER, |
| 4788 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 4789 | .module = ctx->mod, |
| 4790 | .sp = NULL, .parent = NULL, .next = NULL, |
| 4791 | .prev = (struct lysc_node*)&fake_container, |
| 4792 | .name = "fake", |
| 4793 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 4794 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 4795 | }; |
| 4796 | |
| 4797 | if (grp->parent) { |
| 4798 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 4799 | } |
| 4800 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
| 4801 | |
| 4802 | /* cleanup */ |
| 4803 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 4804 | |
| 4805 | return ret; |
| 4806 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4807 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4808 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4809 | * @brief Compile parsed schema node information. |
| 4810 | * @param[in] ctx Compile context |
| 4811 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4812 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 4813 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4814 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4815 | * @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). |
| 4816 | * 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] | 4817 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4818 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4819 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4820 | 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] | 4821 | { |
| 4822 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4823 | struct lysc_node *node; |
| 4824 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4825 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4826 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4827 | 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] | 4828 | |
| 4829 | switch (node_p->nodetype) { |
| 4830 | case LYS_CONTAINER: |
| 4831 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 4832 | node_compile_spec = lys_compile_node_container; |
| 4833 | break; |
| 4834 | case LYS_LEAF: |
| 4835 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 4836 | node_compile_spec = lys_compile_node_leaf; |
| 4837 | break; |
| 4838 | case LYS_LIST: |
| 4839 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4840 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4841 | break; |
| 4842 | case LYS_LEAFLIST: |
| 4843 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 4844 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4845 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4846 | case LYS_CHOICE: |
| 4847 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4848 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4849 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4850 | case LYS_ANYXML: |
| 4851 | case LYS_ANYDATA: |
| 4852 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4853 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4854 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4855 | case LYS_USES: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4856 | return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4857 | default: |
| 4858 | LOGINT(ctx->ctx); |
| 4859 | return LY_EINT; |
| 4860 | } |
| 4861 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 4862 | node->nodetype = node_p->nodetype; |
| 4863 | node->module = ctx->mod; |
| 4864 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 4865 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4866 | |
| 4867 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4868 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4869 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4870 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4871 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 4872 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4873 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4874 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4875 | node->flags |= LYS_CONFIG_R; |
| 4876 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4877 | /* config not explicitely set, inherit it from parent */ |
| 4878 | if (parent) { |
| 4879 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 4880 | } else { |
| 4881 | /* default is config true */ |
| 4882 | node->flags |= LYS_CONFIG_W; |
| 4883 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4884 | } else { |
| 4885 | /* config set explicitely */ |
| 4886 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4887 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4888 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4889 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4890 | "Configuration node cannot be child of any state data node."); |
| 4891 | goto error; |
| 4892 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4893 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 4894 | /* *list ordering */ |
| 4895 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 4896 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4897 | 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] | 4898 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 4899 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 4900 | node->flags &= ~LYS_ORDBY_MASK; |
| 4901 | node->flags |= LYS_ORDBY_SYSTEM; |
| 4902 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 4903 | /* default ordering is system */ |
| 4904 | node->flags |= LYS_ORDBY_SYSTEM; |
| 4905 | } |
| 4906 | } |
| 4907 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4908 | /* status - it is not inherited by specification, but it does not make sense to have |
| 4909 | * 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] | 4910 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 4911 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 4912 | * 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] | 4913 | 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] | 4914 | } |
| 4915 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4916 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4917 | node->sp = node_p; |
| 4918 | } |
| 4919 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4920 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 4921 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4922 | if (node_p->when) { |
| 4923 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4924 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4925 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4926 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4927 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4928 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error); |
| 4929 | 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] | 4930 | |
| 4931 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4932 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4933 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4934 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 4935 | if (node->flags & LYS_MAND_TRUE) { |
| 4936 | lys_compile_mandatory_parents(parent, 1); |
| 4937 | } |
| 4938 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4939 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4940 | if (parent) { |
| 4941 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4942 | 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] | 4943 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4944 | if (uses_status) { |
| 4945 | |
| 4946 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4947 | /* 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] | 4948 | * it directly gets the same status flags as the choice; |
| 4949 | * 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] | 4950 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4951 | node->parent = (struct lysc_node*)cs; |
| 4952 | } else { /* other than choice */ |
| 4953 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4954 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4955 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4956 | } else { |
| 4957 | /* top-level element */ |
| 4958 | if (!ctx->mod->compiled->data) { |
| 4959 | ctx->mod->compiled->data = node; |
| 4960 | } else { |
| 4961 | /* insert at the end of the module's top-level nodes list */ |
| 4962 | ctx->mod->compiled->data->prev->next = node; |
| 4963 | node->prev = ctx->mod->compiled->data->prev; |
| 4964 | ctx->mod->compiled->data->prev = node; |
| 4965 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4966 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 4967 | ctx->mod->compiled->notifs, node->name, node)) { |
| 4968 | return LY_EVALID; |
| 4969 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4970 | } |
| 4971 | |
| 4972 | return LY_SUCCESS; |
| 4973 | |
| 4974 | error: |
| 4975 | lysc_node_free(ctx->ctx, node); |
| 4976 | return ret; |
| 4977 | } |
| 4978 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4979 | static void |
| 4980 | lysc_disconnect(struct lysc_node *node) |
| 4981 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4982 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 4983 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4984 | int remove_cs = 0; |
| 4985 | |
| 4986 | parent = node->parent; |
| 4987 | |
| 4988 | /* parent's first child */ |
| 4989 | if (!parent) { |
| 4990 | return; |
| 4991 | } |
| 4992 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4993 | cs = (struct lysc_node_case*)node; |
| 4994 | } else if (parent->nodetype == LYS_CASE) { |
| 4995 | /* disconnecting some node in a case */ |
| 4996 | cs = (struct lysc_node_case*)parent; |
| 4997 | parent = cs->parent; |
| 4998 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 4999 | if (child == node) { |
| 5000 | if (cs->child == child) { |
| 5001 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5002 | /* case with a single child -> remove also the case */ |
| 5003 | child->parent = NULL; |
| 5004 | remove_cs = 1; |
| 5005 | } else { |
| 5006 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5007 | } |
| 5008 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5009 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5010 | } |
| 5011 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5012 | if (!remove_cs) { |
| 5013 | cs = NULL; |
| 5014 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5015 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5016 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | if (cs) { |
| 5020 | if (remove_cs) { |
| 5021 | /* cs has only one child which is being also removed */ |
| 5022 | lysc_disconnect((struct lysc_node*)cs); |
| 5023 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5024 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5025 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5026 | /* default case removed */ |
| 5027 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5028 | } |
| 5029 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5030 | /* first case removed */ |
| 5031 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5032 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5033 | if (cs->child) { |
| 5034 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5035 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5036 | prev = cs->child->prev; |
| 5037 | } /* else all the children are under a single case */ |
| 5038 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5039 | if (child->parent != (struct lysc_node*)cs) { |
| 5040 | break; |
| 5041 | } |
| 5042 | lysc_node_free(node->module->ctx, child); |
| 5043 | } |
| 5044 | if (prev) { |
| 5045 | if (prev->next) { |
| 5046 | prev->next = child; |
| 5047 | } |
| 5048 | if (child) { |
| 5049 | child->prev = prev; |
| 5050 | } else { |
| 5051 | /* link from the first child under the cases */ |
| 5052 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5053 | } |
| 5054 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5055 | } |
| 5056 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5060 | if (node->prev->next) { |
| 5061 | node->prev->next = node->next; |
| 5062 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5063 | if (node->next) { |
| 5064 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5065 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5066 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5067 | if (child) { |
| 5068 | child->prev = node->prev; |
| 5069 | } |
| 5070 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5071 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5072 | } |
| 5073 | } |
| 5074 | |
| 5075 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5076 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5077 | { |
| 5078 | LY_ERR ret = LY_EVALID; |
| 5079 | struct ly_set devs_p = {0}; |
| 5080 | struct ly_set targets = {0}; |
| 5081 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5082 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5083 | struct lysc_action *rpcs; |
| 5084 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5085 | struct lysp_deviation *dev; |
| 5086 | struct lysp_deviate *d, **dp_new; |
| 5087 | struct lysp_deviate_add *d_add; |
| 5088 | struct lysp_deviate_del *d_del; |
| 5089 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5090 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5091 | struct lysc_deviation { |
| 5092 | const char *nodeid; |
| 5093 | struct lysc_node *target; /* target node of the deviation */ |
| 5094 | 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] | 5095 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5096 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5097 | } **devs = NULL; |
| 5098 | int i; |
| 5099 | size_t prefix_len, name_len; |
| 5100 | const char *prefix, *name, *nodeid; |
| 5101 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5102 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5103 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5104 | |
| 5105 | /* get all deviations from the module and all its submodules ... */ |
| 5106 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5107 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5108 | } |
| 5109 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5110 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5111 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5112 | } |
| 5113 | } |
| 5114 | if (!devs_p.count) { |
| 5115 | /* nothing to do */ |
| 5116 | return LY_SUCCESS; |
| 5117 | } |
| 5118 | |
| 5119 | /* ... and group them by the target node */ |
| 5120 | devs = calloc(devs_p.count, sizeof *devs); |
| 5121 | for (u = 0; u < devs_p.count; ++u) { |
| 5122 | dev = devs_p.objs[u]; |
| 5123 | |
| 5124 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5125 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5126 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5127 | if (target->nodetype == LYS_ACTION) { |
| 5128 | /* move the target pointer to input/output to make them different from the action and |
| 5129 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5130 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5131 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5132 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5133 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5134 | flags |= LYSC_OPT_INTERNAL; |
| 5135 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5136 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5137 | flags |= LYSC_OPT_INTERNAL; |
| 5138 | } |
| 5139 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5140 | /* insert into the set of targets with duplicity detection */ |
| 5141 | i = ly_set_add(&targets, target, 0); |
| 5142 | if (!devs[i]) { |
| 5143 | /* new record */ |
| 5144 | devs[i] = calloc(1, sizeof **devs); |
| 5145 | devs[i]->target = target; |
| 5146 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5147 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5148 | } |
| 5149 | /* add deviates into the deviation's list of deviates */ |
| 5150 | for (d = dev->deviates; d; d = d->next) { |
| 5151 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5152 | *dp_new = d; |
| 5153 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5154 | devs[i]->not_supported = 1; |
| 5155 | } |
| 5156 | } |
| 5157 | } |
| 5158 | |
| 5159 | /* MACROS for deviates checking */ |
| 5160 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5161 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
| 5162 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\ |
| 5163 | goto cleanup; \ |
| 5164 | } |
| 5165 | |
| 5166 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5167 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
| 5168 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \ |
| 5169 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
| 5170 | goto cleanup; \ |
| 5171 | } |
| 5172 | |
| 5173 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5174 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5175 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5176 | "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5177 | devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5178 | goto cleanup; \ |
| 5179 | } |
| 5180 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5181 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5182 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5183 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5184 | "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5185 | devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
| 5186 | goto cleanup; \ |
| 5187 | } |
| 5188 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5189 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5190 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
| 5191 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \ |
| 5192 | goto cleanup; \ |
| 5193 | } |
| 5194 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5195 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5196 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5197 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5198 | "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \ |
| 5199 | devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \ |
| 5200 | goto cleanup; \ |
| 5201 | } |
| 5202 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5203 | #define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \ |
| 5204 | DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \ |
| 5205 | if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \ |
| 5206 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5207 | "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \ |
| 5208 | devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \ |
| 5209 | goto cleanup; \ |
| 5210 | } \ |
| 5211 | DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \ |
| 5212 | ((TYPE)devs[u]->target)->MEMBER_TRG = NULL; |
| 5213 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5214 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5215 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5216 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5217 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5218 | 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] | 5219 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5220 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5221 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5222 | "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5223 | devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5224 | goto cleanup; \ |
| 5225 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5226 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5227 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5228 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5229 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5230 | (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] | 5231 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5232 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5233 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5234 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5235 | } |
| 5236 | |
| 5237 | /* apply deviations */ |
| 5238 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5239 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5240 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5241 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5242 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5243 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5244 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5245 | } |
| 5246 | } |
| 5247 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5248 | /* not-supported */ |
| 5249 | if (devs[u]->not_supported) { |
| 5250 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 5251 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 5252 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5253 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5254 | |
| 5255 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5256 | if (devs[u]->target->parent) { \ |
| 5257 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5258 | } else { \ |
| 5259 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5260 | } \ |
| 5261 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5262 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5263 | } \ |
| 5264 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5265 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5266 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5267 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5268 | } |
| 5269 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5270 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 5271 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5272 | /* remove RPC's/action's input */ |
| 5273 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5274 | 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] | 5275 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5276 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5277 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5278 | /* remove RPC's/action's output */ |
| 5279 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5280 | 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] | 5281 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5282 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5283 | } else { |
| 5284 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5285 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5286 | } |
| 5287 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5288 | /* remove Notification */ |
| 5289 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5290 | } else { |
| 5291 | /* remove the target node */ |
| 5292 | lysc_disconnect(devs[u]->target); |
| 5293 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5294 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5295 | |
| 5296 | continue; |
| 5297 | } |
| 5298 | |
| 5299 | /* list of deviates (not-supported is not present in the list) */ |
| 5300 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5301 | d = devs[u]->deviates[v]; |
| 5302 | |
| 5303 | switch (d->mod) { |
| 5304 | case LYS_DEV_ADD: |
| 5305 | d_add = (struct lysp_deviate_add*)d; |
| 5306 | /* [units-stmt] */ |
| 5307 | if (d_add->units) { |
| 5308 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5309 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5310 | |
| 5311 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5312 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5313 | } |
| 5314 | |
| 5315 | /* *must-stmt */ |
| 5316 | if (d_add->musts) { |
| 5317 | switch (devs[u]->target->nodetype) { |
| 5318 | case LYS_CONTAINER: |
| 5319 | case LYS_LIST: |
| 5320 | 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] | 5321 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5322 | break; |
| 5323 | case LYS_LEAF: |
| 5324 | case LYS_LEAFLIST: |
| 5325 | case LYS_ANYDATA: |
| 5326 | 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] | 5327 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5328 | break; |
| 5329 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5330 | 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] | 5331 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5332 | break; |
| 5333 | case LYS_ACTION: |
| 5334 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5335 | 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] | 5336 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5337 | break; |
| 5338 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5339 | 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] | 5340 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5341 | break; |
| 5342 | } |
| 5343 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5344 | default: |
| 5345 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5346 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5347 | goto cleanup; |
| 5348 | } |
| 5349 | } |
| 5350 | |
| 5351 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5352 | if (d_add->uniques) { |
| 5353 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5354 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5355 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5356 | |
| 5357 | /* *default-stmt */ |
| 5358 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5359 | switch (devs[u]->target->nodetype) { |
| 5360 | case LYS_LEAF: |
| 5361 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5362 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt); |
| 5363 | if (((struct lysc_node_leaf*)devs[u]->target)->dflt) { |
| 5364 | /* first, remove the default value taken from the type */ |
| 5365 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 5366 | ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL; |
| 5367 | } |
| 5368 | 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] | 5369 | /* mark the new default values as leaf's own */ |
| 5370 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5371 | break; |
| 5372 | case LYS_LEAFLIST: |
| 5373 | if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 5374 | /* first, remove the default value taken from the type */ |
| 5375 | LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) { |
| 5376 | lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]); |
| 5377 | } |
| 5378 | LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 5379 | ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL; |
| 5380 | } |
| 5381 | /* add new default value(s) */ |
| 5382 | LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts, |
| 5383 | LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 5384 | for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 5385 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
| 5386 | DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]); |
| 5387 | LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 5388 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5389 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5390 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5391 | break; |
| 5392 | case LYS_CHOICE: |
| 5393 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5394 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5395 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5396 | * to allow making the default case even the augmented case from the deviating module */ |
| 5397 | if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0], |
| 5398 | (struct lysc_node_choice*)devs[u]->target)) { |
| 5399 | goto cleanup; |
| 5400 | } |
| 5401 | break; |
| 5402 | default: |
| 5403 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5404 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5405 | goto cleanup; |
| 5406 | } |
| 5407 | } |
| 5408 | |
| 5409 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5410 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 5411 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) { |
| 5412 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5413 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5414 | goto cleanup; |
| 5415 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5416 | if (devs[u]->flags) { |
| 5417 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).", |
| 5418 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid); |
| 5419 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5420 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5421 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5422 | "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").", |
| 5423 | devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5424 | goto cleanup; |
| 5425 | } |
| 5426 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup); |
| 5427 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5428 | |
| 5429 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5430 | if (d_add->flags & LYS_MAND_MASK) { |
| 5431 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5432 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5433 | "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5434 | devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5435 | goto cleanup; |
| 5436 | } |
| 5437 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup); |
| 5438 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5439 | |
| 5440 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5441 | if (d_add->flags & LYS_SET_MIN) { |
| 5442 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5443 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5444 | /* change value */ |
| 5445 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5446 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5447 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5448 | /* change value */ |
| 5449 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5450 | } else { |
| 5451 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5452 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5453 | goto cleanup; |
| 5454 | } |
| 5455 | if (d_add->min) { |
| 5456 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5457 | } |
| 5458 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5459 | |
| 5460 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5461 | if (d_add->flags & LYS_SET_MAX) { |
| 5462 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5463 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5464 | /* change value */ |
| 5465 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5466 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5467 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5468 | /* change value */ |
| 5469 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5470 | } else { |
| 5471 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5472 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 5473 | goto cleanup; |
| 5474 | } |
| 5475 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5476 | |
| 5477 | break; |
| 5478 | case LYS_DEV_DELETE: |
| 5479 | d_del = (struct lysp_deviate_del*)d; |
| 5480 | |
| 5481 | /* [units-stmt] */ |
| 5482 | if (d_del->units) { |
| 5483 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
| 5484 | DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units"); |
| 5485 | } |
| 5486 | |
| 5487 | /* *must-stmt */ |
| 5488 | if (d_del->musts) { |
| 5489 | switch (devs[u]->target->nodetype) { |
| 5490 | case LYS_CONTAINER: |
| 5491 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5492 | 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] | 5493 | break; |
| 5494 | case LYS_LEAF: |
| 5495 | case LYS_LEAFLIST: |
| 5496 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5497 | 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] | 5498 | break; |
| 5499 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5500 | 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] | 5501 | break; |
| 5502 | case LYS_ACTION: |
| 5503 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5504 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5505 | break; |
| 5506 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5507 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5508 | break; |
| 5509 | } |
| 5510 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5511 | default: |
| 5512 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5513 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5514 | goto cleanup; |
| 5515 | } |
| 5516 | } |
| 5517 | |
| 5518 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5519 | if (d_del->uniques) { |
| 5520 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5521 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 5522 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 5523 | LY_ARRAY_FOR(list->uniques, z) { |
| 5524 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 5525 | nodeid = strpbrk(name, " \t\n"); |
| 5526 | if (nodeid) { |
| 5527 | if (strncmp(name, list->uniques[z][y]->name, nodeid - name) |
| 5528 | || list->uniques[z][y]->name[nodeid - name] != '\0') { |
| 5529 | break; |
| 5530 | } |
| 5531 | while (isspace(*nodeid)) { |
| 5532 | ++nodeid; |
| 5533 | } |
| 5534 | } else { |
| 5535 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 5536 | break; |
| 5537 | } |
| 5538 | } |
| 5539 | } |
| 5540 | if (!name) { |
| 5541 | /* complete match - remove the unique */ |
| 5542 | LY_ARRAY_DECREMENT(list->uniques); |
| 5543 | LY_ARRAY_FREE(list->uniques[z]); |
| 5544 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 5545 | --z; |
| 5546 | break; |
| 5547 | } |
| 5548 | } |
| 5549 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 5550 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5551 | "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 5552 | devs[u]->nodeid, d_del->uniques[x]); |
| 5553 | goto cleanup; |
| 5554 | } |
| 5555 | } |
| 5556 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 5557 | LY_ARRAY_FREE(list->uniques); |
| 5558 | list->uniques = NULL; |
| 5559 | } |
| 5560 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5561 | |
| 5562 | /* *default-stmt */ |
| 5563 | if (d_del->dflts) { |
| 5564 | switch (devs[u]->target->nodetype) { |
| 5565 | case LYS_LEAF: |
| 5566 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 5567 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 5568 | dflt, "deleting", "default", d_del->dflts[0]); |
| 5569 | |
| 5570 | 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] | 5571 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5572 | break; |
| 5573 | case LYS_LEAFLIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5574 | DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default"); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5575 | if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) { |
| 5576 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
| 5577 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5578 | break; |
| 5579 | case LYS_CHOICE: |
| 5580 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 5581 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 5582 | nodeid = d_del->dflts[0]; |
| 5583 | LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 5584 | if (prefix) { |
| 5585 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 5586 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 5587 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
| 5588 | "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. " |
| 5589 | "The prefix does not match any imported module of the deviation module.", |
| 5590 | devs[u]->nodeid, d_del->dflts[0]); |
| 5591 | goto cleanup; |
| 5592 | } |
| 5593 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 5594 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
| 5595 | "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. " |
| 5596 | "The prefix does not match the default case's module.", |
| 5597 | devs[u]->nodeid, d_del->dflts[0]); |
| 5598 | goto cleanup; |
| 5599 | } |
| 5600 | } |
| 5601 | /* else { |
| 5602 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 5603 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 5604 | * the module of the default case, just matching its name */ |
| 5605 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 5606 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5607 | "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 5608 | devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name); |
| 5609 | goto cleanup; |
| 5610 | } |
| 5611 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 5612 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 5613 | break; |
| 5614 | default: |
| 5615 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5616 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5617 | goto cleanup; |
| 5618 | } |
| 5619 | } |
| 5620 | |
| 5621 | break; |
| 5622 | case LYS_DEV_REPLACE: |
| 5623 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 5624 | |
| 5625 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5626 | if (d_rpl->type) { |
| 5627 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 5628 | /* type is mandatory, so checking for its presence is not necessary */ |
| 5629 | 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] | 5630 | 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] | 5631 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5632 | |
| 5633 | /* [units-stmt] */ |
| 5634 | if (d_rpl->units) { |
| 5635 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 5636 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 5637 | units, "replacing", "units", d_rpl->units); |
| 5638 | |
| 5639 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5640 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5641 | } |
| 5642 | |
| 5643 | /* [default-stmt] */ |
| 5644 | if (d_rpl->dflt) { |
| 5645 | switch (devs[u]->target->nodetype) { |
| 5646 | case LYS_LEAF: |
| 5647 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 5648 | dflt, "replacing", "default", d_rpl->dflt); |
| 5649 | |
| 5650 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 5651 | DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 5652 | break; |
| 5653 | case LYS_CHOICE: |
| 5654 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt); |
| 5655 | if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt, |
| 5656 | (struct lysc_node_choice*)devs[u]->target)) { |
| 5657 | goto cleanup; |
| 5658 | } |
| 5659 | break; |
| 5660 | default: |
| 5661 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5662 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5663 | goto cleanup; |
| 5664 | } |
| 5665 | } |
| 5666 | |
| 5667 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5668 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 5669 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) { |
| 5670 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5671 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 5672 | goto cleanup; |
| 5673 | } |
| 5674 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
| 5675 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, |
| 5676 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 5677 | goto cleanup; |
| 5678 | } |
| 5679 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup); |
| 5680 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5681 | |
| 5682 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5683 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 5684 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
| 5685 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, |
| 5686 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 5687 | goto cleanup; |
| 5688 | } |
| 5689 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup); |
| 5690 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5691 | |
| 5692 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5693 | if (d_rpl->flags & LYS_SET_MIN) { |
| 5694 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5695 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5696 | /* change value */ |
| 5697 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 5698 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5699 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5700 | /* change value */ |
| 5701 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 5702 | } else { |
| 5703 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5704 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 5705 | goto cleanup; |
| 5706 | } |
| 5707 | if (d_rpl->min) { |
| 5708 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5709 | } |
| 5710 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5711 | |
| 5712 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5713 | if (d_rpl->flags & LYS_SET_MAX) { |
| 5714 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5715 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5716 | /* change value */ |
| 5717 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 5718 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5719 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5720 | /* change value */ |
| 5721 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 5722 | } else { |
| 5723 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5724 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 5725 | goto cleanup; |
| 5726 | } |
| 5727 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5728 | |
| 5729 | break; |
| 5730 | default: |
| 5731 | LOGINT(ctx->ctx); |
| 5732 | goto cleanup; |
| 5733 | } |
| 5734 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5735 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5736 | /* final check when all deviations of a single target node are applied */ |
| 5737 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5738 | /* check min-max compatibility */ |
| 5739 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5740 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 5741 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 5742 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5743 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 5744 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 5745 | } else { |
| 5746 | min = max = 0; |
| 5747 | } |
| 5748 | if (min > max) { |
| 5749 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
| 5750 | "after deviation (%s): min value %u is bigger than max value %u.", |
| 5751 | devs[u]->nodeid, min, max); |
| 5752 | goto cleanup; |
| 5753 | } |
| 5754 | |
| 5755 | /* check mandatory - default compatibility */ |
| 5756 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 5757 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 5758 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 5759 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5760 | "Invalid deviation (%s) combining default value and mandatory %s.", |
| 5761 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype)); |
| 5762 | goto cleanup; |
| 5763 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 5764 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 5765 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 5766 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5767 | "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid); |
| 5768 | goto cleanup; |
| 5769 | } |
| 5770 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 5771 | /* mandatory node under a default case */ |
| 5772 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5773 | "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 5774 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name); |
| 5775 | goto cleanup; |
| 5776 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5777 | |
| 5778 | /* TODO check default value(s) according to the type */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5779 | } |
| 5780 | |
| 5781 | ret = LY_SUCCESS; |
| 5782 | |
| 5783 | cleanup: |
| 5784 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 5785 | LY_ARRAY_FREE(devs[u]->deviates); |
| 5786 | free(devs[u]); |
| 5787 | } |
| 5788 | free(devs); |
| 5789 | ly_set_erase(&targets, NULL); |
| 5790 | ly_set_erase(&devs_p, NULL); |
| 5791 | |
| 5792 | return ret; |
| 5793 | } |
| 5794 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 5795 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5796 | * @brief Compile the given YANG submodule into the main module. |
| 5797 | * @param[in] ctx Compile context |
| 5798 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5799 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5800 | */ |
| 5801 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5802 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5803 | { |
| 5804 | unsigned int u; |
| 5805 | LY_ERR ret = LY_SUCCESS; |
| 5806 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5807 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5808 | struct lysc_module *mainmod = ctx->mod->compiled; |
| 5809 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5810 | if (!mainmod->mod->off_features) { |
| 5811 | /* features are compiled directly into the compiled module structure, |
| 5812 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 5813 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 5814 | ret = lys_feature_precompile(ctx->ctx, ctx->mod, submod->features, |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5815 | mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features); |
| 5816 | LY_CHECK_GOTO(ret, error); |
| 5817 | } |
| 5818 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5819 | COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5820 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 5821 | /* TODO data nodes */ |
| 5822 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5823 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 5824 | 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] | 5825 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5826 | error: |
| 5827 | return ret; |
| 5828 | } |
| 5829 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5830 | LY_ERR |
| 5831 | lys_compile(struct lys_module *mod, int options) |
| 5832 | { |
| 5833 | struct lysc_ctx ctx = {0}; |
| 5834 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5835 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5836 | struct lysp_module *sp; |
| 5837 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5838 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5839 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5840 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 5841 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5842 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5843 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5844 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 5845 | |
| 5846 | if (!mod->implemented) { |
| 5847 | /* just imported modules are not compiled */ |
| 5848 | return LY_SUCCESS; |
| 5849 | } |
| 5850 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5851 | sp = mod->parsed; |
| 5852 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5853 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5854 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5855 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5856 | ctx.options = options; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5857 | |
| 5858 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5859 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 5860 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5861 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5862 | 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] | 5863 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5864 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5865 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 5866 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5867 | if (mod->off_features) { |
| 5868 | /* there is already precompiled array of features */ |
| 5869 | mod_c->features = mod->off_features; |
| 5870 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5871 | } else { |
| 5872 | /* features are compiled directly into the compiled module structure, |
| 5873 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 5874 | ret = lys_feature_precompile(ctx.ctx, ctx.mod, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5875 | LY_CHECK_GOTO(ret, error); |
| 5876 | } |
| 5877 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 5878 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 5879 | * are present. */ |
| 5880 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5881 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5882 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 5883 | } |
| 5884 | LY_ARRAY_FOR(sp->includes, v) { |
| 5885 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5886 | 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] | 5887 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 5888 | } |
| 5889 | } |
| 5890 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5891 | 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] | 5892 | if (sp->identities) { |
| 5893 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 5894 | } |
| 5895 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5896 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5897 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5898 | ret = lys_compile_node(&ctx, node_p, NULL, 0); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5899 | LY_CHECK_GOTO(ret, error); |
| 5900 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5901 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5902 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 5903 | 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] | 5904 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5905 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5906 | ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5907 | LY_CHECK_GOTO(ret, error); |
| 5908 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5909 | ret = lys_compile_augment(&ctx, augments[u], NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5910 | LY_CHECK_GOTO(ret, error); |
| 5911 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5912 | |
| 5913 | /* deviations */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5914 | ret = lys_compile_deviations(&ctx, sp); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5915 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5916 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5917 | 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] | 5918 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5919 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5920 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 5921 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 5922 | * 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] | 5923 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5924 | 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] | 5925 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 5926 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 5927 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5928 | 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] | 5929 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 5930 | } else if (type->basetype == LY_TYPE_UNION) { |
| 5931 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 5932 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 5933 | /* validate the path */ |
| 5934 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 5935 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 5936 | LY_CHECK_GOTO(ret, error); |
| 5937 | } |
| 5938 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5939 | } |
| 5940 | } |
| 5941 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5942 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5943 | 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] | 5944 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 5945 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 5946 | /* store pointer to the real type */ |
| 5947 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 5948 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 5949 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 5950 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 5951 | } else if (type->basetype == LY_TYPE_UNION) { |
| 5952 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 5953 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 5954 | /* store pointer to the real type */ |
| 5955 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 5956 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 5957 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 5958 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 5959 | } |
| 5960 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5961 | } |
| 5962 | } |
| 5963 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5964 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5965 | /* validate non-instantiated groupings from the parsed schema, |
| 5966 | * without it we would accept even the schemas with invalid grouping specification */ |
| 5967 | ctx.options |= LYSC_OPT_GROUPING; |
| 5968 | LY_ARRAY_FOR(sp->groupings, u) { |
| 5969 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
| 5970 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error); |
| 5971 | } |
| 5972 | } |
| 5973 | LY_LIST_FOR(sp->data, node_p) { |
| 5974 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 5975 | LY_ARRAY_FOR(grps, u) { |
| 5976 | if (!(grps[u].flags & LYS_USED_GRP)) { |
| 5977 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error); |
| 5978 | } |
| 5979 | } |
| 5980 | } |
| 5981 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5982 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5983 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 5984 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5985 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5986 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5987 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5988 | lysp_module_free(mod->parsed); |
| 5989 | ((struct lys_module*)mod)->parsed = NULL; |
| 5990 | } |
| 5991 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5992 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5993 | /* remove flag of the modules implemented by dependency */ |
| 5994 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 5995 | m = ctx.ctx->list.objs[u]; |
| 5996 | if (m->implemented == 2) { |
| 5997 | m->implemented = 1; |
| 5998 | } |
| 5999 | } |
| 6000 | } |
| 6001 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6002 | ((struct lys_module*)mod)->compiled = mod_c; |
| 6003 | return LY_SUCCESS; |
| 6004 | |
| 6005 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6006 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6007 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6008 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6009 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6010 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6011 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6012 | mod->compiled = NULL; |
| 6013 | |
| 6014 | /* revert compilation of modules implemented by dependency */ |
| 6015 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6016 | m = ctx.ctx->list.objs[u]; |
| 6017 | if (m->implemented == 2) { |
| 6018 | /* revert features list to the precompiled state */ |
| 6019 | lys_feature_precompile_revert(&ctx, m); |
| 6020 | /* mark module as imported-only / not-implemented */ |
| 6021 | m->implemented = 0; |
| 6022 | /* free the compiled version of the module */ |
| 6023 | lysc_module_free(m->compiled, NULL); |
| 6024 | m->compiled = NULL; |
| 6025 | } |
| 6026 | } |
| 6027 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6028 | return ret; |
| 6029 | } |