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 | |
| 17 | #include <ctype.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 18 | #include <stdio.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 19 | |
| 20 | #include "libyang.h" |
| 21 | #include "context.h" |
| 22 | #include "tree_schema_internal.h" |
| 23 | #include "xpath.h" |
| 24 | |
| 25 | /** |
| 26 | * @brief Duplicate string into dictionary |
| 27 | * @param[in] CTX libyang context of the dictionary. |
| 28 | * @param[in] ORIG String to duplicate. |
| 29 | * @param[out] DUP Where to store the result. |
| 30 | */ |
| 31 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 32 | |
| 33 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \ |
| 34 | if (ARRAY_P) { \ |
| 35 | 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] | 36 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 37 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 38 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 39 | RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER + __array_offset]); \ |
| 40 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 41 | } \ |
| 42 | } |
| 43 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 44 | #define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, OPTIONS, ITER, FUNC, USES_STATUS, RET, GOTO) \ |
| 45 | if (ARRAY_P) { \ |
| 46 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 47 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 48 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 49 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
| 50 | RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
| 51 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 52 | } \ |
| 53 | } |
| 54 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 55 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \ |
| 56 | if (ARRAY_P) { \ |
| 57 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 58 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 59 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 60 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
| 61 | RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 62 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 63 | } \ |
| 64 | } |
| 65 | |
| 66 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \ |
| 67 | if (MEMBER_P) { \ |
| 68 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 69 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
| 70 | RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \ |
| 71 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 72 | } |
| 73 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 74 | #define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, OPTIONS, FUNC, RET, GOTO) \ |
| 75 | if (MEMBER_P) { \ |
| 76 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
| 77 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 78 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
| 79 | RET = FUNC(CTX, MEMBER_P, OPTIONS, &(ARRAY_C)[__array_offset]); \ |
| 80 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 81 | } |
| 82 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 83 | #define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 84 | if (ARRAY) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 85 | for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
| 86 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 87 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 88 | return LY_EVALID; \ |
| 89 | } \ |
| 90 | } \ |
| 91 | } |
| 92 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 93 | static struct lysc_ext_instance * |
| 94 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 95 | { |
| 96 | /* TODO */ |
| 97 | (void) ctx; |
| 98 | (void) orig; |
| 99 | return NULL; |
| 100 | } |
| 101 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 102 | /** |
| 103 | * @brief Duplicate the compiled pattern structure. |
| 104 | * |
| 105 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 106 | * |
| 107 | * @param[in] orig The pattern structure to duplicate. |
| 108 | * @return The duplicated structure to use. |
| 109 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 110 | static struct lysc_pattern* |
| 111 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 112 | { |
| 113 | ++orig->refcount; |
| 114 | return orig; |
| 115 | } |
| 116 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 117 | /** |
| 118 | * @brief Duplicate the array of compiled patterns. |
| 119 | * |
| 120 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 121 | * |
| 122 | * @param[in] ctx Libyang context for logging. |
| 123 | * @param[in] orig The patterns sized array to duplicate. |
| 124 | * @return New sized array as a copy of @p orig. |
| 125 | * @return NULL in case of memory allocation error. |
| 126 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 127 | static struct lysc_pattern** |
| 128 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 129 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 130 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 131 | unsigned int u; |
| 132 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 133 | assert(orig); |
| 134 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 135 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 136 | LY_ARRAY_FOR(orig, u) { |
| 137 | dup[u] = lysc_pattern_dup(orig[u]); |
| 138 | LY_ARRAY_INCREMENT(dup); |
| 139 | } |
| 140 | return dup; |
| 141 | } |
| 142 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 143 | /** |
| 144 | * @brief Duplicate compiled range structure. |
| 145 | * |
| 146 | * @param[in] ctx Libyang context for logging. |
| 147 | * @param[in] orig The range structure to be duplicated. |
| 148 | * @return New compiled range structure as a copy of @p orig. |
| 149 | * @return NULL in case of memory allocation error. |
| 150 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 151 | struct lysc_range* |
| 152 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 153 | { |
| 154 | struct lysc_range *dup; |
| 155 | LY_ERR ret; |
| 156 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 157 | assert(orig); |
| 158 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 159 | dup = calloc(1, sizeof *dup); |
| 160 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 161 | if (orig->parts) { |
| 162 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 163 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 164 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 165 | } |
| 166 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 167 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 168 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 169 | |
| 170 | return dup; |
| 171 | cleanup: |
| 172 | free(dup); |
| 173 | (void) ret; /* set but not used due to the return type */ |
| 174 | return NULL; |
| 175 | } |
| 176 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 177 | /** |
| 178 | * @brief Stack for processing if-feature expressions. |
| 179 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 180 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 181 | int size; /**< number of items in the stack */ |
| 182 | int index; /**< first empty item */ |
| 183 | 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] | 184 | }; |
| 185 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 186 | /** |
| 187 | * @brief Add @ref ifftokens into the stack. |
| 188 | * @param[in] stack The if-feature stack to use. |
| 189 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 190 | * @return LY_EMEM in case of memory allocation error |
| 191 | * @return LY_ESUCCESS if the value successfully stored. |
| 192 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 193 | static LY_ERR |
| 194 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 195 | { |
| 196 | if (stack->index == stack->size) { |
| 197 | stack->size += 4; |
| 198 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 199 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 200 | } |
| 201 | stack->stack[stack->index++] = value; |
| 202 | return LY_SUCCESS; |
| 203 | } |
| 204 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 205 | /** |
| 206 | * @brief Get (and remove) the last item form the stack. |
| 207 | * @param[in] stack The if-feature stack to use. |
| 208 | * @return The value from the top of the stack. |
| 209 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 210 | static uint8_t |
| 211 | iff_stack_pop(struct iff_stack *stack) |
| 212 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 213 | assert(stack && stack->index); |
| 214 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 215 | stack->index--; |
| 216 | return stack->stack[stack->index]; |
| 217 | } |
| 218 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 219 | /** |
| 220 | * @brief Clean up the stack. |
| 221 | * @param[in] stack The if-feature stack to use. |
| 222 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 223 | static void |
| 224 | iff_stack_clean(struct iff_stack *stack) |
| 225 | { |
| 226 | stack->size = 0; |
| 227 | free(stack->stack); |
| 228 | } |
| 229 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 230 | /** |
| 231 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 232 | * (libyang format of the if-feature expression). |
| 233 | * @param[in,out] list The 2bits array to modify. |
| 234 | * @param[in] op The operand (@ref ifftokens) to store. |
| 235 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 236 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 237 | static void |
| 238 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 239 | { |
| 240 | uint8_t *item; |
| 241 | uint8_t mask = 3; |
| 242 | |
| 243 | assert(pos >= 0); |
| 244 | assert(op <= 3); /* max 2 bits */ |
| 245 | |
| 246 | item = &list[pos / 4]; |
| 247 | mask = mask << 2 * (pos % 4); |
| 248 | *item = (*item) & ~mask; |
| 249 | *item = (*item) | (op << 2 * (pos % 4)); |
| 250 | } |
| 251 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 252 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 253 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 254 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 255 | /** |
| 256 | * @brief Find a feature of the given name and referenced in the given module. |
| 257 | * |
| 258 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 259 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 260 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 261 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 262 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 263 | * assigned till that time will be still valid. |
| 264 | * |
| 265 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 266 | * @param[in] name Name of the feature including possible prefix. |
| 267 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 268 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 269 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 270 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 271 | 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] | 272 | { |
| 273 | size_t i; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 274 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 275 | |
| 276 | for (i = 0; i < len; ++i) { |
| 277 | if (name[i] == ':') { |
| 278 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 279 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 280 | LY_CHECK_RET(!mod, NULL); |
| 281 | |
| 282 | name = &name[i + 1]; |
| 283 | len = len - i - 1; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 288 | if (mod->implemented) { |
| 289 | /* module is implemented so there is already the compiled schema */ |
| 290 | flist = mod->compiled->features; |
| 291 | } else { |
| 292 | flist = mod->off_features; |
| 293 | } |
| 294 | LY_ARRAY_FOR(flist, i) { |
| 295 | f = &flist[i]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 296 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 297 | return f; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return NULL; |
| 302 | } |
| 303 | |
| 304 | static LY_ERR |
| 305 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext) |
| 306 | { |
| 307 | const char *name; |
| 308 | unsigned int u; |
| 309 | const struct lys_module *mod; |
| 310 | struct lysp_ext *edef = NULL; |
| 311 | |
| 312 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 313 | ext->insubstmt = ext_p->insubstmt; |
| 314 | ext->insubstmt_index = ext_p->insubstmt_index; |
| 315 | |
| 316 | /* get module where the extension definition should be placed */ |
| 317 | for (u = 0; ext_p->name[u] != ':'; ++u); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 318 | mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 319 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 320 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 321 | LY_EVALID); |
| 322 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 323 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 324 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 325 | ext_p->name, mod->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 326 | LY_EVALID); |
| 327 | name = &ext_p->name[u + 1]; |
| 328 | /* find the extension definition there */ |
| 329 | for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) { |
| 330 | if (!strcmp(name, mod->parsed->extensions[u].name)) { |
| 331 | edef = &mod->parsed->extensions[u]; |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 336 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
| 337 | LY_EVALID); |
| 338 | /* TODO plugins */ |
| 339 | |
| 340 | return LY_SUCCESS; |
| 341 | } |
| 342 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 343 | /** |
| 344 | * @brief Compile information from the if-feature statement |
| 345 | * @param[in] ctx Compile context. |
| 346 | * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions. |
| 347 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 348 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 349 | * @return LY_ERR value. |
| 350 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 351 | static LY_ERR |
| 352 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff) |
| 353 | { |
| 354 | const char *c = *value; |
| 355 | int r, rc = EXIT_FAILURE; |
| 356 | int i, j, last_not, checkversion = 0; |
| 357 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 358 | uint8_t op; |
| 359 | struct iff_stack stack = {0, 0, NULL}; |
| 360 | struct lysc_feature *f; |
| 361 | |
| 362 | assert(c); |
| 363 | |
| 364 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 365 | for (i = j = last_not = 0; c[i]; i++) { |
| 366 | if (c[i] == '(') { |
| 367 | j++; |
| 368 | checkversion = 1; |
| 369 | continue; |
| 370 | } else if (c[i] == ')') { |
| 371 | j--; |
| 372 | continue; |
| 373 | } else if (isspace(c[i])) { |
| 374 | checkversion = 1; |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 379 | if (c[i + r] == '\0') { |
| 380 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 381 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 382 | return LY_EVALID; |
| 383 | } else if (!isspace(c[i + r])) { |
| 384 | /* feature name starting with the not/and/or */ |
| 385 | last_not = 0; |
| 386 | f_size++; |
| 387 | } else if (c[i] == 'n') { /* not operation */ |
| 388 | if (last_not) { |
| 389 | /* double not */ |
| 390 | expr_size = expr_size - 2; |
| 391 | last_not = 0; |
| 392 | } else { |
| 393 | last_not = 1; |
| 394 | } |
| 395 | } else { /* and, or */ |
| 396 | f_exp++; |
| 397 | /* not a not operation */ |
| 398 | last_not = 0; |
| 399 | } |
| 400 | i += r; |
| 401 | } else { |
| 402 | f_size++; |
| 403 | last_not = 0; |
| 404 | } |
| 405 | expr_size++; |
| 406 | |
| 407 | while (!isspace(c[i])) { |
| 408 | if (!c[i] || c[i] == ')') { |
| 409 | i--; |
| 410 | break; |
| 411 | } |
| 412 | i++; |
| 413 | } |
| 414 | } |
| 415 | if (j || f_exp != f_size) { |
| 416 | /* not matching count of ( and ) */ |
| 417 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 418 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 419 | return LY_EVALID; |
| 420 | } |
| 421 | |
| 422 | if (checkversion || expr_size > 1) { |
| 423 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 424 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 425 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 426 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 427 | return LY_EVALID; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* allocate the memory */ |
| 432 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 433 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 434 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 435 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 436 | |
| 437 | stack.size = expr_size; |
| 438 | f_size--; expr_size--; /* used as indexes from now */ |
| 439 | |
| 440 | for (i--; i >= 0; i--) { |
| 441 | if (c[i] == ')') { |
| 442 | /* push it on stack */ |
| 443 | iff_stack_push(&stack, LYS_IFF_RP); |
| 444 | continue; |
| 445 | } else if (c[i] == '(') { |
| 446 | /* pop from the stack into result all operators until ) */ |
| 447 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 448 | iff_setop(iff->expr, op, expr_size--); |
| 449 | } |
| 450 | continue; |
| 451 | } else if (isspace(c[i])) { |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | /* end of operator or operand -> find beginning and get what is it */ |
| 456 | j = i + 1; |
| 457 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 458 | i--; |
| 459 | } |
| 460 | i++; /* go back by one step */ |
| 461 | |
| 462 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 463 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 464 | /* double not */ |
| 465 | iff_stack_pop(&stack); |
| 466 | } else { |
| 467 | /* not has the highest priority, so do not pop from the stack |
| 468 | * as in case of AND and OR */ |
| 469 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 470 | } |
| 471 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 472 | /* as for OR - pop from the stack all operators with the same or higher |
| 473 | * priority and store them to the result, then push the AND to the stack */ |
| 474 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 475 | op = iff_stack_pop(&stack); |
| 476 | iff_setop(iff->expr, op, expr_size--); |
| 477 | } |
| 478 | iff_stack_push(&stack, LYS_IFF_AND); |
| 479 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 480 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 481 | op = iff_stack_pop(&stack); |
| 482 | iff_setop(iff->expr, op, expr_size--); |
| 483 | } |
| 484 | iff_stack_push(&stack, LYS_IFF_OR); |
| 485 | } else { |
| 486 | /* feature name, length is j - i */ |
| 487 | |
| 488 | /* add it to the expression */ |
| 489 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 490 | |
| 491 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 492 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 493 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 494 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 495 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 496 | iff->features[f_size] = f; |
| 497 | LY_ARRAY_INCREMENT(iff->features); |
| 498 | f_size--; |
| 499 | } |
| 500 | } |
| 501 | while (stack.index) { |
| 502 | op = iff_stack_pop(&stack); |
| 503 | iff_setop(iff->expr, op, expr_size--); |
| 504 | } |
| 505 | |
| 506 | if (++expr_size || ++f_size) { |
| 507 | /* not all expected operators and operands found */ |
| 508 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 509 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 510 | rc = LY_EINT; |
| 511 | } else { |
| 512 | rc = LY_SUCCESS; |
| 513 | } |
| 514 | |
| 515 | error: |
| 516 | /* cleanup */ |
| 517 | iff_stack_clean(&stack); |
| 518 | |
| 519 | return rc; |
| 520 | } |
| 521 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 522 | /** |
| 523 | * @brief Compile information from the when statement |
| 524 | * @param[in] ctx Compile context. |
| 525 | * @param[in] when_p The parsed when statement structure. |
| 526 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 527 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 528 | * @return LY_ERR value. |
| 529 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 530 | static LY_ERR |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 531 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when **when) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 532 | { |
| 533 | unsigned int u; |
| 534 | LY_ERR ret = LY_SUCCESS; |
| 535 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 536 | *when = calloc(1, sizeof **when); |
| 537 | (*when)->refcount = 1; |
| 538 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
| 539 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 540 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 541 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
| 542 | COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, options, u, lys_compile_ext, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 543 | |
| 544 | done: |
| 545 | return ret; |
| 546 | } |
| 547 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 548 | /** |
| 549 | * @brief Compile information from the must statement |
| 550 | * @param[in] ctx Compile context. |
| 551 | * @param[in] must_p The parsed must statement structure. |
| 552 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 553 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 554 | * @return LY_ERR value. |
| 555 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 556 | static LY_ERR |
| 557 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must) |
| 558 | { |
| 559 | unsigned int u; |
| 560 | LY_ERR ret = LY_SUCCESS; |
| 561 | |
| 562 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 563 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
| 564 | |
| 565 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 566 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 567 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 568 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 569 | COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done); |
| 570 | |
| 571 | done: |
| 572 | return ret; |
| 573 | } |
| 574 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 575 | /** |
| 576 | * @brief Compile information from the import statement |
| 577 | * @param[in] ctx Compile context. |
| 578 | * @param[in] imp_p The parsed import statement structure. |
| 579 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 580 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 581 | * @return LY_ERR value. |
| 582 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 583 | static LY_ERR |
| 584 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp) |
| 585 | { |
| 586 | unsigned int u; |
| 587 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 588 | LY_ERR ret = LY_SUCCESS; |
| 589 | |
| 590 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
| 591 | COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done); |
| 592 | imp->module = imp_p->module; |
| 593 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 594 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 595 | * 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] | 596 | * 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] | 597 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 598 | /* try to use filepath if present */ |
| 599 | if (imp->module->filepath) { |
| 600 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 601 | !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] | 602 | if (mod != imp->module) { |
| 603 | 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] | 604 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 605 | mod = NULL; |
| 606 | } |
| 607 | } |
| 608 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 609 | 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] | 610 | 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] | 611 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 612 | return LY_ENOTFOUND; |
| 613 | } |
| 614 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | done: |
| 618 | return ret; |
| 619 | } |
| 620 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 621 | /** |
| 622 | * @brief Compile information from the identity statement |
| 623 | * |
| 624 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 625 | * |
| 626 | * @param[in] ctx Compile context. |
| 627 | * @param[in] ident_p The parsed identity statement structure. |
| 628 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 629 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 630 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 631 | * @return LY_ERR value. |
| 632 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 633 | static LY_ERR |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 634 | lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *idents, struct lysc_ident *ident) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 635 | { |
| 636 | unsigned int u; |
| 637 | LY_ERR ret = LY_SUCCESS; |
| 638 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 639 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 640 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 641 | DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc); |
| 642 | DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 643 | COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 644 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
| 645 | COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done); |
| 646 | ident->flags = ident_p->flags; |
| 647 | |
| 648 | done: |
| 649 | return ret; |
| 650 | } |
| 651 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 652 | /** |
| 653 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 654 | * |
| 655 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 656 | * |
| 657 | * @param[in] ctx Compile context for logging. |
| 658 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 659 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 660 | * @return LY_SUCCESS if everything is ok. |
| 661 | * @return LY_EVALID if the identity is derived from itself. |
| 662 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 663 | static LY_ERR |
| 664 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 665 | { |
| 666 | LY_ERR ret = LY_EVALID; |
| 667 | unsigned int u, v; |
| 668 | struct ly_set recursion = {0}; |
| 669 | struct lysc_ident *drv; |
| 670 | |
| 671 | if (!derived) { |
| 672 | return LY_SUCCESS; |
| 673 | } |
| 674 | |
| 675 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 676 | if (ident == derived[u]) { |
| 677 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 678 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 679 | goto cleanup; |
| 680 | } |
| 681 | ly_set_add(&recursion, derived[u], 0); |
| 682 | } |
| 683 | |
| 684 | for (v = 0; v < recursion.count; ++v) { |
| 685 | drv = recursion.objs[v]; |
| 686 | if (!drv->derived) { |
| 687 | continue; |
| 688 | } |
| 689 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 690 | if (ident == drv->derived[u]) { |
| 691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 692 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 693 | goto cleanup; |
| 694 | } |
| 695 | ly_set_add(&recursion, drv->derived[u], 0); |
| 696 | } |
| 697 | } |
| 698 | ret = LY_SUCCESS; |
| 699 | |
| 700 | cleanup: |
| 701 | ly_set_erase(&recursion, NULL); |
| 702 | return ret; |
| 703 | } |
| 704 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 705 | /** |
| 706 | * @brief Find and process the referenced base identities from another identity or identityref |
| 707 | * |
| 708 | * For bases in identity se backlinks to them from the base identities. For identityref, store |
| 709 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 710 | * to distinguish these two use cases. |
| 711 | * |
| 712 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 713 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 714 | * @param[in] ident Referencing identity to work with. |
| 715 | * @param[in] bases Array of bases of identityref to fill in. |
| 716 | * @return LY_ERR value. |
| 717 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 718 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 719 | 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] | 720 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 721 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 722 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 723 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 724 | struct lysc_ident **idref; |
| 725 | |
| 726 | assert(ident || bases); |
| 727 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 728 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 729 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 730 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 731 | return LY_EVALID; |
| 732 | } |
| 733 | |
| 734 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 735 | s = strchr(bases_p[u], ':'); |
| 736 | if (s) { |
| 737 | /* prefixed identity */ |
| 738 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 739 | 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] | 740 | } else { |
| 741 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 742 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 743 | } |
| 744 | if (!mod) { |
| 745 | if (ident) { |
| 746 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 747 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 748 | } else { |
| 749 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 750 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 751 | } |
| 752 | return LY_EVALID; |
| 753 | } |
| 754 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 755 | if (mod->compiled && mod->compiled->identities) { |
| 756 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 757 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 758 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 759 | if (ident == &mod->compiled->identities[v]) { |
| 760 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 761 | "Identity \"%s\" is derived from itself.", ident->name); |
| 762 | return LY_EVALID; |
| 763 | } |
| 764 | 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] | 765 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 766 | 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] | 767 | *idref = ident; |
| 768 | } else { |
| 769 | /* we have match! store the found identity */ |
| 770 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 771 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 772 | } |
| 773 | break; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | if (!idref || !(*idref)) { |
| 778 | if (ident) { |
| 779 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 780 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 781 | } else { |
| 782 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 783 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 784 | } |
| 785 | return LY_EVALID; |
| 786 | } |
| 787 | } |
| 788 | return LY_SUCCESS; |
| 789 | } |
| 790 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 791 | /** |
| 792 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 793 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 794 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 795 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 796 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 797 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 798 | static LY_ERR |
| 799 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 800 | { |
| 801 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 802 | |
| 803 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 804 | if (!idents_p[i].bases) { |
| 805 | continue; |
| 806 | } |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 807 | 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] | 808 | } |
| 809 | return LY_SUCCESS; |
| 810 | } |
| 811 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 812 | LY_ERR |
| 813 | lys_feature_precompile(struct ly_ctx *ctx, struct lysp_feature *features_p, struct lysc_feature **features) |
| 814 | { |
| 815 | unsigned int offset = 0, u; |
| 816 | struct lysc_ctx context = {0}; |
| 817 | |
| 818 | assert(ctx); |
| 819 | context.ctx = ctx; |
| 820 | |
| 821 | if (!features_p) { |
| 822 | return LY_SUCCESS; |
| 823 | } |
| 824 | if (*features) { |
| 825 | offset = LY_ARRAY_SIZE(*features); |
| 826 | } |
| 827 | |
| 828 | LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM); |
| 829 | LY_ARRAY_FOR(features_p, u) { |
| 830 | LY_ARRAY_INCREMENT(*features); |
| 831 | COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 832 | DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name); |
| 833 | DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 834 | DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref); |
| 835 | (*features)[offset + u].flags = features_p[u].flags; |
| 836 | } |
| 837 | |
| 838 | return LY_SUCCESS; |
| 839 | } |
| 840 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 841 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 842 | * @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] | 843 | * |
| 844 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 845 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 846 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 847 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 848 | * being currently processed). |
| 849 | * @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] | 850 | * @return LY_SUCCESS if everything is ok. |
| 851 | * @return LY_EVALID if the feature references indirectly itself. |
| 852 | */ |
| 853 | static LY_ERR |
| 854 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 855 | { |
| 856 | LY_ERR ret = LY_EVALID; |
| 857 | unsigned int u, v; |
| 858 | struct ly_set recursion = {0}; |
| 859 | struct lysc_feature *drv; |
| 860 | |
| 861 | if (!depfeatures) { |
| 862 | return LY_SUCCESS; |
| 863 | } |
| 864 | |
| 865 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 866 | if (feature == depfeatures[u]) { |
| 867 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 868 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 869 | goto cleanup; |
| 870 | } |
| 871 | ly_set_add(&recursion, depfeatures[u], 0); |
| 872 | } |
| 873 | |
| 874 | for (v = 0; v < recursion.count; ++v) { |
| 875 | drv = recursion.objs[v]; |
| 876 | if (!drv->depfeatures) { |
| 877 | continue; |
| 878 | } |
| 879 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 880 | if (feature == drv->depfeatures[u]) { |
| 881 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 882 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 883 | goto cleanup; |
| 884 | } |
| 885 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 886 | } |
| 887 | } |
| 888 | ret = LY_SUCCESS; |
| 889 | |
| 890 | cleanup: |
| 891 | ly_set_erase(&recursion, NULL); |
| 892 | return ret; |
| 893 | } |
| 894 | |
| 895 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 896 | * @brief Create pre-compiled features array. |
| 897 | * |
| 898 | * See lys_feature_precompile() for more details. |
| 899 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 900 | * @param[in] ctx Compile context. |
| 901 | * @param[in] feature_p Parsed feature definition to compile. |
| 902 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 903 | * @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] | 904 | * @return LY_ERR value. |
| 905 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 906 | static LY_ERR |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 907 | lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *features) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 908 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 909 | unsigned int u, v, x; |
| 910 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 911 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 912 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 913 | /* find the preprecompiled feature */ |
| 914 | LY_ARRAY_FOR(features, x) { |
| 915 | if (strcmp(features[x].name, feature_p->name)) { |
| 916 | continue; |
| 917 | } |
| 918 | feature = &features[x]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 919 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 920 | /* finish compilation started in lys_feature_precompile() */ |
| 921 | COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done); |
| 922 | COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 923 | if (feature->iffeatures) { |
| 924 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 925 | if (feature->iffeatures[u].features) { |
| 926 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 927 | /* check for circular dependency - direct reference first,... */ |
| 928 | if (feature == feature->iffeatures[u].features[v]) { |
| 929 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 930 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 931 | return LY_EVALID; |
| 932 | } |
| 933 | /* ... and indirect circular reference */ |
| 934 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 935 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 936 | /* add itself into the dependants list */ |
| 937 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 938 | *df = feature; |
| 939 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 940 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 941 | } |
| 942 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 943 | done: |
| 944 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 945 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 946 | |
| 947 | LOGINT(ctx->ctx); |
| 948 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 949 | } |
| 950 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 951 | /** |
| 952 | * @brief Revert compiled list of features back to the precompiled state. |
| 953 | * |
| 954 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 955 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 956 | * |
| 957 | * @param[in] ctx Compilation context. |
| 958 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 959 | * and supposed to hold the off_features list. |
| 960 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 961 | static void |
| 962 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 963 | { |
| 964 | unsigned int u, v; |
| 965 | |
| 966 | /* keep the off_features list until the complete lys_module is freed */ |
| 967 | mod->off_features = mod->compiled->features; |
| 968 | mod->compiled->features = NULL; |
| 969 | |
| 970 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 971 | * which may points into the data being freed here */ |
| 972 | LY_ARRAY_FOR(mod->off_features, u) { |
| 973 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 974 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 975 | } |
| 976 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 977 | mod->off_features[u].iffeatures = NULL; |
| 978 | |
| 979 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 980 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 981 | } |
| 982 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 983 | mod->off_features[u].exts = NULL; |
| 984 | } |
| 985 | } |
| 986 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 987 | /** |
| 988 | * @brief Validate and normalize numeric value from a range definition. |
| 989 | * @param[in] ctx Compile context. |
| 990 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 991 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 992 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 993 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 994 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 995 | * @param[in] value String value of the range boundary. |
| 996 | * @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. |
| 997 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 998 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 999 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1000 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1001 | 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] | 1002 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1003 | size_t fraction = 0, size; |
| 1004 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1005 | *len = 0; |
| 1006 | |
| 1007 | assert(value); |
| 1008 | /* parse value */ |
| 1009 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1010 | return LY_EVALID; |
| 1011 | } |
| 1012 | |
| 1013 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1014 | ++(*len); |
| 1015 | } |
| 1016 | |
| 1017 | while (isdigit(value[*len])) { |
| 1018 | ++(*len); |
| 1019 | } |
| 1020 | |
| 1021 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1022 | if (basetype == LY_TYPE_DEC64) { |
| 1023 | goto decimal; |
| 1024 | } else { |
| 1025 | *valcopy = strndup(value, *len); |
| 1026 | return LY_SUCCESS; |
| 1027 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1028 | } |
| 1029 | fraction = *len; |
| 1030 | |
| 1031 | ++(*len); |
| 1032 | while (isdigit(value[*len])) { |
| 1033 | ++(*len); |
| 1034 | } |
| 1035 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1036 | if (basetype == LY_TYPE_DEC64) { |
| 1037 | decimal: |
| 1038 | assert(frdigits); |
| 1039 | if (*len - 1 - fraction > frdigits) { |
| 1040 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1041 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1042 | *len, value, frdigits); |
| 1043 | return LY_EINVAL; |
| 1044 | } |
| 1045 | if (fraction) { |
| 1046 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1047 | } else { |
| 1048 | size = (*len) + frdigits + 1; |
| 1049 | } |
| 1050 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1051 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1052 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1053 | (*valcopy)[size - 1] = '\0'; |
| 1054 | if (fraction) { |
| 1055 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1056 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1057 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1058 | } else { |
| 1059 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1060 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1061 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1062 | } |
| 1063 | return LY_SUCCESS; |
| 1064 | } |
| 1065 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1066 | /** |
| 1067 | * @brief Check that values in range are in ascendant order. |
| 1068 | * @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] | 1069 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1070 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1071 | * @param[in] value Current value to check. |
| 1072 | * @param[in] prev_value The last seen value. |
| 1073 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1074 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1075 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1076 | 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] | 1077 | { |
| 1078 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1079 | 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] | 1080 | return LY_EEXIST; |
| 1081 | } |
| 1082 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1083 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1084 | return LY_EEXIST; |
| 1085 | } |
| 1086 | } |
| 1087 | return LY_SUCCESS; |
| 1088 | } |
| 1089 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1090 | /** |
| 1091 | * @brief Set min/max value of the range part. |
| 1092 | * @param[in] ctx Compile context. |
| 1093 | * @param[in] part Range part structure to fill. |
| 1094 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1095 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1096 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1097 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1098 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1099 | * @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] | 1100 | * @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] | 1101 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1102 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1103 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1104 | * frdigits value), LY_EMEM. |
| 1105 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1106 | static LY_ERR |
| 1107 | 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] | 1108 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1109 | { |
| 1110 | LY_ERR ret = LY_SUCCESS; |
| 1111 | char *valcopy = NULL; |
| 1112 | size_t len; |
| 1113 | |
| 1114 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1115 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1116 | LY_CHECK_GOTO(ret, finalize); |
| 1117 | } |
| 1118 | if (!valcopy && base_range) { |
| 1119 | if (max) { |
| 1120 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1121 | } else { |
| 1122 | part->min_64 = base_range->parts[0].min_64; |
| 1123 | } |
| 1124 | if (!first) { |
| 1125 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1126 | } |
| 1127 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1131 | case LY_TYPE_INT8: /* range */ |
| 1132 | if (valcopy) { |
| 1133 | ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64); |
| 1134 | } else if (max) { |
| 1135 | part->max_64 = INT64_C(127); |
| 1136 | } else { |
| 1137 | part->min_64 = INT64_C(-128); |
| 1138 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1139 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1140 | 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] | 1141 | } |
| 1142 | break; |
| 1143 | case LY_TYPE_INT16: /* range */ |
| 1144 | if (valcopy) { |
| 1145 | ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
| 1146 | } else if (max) { |
| 1147 | part->max_64 = INT64_C(32767); |
| 1148 | } else { |
| 1149 | part->min_64 = INT64_C(-32768); |
| 1150 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1151 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1152 | 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] | 1153 | } |
| 1154 | break; |
| 1155 | case LY_TYPE_INT32: /* range */ |
| 1156 | if (valcopy) { |
| 1157 | ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
| 1158 | } else if (max) { |
| 1159 | part->max_64 = INT64_C(2147483647); |
| 1160 | } else { |
| 1161 | part->min_64 = INT64_C(-2147483648); |
| 1162 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1163 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1164 | 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] | 1165 | } |
| 1166 | break; |
| 1167 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1168 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1169 | if (valcopy) { |
| 1170 | ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
| 1171 | max ? &part->max_64 : &part->min_64); |
| 1172 | } else if (max) { |
| 1173 | part->max_64 = INT64_C(9223372036854775807); |
| 1174 | } else { |
| 1175 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1176 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1177 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1178 | 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] | 1179 | } |
| 1180 | break; |
| 1181 | case LY_TYPE_UINT8: /* range */ |
| 1182 | if (valcopy) { |
| 1183 | ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64); |
| 1184 | } else if (max) { |
| 1185 | part->max_u64 = UINT64_C(255); |
| 1186 | } else { |
| 1187 | part->min_u64 = UINT64_C(0); |
| 1188 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1189 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1190 | 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] | 1191 | } |
| 1192 | break; |
| 1193 | case LY_TYPE_UINT16: /* range */ |
| 1194 | if (valcopy) { |
| 1195 | ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
| 1196 | } else if (max) { |
| 1197 | part->max_u64 = UINT64_C(65535); |
| 1198 | } else { |
| 1199 | part->min_u64 = UINT64_C(0); |
| 1200 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1201 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1202 | 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] | 1203 | } |
| 1204 | break; |
| 1205 | case LY_TYPE_UINT32: /* range */ |
| 1206 | if (valcopy) { |
| 1207 | ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64); |
| 1208 | } else if (max) { |
| 1209 | part->max_u64 = UINT64_C(4294967295); |
| 1210 | } else { |
| 1211 | part->min_u64 = UINT64_C(0); |
| 1212 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1213 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1214 | 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] | 1215 | } |
| 1216 | break; |
| 1217 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1218 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1219 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1220 | if (valcopy) { |
| 1221 | ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
| 1222 | } else if (max) { |
| 1223 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1224 | } else { |
| 1225 | part->min_u64 = UINT64_C(0); |
| 1226 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1227 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1228 | 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] | 1229 | } |
| 1230 | break; |
| 1231 | default: |
| 1232 | LOGINT(ctx->ctx); |
| 1233 | ret = LY_EINT; |
| 1234 | } |
| 1235 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1236 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1237 | if (ret == LY_EDENIED) { |
| 1238 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1239 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1240 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1241 | } else if (ret == LY_EVALID) { |
| 1242 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1243 | "Invalid %s restriction - invalid value \"%s\".", |
| 1244 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1245 | } else if (ret == LY_EEXIST) { |
| 1246 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1247 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1248 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1249 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1250 | } else if (!ret && value) { |
| 1251 | *value = *value + len; |
| 1252 | } |
| 1253 | free(valcopy); |
| 1254 | return ret; |
| 1255 | } |
| 1256 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1257 | /** |
| 1258 | * @brief Compile the parsed range restriction. |
| 1259 | * @param[in] ctx Compile context. |
| 1260 | * @param[in] range_p Parsed range structure to compile. |
| 1261 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1262 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1263 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1264 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1265 | * range restriction must be more restrictive than the base_range. |
| 1266 | * @param[in,out] range Pointer to the created current range structure. |
| 1267 | * @return LY_ERR value. |
| 1268 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1269 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1270 | 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] | 1271 | struct lysc_range *base_range, struct lysc_range **range) |
| 1272 | { |
| 1273 | LY_ERR ret = LY_EVALID; |
| 1274 | const char *expr; |
| 1275 | struct lysc_range_part *parts = NULL, *part; |
| 1276 | int range_expected = 0, uns; |
| 1277 | unsigned int parts_done = 0, u, v; |
| 1278 | |
| 1279 | assert(range); |
| 1280 | assert(range_p); |
| 1281 | |
| 1282 | expr = range_p->arg; |
| 1283 | while(1) { |
| 1284 | if (isspace(*expr)) { |
| 1285 | ++expr; |
| 1286 | } else if (*expr == '\0') { |
| 1287 | if (range_expected) { |
| 1288 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1289 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1290 | length_restr ? "length" : "range", range_p->arg); |
| 1291 | goto cleanup; |
| 1292 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1293 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1294 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1295 | length_restr ? "length" : "range", range_p->arg); |
| 1296 | goto cleanup; |
| 1297 | } |
| 1298 | parts_done++; |
| 1299 | break; |
| 1300 | } else if (!strncmp(expr, "min", 3)) { |
| 1301 | if (parts) { |
| 1302 | /* min cannot be used elsewhere than in the first part */ |
| 1303 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1304 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1305 | expr - range_p->arg, range_p->arg); |
| 1306 | goto cleanup; |
| 1307 | } |
| 1308 | expr += 3; |
| 1309 | |
| 1310 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1311 | 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] | 1312 | part->max_64 = part->min_64; |
| 1313 | } else if (*expr == '|') { |
| 1314 | if (!parts || range_expected) { |
| 1315 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1316 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1317 | goto cleanup; |
| 1318 | } |
| 1319 | expr++; |
| 1320 | parts_done++; |
| 1321 | /* process next part of the expression */ |
| 1322 | } else if (!strncmp(expr, "..", 2)) { |
| 1323 | expr += 2; |
| 1324 | while (isspace(*expr)) { |
| 1325 | expr++; |
| 1326 | } |
| 1327 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1328 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1329 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1330 | goto cleanup; |
| 1331 | } |
| 1332 | /* continue expecting the upper boundary */ |
| 1333 | range_expected = 1; |
| 1334 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1335 | /* number */ |
| 1336 | if (range_expected) { |
| 1337 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1338 | 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] | 1339 | range_expected = 0; |
| 1340 | } else { |
| 1341 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1342 | 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] | 1343 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1344 | part->max_64 = part->min_64; |
| 1345 | } |
| 1346 | |
| 1347 | /* continue with possible another expression part */ |
| 1348 | } else if (!strncmp(expr, "max", 3)) { |
| 1349 | expr += 3; |
| 1350 | while (isspace(*expr)) { |
| 1351 | expr++; |
| 1352 | } |
| 1353 | if (*expr != '\0') { |
| 1354 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1355 | length_restr ? "length" : "range", expr); |
| 1356 | goto cleanup; |
| 1357 | } |
| 1358 | if (range_expected) { |
| 1359 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1360 | 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] | 1361 | range_expected = 0; |
| 1362 | } else { |
| 1363 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1364 | 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] | 1365 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1366 | part->min_64 = part->max_64; |
| 1367 | } |
| 1368 | } else { |
| 1369 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1370 | length_restr ? "length" : "range", expr); |
| 1371 | goto cleanup; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | /* check with the previous range/length restriction */ |
| 1376 | if (base_range) { |
| 1377 | switch (basetype) { |
| 1378 | case LY_TYPE_BINARY: |
| 1379 | case LY_TYPE_UINT8: |
| 1380 | case LY_TYPE_UINT16: |
| 1381 | case LY_TYPE_UINT32: |
| 1382 | case LY_TYPE_UINT64: |
| 1383 | case LY_TYPE_STRING: |
| 1384 | uns = 1; |
| 1385 | break; |
| 1386 | case LY_TYPE_DEC64: |
| 1387 | case LY_TYPE_INT8: |
| 1388 | case LY_TYPE_INT16: |
| 1389 | case LY_TYPE_INT32: |
| 1390 | case LY_TYPE_INT64: |
| 1391 | uns = 0; |
| 1392 | break; |
| 1393 | default: |
| 1394 | LOGINT(ctx->ctx); |
| 1395 | ret = LY_EINT; |
| 1396 | goto cleanup; |
| 1397 | } |
| 1398 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1399 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1400 | goto baseerror; |
| 1401 | } |
| 1402 | /* current lower bound is not lower than the base */ |
| 1403 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1404 | /* base has single value */ |
| 1405 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1406 | /* both lower bounds are the same */ |
| 1407 | if (parts[u].min_64 != parts[u].max_64) { |
| 1408 | /* current continues with a range */ |
| 1409 | goto baseerror; |
| 1410 | } else { |
| 1411 | /* equal single values, move both forward */ |
| 1412 | ++v; |
| 1413 | continue; |
| 1414 | } |
| 1415 | } else { |
| 1416 | /* base is single value lower than current range, so the |
| 1417 | * value from base range is removed in the current, |
| 1418 | * move only base and repeat checking */ |
| 1419 | ++v; |
| 1420 | --u; |
| 1421 | continue; |
| 1422 | } |
| 1423 | } else { |
| 1424 | /* base is the range */ |
| 1425 | if (parts[u].min_64 == parts[u].max_64) { |
| 1426 | /* current is a single value */ |
| 1427 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1428 | /* current is behind the base range, so base range is omitted, |
| 1429 | * move the base and keep the current for further check */ |
| 1430 | ++v; |
| 1431 | --u; |
| 1432 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1433 | continue; |
| 1434 | } else { |
| 1435 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1436 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1437 | /* higher bound is higher than the current higher bound */ |
| 1438 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1439 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1440 | * continue with the same current, but move the base */ |
| 1441 | --u; |
| 1442 | ++v; |
| 1443 | continue; |
| 1444 | } |
| 1445 | /* current range starts within the base range but end behind it */ |
| 1446 | goto baseerror; |
| 1447 | } else { |
| 1448 | /* current range is smaller than the base, |
| 1449 | * move current, but stay with the base */ |
| 1450 | continue; |
| 1451 | } |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | if (u != parts_done) { |
| 1456 | baseerror: |
| 1457 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1458 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1459 | length_restr ? "length" : "range", range_p->arg); |
| 1460 | goto cleanup; |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | if (!(*range)) { |
| 1465 | *range = calloc(1, sizeof **range); |
| 1466 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1467 | } |
| 1468 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1469 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1470 | if (range_p->eapptag) { |
| 1471 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1472 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1473 | } |
| 1474 | if (range_p->emsg) { |
| 1475 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1476 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1477 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1478 | if (range_p->dsc) { |
| 1479 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1480 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1481 | } |
| 1482 | if (range_p->ref) { |
| 1483 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1484 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1485 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1486 | /* extensions are taken only from the last range by the caller */ |
| 1487 | |
| 1488 | (*range)->parts = parts; |
| 1489 | parts = NULL; |
| 1490 | ret = LY_SUCCESS; |
| 1491 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1492 | LY_ARRAY_FREE(parts); |
| 1493 | |
| 1494 | return ret; |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * @brief Checks pattern syntax. |
| 1499 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1500 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1501 | * @param[in] pattern Pattern to check. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1502 | * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed. |
| 1503 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1504 | */ |
| 1505 | static LY_ERR |
| 1506 | lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp) |
| 1507 | { |
| 1508 | int idx, idx2, start, end, err_offset, count; |
| 1509 | char *perl_regex, *ptr; |
| 1510 | const char *err_msg, *orig_ptr; |
| 1511 | pcre *precomp; |
| 1512 | #define URANGE_LEN 19 |
| 1513 | char *ublock2urange[][2] = { |
| 1514 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1515 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1516 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1517 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1518 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1519 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1520 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1521 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1522 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1523 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1524 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1525 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1526 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1527 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1528 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1529 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1530 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1531 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1532 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1533 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1534 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1535 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1536 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1537 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1538 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1539 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1540 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1541 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1542 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1543 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1544 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1545 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1546 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1547 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1548 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1549 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1550 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1551 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1552 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1553 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1554 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1555 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1556 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1557 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1558 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1559 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1560 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1561 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1562 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1563 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1564 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1565 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1566 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1567 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1568 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1569 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1570 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1571 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1572 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1573 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1574 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1575 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1576 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1577 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1578 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1579 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1580 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1581 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1582 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1583 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1584 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1585 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1586 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1587 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1588 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1589 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1590 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1591 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1592 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1593 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1594 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1595 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1596 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1597 | {NULL, NULL} |
| 1598 | }; |
| 1599 | |
| 1600 | /* adjust the expression to a Perl equivalent |
| 1601 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1602 | |
| 1603 | /* we need to replace all "$" with "\$", count them now */ |
| 1604 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1605 | |
| 1606 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1607 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1608 | perl_regex[0] = '\0'; |
| 1609 | |
| 1610 | ptr = perl_regex; |
| 1611 | |
| 1612 | if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) { |
| 1613 | /* we will add line-end anchoring */ |
| 1614 | ptr[0] = '('; |
| 1615 | ++ptr; |
| 1616 | } |
| 1617 | |
| 1618 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1619 | if (orig_ptr[0] == '$') { |
| 1620 | ptr += sprintf(ptr, "\\$"); |
| 1621 | } else if (orig_ptr[0] == '^') { |
| 1622 | ptr += sprintf(ptr, "\\^"); |
| 1623 | } else { |
| 1624 | ptr[0] = orig_ptr[0]; |
| 1625 | ++ptr; |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) { |
| 1630 | ptr += sprintf(ptr, ")$"); |
| 1631 | } else { |
| 1632 | ptr[0] = '\0'; |
| 1633 | ++ptr; |
| 1634 | } |
| 1635 | |
| 1636 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1637 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1638 | start = ptr - perl_regex; |
| 1639 | |
| 1640 | ptr = strchr(ptr, '}'); |
| 1641 | if (!ptr) { |
| 1642 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1643 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1644 | free(perl_regex); |
| 1645 | return LY_EVALID; |
| 1646 | } |
| 1647 | end = (ptr - perl_regex) + 1; |
| 1648 | |
| 1649 | /* need more space */ |
| 1650 | if (end - start < URANGE_LEN) { |
| 1651 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1652 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1653 | } |
| 1654 | |
| 1655 | /* find our range */ |
| 1656 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1657 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1658 | break; |
| 1659 | } |
| 1660 | } |
| 1661 | if (!ublock2urange[idx][0]) { |
| 1662 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1663 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1664 | free(perl_regex); |
| 1665 | return LY_EVALID; |
| 1666 | } |
| 1667 | |
| 1668 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1669 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1670 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1671 | ++count; |
| 1672 | } |
| 1673 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1674 | --count; |
| 1675 | } |
| 1676 | } |
| 1677 | if (count) { |
| 1678 | /* skip brackets */ |
| 1679 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1680 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1681 | } else { |
| 1682 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1683 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | /* must return 0, already checked during parsing */ |
| 1688 | precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE, |
| 1689 | &err_msg, &err_offset, NULL); |
| 1690 | if (!precomp) { |
| 1691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1692 | free(perl_regex); |
| 1693 | return LY_EVALID; |
| 1694 | } |
| 1695 | free(perl_regex); |
| 1696 | |
| 1697 | if (pcre_precomp) { |
| 1698 | *pcre_precomp = precomp; |
| 1699 | } else { |
| 1700 | free(precomp); |
| 1701 | } |
| 1702 | |
| 1703 | return LY_SUCCESS; |
| 1704 | |
| 1705 | #undef URANGE_LEN |
| 1706 | } |
| 1707 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1708 | /** |
| 1709 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1710 | * @param[in] ctx Compile context. |
| 1711 | * @param[in] patterns_p Array of parsed patterns from the current type to compile. |
| 1712 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 1713 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1714 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1715 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1716 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1717 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1718 | static LY_ERR |
| 1719 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options, |
| 1720 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1721 | { |
| 1722 | struct lysc_pattern **pattern; |
| 1723 | unsigned int u, v; |
| 1724 | const char *err_msg; |
| 1725 | LY_ERR ret = LY_SUCCESS; |
| 1726 | |
| 1727 | /* first, copy the patterns from the base type */ |
| 1728 | if (base_patterns) { |
| 1729 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1730 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1731 | } |
| 1732 | |
| 1733 | LY_ARRAY_FOR(patterns_p, u) { |
| 1734 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1735 | *pattern = calloc(1, sizeof **pattern); |
| 1736 | ++(*pattern)->refcount; |
| 1737 | |
| 1738 | ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr); |
| 1739 | LY_CHECK_RET(ret); |
| 1740 | (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg); |
| 1741 | if (err_msg) { |
| 1742 | LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg); |
| 1743 | } |
| 1744 | |
| 1745 | if (patterns_p[u].arg[0] == 0x15) { |
| 1746 | (*pattern)->inverted = 1; |
| 1747 | } |
| 1748 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1749 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1750 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 1751 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1752 | COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, |
| 1753 | options, v, lys_compile_ext, ret, done); |
| 1754 | } |
| 1755 | done: |
| 1756 | return ret; |
| 1757 | } |
| 1758 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1759 | /** |
| 1760 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1761 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1762 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1763 | 0 /* LY_TYPE_UNKNOWN */, |
| 1764 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1765 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1766 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1767 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1768 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 1769 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1770 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1771 | 0 /* LY_TYPE_BOOL */, |
| 1772 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 1773 | 0 /* LY_TYPE_EMPTY */, |
| 1774 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 1775 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 1776 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 1777 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1778 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 1779 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1780 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1781 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1782 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 1783 | }; |
| 1784 | |
| 1785 | /** |
| 1786 | * @brief stringification of the YANG built-in data types |
| 1787 | */ |
| 1788 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 1789 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 1790 | "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] | 1791 | }; |
| 1792 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1793 | /** |
| 1794 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 1795 | * @param[in] ctx Compile context. |
| 1796 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 1797 | * @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. |
| 1798 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 1799 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 1800 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 1801 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1802 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1803 | static LY_ERR |
| 1804 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options, |
| 1805 | struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums) |
| 1806 | { |
| 1807 | LY_ERR ret = LY_SUCCESS; |
| 1808 | unsigned int u, v, match; |
| 1809 | int32_t value = 0; |
| 1810 | uint32_t position = 0; |
| 1811 | struct lysc_type_enum_item *e, storage; |
| 1812 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1813 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1814 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 1815 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 1816 | return LY_EVALID; |
| 1817 | } |
| 1818 | |
| 1819 | LY_ARRAY_FOR(enums_p, u) { |
| 1820 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 1821 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1822 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 1823 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1824 | if (base_enums) { |
| 1825 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 1826 | LY_ARRAY_FOR(base_enums, v) { |
| 1827 | if (!strcmp(e->name, base_enums[v].name)) { |
| 1828 | break; |
| 1829 | } |
| 1830 | } |
| 1831 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 1832 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1833 | "Invalid %s - derived type adds new item \"%s\".", |
| 1834 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 1835 | return LY_EVALID; |
| 1836 | } |
| 1837 | match = v; |
| 1838 | } |
| 1839 | |
| 1840 | if (basetype == LY_TYPE_ENUM) { |
| 1841 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1842 | e->value = (int32_t)enums_p[u].value; |
| 1843 | if (!u || e->value >= value) { |
| 1844 | value = e->value + 1; |
| 1845 | } |
| 1846 | /* check collision with other values */ |
| 1847 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1848 | if (e->value == (*enums)[v].value) { |
| 1849 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1850 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 1851 | e->value, e->name, (*enums)[v].name); |
| 1852 | return LY_EVALID; |
| 1853 | } |
| 1854 | } |
| 1855 | } else if (base_enums) { |
| 1856 | /* inherit the assigned value */ |
| 1857 | e->value = base_enums[match].value; |
| 1858 | if (!u || e->value >= value) { |
| 1859 | value = e->value + 1; |
| 1860 | } |
| 1861 | } else { |
| 1862 | /* assign value automatically */ |
| 1863 | if (u && value == INT32_MIN) { |
| 1864 | /* counter overflow */ |
| 1865 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1866 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 1867 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 1868 | return LY_EVALID; |
| 1869 | } |
| 1870 | e->value = value++; |
| 1871 | } |
| 1872 | } else { /* LY_TYPE_BITS */ |
| 1873 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1874 | e->value = (int32_t)enums_p[u].value; |
| 1875 | if (!u || (uint32_t)e->value >= position) { |
| 1876 | position = (uint32_t)e->value + 1; |
| 1877 | } |
| 1878 | /* check collision with other values */ |
| 1879 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1880 | if (e->value == (*enums)[v].value) { |
| 1881 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1882 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 1883 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 1884 | return LY_EVALID; |
| 1885 | } |
| 1886 | } |
| 1887 | } else if (base_enums) { |
| 1888 | /* inherit the assigned value */ |
| 1889 | e->value = base_enums[match].value; |
| 1890 | if (!u || (uint32_t)e->value >= position) { |
| 1891 | position = (uint32_t)e->value + 1; |
| 1892 | } |
| 1893 | } else { |
| 1894 | /* assign value automatically */ |
| 1895 | if (u && position == 0) { |
| 1896 | /* counter overflow */ |
| 1897 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1898 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 1899 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 1900 | return LY_EVALID; |
| 1901 | } |
| 1902 | e->value = position++; |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | if (base_enums) { |
| 1907 | /* the assigned values must not change from the derived type */ |
| 1908 | if (e->value != base_enums[match].value) { |
| 1909 | if (basetype == LY_TYPE_ENUM) { |
| 1910 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1911 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 1912 | e->name, base_enums[match].value, e->value); |
| 1913 | } else { |
| 1914 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1915 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 1916 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 1917 | } |
| 1918 | return LY_EVALID; |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done); |
| 1923 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done); |
| 1924 | |
| 1925 | if (basetype == LY_TYPE_BITS) { |
| 1926 | /* keep bits ordered by position */ |
| 1927 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 1928 | if (v != u) { |
| 1929 | memcpy(&storage, e, sizeof *e); |
| 1930 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 1931 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 1932 | } |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | done: |
| 1937 | return ret; |
| 1938 | } |
| 1939 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1940 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 1941 | for ((NODE) = (NODE)->parent; \ |
| 1942 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 1943 | (NODE) = (NODE)->parent); \ |
| 1944 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 1945 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 1946 | TERM; \ |
| 1947 | } |
| 1948 | |
| 1949 | /** |
| 1950 | * @brief Validate the predicate(s) from the leafref path. |
| 1951 | * @param[in] ctx Compile context |
| 1952 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 1953 | * 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] | 1954 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1955 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1956 | * @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] | 1957 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1958 | */ |
| 1959 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1960 | 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] | 1961 | 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] | 1962 | { |
| 1963 | LY_ERR ret = LY_EVALID; |
| 1964 | const struct lys_module *mod; |
| 1965 | const struct lysc_node *src_node, *dst_node; |
| 1966 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 1967 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1968 | unsigned int dest_parent_times, c, u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1969 | const char *start, *end, *pke_end; |
| 1970 | struct ly_set keys = {0}; |
| 1971 | int i; |
| 1972 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1973 | assert(path_context); |
| 1974 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1975 | while (**predicate == '[') { |
| 1976 | start = (*predicate)++; |
| 1977 | |
| 1978 | while (isspace(**predicate)) { |
| 1979 | ++(*predicate); |
| 1980 | } |
| 1981 | LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup); |
| 1982 | while (isspace(**predicate)) { |
| 1983 | ++(*predicate); |
| 1984 | } |
| 1985 | if (**predicate != '=') { |
| 1986 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1987 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 1988 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1989 | goto cleanup; |
| 1990 | } |
| 1991 | ++(*predicate); |
| 1992 | while (isspace(**predicate)) { |
| 1993 | ++(*predicate); |
| 1994 | } |
| 1995 | |
| 1996 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 1997 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1998 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 1999 | goto cleanup; |
| 2000 | } |
| 2001 | --pke_end; |
| 2002 | while (isspace(*pke_end)) { |
| 2003 | --pke_end; |
| 2004 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2005 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2006 | /* localize path-key-expr */ |
| 2007 | pke_start = path_key_expr = *predicate; |
| 2008 | /* move after the current predicate */ |
| 2009 | *predicate = end + 1; |
| 2010 | |
| 2011 | /* source (must be leaf or leaf-list) */ |
| 2012 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2013 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2014 | if (!mod) { |
| 2015 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2016 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2017 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2018 | goto cleanup; |
| 2019 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2020 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2021 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2022 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2023 | src_node = NULL; |
| 2024 | if (context_node->keys) { |
| 2025 | for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) { |
| 2026 | if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') { |
| 2027 | src_node = (const struct lysc_node*)context_node->keys[u]; |
| 2028 | break; |
| 2029 | } |
| 2030 | } |
| 2031 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2032 | if (!src_node) { |
| 2033 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2034 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2035 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2036 | goto cleanup; |
| 2037 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2038 | |
| 2039 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2040 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2041 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2042 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2043 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2044 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2045 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2046 | *predicate - start, start, src_node->name); |
| 2047 | goto cleanup; |
| 2048 | } |
| 2049 | |
| 2050 | /* destination */ |
| 2051 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2052 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2053 | |
| 2054 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 2055 | if (strncmp(path_key_expr, "current()", 9)) { |
| 2056 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2057 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2058 | *predicate - start, start); |
| 2059 | goto cleanup; |
| 2060 | } |
| 2061 | path_key_expr += 9; |
| 2062 | while (isspace(*path_key_expr)) { |
| 2063 | ++path_key_expr; |
| 2064 | } |
| 2065 | |
| 2066 | if (*path_key_expr != '/') { |
| 2067 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2068 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2069 | *predicate - start, start); |
| 2070 | goto cleanup; |
| 2071 | } |
| 2072 | ++path_key_expr; |
| 2073 | while (isspace(*path_key_expr)) { |
| 2074 | ++path_key_expr; |
| 2075 | } |
| 2076 | |
| 2077 | /* rel-path-keyexpr: |
| 2078 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2079 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2080 | ++dest_parent_times; |
| 2081 | path_key_expr += 2; |
| 2082 | while (isspace(*path_key_expr)) { |
| 2083 | ++path_key_expr; |
| 2084 | } |
| 2085 | if (*path_key_expr != '/') { |
| 2086 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2087 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2088 | *predicate - start, start); |
| 2089 | goto cleanup; |
| 2090 | } |
| 2091 | ++path_key_expr; |
| 2092 | while (isspace(*path_key_expr)) { |
| 2093 | ++path_key_expr; |
| 2094 | } |
| 2095 | |
| 2096 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2097 | * all schema nodes that cannot be instantiated in data tree */ |
| 2098 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2099 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2100 | *predicate - start, start); |
| 2101 | } |
| 2102 | if (!dest_parent_times) { |
| 2103 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2104 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2105 | *predicate - start, start); |
| 2106 | goto cleanup; |
| 2107 | } |
| 2108 | if (path_key_expr == pke_end) { |
| 2109 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2110 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2111 | *predicate - start, start); |
| 2112 | goto cleanup; |
| 2113 | } |
| 2114 | |
| 2115 | while(path_key_expr != pke_end) { |
| 2116 | if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) { |
| 2117 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2118 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2119 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2120 | goto cleanup; |
| 2121 | } |
| 2122 | |
| 2123 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2124 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2125 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2126 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2127 | } |
| 2128 | if (!mod) { |
| 2129 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2130 | "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.", |
| 2131 | *predicate - start, start, dst_len, dst); |
| 2132 | goto cleanup; |
| 2133 | } |
| 2134 | |
| 2135 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2136 | if (!dst_node) { |
| 2137 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2138 | "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.", |
| 2139 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2140 | goto cleanup; |
| 2141 | } |
| 2142 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2143 | 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] | 2144 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2145 | "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] | 2146 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2147 | goto cleanup; |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | ret = LY_SUCCESS; |
| 2152 | cleanup: |
| 2153 | ly_set_erase(&keys, NULL); |
| 2154 | return ret; |
| 2155 | } |
| 2156 | |
| 2157 | /** |
| 2158 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2159 | * |
| 2160 | * path-arg = absolute-path / relative-path |
| 2161 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2162 | * relative-path = 1*(".." "/") descendant-path |
| 2163 | * |
| 2164 | * @param[in,out] path Path to parse. |
| 2165 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2166 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2167 | * @param[out] name Name of the token. |
| 2168 | * @param[out] nam_len Length of the name. |
| 2169 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2170 | * must not be changed between consecutive calls. -1 if the |
| 2171 | * path is absolute. |
| 2172 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2173 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2174 | */ |
| 2175 | static LY_ERR |
| 2176 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2177 | int *parent_times, int *has_predicate) |
| 2178 | { |
| 2179 | int par_times = 0; |
| 2180 | |
| 2181 | assert(path && *path); |
| 2182 | assert(parent_times); |
| 2183 | assert(prefix); |
| 2184 | assert(prefix_len); |
| 2185 | assert(name); |
| 2186 | assert(name_len); |
| 2187 | assert(has_predicate); |
| 2188 | |
| 2189 | *prefix = NULL; |
| 2190 | *prefix_len = 0; |
| 2191 | *name = NULL; |
| 2192 | *name_len = 0; |
| 2193 | *has_predicate = 0; |
| 2194 | |
| 2195 | if (!*parent_times) { |
| 2196 | if (!strncmp(*path, "..", 2)) { |
| 2197 | *path += 2; |
| 2198 | ++par_times; |
| 2199 | while (!strncmp(*path, "/..", 3)) { |
| 2200 | *path += 3; |
| 2201 | ++par_times; |
| 2202 | } |
| 2203 | } |
| 2204 | if (par_times) { |
| 2205 | *parent_times = par_times; |
| 2206 | } else { |
| 2207 | *parent_times = -1; |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | if (**path != '/') { |
| 2212 | return LY_EINVAL; |
| 2213 | } |
| 2214 | /* skip '/' */ |
| 2215 | ++(*path); |
| 2216 | |
| 2217 | /* node-identifier ([prefix:]name) */ |
| 2218 | LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len)); |
| 2219 | |
| 2220 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2221 | /* path continues by another token or this is the last token */ |
| 2222 | return LY_SUCCESS; |
| 2223 | } else if ((*path)[0] != '[') { |
| 2224 | /* unexpected character */ |
| 2225 | return LY_EINVAL; |
| 2226 | } else { |
| 2227 | /* predicate starting with [ */ |
| 2228 | *has_predicate = 1; |
| 2229 | return LY_SUCCESS; |
| 2230 | } |
| 2231 | } |
| 2232 | |
| 2233 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2234 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2235 | * |
| 2236 | * The set of features used for target must be a subset of features used for the leafref. |
| 2237 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2238 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2239 | * |
| 2240 | * @param[in] refnode The leafref node. |
| 2241 | * @param[in] target Tha target node of the leafref. |
| 2242 | * @return LY_SUCCESS or LY_EVALID; |
| 2243 | */ |
| 2244 | static LY_ERR |
| 2245 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2246 | { |
| 2247 | LY_ERR ret = LY_EVALID; |
| 2248 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2249 | unsigned int u, v, count; |
| 2250 | struct ly_set features = {0}; |
| 2251 | |
| 2252 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2253 | if (iter->iffeatures) { |
| 2254 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2255 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2256 | 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] | 2257 | } |
| 2258 | } |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2263 | * So when adding features applicable to the target into the features set, we should not be |
| 2264 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2265 | * to the leafref itself. */ |
| 2266 | count = features.count; |
| 2267 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2268 | if (iter->iffeatures) { |
| 2269 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2270 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2271 | 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] | 2272 | /* new feature was added (or LY_EMEM) */ |
| 2273 | goto cleanup; |
| 2274 | } |
| 2275 | } |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | ret = LY_SUCCESS; |
| 2280 | |
| 2281 | cleanup: |
| 2282 | ly_set_erase(&features, NULL); |
| 2283 | return ret; |
| 2284 | } |
| 2285 | |
| 2286 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2287 | * @brief Validate the leafref path. |
| 2288 | * @param[in] ctx Compile context |
| 2289 | * @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] | 2290 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2291 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2292 | */ |
| 2293 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2294 | 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] | 2295 | { |
| 2296 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2297 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2298 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2299 | const char *id, *prefix, *name; |
| 2300 | size_t prefix_len, name_len; |
| 2301 | int parent_times = 0, has_predicate; |
| 2302 | unsigned int iter, u; |
| 2303 | LY_ERR ret = LY_SUCCESS; |
| 2304 | |
| 2305 | assert(ctx); |
| 2306 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2307 | assert(leafref); |
| 2308 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2309 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2310 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2311 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2312 | if (!iter) { /* first iteration */ |
| 2313 | /* precess ".." in relative paths */ |
| 2314 | if (parent_times > 0) { |
| 2315 | /* move from the context node */ |
| 2316 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2317 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2318 | * all schema nodes that cannot be instantiated in data tree */ |
| 2319 | 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] | 2320 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2321 | } |
| 2322 | } |
| 2323 | } |
| 2324 | |
| 2325 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2326 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2327 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2328 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2329 | } |
| 2330 | if (!mod) { |
| 2331 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2332 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2333 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2334 | return LY_EVALID; |
| 2335 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2336 | if (!mod->implemented) { |
| 2337 | /* make the module implemented */ |
| 2338 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2339 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2340 | |
| 2341 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2342 | if (!node) { |
| 2343 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2344 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2345 | return LY_EVALID; |
| 2346 | } |
| 2347 | parent = node; |
| 2348 | |
| 2349 | if (has_predicate) { |
| 2350 | /* we have predicate, so the current result must be list */ |
| 2351 | if (node->nodetype != LYS_LIST) { |
| 2352 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2353 | "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] | 2354 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2355 | return LY_EVALID; |
| 2356 | } |
| 2357 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2358 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2359 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | ++iter; |
| 2363 | } |
| 2364 | if (ret) { |
| 2365 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2366 | "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] | 2367 | return LY_EVALID; |
| 2368 | } |
| 2369 | |
| 2370 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2371 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2372 | "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] | 2373 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2374 | return LY_EVALID; |
| 2375 | } |
| 2376 | |
| 2377 | /* check status */ |
| 2378 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2379 | return LY_EVALID; |
| 2380 | } |
| 2381 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2382 | /* check config */ |
| 2383 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2384 | if (node->flags & LYS_CONFIG_R) { |
| 2385 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2386 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2387 | leafref->path); |
| 2388 | return LY_EVALID; |
| 2389 | } |
| 2390 | } |
| 2391 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2392 | /* store the target's type and check for circular chain of leafrefs */ |
| 2393 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2394 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2395 | if (type == (struct lysc_type*)leafref) { |
| 2396 | /* circular chain detected */ |
| 2397 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2398 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2399 | return LY_EVALID; |
| 2400 | } |
| 2401 | } |
| 2402 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2403 | /* check if leafref and its target are under common if-features */ |
| 2404 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2405 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2406 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2407 | leafref->path); |
| 2408 | return LY_EVALID; |
| 2409 | } |
| 2410 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2411 | return LY_SUCCESS; |
| 2412 | } |
| 2413 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2414 | 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 | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2415 | struct lysp_type *type_p, int options, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2416 | /** |
| 2417 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2418 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2419 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2420 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2421 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2422 | * @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] | 2423 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2424 | * @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] | 2425 | * @param[in] basetype Base YANG built-in type of the type to compile. |
| 2426 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2427 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2428 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2429 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2430 | * @return LY_ERR value. |
| 2431 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2432 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2433 | 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, |
| 2434 | struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname, |
| 2435 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2436 | { |
| 2437 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2438 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2439 | struct lysc_type_bin *bin; |
| 2440 | struct lysc_type_num *num; |
| 2441 | struct lysc_type_str *str; |
| 2442 | struct lysc_type_bits *bits; |
| 2443 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2444 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2445 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2446 | struct lysc_type_union *un, *un_aux; |
| 2447 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2448 | |
| 2449 | switch (basetype) { |
| 2450 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2451 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2452 | |
| 2453 | /* 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] | 2454 | if (type_p->length) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2455 | ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2456 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length); |
| 2457 | LY_CHECK_RET(ret); |
| 2458 | if (!tpdfname) { |
| 2459 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, |
| 2460 | options, u, lys_compile_ext, ret, done); |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | if (tpdfname) { |
| 2465 | type_p->compiled = *type; |
| 2466 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2467 | } |
| 2468 | break; |
| 2469 | case LY_TYPE_BITS: |
| 2470 | /* RFC 7950 9.7 - bits */ |
| 2471 | bits = (struct lysc_type_bits*)(*type); |
| 2472 | if (type_p->bits) { |
| 2473 | ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options, |
| 2474 | base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2475 | (struct lysc_type_enum_item**)&bits->bits); |
| 2476 | LY_CHECK_RET(ret); |
| 2477 | } |
| 2478 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2479 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2480 | /* 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] | 2481 | if (tpdfname) { |
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 ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2483 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2484 | 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] | 2485 | free(*type); |
| 2486 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2487 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2488 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | if (tpdfname) { |
| 2492 | type_p->compiled = *type; |
| 2493 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2494 | } |
| 2495 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2496 | case LY_TYPE_DEC64: |
| 2497 | dec = (struct lysc_type_dec*)(*type); |
| 2498 | |
| 2499 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2500 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2501 | if (!type_p->fraction_digits) { |
| 2502 | if (tpdfname) { |
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 ", tpdfname); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2504 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2505 | 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] | 2506 | free(*type); |
| 2507 | *type = NULL; |
| 2508 | } |
| 2509 | return LY_EVALID; |
| 2510 | } |
| 2511 | } else if (type_p->fraction_digits) { |
| 2512 | /* 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] | 2513 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2514 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2515 | "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] | 2516 | tpdfname); |
| 2517 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2518 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2519 | "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] | 2520 | free(*type); |
| 2521 | *type = NULL; |
| 2522 | } |
| 2523 | return LY_EVALID; |
| 2524 | } |
| 2525 | dec->fraction_digits = type_p->fraction_digits; |
| 2526 | |
| 2527 | /* RFC 7950 9.2.4 - range */ |
| 2528 | if (type_p->range) { |
| 2529 | ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2530 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range); |
| 2531 | LY_CHECK_RET(ret); |
| 2532 | if (!tpdfname) { |
| 2533 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, |
| 2534 | options, u, lys_compile_ext, ret, done); |
| 2535 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | if (tpdfname) { |
| 2539 | type_p->compiled = *type; |
| 2540 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2541 | } |
| 2542 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2543 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2544 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2545 | |
| 2546 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2547 | if (type_p->length) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2548 | ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2549 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length); |
| 2550 | LY_CHECK_RET(ret); |
| 2551 | if (!tpdfname) { |
| 2552 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, |
| 2553 | options, u, lys_compile_ext, ret, done); |
| 2554 | } |
| 2555 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2556 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2557 | } |
| 2558 | |
| 2559 | /* RFC 7950 9.4.5 - pattern */ |
| 2560 | if (type_p->patterns) { |
| 2561 | ret = lys_compile_type_patterns(ctx, type_p->patterns, options, |
| 2562 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns); |
| 2563 | LY_CHECK_RET(ret); |
| 2564 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2565 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2566 | } |
| 2567 | |
| 2568 | if (tpdfname) { |
| 2569 | type_p->compiled = *type; |
| 2570 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2571 | } |
| 2572 | break; |
| 2573 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2574 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2575 | |
| 2576 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2577 | if (type_p->enums) { |
| 2578 | ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options, |
| 2579 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums); |
| 2580 | LY_CHECK_RET(ret); |
| 2581 | } |
| 2582 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2583 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2584 | /* 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] | 2585 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2586 | 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] | 2587 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2588 | 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] | 2589 | free(*type); |
| 2590 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2591 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2592 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | if (tpdfname) { |
| 2596 | type_p->compiled = *type; |
| 2597 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2598 | } |
| 2599 | break; |
| 2600 | case LY_TYPE_INT8: |
| 2601 | case LY_TYPE_UINT8: |
| 2602 | case LY_TYPE_INT16: |
| 2603 | case LY_TYPE_UINT16: |
| 2604 | case LY_TYPE_INT32: |
| 2605 | case LY_TYPE_UINT32: |
| 2606 | case LY_TYPE_INT64: |
| 2607 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2608 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2609 | |
| 2610 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2611 | if (type_p->range) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2612 | ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2613 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range); |
| 2614 | LY_CHECK_RET(ret); |
| 2615 | if (!tpdfname) { |
| 2616 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, |
| 2617 | options, u, lys_compile_ext, ret, done); |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | if (tpdfname) { |
| 2622 | type_p->compiled = *type; |
| 2623 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2624 | } |
| 2625 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2626 | case LY_TYPE_IDENT: |
| 2627 | idref = (struct lysc_type_identityref*)(*type); |
| 2628 | |
| 2629 | /* RFC 7950 9.10.2 - base */ |
| 2630 | if (type_p->bases) { |
| 2631 | if (base) { |
| 2632 | /* only the directly derived identityrefs can contain base specification */ |
| 2633 | if (tpdfname) { |
| 2634 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2635 | "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] | 2636 | tpdfname); |
| 2637 | } else { |
| 2638 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2639 | "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] | 2640 | free(*type); |
| 2641 | *type = NULL; |
| 2642 | } |
| 2643 | return LY_EVALID; |
| 2644 | } |
| 2645 | ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases); |
| 2646 | LY_CHECK_RET(ret); |
| 2647 | } |
| 2648 | |
| 2649 | if (!base && !type_p->flags) { |
| 2650 | /* type derived from identityref built-in type must contain at least one base */ |
| 2651 | if (tpdfname) { |
| 2652 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2653 | } else { |
| 2654 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2655 | free(*type); |
| 2656 | *type = NULL; |
| 2657 | } |
| 2658 | return LY_EVALID; |
| 2659 | } |
| 2660 | |
| 2661 | if (tpdfname) { |
| 2662 | type_p->compiled = *type; |
| 2663 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2664 | } |
| 2665 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2666 | case LY_TYPE_LEAFREF: |
| 2667 | /* RFC 7950 9.9.3 - require-instance */ |
| 2668 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2669 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2670 | if (tpdfname) { |
| 2671 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2672 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2673 | } else { |
| 2674 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2675 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2676 | free(*type); |
| 2677 | *type = NULL; |
| 2678 | } |
| 2679 | return LY_EVALID; |
| 2680 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2681 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2682 | } else if (base) { |
| 2683 | /* inherit */ |
| 2684 | ((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] | 2685 | } else { |
| 2686 | /* default is true */ |
| 2687 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2688 | } |
| 2689 | if (type_p->path) { |
| 2690 | 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] | 2691 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2692 | } else if (base) { |
| 2693 | 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] | 2694 | ((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] | 2695 | } else if (tpdfname) { |
| 2696 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2697 | return LY_EVALID; |
| 2698 | } else { |
| 2699 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2700 | free(*type); |
| 2701 | *type = NULL; |
| 2702 | return LY_EVALID; |
| 2703 | } |
| 2704 | if (tpdfname) { |
| 2705 | type_p->compiled = *type; |
| 2706 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2707 | } |
| 2708 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2709 | case LY_TYPE_INST: |
| 2710 | /* RFC 7950 9.9.3 - require-instance */ |
| 2711 | if (type_p->flags & LYS_SET_REQINST) { |
| 2712 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2713 | } else { |
| 2714 | /* default is true */ |
| 2715 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2716 | } |
| 2717 | |
| 2718 | if (tpdfname) { |
| 2719 | type_p->compiled = *type; |
| 2720 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2721 | } |
| 2722 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2723 | case LY_TYPE_UNION: |
| 2724 | un = (struct lysc_type_union*)(*type); |
| 2725 | |
| 2726 | /* RFC 7950 7.4 - type */ |
| 2727 | if (type_p->types) { |
| 2728 | if (base) { |
| 2729 | /* only the directly derived union can contain types specification */ |
| 2730 | if (tpdfname) { |
| 2731 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2732 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2733 | tpdfname); |
| 2734 | } else { |
| 2735 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2736 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2737 | free(*type); |
| 2738 | *type = NULL; |
| 2739 | } |
| 2740 | return LY_EVALID; |
| 2741 | } |
| 2742 | /* compile the type */ |
| 2743 | additional = 0; |
| 2744 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 2745 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2746 | ret = lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], options, &un->types[u + additional], NULL); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2747 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2748 | /* add space for additional types from the union subtype */ |
| 2749 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 2750 | 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))); |
| 2751 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2752 | un->types = (void*)((uint32_t*)(p) + 1); |
| 2753 | |
| 2754 | /* copy subtypes of the subtype union */ |
| 2755 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 2756 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2757 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2758 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2759 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2760 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 2761 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 2762 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 2763 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2764 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 2765 | /* TODO extensions */ |
| 2766 | |
| 2767 | } else { |
| 2768 | un->types[u + additional] = un_aux->types[v]; |
| 2769 | ++un_aux->types[v]->refcount; |
| 2770 | } |
| 2771 | ++additional; |
| 2772 | LY_ARRAY_INCREMENT(un->types); |
| 2773 | } |
| 2774 | /* compensate u increment in main loop */ |
| 2775 | --additional; |
| 2776 | |
| 2777 | /* free the replaced union subtype */ |
| 2778 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2779 | } else { |
| 2780 | LY_ARRAY_INCREMENT(un->types); |
| 2781 | } |
| 2782 | LY_CHECK_RET(ret); |
| 2783 | } |
| 2784 | } |
| 2785 | |
| 2786 | if (!base && !type_p->flags) { |
| 2787 | /* type derived from union built-in type must contain at least one type */ |
| 2788 | if (tpdfname) { |
| 2789 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2790 | } else { |
| 2791 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2792 | free(*type); |
| 2793 | *type = NULL; |
| 2794 | } |
| 2795 | return LY_EVALID; |
| 2796 | } |
| 2797 | |
| 2798 | if (tpdfname) { |
| 2799 | type_p->compiled = *type; |
| 2800 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2801 | } |
| 2802 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2803 | case LY_TYPE_BOOL: |
| 2804 | case LY_TYPE_EMPTY: |
| 2805 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2806 | break; |
| 2807 | } |
| 2808 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2809 | done: |
| 2810 | return ret; |
| 2811 | } |
| 2812 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2813 | /** |
| 2814 | * @brief Compile information about the leaf/leaf-list's type. |
| 2815 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2816 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2817 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2818 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2819 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2820 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2821 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2822 | * @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] | 2823 | * @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] | 2824 | * @return LY_ERR value. |
| 2825 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2826 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2827 | 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 | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2828 | struct lysp_type *type_p, int options, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2829 | { |
| 2830 | LY_ERR ret = LY_SUCCESS; |
| 2831 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2832 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2833 | struct type_context { |
| 2834 | const struct lysp_tpdf *tpdf; |
| 2835 | struct lysp_node *node; |
| 2836 | struct lysp_module *mod; |
| 2837 | } *tctx, *tctx_prev = NULL; |
| 2838 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2839 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2840 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2841 | const char *dflt = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2842 | |
| 2843 | (*type) = NULL; |
| 2844 | |
| 2845 | tctx = calloc(1, sizeof *tctx); |
| 2846 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2847 | 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] | 2848 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2849 | ret == LY_SUCCESS; |
| 2850 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2851 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2852 | if (basetype) { |
| 2853 | break; |
| 2854 | } |
| 2855 | |
| 2856 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2857 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2858 | 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] | 2859 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2860 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2861 | if (units && !*units) { |
| 2862 | /* inherit units */ |
| 2863 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2864 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2865 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2866 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2867 | dflt = tctx->tpdf->dflt; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2868 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2869 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2870 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2871 | break; |
| 2872 | } |
| 2873 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2874 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2875 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2876 | * 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] | 2877 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2878 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2879 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2880 | dummyloops = 1; |
| 2881 | goto preparenext; |
| 2882 | } else { |
| 2883 | tctx = NULL; |
| 2884 | break; |
| 2885 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | /* store information for the following processing */ |
| 2889 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2890 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2891 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2892 | /* prepare next loop */ |
| 2893 | tctx_prev = tctx; |
| 2894 | tctx = calloc(1, sizeof *tctx); |
| 2895 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2896 | } |
| 2897 | free(tctx); |
| 2898 | |
| 2899 | /* allocate type according to the basetype */ |
| 2900 | switch (basetype) { |
| 2901 | case LY_TYPE_BINARY: |
| 2902 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2903 | break; |
| 2904 | case LY_TYPE_BITS: |
| 2905 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2906 | break; |
| 2907 | case LY_TYPE_BOOL: |
| 2908 | case LY_TYPE_EMPTY: |
| 2909 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2910 | break; |
| 2911 | case LY_TYPE_DEC64: |
| 2912 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2913 | break; |
| 2914 | case LY_TYPE_ENUM: |
| 2915 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2916 | break; |
| 2917 | case LY_TYPE_IDENT: |
| 2918 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2919 | break; |
| 2920 | case LY_TYPE_INST: |
| 2921 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2922 | break; |
| 2923 | case LY_TYPE_LEAFREF: |
| 2924 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2925 | break; |
| 2926 | case LY_TYPE_STRING: |
| 2927 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2928 | break; |
| 2929 | case LY_TYPE_UNION: |
| 2930 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2931 | break; |
| 2932 | case LY_TYPE_INT8: |
| 2933 | case LY_TYPE_UINT8: |
| 2934 | case LY_TYPE_INT16: |
| 2935 | case LY_TYPE_UINT16: |
| 2936 | case LY_TYPE_INT32: |
| 2937 | case LY_TYPE_UINT32: |
| 2938 | case LY_TYPE_INT64: |
| 2939 | case LY_TYPE_UINT64: |
| 2940 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2941 | break; |
| 2942 | case LY_TYPE_UNKNOWN: |
| 2943 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2944 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 2945 | ret = LY_EVALID; |
| 2946 | goto cleanup; |
| 2947 | } |
| 2948 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2949 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2950 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 2951 | ly_data_type2str[basetype]); |
| 2952 | free(*type); |
| 2953 | (*type) = NULL; |
| 2954 | ret = LY_EVALID; |
| 2955 | goto cleanup; |
| 2956 | } |
| 2957 | |
| 2958 | /* get restrictions from the referred typedefs */ |
| 2959 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 2960 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2961 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2962 | base = tctx->tpdf->type.compiled; |
| 2963 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2964 | } 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] | 2965 | /* no change, just use the type information from the base */ |
| 2966 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 2967 | ++base->refcount; |
| 2968 | continue; |
| 2969 | } |
| 2970 | |
| 2971 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2972 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 2973 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 2974 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 2975 | ret = LY_EVALID; |
| 2976 | goto cleanup; |
| 2977 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 2978 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2979 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 2980 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 2981 | ret = LY_EVALID; |
| 2982 | goto cleanup; |
| 2983 | } |
| 2984 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2985 | (*type)->basetype = basetype; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2986 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2987 | 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] | 2988 | basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL, |
| 2989 | basetype, options, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2990 | LY_CHECK_GOTO(ret, cleanup); |
| 2991 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2992 | } |
| 2993 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2994 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2995 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2996 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2997 | (*type)->basetype = basetype; |
| 2998 | ++(*type)->refcount; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2999 | ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, options, NULL, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3000 | LY_CHECK_GOTO(ret, cleanup); |
| 3001 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3002 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3003 | free(*type); |
| 3004 | (*type) = base; |
| 3005 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3006 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3007 | if (!(*type)->dflt) { |
| 3008 | DUP_STRING(ctx->ctx, dflt, (*type)->dflt); |
| 3009 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3010 | |
| 3011 | COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup); |
| 3012 | |
| 3013 | cleanup: |
| 3014 | ly_set_erase(&tpdf_chain, free); |
| 3015 | return ret; |
| 3016 | } |
| 3017 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3018 | /** |
| 3019 | * @brief Compile status information of the given node. |
| 3020 | * |
| 3021 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3022 | * has the status correctly set during the compilation. |
| 3023 | * |
| 3024 | * @param[in] ctx Compile context |
| 3025 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3026 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3027 | * the compatibility with the parent's status value. |
| 3028 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3029 | * @return LY_ERR value. |
| 3030 | */ |
| 3031 | static LY_ERR |
| 3032 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3033 | { |
| 3034 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3035 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3036 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3037 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3038 | if ((parent_flags & 0x3) != 0x3) { |
| 3039 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3040 | * combination of bits (0x3) which marks the uses_status value */ |
| 3041 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3042 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3043 | } |
| 3044 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3045 | } else { |
| 3046 | (*node_flags) |= LYS_STATUS_CURR; |
| 3047 | } |
| 3048 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3049 | /* check status compatibility with the parent */ |
| 3050 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3051 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3052 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3053 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3054 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3055 | } else { /* LYS_STATUS_DEPRC */ |
| 3056 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3057 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3058 | } |
| 3059 | return LY_EVALID; |
| 3060 | } |
| 3061 | } |
| 3062 | return LY_SUCCESS; |
| 3063 | } |
| 3064 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3065 | /** |
| 3066 | * @brief Check uniqness of the node/action/notification name. |
| 3067 | * |
| 3068 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3069 | * structures, but they share the namespace so we need to check their name collisions. |
| 3070 | * |
| 3071 | * @param[in] ctx Compile context. |
| 3072 | * @param[in] children List (linked list) of data nodes to go through. |
| 3073 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3074 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3075 | * @param[in] name Name of the item to find in the given lists. |
| 3076 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3077 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3078 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3079 | */ |
| 3080 | static LY_ERR |
| 3081 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3082 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3083 | const char *name, void *exclude) |
| 3084 | { |
| 3085 | const struct lysc_node *iter; |
| 3086 | unsigned int u; |
| 3087 | |
| 3088 | LY_LIST_FOR(children, iter) { |
| 3089 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3090 | goto error; |
| 3091 | } |
| 3092 | } |
| 3093 | LY_ARRAY_FOR(actions, u) { |
| 3094 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3095 | goto error; |
| 3096 | } |
| 3097 | } |
| 3098 | LY_ARRAY_FOR(notifs, u) { |
| 3099 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3100 | goto error; |
| 3101 | } |
| 3102 | } |
| 3103 | return LY_SUCCESS; |
| 3104 | error: |
| 3105 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification"); |
| 3106 | return LY_EEXIST; |
| 3107 | } |
| 3108 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3109 | static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent, uint16_t uses_status); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3110 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3111 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3112 | * @brief Compile parsed RPC/action schema node information. |
| 3113 | * @param[in] ctx Compile context |
| 3114 | * @param[in] node_p Parsed RPC/action schema node. |
| 3115 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3116 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3117 | * @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). |
| 3118 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3119 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3120 | */ |
| 3121 | static LY_ERR |
| 3122 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, int options, |
| 3123 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3124 | { |
| 3125 | LY_ERR ret = LY_SUCCESS; |
| 3126 | struct lysp_node *child_p; |
| 3127 | unsigned int u; |
| 3128 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3129 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3130 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3131 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3132 | action_p->name, action)) { |
| 3133 | return LY_EVALID; |
| 3134 | } |
| 3135 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 3136 | if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3137 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 3138 | "Action \"%s\" is placed inside %s.", action_p->name, |
| 3139 | options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3140 | return LY_EVALID; |
| 3141 | } |
| 3142 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3143 | action->nodetype = LYS_ACTION; |
| 3144 | action->module = ctx->mod; |
| 3145 | action->parent = parent; |
| 3146 | if (!(options & LYSC_OPT_FREE_SP)) { |
| 3147 | action->sp = action_p; |
| 3148 | } |
| 3149 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3150 | |
| 3151 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3152 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3153 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3154 | |
| 3155 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3156 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3157 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
| 3158 | COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, options, u, lys_compile_iffeature, ret, cleanup); |
| 3159 | COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, options, u, lys_compile_ext, ret, cleanup); |
| 3160 | |
| 3161 | /* input */ |
| 3162 | COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, options, u, lys_compile_must, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 3163 | COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, options, u, lys_compile_ext, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3164 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3165 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_RPC_INPUT, (struct lysc_node*)action, uses_status)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | /* output */ |
| 3169 | COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, options, u, lys_compile_must, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 3170 | COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, options, u, lys_compile_ext, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3171 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3172 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_RPC_OUTPUT, (struct lysc_node*)action, uses_status)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | cleanup: |
| 3176 | return ret; |
| 3177 | } |
| 3178 | |
| 3179 | /** |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 3180 | * @brief Compile parsed RPC/action schema node information. |
| 3181 | * @param[in] ctx Compile context |
| 3182 | * @param[in] node_p Parsed RPC/action schema node. |
| 3183 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3184 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3185 | * @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). |
| 3186 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3187 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3188 | */ |
| 3189 | static LY_ERR |
| 3190 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, int options, |
| 3191 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3192 | { |
| 3193 | LY_ERR ret = LY_SUCCESS; |
| 3194 | struct lysp_node *child_p; |
| 3195 | unsigned int u; |
| 3196 | |
| 3197 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3198 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3199 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3200 | notif_p->name, notif)) { |
| 3201 | return LY_EVALID; |
| 3202 | } |
| 3203 | |
| 3204 | if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
| 3205 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3206 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
| 3207 | options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification"); |
| 3208 | return LY_EVALID; |
| 3209 | } |
| 3210 | |
| 3211 | notif->nodetype = LYS_NOTIF; |
| 3212 | notif->module = ctx->mod; |
| 3213 | notif->parent = parent; |
| 3214 | if (!(options & LYSC_OPT_FREE_SP)) { |
| 3215 | notif->sp = notif_p; |
| 3216 | } |
| 3217 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3218 | |
| 3219 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3220 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3221 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3222 | |
| 3223 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3224 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3225 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
| 3226 | COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, options, u, lys_compile_iffeature, ret, cleanup); |
| 3227 | COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, options, u, lys_compile_ext, ret, cleanup); |
| 3228 | COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, options, u, lys_compile_must, ret, cleanup); |
| 3229 | |
| 3230 | LY_LIST_FOR(notif_p->data, child_p) { |
| 3231 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_NOTIFICATION, (struct lysc_node*)notif, uses_status)); |
| 3232 | } |
| 3233 | |
| 3234 | cleanup: |
| 3235 | return ret; |
| 3236 | } |
| 3237 | |
| 3238 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3239 | * @brief Compile parsed container node information. |
| 3240 | * @param[in] ctx Compile context |
| 3241 | * @param[in] node_p Parsed container node. |
| 3242 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3243 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3244 | * is enriched with the container-specific information. |
| 3245 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3246 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3247 | static LY_ERR |
| 3248 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3249 | { |
| 3250 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3251 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3252 | struct lysp_node *child_p; |
| 3253 | unsigned int u; |
| 3254 | LY_ERR ret = LY_SUCCESS; |
| 3255 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3256 | if (cont_p->presence) { |
| 3257 | cont->flags |= LYS_PRESENCE; |
| 3258 | } |
| 3259 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3260 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3261 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3265 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, options, u, lys_compile_action, 0, ret, done); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 3266 | COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, options, u, lys_compile_notif, 0, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3267 | |
| 3268 | done: |
| 3269 | return ret; |
| 3270 | } |
| 3271 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3272 | /* |
| 3273 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3274 | * @param[in] ctx Compile context. |
| 3275 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3276 | * @param[in] type_p Parsed type to compile. |
| 3277 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3278 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3279 | * @return LY_ERR value. |
| 3280 | */ |
| 3281 | static LY_ERR |
| 3282 | lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, int options, struct lysc_node_leaf *leaf) |
| 3283 | { |
| 3284 | unsigned int u, v; |
| 3285 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3286 | |
| 3287 | LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, options, &leaf->type, |
| 3288 | leaf->units ? NULL : &leaf->units)); |
| 3289 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3290 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
| 3291 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
| 3292 | DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3293 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3294 | } |
| 3295 | } else { |
| 3296 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
| 3297 | DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3298 | } |
| 3299 | } |
| 3300 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3301 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3302 | ly_set_add(&ctx->unres, leaf, 0); |
| 3303 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3304 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3305 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3306 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3307 | ly_set_add(&ctx->unres, leaf, 0); |
| 3308 | } |
| 3309 | } |
| 3310 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
| 3311 | if (leaf->flags & LYS_SET_DFLT) { |
| 3312 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).", |
| 3313 | leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt); |
| 3314 | return LY_EVALID; |
| 3315 | } |
| 3316 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3317 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3318 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3319 | return LY_EVALID; |
| 3320 | } |
| 3321 | } |
| 3322 | |
| 3323 | if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3324 | /* configuration data values must be unique - so check the default values */ |
| 3325 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3326 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3327 | if (!strcmp(llist->dflts[u], llist->dflts[v])) { |
| 3328 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3329 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]); |
| 3330 | return LY_EVALID; |
| 3331 | } |
| 3332 | } |
| 3333 | } |
| 3334 | } |
| 3335 | |
| 3336 | /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */ |
| 3337 | |
| 3338 | return LY_SUCCESS; |
| 3339 | } |
| 3340 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3341 | /** |
| 3342 | * @brief Compile parsed leaf node information. |
| 3343 | * @param[in] ctx Compile context |
| 3344 | * @param[in] node_p Parsed leaf node. |
| 3345 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3346 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3347 | * is enriched with the leaf-specific information. |
| 3348 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3349 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3350 | static LY_ERR |
| 3351 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3352 | { |
| 3353 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3354 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3355 | unsigned int u; |
| 3356 | LY_ERR ret = LY_SUCCESS; |
| 3357 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3358 | COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3359 | if (leaf_p->units) { |
| 3360 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3361 | leaf->flags |= LYS_SET_UNITS; |
| 3362 | } |
| 3363 | if (leaf_p->dflt) { |
| 3364 | leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3365 | leaf->flags |= LYS_SET_DFLT; |
| 3366 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3367 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3368 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, options, leaf); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3369 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3370 | done: |
| 3371 | return ret; |
| 3372 | } |
| 3373 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3374 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3375 | * @brief Compile parsed leaf-list node information. |
| 3376 | * @param[in] ctx Compile context |
| 3377 | * @param[in] node_p Parsed leaf-list node. |
| 3378 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3379 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3380 | * is enriched with the leaf-list-specific information. |
| 3381 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3382 | */ |
| 3383 | static LY_ERR |
| 3384 | lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3385 | { |
| 3386 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3387 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3388 | unsigned int u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3389 | LY_ERR ret = LY_SUCCESS; |
| 3390 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3391 | COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3392 | if (llist_p->units) { |
| 3393 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3394 | llist->flags |= LYS_SET_UNITS; |
| 3395 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3396 | |
| 3397 | if (llist_p->dflts) { |
| 3398 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3399 | LY_ARRAY_FOR(llist_p->dflts, u) { |
| 3400 | DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]); |
| 3401 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3402 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3403 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3407 | if (llist->min) { |
| 3408 | llist->flags |= LYS_MAND_TRUE; |
| 3409 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3410 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3411 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3412 | ret = lys_compile_node_type(ctx, node_p, &llist_p->type, options, (struct lysc_node_leaf*)llist); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3413 | |
| 3414 | done: |
| 3415 | return ret; |
| 3416 | } |
| 3417 | |
| 3418 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3419 | * @brief Compile information about list's uniques. |
| 3420 | * @param[in] ctx Compile context. |
| 3421 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3422 | * @param[in] uniques Sized array list of unique statements. |
| 3423 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3424 | * @return LY_ERR value. |
| 3425 | */ |
| 3426 | static LY_ERR |
| 3427 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3428 | { |
| 3429 | LY_ERR ret = LY_SUCCESS; |
| 3430 | struct lysc_node_leaf **key, ***unique; |
| 3431 | const char *keystr, *delim; |
| 3432 | size_t len; |
| 3433 | unsigned int v; |
| 3434 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3435 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3436 | |
| 3437 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3438 | config = -1; |
| 3439 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3440 | keystr = uniques[v]; |
| 3441 | while (keystr) { |
| 3442 | delim = strpbrk(keystr, " \t\n"); |
| 3443 | if (delim) { |
| 3444 | len = delim - keystr; |
| 3445 | while (isspace(*delim)) { |
| 3446 | ++delim; |
| 3447 | } |
| 3448 | } else { |
| 3449 | len = strlen(keystr); |
| 3450 | } |
| 3451 | |
| 3452 | /* unique node must be present */ |
| 3453 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3454 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3455 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3456 | if (ret != LY_SUCCESS) { |
| 3457 | if (ret == LY_EDENIED) { |
| 3458 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3459 | "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] | 3460 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3461 | } |
| 3462 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3463 | } else if (flags) { |
| 3464 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3465 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3466 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
| 3467 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3468 | } |
| 3469 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3470 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3471 | /* all referenced leafs must be of the same config type */ |
| 3472 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3473 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3474 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3475 | return LY_EVALID; |
| 3476 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3477 | config = 1; |
| 3478 | } else { /* LYS_CONFIG_R */ |
| 3479 | config = 0; |
| 3480 | } |
| 3481 | |
| 3482 | /* check status */ |
| 3483 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3484 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3485 | |
| 3486 | /* mark leaf as unique */ |
| 3487 | (*key)->flags |= LYS_UNIQUE; |
| 3488 | |
| 3489 | /* next unique value in line */ |
| 3490 | keystr = delim; |
| 3491 | } |
| 3492 | /* next unique definition */ |
| 3493 | } |
| 3494 | |
| 3495 | return LY_SUCCESS; |
| 3496 | } |
| 3497 | |
| 3498 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3499 | * @brief Compile parsed list node information. |
| 3500 | * @param[in] ctx Compile context |
| 3501 | * @param[in] node_p Parsed list node. |
| 3502 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3503 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3504 | * is enriched with the list-specific information. |
| 3505 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3506 | */ |
| 3507 | static LY_ERR |
| 3508 | lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3509 | { |
| 3510 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3511 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3512 | struct lysp_node *child_p; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3513 | struct lysc_node_leaf **key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3514 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3515 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3516 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3517 | LY_ERR ret = LY_SUCCESS; |
| 3518 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3519 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3520 | if (list->min) { |
| 3521 | list->flags |= LYS_MAND_TRUE; |
| 3522 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3523 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3524 | |
| 3525 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3526 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done); |
| 3530 | |
| 3531 | /* keys */ |
| 3532 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3533 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3534 | return LY_EVALID; |
| 3535 | } |
| 3536 | |
| 3537 | /* find all the keys (must be direct children) */ |
| 3538 | keystr = list_p->key; |
| 3539 | while (keystr) { |
| 3540 | delim = strpbrk(keystr, " \t\n"); |
| 3541 | if (delim) { |
| 3542 | len = delim - keystr; |
| 3543 | while (isspace(*delim)) { |
| 3544 | ++delim; |
| 3545 | } |
| 3546 | } else { |
| 3547 | len = strlen(keystr); |
| 3548 | } |
| 3549 | |
| 3550 | /* key node must be present */ |
| 3551 | LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM); |
| 3552 | *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 3553 | if (!(*key)) { |
| 3554 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3555 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3556 | return LY_EVALID; |
| 3557 | } |
| 3558 | /* keys must be unique */ |
| 3559 | for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) { |
| 3560 | if (*key == list->keys[u]) { |
| 3561 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3562 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3563 | return LY_EVALID; |
| 3564 | } |
| 3565 | } |
| 3566 | /* key must have the same config flag as the list itself */ |
| 3567 | if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) { |
| 3568 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3569 | return LY_EVALID; |
| 3570 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3571 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3572 | /* YANG 1.0 denies key to be of empty type */ |
| 3573 | if ((*key)->type->basetype == LY_TYPE_EMPTY) { |
| 3574 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3575 | "Key of a list can be of type \"empty\" only in YANG 1.1 modules."); |
| 3576 | return LY_EVALID; |
| 3577 | } |
| 3578 | } else { |
| 3579 | /* when and if-feature are illegal on list keys */ |
| 3580 | if ((*key)->when) { |
| 3581 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3582 | "List's key \"%s\" must not have any \"when\" statement.", (*key)->name); |
| 3583 | return LY_EVALID; |
| 3584 | } |
| 3585 | if ((*key)->iffeatures) { |
| 3586 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3587 | "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name); |
| 3588 | return LY_EVALID; |
| 3589 | } |
| 3590 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3591 | |
| 3592 | /* check status */ |
| 3593 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3594 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3595 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3596 | /* ignore default values of the key */ |
| 3597 | if ((*key)->dflt) { |
| 3598 | lydict_remove(ctx->ctx, (*key)->dflt); |
| 3599 | (*key)->dflt = NULL; |
| 3600 | } |
| 3601 | /* mark leaf as key */ |
| 3602 | (*key)->flags |= LYS_KEY; |
| 3603 | |
| 3604 | /* next key value */ |
| 3605 | keystr = delim; |
| 3606 | } |
| 3607 | |
| 3608 | /* uniques */ |
| 3609 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3610 | 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] | 3611 | } |
| 3612 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3613 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, options, u, lys_compile_action, 0, ret, done); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 3614 | COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, options, u, lys_compile_notif, 0, ret, done); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3615 | |
| 3616 | done: |
| 3617 | return ret; |
| 3618 | } |
| 3619 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3620 | /** |
| 3621 | * @brief Do some checks and set the default choice's case. |
| 3622 | * |
| 3623 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3624 | * |
| 3625 | * @param[in] ctx Compile context. |
| 3626 | * @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, |
| 3627 | * not the reference to the imported module. |
| 3628 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3629 | * @return LY_ERR value. |
| 3630 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3631 | static LY_ERR |
| 3632 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3633 | { |
| 3634 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3635 | const char *prefix = NULL, *name; |
| 3636 | size_t prefix_len = 0; |
| 3637 | |
| 3638 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3639 | name = strchr(dflt, ':'); |
| 3640 | if (name) { |
| 3641 | prefix = dflt; |
| 3642 | prefix_len = name - prefix; |
| 3643 | ++name; |
| 3644 | } else { |
| 3645 | name = dflt; |
| 3646 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3647 | 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] | 3648 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3649 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3650 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3651 | prefix_len, prefix); |
| 3652 | return LY_EVALID; |
| 3653 | } |
| 3654 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3655 | if (!ch->dflt) { |
| 3656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3657 | "Default case \"%s\" not found.", dflt); |
| 3658 | return LY_EVALID; |
| 3659 | } |
| 3660 | /* no mandatory nodes directly under the default case */ |
| 3661 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3662 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3663 | break; |
| 3664 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3665 | if (iter->flags & LYS_MAND_TRUE) { |
| 3666 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3667 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3668 | return LY_EVALID; |
| 3669 | } |
| 3670 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3671 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3672 | return LY_SUCCESS; |
| 3673 | } |
| 3674 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3675 | static LY_ERR |
| 3676 | lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch) |
| 3677 | { |
| 3678 | struct lys_module *mod; |
| 3679 | const char *prefix = NULL, *name; |
| 3680 | size_t prefix_len = 0; |
| 3681 | struct lysc_node_case *cs; |
| 3682 | struct lysc_node *node; |
| 3683 | |
| 3684 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3685 | name = strchr(dflt, ':'); |
| 3686 | if (name) { |
| 3687 | prefix = dflt; |
| 3688 | prefix_len = name - prefix; |
| 3689 | ++name; |
| 3690 | } else { |
| 3691 | name = dflt; |
| 3692 | } |
| 3693 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3694 | if (prefix) { |
| 3695 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3696 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3697 | "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. " |
| 3698 | "The prefix does not match any imported module of the deviation module.", |
| 3699 | devnodeid, dflt); |
| 3700 | return LY_EVALID; |
| 3701 | } |
| 3702 | } else { |
| 3703 | mod = ctx->mod; |
| 3704 | } |
| 3705 | /* get the default case */ |
| 3706 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3707 | if (!cs) { |
| 3708 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3709 | "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.", |
| 3710 | devnodeid, dflt); |
| 3711 | return LY_EVALID; |
| 3712 | } |
| 3713 | |
| 3714 | /* check that there is no mandatory node */ |
| 3715 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3716 | if (node->parent != (struct lysc_node*)cs) { |
| 3717 | break; |
| 3718 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3719 | if (node->flags & LYS_MAND_TRUE) { |
| 3720 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3721 | "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - " |
| 3722 | "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name); |
| 3723 | return LY_EVALID; |
| 3724 | } |
| 3725 | } |
| 3726 | |
| 3727 | /* set the default case in choice */ |
| 3728 | ch->dflt = cs; |
| 3729 | cs->flags |= LYS_SET_DFLT; |
| 3730 | |
| 3731 | return LY_SUCCESS; |
| 3732 | } |
| 3733 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3734 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3735 | * @brief Compile parsed choice node information. |
| 3736 | * @param[in] ctx Compile context |
| 3737 | * @param[in] node_p Parsed choice node. |
| 3738 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3739 | * @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] | 3740 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3741 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3742 | */ |
| 3743 | static LY_ERR |
| 3744 | lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3745 | { |
| 3746 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 3747 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 3748 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3749 | struct lys_module; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3750 | LY_ERR ret = LY_SUCCESS; |
| 3751 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3752 | LY_LIST_FOR(ch_p->child, child_p) { |
| 3753 | if (child_p->nodetype == LYS_CASE) { |
| 3754 | LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3755 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3756 | } |
| 3757 | } else { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3758 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3759 | } |
| 3760 | } |
| 3761 | |
| 3762 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3763 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3764 | 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] | 3765 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3766 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3767 | return ret; |
| 3768 | } |
| 3769 | |
| 3770 | /** |
| 3771 | * @brief Compile parsed anydata or anyxml node information. |
| 3772 | * @param[in] ctx Compile context |
| 3773 | * @param[in] node_p Parsed anydata or anyxml node. |
| 3774 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3775 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3776 | * is enriched with the any-specific information. |
| 3777 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3778 | */ |
| 3779 | static LY_ERR |
| 3780 | lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3781 | { |
| 3782 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 3783 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 3784 | unsigned int u; |
| 3785 | LY_ERR ret = LY_SUCCESS; |
| 3786 | |
| 3787 | COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done); |
| 3788 | |
| 3789 | if (any->flags & LYS_CONFIG_W) { |
| 3790 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
| 3791 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML)); |
| 3792 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3793 | done: |
| 3794 | return ret; |
| 3795 | } |
| 3796 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3797 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3798 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 3799 | * |
| 3800 | * @param[in] ctx Compile context |
| 3801 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 3802 | * the choice itself is expected instead of a specific case node. |
| 3803 | * @param[in] node Schema node to connect into the list. |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3804 | * @param[in] options Compile options to distinguish input/output when placing node into RPC/action. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3805 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 3806 | */ |
| 3807 | static LY_ERR |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3808 | lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node, int options) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3809 | { |
| 3810 | struct lysc_node **children; |
| 3811 | |
| 3812 | if (node->nodetype == LYS_CASE) { |
| 3813 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 3814 | } else { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3815 | children = lysc_node_children_p(parent, options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3816 | } |
| 3817 | if (children) { |
| 3818 | if (!(*children)) { |
| 3819 | /* first child */ |
| 3820 | *children = node; |
| 3821 | } else if (*children != node) { |
| 3822 | /* by the condition in previous branch we cover the choice/case children |
| 3823 | * - the children list is shared by the choice and the the first case, in addition |
| 3824 | * the first child of each case must be referenced from the case node. So the node is |
| 3825 | * actually always already inserted in case it is the first children - so here such |
| 3826 | * a situation actually corresponds to the first branch */ |
| 3827 | /* insert at the end of the parent's children list */ |
| 3828 | (*children)->prev->next = node; |
| 3829 | node->prev = (*children)->prev; |
| 3830 | (*children)->prev = node; |
| 3831 | |
| 3832 | /* check the name uniqueness */ |
| 3833 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 3834 | lysc_node_notifs(parent), node->name, node)) { |
| 3835 | return LY_EEXIST; |
| 3836 | } |
| 3837 | } |
| 3838 | } |
| 3839 | return LY_SUCCESS; |
| 3840 | } |
| 3841 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3842 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3843 | * @brief Get the XPath context node for the given schema node. |
| 3844 | * @param[in] start The schema node where the XPath expression appears. |
| 3845 | * @return The context node to evaluate XPath expression in given schema node. |
| 3846 | * @return NULL in case the context node is the root node. |
| 3847 | */ |
| 3848 | static struct lysc_node * |
| 3849 | lysc_xpath_context(struct lysc_node *start) |
| 3850 | { |
| 3851 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 3852 | start = start->parent); |
| 3853 | return start; |
| 3854 | } |
| 3855 | |
| 3856 | /** |
| 3857 | * @brief Prepare the case structure in choice node for the new data node. |
| 3858 | * |
| 3859 | * 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 |
| 3860 | * created in the choice when the first child was processed. |
| 3861 | * |
| 3862 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3863 | * @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, |
| 3864 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3865 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3866 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 3867 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 3868 | * @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, |
| 3869 | * 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] | 3870 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3871 | static struct lysc_node_case* |
| 3872 | lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child) |
| 3873 | { |
| 3874 | struct lysc_node *iter; |
| 3875 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 3876 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3877 | unsigned int u; |
| 3878 | LY_ERR ret; |
| 3879 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3880 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3881 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3882 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3883 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 3884 | return NULL; \ |
| 3885 | } \ |
| 3886 | } |
| 3887 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3888 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 3889 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3890 | |
| 3891 | /* we have to add an implicit case node into the parent choice */ |
| 3892 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 3893 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 3894 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3895 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3896 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 3897 | /* the case is already present since the child is not its first children */ |
| 3898 | return (struct lysc_node_case*)ch->cases->prev; |
| 3899 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3900 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3901 | |
| 3902 | /* explicit parent case is not present (this is its first child) */ |
| 3903 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 3904 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 3905 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 3906 | cs->sp = node_p; |
| 3907 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3908 | /* 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] | 3909 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 3910 | |
| 3911 | if (node_p->when) { |
| 3912 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
| 3913 | ret = lys_compile_when(ctx, node_p->when, options, when); |
| 3914 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3915 | (*when)->context = lysc_xpath_context(ch->parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 3916 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3917 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3918 | } else { |
| 3919 | LOGINT(ctx->ctx); |
| 3920 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3921 | } |
| 3922 | cs->module = ctx->mod; |
| 3923 | cs->prev = (struct lysc_node*)cs; |
| 3924 | cs->nodetype = LYS_CASE; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3925 | lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs, options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3926 | cs->parent = (struct lysc_node*)ch; |
| 3927 | cs->child = child; |
| 3928 | |
| 3929 | return cs; |
| 3930 | error: |
Radek Krejci | bb9b198 | 2019-04-08 14:24:59 +0200 | [diff] [blame] | 3931 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3932 | return NULL; |
| 3933 | |
| 3934 | #undef UNIQUE_CHECK |
| 3935 | } |
| 3936 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3937 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3938 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3939 | * |
| 3940 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3941 | * @param[in] node Target node where the config is supposed to be changed. |
| 3942 | * @param[in] config_flag Node's config flag to be applied to the @p node. |
| 3943 | * @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] | 3944 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 3945 | * 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] | 3946 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 3947 | * @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] | 3948 | * @return LY_ERR value. |
| 3949 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3950 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3951 | lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag, |
| 3952 | const char *nodeid, int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3953 | { |
| 3954 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3955 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3956 | |
| 3957 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 3958 | /* nothing to do */ |
| 3959 | return LY_SUCCESS; |
| 3960 | } |
| 3961 | |
| 3962 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3963 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3964 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 3965 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3966 | "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.", |
| 3967 | refine_flag ? "refine" : "deviation", nodeid); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3968 | return LY_EVALID; |
| 3969 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3970 | node->flags |= LYS_SET_CONFIG; |
| 3971 | } else { |
| 3972 | if (node->flags & LYS_SET_CONFIG) { |
| 3973 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 3974 | /* setting config flags, but have node with explicit config true */ |
| 3975 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3976 | "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.", |
| 3977 | refine_flag ? "refine" : "deviation", nodeid); |
| 3978 | return LY_EVALID; |
| 3979 | } |
| 3980 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 3981 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 3982 | return LY_SUCCESS; |
| 3983 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3984 | } |
| 3985 | node->flags &= ~LYS_CONFIG_MASK; |
| 3986 | node->flags |= config; |
| 3987 | |
| 3988 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3989 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3990 | 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] | 3991 | } |
| 3992 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3993 | return LY_SUCCESS; |
| 3994 | } |
| 3995 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3996 | /** |
| 3997 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 3998 | * |
| 3999 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4000 | * the flag to such parents from a mandatory children. |
| 4001 | * |
| 4002 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4003 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4004 | * (mandatory children was removed). |
| 4005 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4006 | void |
| 4007 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4008 | { |
| 4009 | struct lysc_node *iter; |
| 4010 | |
| 4011 | if (add) { /* set flag */ |
| 4012 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4013 | parent = parent->parent) { |
| 4014 | parent->flags |= LYS_MAND_TRUE; |
| 4015 | } |
| 4016 | } else { /* unset flag */ |
| 4017 | 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] | 4018 | 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] | 4019 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4020 | /* there is another mandatory node */ |
| 4021 | return; |
| 4022 | } |
| 4023 | } |
| 4024 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4025 | parent->flags &= ~LYS_MAND_TRUE; |
| 4026 | } |
| 4027 | } |
| 4028 | } |
| 4029 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4030 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4031 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4032 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4033 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4034 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4035 | */ |
| 4036 | static void |
| 4037 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4038 | { |
| 4039 | unsigned int v; |
| 4040 | size_t len; |
| 4041 | |
| 4042 | len = strlen(aug_p->nodeid); |
| 4043 | LY_ARRAY_FOR(result, v) { |
| 4044 | if (strlen(result[v]->nodeid) <= len) { |
| 4045 | continue; |
| 4046 | } |
| 4047 | if (v < LY_ARRAY_SIZE(result)) { |
| 4048 | /* move the rest of array */ |
| 4049 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4050 | break; |
| 4051 | } |
| 4052 | } |
| 4053 | result[v] = aug_p; |
| 4054 | LY_ARRAY_INCREMENT(result); |
| 4055 | } |
| 4056 | |
| 4057 | /** |
| 4058 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4059 | * |
| 4060 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4061 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4062 | * |
| 4063 | * @param[in] ctx Compile context. |
| 4064 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4065 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4066 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4067 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4068 | * @return LY_ERR value. |
| 4069 | */ |
| 4070 | LY_ERR |
| 4071 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4072 | { |
| 4073 | struct lysp_augment **result = NULL; |
| 4074 | unsigned int u, v; |
| 4075 | size_t count = 0; |
| 4076 | |
| 4077 | assert(augments); |
| 4078 | |
| 4079 | /* get count of the augments in module and all its submodules */ |
| 4080 | if (aug_p) { |
| 4081 | count += LY_ARRAY_SIZE(aug_p); |
| 4082 | } |
| 4083 | LY_ARRAY_FOR(inc_p, u) { |
| 4084 | if (inc_p[u].submodule->augments) { |
| 4085 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4086 | } |
| 4087 | } |
| 4088 | |
| 4089 | if (!count) { |
| 4090 | *augments = NULL; |
| 4091 | return LY_SUCCESS; |
| 4092 | } |
| 4093 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4094 | |
| 4095 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4096 | * together, so there can be even /z/y betwwen them. */ |
| 4097 | LY_ARRAY_FOR(aug_p, u) { |
| 4098 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4099 | } |
| 4100 | LY_ARRAY_FOR(inc_p, u) { |
| 4101 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4102 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4103 | } |
| 4104 | } |
| 4105 | |
| 4106 | *augments = result; |
| 4107 | return LY_SUCCESS; |
| 4108 | } |
| 4109 | |
| 4110 | /** |
| 4111 | * @brief Compile the parsed augment connecting it into its target. |
| 4112 | * |
| 4113 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4114 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4115 | * are already implemented and compiled. |
| 4116 | * |
| 4117 | * @param[in] ctx Compile context. |
| 4118 | * @param[in] aug_p Parsed augment to compile. |
| 4119 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 4120 | * @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 |
| 4121 | * children in case of the augmenting uses data. |
| 4122 | * @return LY_SUCCESS on success. |
| 4123 | * @return LY_EVALID on failure. |
| 4124 | */ |
| 4125 | LY_ERR |
| 4126 | lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent) |
| 4127 | { |
| 4128 | LY_ERR ret = LY_SUCCESS; |
| 4129 | struct lysp_node *node_p, *case_node_p; |
| 4130 | struct lysc_node *target; /* target target of the augment */ |
| 4131 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4132 | struct lysc_when **when, *when_shared; |
| 4133 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4134 | uint16_t flags = 0; |
| 4135 | unsigned int u; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4136 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4137 | 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] | 4138 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4139 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4140 | if (ret != LY_SUCCESS) { |
| 4141 | if (ret == LY_EDENIED) { |
| 4142 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4143 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4144 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4145 | } |
| 4146 | return LY_EVALID; |
| 4147 | } |
| 4148 | |
| 4149 | /* check for mandatory nodes |
| 4150 | * - new cases augmenting some choice can have mandatory nodes |
| 4151 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4152 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4153 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4154 | allow_mandatory = 1; |
| 4155 | } |
| 4156 | |
| 4157 | when_shared = NULL; |
| 4158 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4159 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4160 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4161 | && !((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] | 4162 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4163 | && !(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] | 4164 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4165 | "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".", |
| 4166 | aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
| 4167 | return LY_EVALID; |
| 4168 | } |
| 4169 | |
| 4170 | /* compile the children */ |
| 4171 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4172 | LY_CHECK_RET(lys_compile_node(ctx, node_p, options | flags, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4173 | } else { |
| 4174 | LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4175 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options | flags, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4176 | } |
| 4177 | } |
| 4178 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4179 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4180 | * 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] | 4181 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4182 | /* 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] | 4183 | 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] | 4184 | } else if (target->nodetype == LYS_CHOICE) { |
| 4185 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4186 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4187 | } else { |
| 4188 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4189 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4190 | } |
| 4191 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4192 | 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] | 4193 | node->flags &= ~LYS_MAND_TRUE; |
| 4194 | lys_compile_mandatory_parents(target, 0); |
| 4195 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4196 | "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.", |
| 4197 | aug_p->nodeid, node->name); |
| 4198 | return LY_EVALID; |
| 4199 | } |
| 4200 | |
| 4201 | /* pass augment's when to all the children */ |
| 4202 | if (aug_p->when) { |
| 4203 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4204 | if (!when_shared) { |
| 4205 | ret = lys_compile_when(ctx, aug_p->when, options, when); |
| 4206 | LY_CHECK_GOTO(ret, error); |
| 4207 | (*when)->context = lysc_xpath_context(target); |
| 4208 | when_shared = *when; |
| 4209 | } else { |
| 4210 | ++when_shared->refcount; |
| 4211 | (*when) = when_shared; |
| 4212 | } |
| 4213 | } |
| 4214 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4215 | |
| 4216 | switch (target->nodetype) { |
| 4217 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4218 | COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target, |
| 4219 | options | flags, u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4220 | COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target, |
| 4221 | options | flags, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4222 | break; |
| 4223 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4224 | COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target, |
| 4225 | options | flags, u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4226 | COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target, |
| 4227 | options | flags, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4228 | break; |
| 4229 | default: |
| 4230 | if (aug_p->actions) { |
| 4231 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4232 | "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4233 | aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
| 4234 | return LY_EVALID; |
| 4235 | } |
| 4236 | if (aug_p->notifs) { |
| 4237 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4238 | "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".", |
| 4239 | aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
| 4240 | return LY_EVALID; |
| 4241 | } |
| 4242 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4243 | |
| 4244 | error: |
| 4245 | return ret; |
| 4246 | } |
| 4247 | |
| 4248 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4249 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4250 | * |
| 4251 | * @param[in] ctx Compile context. |
| 4252 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4253 | * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node. |
| 4254 | * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging). |
| 4255 | * @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] | 4256 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4257 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4258 | * 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] | 4259 | * @return LY_ERR value. |
| 4260 | */ |
| 4261 | static LY_ERR |
| 4262 | lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag) |
| 4263 | { |
| 4264 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4265 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4266 | "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.", |
| 4267 | refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype)); |
| 4268 | return LY_EVALID; |
| 4269 | } |
| 4270 | |
| 4271 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4272 | /* check if node has default value */ |
| 4273 | if (node->nodetype & LYS_LEAF) { |
| 4274 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4275 | if (refine_flag) { |
| 4276 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4277 | "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid); |
| 4278 | return LY_EVALID; |
| 4279 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4280 | } else { |
| 4281 | /* remove the default value taken from the leaf's type */ |
| 4282 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt); |
| 4283 | ((struct lysc_node_leaf*)node)->dflt = NULL; |
| 4284 | } |
| 4285 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4286 | if (refine_flag) { |
| 4287 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4288 | "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid); |
| 4289 | return LY_EVALID; |
| 4290 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4291 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4292 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4293 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4294 | "Invalid refine of mandatory in \"%s\" under the default case.", nodeid); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4295 | return LY_EVALID; |
| 4296 | } |
| 4297 | |
| 4298 | node->flags &= ~LYS_MAND_FALSE; |
| 4299 | node->flags |= LYS_MAND_TRUE; |
| 4300 | lys_compile_mandatory_parents(node->parent, 1); |
| 4301 | } else { |
| 4302 | /* make mandatory false */ |
| 4303 | node->flags &= ~LYS_MAND_TRUE; |
| 4304 | node->flags |= LYS_MAND_FALSE; |
| 4305 | lys_compile_mandatory_parents(node->parent, 0); |
| 4306 | if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) { |
| 4307 | /* get the type's default value if any */ |
| 4308 | DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt); |
| 4309 | } |
| 4310 | } |
| 4311 | return LY_SUCCESS; |
| 4312 | } |
| 4313 | |
| 4314 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4315 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4316 | * If present, also apply uses's modificators. |
| 4317 | * |
| 4318 | * @param[in] ctx Compile context |
| 4319 | * @param[in] uses_p Parsed uses schema node. |
| 4320 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 4321 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4322 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4323 | * the compile context. |
| 4324 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4325 | */ |
| 4326 | static LY_ERR |
| 4327 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent) |
| 4328 | { |
| 4329 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4330 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4331 | /* 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] | 4332 | struct lysc_node_container context_node_fake = |
| 4333 | {.nodetype = LYS_CONTAINER, |
| 4334 | .module = ctx->mod, |
| 4335 | .flags = parent ? parent->flags : 0, |
| 4336 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4337 | .prev = (struct lysc_node*)&context_node_fake, |
| 4338 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4339 | const struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4340 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4341 | int found; |
| 4342 | const char *id, *name, *prefix; |
| 4343 | size_t prefix_len, name_len; |
| 4344 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4345 | struct lysp_refine *rfn; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4346 | LY_ERR ret = LY_EVALID; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4347 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4348 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4349 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4350 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4351 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4352 | unsigned int actions_index, notifs_index; |
| 4353 | struct lysc_notif **notifs = NULL; |
| 4354 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4355 | |
| 4356 | /* search for the grouping definition */ |
| 4357 | found = 0; |
| 4358 | id = uses_p->name; |
Radek Krejci | 15e99fc | 2019-04-08 14:29:39 +0200 | [diff] [blame] | 4359 | 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] | 4360 | if (prefix) { |
| 4361 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4362 | if (!mod) { |
| 4363 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4364 | "Invalid prefix used for grouping reference (%s).", uses_p->name); |
| 4365 | return LY_EVALID; |
| 4366 | } |
| 4367 | } else { |
| 4368 | mod = ctx->mod_def; |
| 4369 | } |
| 4370 | if (mod == ctx->mod_def) { |
| 4371 | for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) { |
| 4372 | grp = lysp_node_groupings(node_p); |
| 4373 | LY_ARRAY_FOR(grp, u) { |
| 4374 | if (!strcmp(grp[u].name, name)) { |
| 4375 | grp = &grp[u]; |
| 4376 | found = 1; |
| 4377 | break; |
| 4378 | } |
| 4379 | } |
| 4380 | } |
| 4381 | } |
| 4382 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4383 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4384 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4385 | if (grp) { |
| 4386 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4387 | if (!strcmp(grp[u].name, name)) { |
| 4388 | grp = &grp[u]; |
| 4389 | found = 1; |
| 4390 | } |
| 4391 | } |
| 4392 | } |
| 4393 | if (!found && mod->parsed->includes) { |
| 4394 | /* ... and all the submodules */ |
| 4395 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4396 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4397 | if (grp) { |
| 4398 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4399 | if (!strcmp(grp[v].name, name)) { |
| 4400 | grp = &grp[v]; |
| 4401 | found = 1; |
| 4402 | } |
| 4403 | } |
| 4404 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4405 | } |
| 4406 | } |
| 4407 | } |
| 4408 | if (!found) { |
| 4409 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4410 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4411 | return LY_EVALID; |
| 4412 | } |
| 4413 | |
| 4414 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4415 | grp_stack_count = ctx->groupings.count; |
| 4416 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4417 | if (grp_stack_count == ctx->groupings.count) { |
| 4418 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4419 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4420 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4421 | return LY_EVALID; |
| 4422 | } |
| 4423 | |
| 4424 | /* switch context's mod_def */ |
| 4425 | mod_old = ctx->mod_def; |
| 4426 | ctx->mod_def = mod; |
| 4427 | |
| 4428 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4429 | 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] | 4430 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4431 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4432 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4433 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4434 | LY_CHECK_GOTO(lys_compile_node(ctx, node_p, options, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4435 | child = parent ? lysc_node_children(parent, options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4436 | |
| 4437 | /* some preparation for applying refines */ |
| 4438 | if (grp->data == node_p) { |
| 4439 | /* remember the first child */ |
| 4440 | context_node_fake.child = child; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4441 | } |
| 4442 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4443 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4444 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4445 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4446 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4447 | /* 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] | 4448 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4449 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4450 | if (!when_shared) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4451 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, options, when), cleanup); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4452 | (*when)->context = lysc_xpath_context(parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4453 | when_shared = *when; |
| 4454 | } else { |
| 4455 | ++when_shared->refcount; |
| 4456 | (*when) = when_shared; |
| 4457 | } |
| 4458 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4459 | } |
| 4460 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4461 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4462 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4463 | /* fix child link of our fake container to point to the first child of the original list */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4464 | context_node_fake.child->prev = parent ? lysc_node_children(parent, options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4465 | } |
| 4466 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4467 | /* compile actions */ |
| 4468 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4469 | if (actions) { |
| 4470 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
| 4471 | COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, options, u, lys_compile_action, 0, ret, cleanup); |
| 4472 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4473 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4474 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4475 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4476 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4477 | } |
| 4478 | } |
| 4479 | |
| 4480 | /* compile notifications */ |
| 4481 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4482 | if (notifs) { |
| 4483 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
| 4484 | COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, options, u, lys_compile_notif, 0, ret, cleanup); |
| 4485 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4486 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4487 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4488 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4489 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4494 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4495 | 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] | 4496 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4497 | LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], options, (struct lysc_node*)&context_node_fake), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4498 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4499 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4500 | /* reload previous context's mod_def */ |
| 4501 | ctx->mod_def = mod_old; |
| 4502 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4503 | /* apply refine */ |
| 4504 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4505 | 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] | 4506 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4507 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4508 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4509 | |
| 4510 | /* default value */ |
| 4511 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4512 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4513 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4514 | "Invalid refine of default in \"%s\" - %s cannot hold %d default values.", |
| 4515 | rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4516 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4517 | } |
| 4518 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4519 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4520 | "Invalid refine of default in \"%s\" - %s cannot hold default value(s).", |
| 4521 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4522 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4523 | } |
| 4524 | if (node->nodetype == LYS_LEAF) { |
| 4525 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt); |
| 4526 | 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] | 4527 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4528 | /* TODO check the default value according to type */ |
| 4529 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4530 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4531 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4532 | "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^] | 4533 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4534 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4535 | LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) { |
| 4536 | lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]); |
| 4537 | } |
| 4538 | LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4539 | ((struct lysc_node_leaflist*)node)->dflts = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4540 | 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] | 4541 | LY_ARRAY_FOR(rfn->dflts, u) { |
| 4542 | LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts); |
| 4543 | DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]); |
| 4544 | } |
| 4545 | /* TODO check the default values according to type */ |
| 4546 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4547 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4548 | /* unset LYS_SET_DFLT from the current default case */ |
| 4549 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4550 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4551 | 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] | 4552 | } |
| 4553 | } |
| 4554 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4555 | /* description */ |
| 4556 | if (rfn->dsc) { |
| 4557 | FREE_STRING(ctx->ctx, node->dsc); |
| 4558 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4559 | } |
| 4560 | |
| 4561 | /* reference */ |
| 4562 | if (rfn->ref) { |
| 4563 | FREE_STRING(ctx->ctx, node->ref); |
| 4564 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4565 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4566 | |
| 4567 | /* config */ |
| 4568 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4569 | if (!flags) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4570 | 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] | 4571 | } else { |
| 4572 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 4573 | flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path); |
| 4574 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | /* mandatory */ |
| 4578 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4579 | 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] | 4580 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4581 | |
| 4582 | /* presence */ |
| 4583 | if (rfn->presence) { |
| 4584 | if (node->nodetype != LYS_CONTAINER) { |
| 4585 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4586 | "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.", |
| 4587 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4588 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4589 | } |
| 4590 | node->flags |= LYS_PRESENCE; |
| 4591 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4592 | |
| 4593 | /* must */ |
| 4594 | if (rfn->musts) { |
| 4595 | switch (node->nodetype) { |
| 4596 | case LYS_LEAF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4597 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, options, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4598 | break; |
| 4599 | case LYS_LEAFLIST: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4600 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, options, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4601 | break; |
| 4602 | case LYS_LIST: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4603 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, options, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4604 | break; |
| 4605 | case LYS_CONTAINER: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4606 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, options, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4607 | break; |
| 4608 | case LYS_ANYXML: |
| 4609 | case LYS_ANYDATA: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4610 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, options, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4611 | break; |
| 4612 | default: |
| 4613 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4614 | "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.", |
| 4615 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4616 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4617 | } |
| 4618 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4619 | |
| 4620 | /* min/max-elements */ |
| 4621 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4622 | switch (node->nodetype) { |
| 4623 | case LYS_LEAFLIST: |
| 4624 | if (rfn->flags & LYS_SET_MAX) { |
| 4625 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4626 | } |
| 4627 | if (rfn->flags & LYS_SET_MIN) { |
| 4628 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4629 | if (rfn->min) { |
| 4630 | node->flags |= LYS_MAND_TRUE; |
| 4631 | lys_compile_mandatory_parents(node->parent, 1); |
| 4632 | } else { |
| 4633 | node->flags &= ~LYS_MAND_TRUE; |
| 4634 | lys_compile_mandatory_parents(node->parent, 0); |
| 4635 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4636 | } |
| 4637 | break; |
| 4638 | case LYS_LIST: |
| 4639 | if (rfn->flags & LYS_SET_MAX) { |
| 4640 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4641 | } |
| 4642 | if (rfn->flags & LYS_SET_MIN) { |
| 4643 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4644 | if (rfn->min) { |
| 4645 | node->flags |= LYS_MAND_TRUE; |
| 4646 | lys_compile_mandatory_parents(node->parent, 1); |
| 4647 | } else { |
| 4648 | node->flags &= ~LYS_MAND_TRUE; |
| 4649 | lys_compile_mandatory_parents(node->parent, 0); |
| 4650 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4651 | } |
| 4652 | break; |
| 4653 | default: |
| 4654 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4655 | "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.", |
| 4656 | (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^] | 4657 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4658 | } |
| 4659 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4660 | |
| 4661 | /* if-feature */ |
| 4662 | if (rfn->iffeatures) { |
| 4663 | /* any node in compiled tree can get additional if-feature, so do not check nodetype */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4664 | COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4665 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4666 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4667 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4668 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 4669 | for (u = 0; u < refined.count; ++u) { |
| 4670 | node = (struct lysc_node*)refined.objs[u]; |
| 4671 | rfn = &uses_p->refines[u]; |
| 4672 | |
| 4673 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 4674 | if ((node->flags & LYS_MAND_TRUE) && |
| 4675 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 4676 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 4677 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4678 | "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4679 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4683 | if (node->nodetype == LYS_LIST) { |
| 4684 | min = ((struct lysc_node_list*)node)->min; |
| 4685 | max = ((struct lysc_node_list*)node)->max; |
| 4686 | } else { |
| 4687 | min = ((struct lysc_node_leaflist*)node)->min; |
| 4688 | max = ((struct lysc_node_leaflist*)node)->max; |
| 4689 | } |
| 4690 | if (min > max) { |
| 4691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4692 | "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".", |
| 4693 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4694 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4695 | } |
| 4696 | } |
| 4697 | } |
| 4698 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4699 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4700 | |
| 4701 | cleanup: |
| 4702 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 4703 | if (context_node_fake.child) { |
| 4704 | context_node_fake.child->prev = child; |
| 4705 | } |
| 4706 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4707 | child->parent = parent; |
| 4708 | } |
| 4709 | |
| 4710 | if (uses_p->augments || uses_p->refines) { |
| 4711 | /* return back actions and notifications in case they were separated for augment/refine processing */ |
| 4712 | if (actions && *actions) { |
| 4713 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4714 | LY_ARRAY_FREE(context_node_fake.actions); |
| 4715 | } |
| 4716 | if (notifs && *notifs) { |
| 4717 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4718 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 4719 | } |
| 4720 | } |
| 4721 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4722 | /* reload previous context's mod_def */ |
| 4723 | ctx->mod_def = mod_old; |
| 4724 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 4725 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 4726 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4727 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4728 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4729 | |
| 4730 | return ret; |
| 4731 | } |
| 4732 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4733 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4734 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4735 | * @brief Compile parsed schema node information. |
| 4736 | * @param[in] ctx Compile context |
| 4737 | * @param[in] node_p Parsed schema node. |
| 4738 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 4739 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 4740 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4741 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4742 | * @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). |
| 4743 | * 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] | 4744 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4745 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4746 | static LY_ERR |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4747 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent, uint16_t uses_status) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4748 | { |
| 4749 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4750 | struct lysc_node *node; |
| 4751 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4752 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4753 | unsigned int u; |
| 4754 | LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*); |
| 4755 | |
| 4756 | switch (node_p->nodetype) { |
| 4757 | case LYS_CONTAINER: |
| 4758 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 4759 | node_compile_spec = lys_compile_node_container; |
| 4760 | break; |
| 4761 | case LYS_LEAF: |
| 4762 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 4763 | node_compile_spec = lys_compile_node_leaf; |
| 4764 | break; |
| 4765 | case LYS_LIST: |
| 4766 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4767 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4768 | break; |
| 4769 | case LYS_LEAFLIST: |
| 4770 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 4771 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4772 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4773 | case LYS_CHOICE: |
| 4774 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4775 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4776 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4777 | case LYS_ANYXML: |
| 4778 | case LYS_ANYDATA: |
| 4779 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4780 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4781 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4782 | case LYS_USES: |
| 4783 | return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4784 | default: |
| 4785 | LOGINT(ctx->ctx); |
| 4786 | return LY_EINT; |
| 4787 | } |
| 4788 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 4789 | node->nodetype = node_p->nodetype; |
| 4790 | node->module = ctx->mod; |
| 4791 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 4792 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4793 | |
| 4794 | /* config */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4795 | if (options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
| 4796 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4797 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4798 | node->flags |= (options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 4799 | } else if (options & LYSC_OPT_NOTIFICATION) { |
| 4800 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4801 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4802 | node->flags |= LYS_CONFIG_R; |
| 4803 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4804 | /* config not explicitely set, inherit it from parent */ |
| 4805 | if (parent) { |
| 4806 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 4807 | } else { |
| 4808 | /* default is config true */ |
| 4809 | node->flags |= LYS_CONFIG_W; |
| 4810 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4811 | } else { |
| 4812 | /* config set explicitely */ |
| 4813 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4814 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4815 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4816 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4817 | "Configuration node cannot be child of any state data node."); |
| 4818 | goto error; |
| 4819 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4820 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 4821 | /* *list ordering */ |
| 4822 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 4823 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 4824 | LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).", |
| 4825 | (options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 4826 | (options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 4827 | node->flags &= ~LYS_ORDBY_MASK; |
| 4828 | node->flags |= LYS_ORDBY_SYSTEM; |
| 4829 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 4830 | /* default ordering is system */ |
| 4831 | node->flags |= LYS_ORDBY_SYSTEM; |
| 4832 | } |
| 4833 | } |
| 4834 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4835 | /* status - it is not inherited by specification, but it does not make sense to have |
| 4836 | * 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] | 4837 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 4838 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 4839 | * 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] | 4840 | 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] | 4841 | } |
| 4842 | |
| 4843 | if (!(options & LYSC_OPT_FREE_SP)) { |
| 4844 | node->sp = node_p; |
| 4845 | } |
| 4846 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4847 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 4848 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4849 | if (node_p->when) { |
| 4850 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4851 | ret = lys_compile_when(ctx, node_p->when, options, when); |
| 4852 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4853 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4854 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4855 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4856 | COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error); |
| 4857 | |
| 4858 | /* nodetype-specific part */ |
| 4859 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error); |
| 4860 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4861 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 4862 | if (node->flags & LYS_MAND_TRUE) { |
| 4863 | lys_compile_mandatory_parents(parent, 1); |
| 4864 | } |
| 4865 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4866 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4867 | if (parent) { |
| 4868 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4869 | cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node); |
| 4870 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4871 | if (uses_status) { |
| 4872 | |
| 4873 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4874 | /* 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] | 4875 | * it directly gets the same status flags as the choice; |
| 4876 | * 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] | 4877 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4878 | node->parent = (struct lysc_node*)cs; |
| 4879 | } else { /* other than choice */ |
| 4880 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4881 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4882 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node, options), LY_EVALID); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4883 | } else { |
| 4884 | /* top-level element */ |
| 4885 | if (!ctx->mod->compiled->data) { |
| 4886 | ctx->mod->compiled->data = node; |
| 4887 | } else { |
| 4888 | /* insert at the end of the module's top-level nodes list */ |
| 4889 | ctx->mod->compiled->data->prev->next = node; |
| 4890 | node->prev = ctx->mod->compiled->data->prev; |
| 4891 | ctx->mod->compiled->data->prev = node; |
| 4892 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4893 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 4894 | ctx->mod->compiled->notifs, node->name, node)) { |
| 4895 | return LY_EVALID; |
| 4896 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | return LY_SUCCESS; |
| 4900 | |
| 4901 | error: |
| 4902 | lysc_node_free(ctx->ctx, node); |
| 4903 | return ret; |
| 4904 | } |
| 4905 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4906 | static void |
| 4907 | lysc_disconnect(struct lysc_node *node) |
| 4908 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4909 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 4910 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4911 | int remove_cs = 0; |
| 4912 | |
| 4913 | parent = node->parent; |
| 4914 | |
| 4915 | /* parent's first child */ |
| 4916 | if (!parent) { |
| 4917 | return; |
| 4918 | } |
| 4919 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4920 | cs = (struct lysc_node_case*)node; |
| 4921 | } else if (parent->nodetype == LYS_CASE) { |
| 4922 | /* disconnecting some node in a case */ |
| 4923 | cs = (struct lysc_node_case*)parent; |
| 4924 | parent = cs->parent; |
| 4925 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 4926 | if (child == node) { |
| 4927 | if (cs->child == child) { |
| 4928 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 4929 | /* case with a single child -> remove also the case */ |
| 4930 | child->parent = NULL; |
| 4931 | remove_cs = 1; |
| 4932 | } else { |
| 4933 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4934 | } |
| 4935 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4936 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4937 | } |
| 4938 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4939 | if (!remove_cs) { |
| 4940 | cs = NULL; |
| 4941 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4942 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 4943 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | if (cs) { |
| 4947 | if (remove_cs) { |
| 4948 | /* cs has only one child which is being also removed */ |
| 4949 | lysc_disconnect((struct lysc_node*)cs); |
| 4950 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 4951 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4952 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 4953 | /* default case removed */ |
| 4954 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 4955 | } |
| 4956 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 4957 | /* first case removed */ |
| 4958 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 4959 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4960 | if (cs->child) { |
| 4961 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 4962 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 4963 | prev = cs->child->prev; |
| 4964 | } /* else all the children are under a single case */ |
| 4965 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 4966 | if (child->parent != (struct lysc_node*)cs) { |
| 4967 | break; |
| 4968 | } |
| 4969 | lysc_node_free(node->module->ctx, child); |
| 4970 | } |
| 4971 | if (prev) { |
| 4972 | if (prev->next) { |
| 4973 | prev->next = child; |
| 4974 | } |
| 4975 | if (child) { |
| 4976 | child->prev = prev; |
| 4977 | } else { |
| 4978 | /* link from the first child under the cases */ |
| 4979 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 4980 | } |
| 4981 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4982 | } |
| 4983 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4987 | if (node->prev->next) { |
| 4988 | node->prev->next = node->next; |
| 4989 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4990 | if (node->next) { |
| 4991 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4992 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4993 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4994 | if (child) { |
| 4995 | child->prev = node->prev; |
| 4996 | } |
| 4997 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 4998 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4999 | } |
| 5000 | } |
| 5001 | |
| 5002 | LY_ERR |
| 5003 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p, int options) |
| 5004 | { |
| 5005 | LY_ERR ret = LY_EVALID; |
| 5006 | struct ly_set devs_p = {0}; |
| 5007 | struct ly_set targets = {0}; |
| 5008 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5009 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 5010 | struct lysc_action *rpcs; |
| 5011 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5012 | struct lysp_deviation *dev; |
| 5013 | struct lysp_deviate *d, **dp_new; |
| 5014 | struct lysp_deviate_add *d_add; |
| 5015 | struct lysp_deviate_del *d_del; |
| 5016 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5017 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5018 | struct lysc_deviation { |
| 5019 | const char *nodeid; |
| 5020 | struct lysc_node *target; /* target node of the deviation */ |
| 5021 | 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] | 5022 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5023 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5024 | } **devs = NULL; |
| 5025 | int i; |
| 5026 | size_t prefix_len, name_len; |
| 5027 | const char *prefix, *name, *nodeid; |
| 5028 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5029 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5030 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5031 | |
| 5032 | /* get all deviations from the module and all its submodules ... */ |
| 5033 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5034 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5035 | } |
| 5036 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5037 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5038 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5039 | } |
| 5040 | } |
| 5041 | if (!devs_p.count) { |
| 5042 | /* nothing to do */ |
| 5043 | return LY_SUCCESS; |
| 5044 | } |
| 5045 | |
| 5046 | /* ... and group them by the target node */ |
| 5047 | devs = calloc(devs_p.count, sizeof *devs); |
| 5048 | for (u = 0; u < devs_p.count; ++u) { |
| 5049 | dev = devs_p.objs[u]; |
| 5050 | |
| 5051 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5052 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5053 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5054 | if (target->nodetype == LYS_ACTION) { |
| 5055 | /* move the target pointer to input/output to make them different from the action and |
| 5056 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5057 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5058 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5059 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5060 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5061 | flags |= LYSC_OPT_INTERNAL; |
| 5062 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5063 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5064 | flags |= LYSC_OPT_INTERNAL; |
| 5065 | } |
| 5066 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5067 | /* insert into the set of targets with duplicity detection */ |
| 5068 | i = ly_set_add(&targets, target, 0); |
| 5069 | if (!devs[i]) { |
| 5070 | /* new record */ |
| 5071 | devs[i] = calloc(1, sizeof **devs); |
| 5072 | devs[i]->target = target; |
| 5073 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5074 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5075 | } |
| 5076 | /* add deviates into the deviation's list of deviates */ |
| 5077 | for (d = dev->deviates; d; d = d->next) { |
| 5078 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5079 | *dp_new = d; |
| 5080 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5081 | devs[i]->not_supported = 1; |
| 5082 | } |
| 5083 | } |
| 5084 | } |
| 5085 | |
| 5086 | /* MACROS for deviates checking */ |
| 5087 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5088 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
| 5089 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\ |
| 5090 | goto cleanup; \ |
| 5091 | } |
| 5092 | |
| 5093 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5094 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
| 5095 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \ |
| 5096 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
| 5097 | goto cleanup; \ |
| 5098 | } |
| 5099 | |
| 5100 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5101 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5102 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5103 | "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5104 | devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5105 | goto cleanup; \ |
| 5106 | } |
| 5107 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5108 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5109 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5110 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5111 | "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5112 | devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
| 5113 | goto cleanup; \ |
| 5114 | } |
| 5115 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5116 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5117 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
| 5118 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \ |
| 5119 | goto cleanup; \ |
| 5120 | } |
| 5121 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5122 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5123 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5124 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5125 | "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \ |
| 5126 | devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \ |
| 5127 | goto cleanup; \ |
| 5128 | } |
| 5129 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5130 | #define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \ |
| 5131 | DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \ |
| 5132 | if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \ |
| 5133 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5134 | "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \ |
| 5135 | devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \ |
| 5136 | goto cleanup; \ |
| 5137 | } \ |
| 5138 | DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \ |
| 5139 | ((TYPE)devs[u]->target)->MEMBER_TRG = NULL; |
| 5140 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5141 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5142 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5143 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5144 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5145 | 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] | 5146 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5147 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5148 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5149 | "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] | 5150 | devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5151 | goto cleanup; \ |
| 5152 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5153 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5154 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5155 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5156 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5157 | (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] | 5158 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5159 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5160 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5161 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | /* apply deviations */ |
| 5165 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5166 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5167 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5168 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5169 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5170 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5171 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5172 | } |
| 5173 | } |
| 5174 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5175 | /* not-supported */ |
| 5176 | if (devs[u]->not_supported) { |
| 5177 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 5178 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 5179 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5180 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 5181 | |
| 5182 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5183 | if (devs[u]->target->parent) { \ |
| 5184 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5185 | } else { \ |
| 5186 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5187 | } \ |
| 5188 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5189 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5190 | } \ |
| 5191 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5192 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5193 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5194 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5195 | } |
| 5196 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5197 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 5198 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5199 | /* remove RPC's/action's input */ |
| 5200 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5201 | 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^] | 5202 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5203 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5204 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5205 | /* remove RPC's/action's output */ |
| 5206 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5207 | 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^] | 5208 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5209 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5210 | } else { |
| 5211 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 5212 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5213 | } |
| 5214 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 5215 | /* remove Notification */ |
| 5216 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5217 | } else { |
| 5218 | /* remove the target node */ |
| 5219 | lysc_disconnect(devs[u]->target); |
| 5220 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5221 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5222 | |
| 5223 | continue; |
| 5224 | } |
| 5225 | |
| 5226 | /* list of deviates (not-supported is not present in the list) */ |
| 5227 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5228 | d = devs[u]->deviates[v]; |
| 5229 | |
| 5230 | switch (d->mod) { |
| 5231 | case LYS_DEV_ADD: |
| 5232 | d_add = (struct lysp_deviate_add*)d; |
| 5233 | /* [units-stmt] */ |
| 5234 | if (d_add->units) { |
| 5235 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5236 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5237 | |
| 5238 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5239 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5240 | } |
| 5241 | |
| 5242 | /* *must-stmt */ |
| 5243 | if (d_add->musts) { |
| 5244 | switch (devs[u]->target->nodetype) { |
| 5245 | case LYS_CONTAINER: |
| 5246 | case LYS_LIST: |
| 5247 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 5248 | options, x, lys_compile_must, ret, cleanup); |
| 5249 | break; |
| 5250 | case LYS_LEAF: |
| 5251 | case LYS_LEAFLIST: |
| 5252 | case LYS_ANYDATA: |
| 5253 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 5254 | options, x, lys_compile_must, ret, cleanup); |
| 5255 | break; |
| 5256 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 5257 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 5258 | options, x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5259 | break; |
| 5260 | case LYS_ACTION: |
| 5261 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5262 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 5263 | options, x, lys_compile_must, ret, cleanup); |
| 5264 | break; |
| 5265 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5266 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 5267 | options, x, lys_compile_must, ret, cleanup); |
| 5268 | break; |
| 5269 | } |
| 5270 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5271 | default: |
| 5272 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5273 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5274 | goto cleanup; |
| 5275 | } |
| 5276 | } |
| 5277 | |
| 5278 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5279 | if (d_add->uniques) { |
| 5280 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5281 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5282 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5283 | |
| 5284 | /* *default-stmt */ |
| 5285 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5286 | switch (devs[u]->target->nodetype) { |
| 5287 | case LYS_LEAF: |
| 5288 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5289 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt); |
| 5290 | if (((struct lysc_node_leaf*)devs[u]->target)->dflt) { |
| 5291 | /* first, remove the default value taken from the type */ |
| 5292 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 5293 | ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL; |
| 5294 | } |
| 5295 | 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] | 5296 | /* mark the new default values as leaf's own */ |
| 5297 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5298 | break; |
| 5299 | case LYS_LEAFLIST: |
| 5300 | if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 5301 | /* first, remove the default value taken from the type */ |
| 5302 | LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) { |
| 5303 | lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]); |
| 5304 | } |
| 5305 | LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 5306 | ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL; |
| 5307 | } |
| 5308 | /* add new default value(s) */ |
| 5309 | LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts, |
| 5310 | LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 5311 | for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 5312 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
| 5313 | DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]); |
| 5314 | LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 5315 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5316 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5317 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5318 | break; |
| 5319 | case LYS_CHOICE: |
| 5320 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5321 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5322 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5323 | * to allow making the default case even the augmented case from the deviating module */ |
| 5324 | if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0], |
| 5325 | (struct lysc_node_choice*)devs[u]->target)) { |
| 5326 | goto cleanup; |
| 5327 | } |
| 5328 | break; |
| 5329 | default: |
| 5330 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5331 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5332 | goto cleanup; |
| 5333 | } |
| 5334 | } |
| 5335 | |
| 5336 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5337 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 5338 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) { |
| 5339 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5340 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5341 | goto cleanup; |
| 5342 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5343 | if (devs[u]->flags) { |
| 5344 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).", |
| 5345 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid); |
| 5346 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5347 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5348 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5349 | "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").", |
| 5350 | devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5351 | goto cleanup; |
| 5352 | } |
| 5353 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup); |
| 5354 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5355 | |
| 5356 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5357 | if (d_add->flags & LYS_MAND_MASK) { |
| 5358 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5359 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5360 | "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5361 | devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5362 | goto cleanup; |
| 5363 | } |
| 5364 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup); |
| 5365 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5366 | |
| 5367 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5368 | if (d_add->flags & LYS_SET_MIN) { |
| 5369 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5370 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5371 | /* change value */ |
| 5372 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5373 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5374 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5375 | /* change value */ |
| 5376 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5377 | } else { |
| 5378 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5379 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5380 | goto cleanup; |
| 5381 | } |
| 5382 | if (d_add->min) { |
| 5383 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5384 | } |
| 5385 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5386 | |
| 5387 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5388 | if (d_add->flags & LYS_SET_MAX) { |
| 5389 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5390 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5391 | /* change value */ |
| 5392 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5393 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5394 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5395 | /* change value */ |
| 5396 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5397 | } else { |
| 5398 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5399 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 5400 | goto cleanup; |
| 5401 | } |
| 5402 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5403 | |
| 5404 | break; |
| 5405 | case LYS_DEV_DELETE: |
| 5406 | d_del = (struct lysp_deviate_del*)d; |
| 5407 | |
| 5408 | /* [units-stmt] */ |
| 5409 | if (d_del->units) { |
| 5410 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
| 5411 | DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units"); |
| 5412 | } |
| 5413 | |
| 5414 | /* *must-stmt */ |
| 5415 | if (d_del->musts) { |
| 5416 | switch (devs[u]->target->nodetype) { |
| 5417 | case LYS_CONTAINER: |
| 5418 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5419 | 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] | 5420 | break; |
| 5421 | case LYS_LEAF: |
| 5422 | case LYS_LEAFLIST: |
| 5423 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5424 | 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] | 5425 | break; |
| 5426 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 5427 | 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] | 5428 | break; |
| 5429 | case LYS_ACTION: |
| 5430 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5431 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5432 | break; |
| 5433 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5434 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5435 | break; |
| 5436 | } |
| 5437 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5438 | default: |
| 5439 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5440 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5441 | goto cleanup; |
| 5442 | } |
| 5443 | } |
| 5444 | |
| 5445 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5446 | if (d_del->uniques) { |
| 5447 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5448 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 5449 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 5450 | LY_ARRAY_FOR(list->uniques, z) { |
| 5451 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 5452 | nodeid = strpbrk(name, " \t\n"); |
| 5453 | if (nodeid) { |
| 5454 | if (strncmp(name, list->uniques[z][y]->name, nodeid - name) |
| 5455 | || list->uniques[z][y]->name[nodeid - name] != '\0') { |
| 5456 | break; |
| 5457 | } |
| 5458 | while (isspace(*nodeid)) { |
| 5459 | ++nodeid; |
| 5460 | } |
| 5461 | } else { |
| 5462 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 5463 | break; |
| 5464 | } |
| 5465 | } |
| 5466 | } |
| 5467 | if (!name) { |
| 5468 | /* complete match - remove the unique */ |
| 5469 | LY_ARRAY_DECREMENT(list->uniques); |
| 5470 | LY_ARRAY_FREE(list->uniques[z]); |
| 5471 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 5472 | --z; |
| 5473 | break; |
| 5474 | } |
| 5475 | } |
| 5476 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 5477 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5478 | "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 5479 | devs[u]->nodeid, d_del->uniques[x]); |
| 5480 | goto cleanup; |
| 5481 | } |
| 5482 | } |
| 5483 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 5484 | LY_ARRAY_FREE(list->uniques); |
| 5485 | list->uniques = NULL; |
| 5486 | } |
| 5487 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5488 | |
| 5489 | /* *default-stmt */ |
| 5490 | if (d_del->dflts) { |
| 5491 | switch (devs[u]->target->nodetype) { |
| 5492 | case LYS_LEAF: |
| 5493 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 5494 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 5495 | dflt, "deleting", "default", d_del->dflts[0]); |
| 5496 | |
| 5497 | 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] | 5498 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5499 | break; |
| 5500 | case LYS_LEAFLIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5501 | DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default"); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5502 | if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) { |
| 5503 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
| 5504 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5505 | break; |
| 5506 | case LYS_CHOICE: |
| 5507 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 5508 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 5509 | nodeid = d_del->dflts[0]; |
| 5510 | LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 5511 | if (prefix) { |
| 5512 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 5513 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 5514 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
| 5515 | "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. " |
| 5516 | "The prefix does not match any imported module of the deviation module.", |
| 5517 | devs[u]->nodeid, d_del->dflts[0]); |
| 5518 | goto cleanup; |
| 5519 | } |
| 5520 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 5521 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
| 5522 | "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. " |
| 5523 | "The prefix does not match the default case's module.", |
| 5524 | devs[u]->nodeid, d_del->dflts[0]); |
| 5525 | goto cleanup; |
| 5526 | } |
| 5527 | } |
| 5528 | /* else { |
| 5529 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 5530 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 5531 | * the module of the default case, just matching its name */ |
| 5532 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 5533 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5534 | "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 5535 | devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name); |
| 5536 | goto cleanup; |
| 5537 | } |
| 5538 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 5539 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 5540 | break; |
| 5541 | default: |
| 5542 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5543 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5544 | goto cleanup; |
| 5545 | } |
| 5546 | } |
| 5547 | |
| 5548 | break; |
| 5549 | case LYS_DEV_REPLACE: |
| 5550 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 5551 | |
| 5552 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5553 | if (d_rpl->type) { |
| 5554 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 5555 | /* type is mandatory, so checking for its presence is not necessary */ |
| 5556 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type); |
| 5557 | LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, options, (struct lysc_node_leaf*)devs[u]->target), cleanup); |
| 5558 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5559 | |
| 5560 | /* [units-stmt] */ |
| 5561 | if (d_rpl->units) { |
| 5562 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 5563 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 5564 | units, "replacing", "units", d_rpl->units); |
| 5565 | |
| 5566 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5567 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5568 | } |
| 5569 | |
| 5570 | /* [default-stmt] */ |
| 5571 | if (d_rpl->dflt) { |
| 5572 | switch (devs[u]->target->nodetype) { |
| 5573 | case LYS_LEAF: |
| 5574 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 5575 | dflt, "replacing", "default", d_rpl->dflt); |
| 5576 | |
| 5577 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 5578 | DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 5579 | break; |
| 5580 | case LYS_CHOICE: |
| 5581 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt); |
| 5582 | if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt, |
| 5583 | (struct lysc_node_choice*)devs[u]->target)) { |
| 5584 | goto cleanup; |
| 5585 | } |
| 5586 | break; |
| 5587 | default: |
| 5588 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5589 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5590 | goto cleanup; |
| 5591 | } |
| 5592 | } |
| 5593 | |
| 5594 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5595 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 5596 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) { |
| 5597 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5598 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 5599 | goto cleanup; |
| 5600 | } |
| 5601 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
| 5602 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, |
| 5603 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 5604 | goto cleanup; |
| 5605 | } |
| 5606 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup); |
| 5607 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5608 | |
| 5609 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5610 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 5611 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
| 5612 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, |
| 5613 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 5614 | goto cleanup; |
| 5615 | } |
| 5616 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup); |
| 5617 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5618 | |
| 5619 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5620 | if (d_rpl->flags & LYS_SET_MIN) { |
| 5621 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5622 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5623 | /* change value */ |
| 5624 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 5625 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5626 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5627 | /* change value */ |
| 5628 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 5629 | } else { |
| 5630 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5631 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 5632 | goto cleanup; |
| 5633 | } |
| 5634 | if (d_rpl->min) { |
| 5635 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5636 | } |
| 5637 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5638 | |
| 5639 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5640 | if (d_rpl->flags & LYS_SET_MAX) { |
| 5641 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5642 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5643 | /* change value */ |
| 5644 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 5645 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5646 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5647 | /* change value */ |
| 5648 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 5649 | } else { |
| 5650 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5651 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 5652 | goto cleanup; |
| 5653 | } |
| 5654 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5655 | |
| 5656 | break; |
| 5657 | default: |
| 5658 | LOGINT(ctx->ctx); |
| 5659 | goto cleanup; |
| 5660 | } |
| 5661 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5662 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5663 | /* final check when all deviations of a single target node are applied */ |
| 5664 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5665 | /* check min-max compatibility */ |
| 5666 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5667 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 5668 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 5669 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5670 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 5671 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 5672 | } else { |
| 5673 | min = max = 0; |
| 5674 | } |
| 5675 | if (min > max) { |
| 5676 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
| 5677 | "after deviation (%s): min value %u is bigger than max value %u.", |
| 5678 | devs[u]->nodeid, min, max); |
| 5679 | goto cleanup; |
| 5680 | } |
| 5681 | |
| 5682 | /* check mandatory - default compatibility */ |
| 5683 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 5684 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 5685 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 5686 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5687 | "Invalid deviation (%s) combining default value and mandatory %s.", |
| 5688 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype)); |
| 5689 | goto cleanup; |
| 5690 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 5691 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 5692 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 5693 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5694 | "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid); |
| 5695 | goto cleanup; |
| 5696 | } |
| 5697 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 5698 | /* mandatory node under a default case */ |
| 5699 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5700 | "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 5701 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name); |
| 5702 | goto cleanup; |
| 5703 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5704 | |
| 5705 | /* TODO check default value(s) according to the type */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5706 | } |
| 5707 | |
| 5708 | ret = LY_SUCCESS; |
| 5709 | |
| 5710 | cleanup: |
| 5711 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 5712 | LY_ARRAY_FREE(devs[u]->deviates); |
| 5713 | free(devs[u]); |
| 5714 | } |
| 5715 | free(devs); |
| 5716 | ly_set_erase(&targets, NULL); |
| 5717 | ly_set_erase(&devs_p, NULL); |
| 5718 | |
| 5719 | return ret; |
| 5720 | } |
| 5721 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 5722 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5723 | * @brief Compile the given YANG submodule into the main module. |
| 5724 | * @param[in] ctx Compile context |
| 5725 | * @param[in] inc Include structure from the main module defining the submodule. |
| 5726 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 5727 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5728 | */ |
| 5729 | LY_ERR |
| 5730 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options) |
| 5731 | { |
| 5732 | unsigned int u; |
| 5733 | LY_ERR ret = LY_SUCCESS; |
| 5734 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5735 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5736 | struct lysc_module *mainmod = ctx->mod->compiled; |
| 5737 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5738 | if (!mainmod->mod->off_features) { |
| 5739 | /* features are compiled directly into the compiled module structure, |
| 5740 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 5741 | * The features compilation is finished in the main module (lys_compile()). */ |
| 5742 | ret = lys_feature_precompile(ctx->ctx, submod->features, |
| 5743 | mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features); |
| 5744 | LY_CHECK_GOTO(ret, error); |
| 5745 | } |
| 5746 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5747 | COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error); |
| 5748 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 5749 | /* TODO data nodes */ |
| 5750 | |
| 5751 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, options, u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 5752 | COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, options, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 5753 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5754 | error: |
| 5755 | return ret; |
| 5756 | } |
| 5757 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5758 | LY_ERR |
| 5759 | lys_compile(struct lys_module *mod, int options) |
| 5760 | { |
| 5761 | struct lysc_ctx ctx = {0}; |
| 5762 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5763 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5764 | struct lysp_module *sp; |
| 5765 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5766 | struct lysp_augment **augments = NULL; |
| 5767 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 5768 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5769 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5770 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5771 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 5772 | |
| 5773 | if (!mod->implemented) { |
| 5774 | /* just imported modules are not compiled */ |
| 5775 | return LY_SUCCESS; |
| 5776 | } |
| 5777 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5778 | sp = mod->parsed; |
| 5779 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5780 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5781 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5782 | ctx.mod_def = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5783 | |
| 5784 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5785 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 5786 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5787 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5788 | COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5789 | LY_ARRAY_FOR(sp->includes, u) { |
| 5790 | ret = lys_compile_submodule(&ctx, &sp->includes[u], options); |
| 5791 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 5792 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5793 | if (mod->off_features) { |
| 5794 | /* there is already precompiled array of features */ |
| 5795 | mod_c->features = mod->off_features; |
| 5796 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5797 | } else { |
| 5798 | /* features are compiled directly into the compiled module structure, |
| 5799 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
| 5800 | ret = lys_feature_precompile(ctx.ctx, sp->features, &mod_c->features); |
| 5801 | LY_CHECK_GOTO(ret, error); |
| 5802 | } |
| 5803 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 5804 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 5805 | * are present. */ |
| 5806 | LY_ARRAY_FOR(sp->features, u) { |
| 5807 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features); |
| 5808 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 5809 | } |
| 5810 | LY_ARRAY_FOR(sp->includes, v) { |
| 5811 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
| 5812 | ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features); |
| 5813 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 5814 | } |
| 5815 | } |
| 5816 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5817 | COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5818 | if (sp->identities) { |
| 5819 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 5820 | } |
| 5821 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5822 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5823 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5824 | ret = lys_compile_node(&ctx, node_p, options, NULL, 0); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5825 | LY_CHECK_GOTO(ret, error); |
| 5826 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5827 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5828 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, options, u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame^] | 5829 | COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, options, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5830 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5831 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5832 | ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5833 | LY_CHECK_GOTO(ret, error); |
| 5834 | LY_ARRAY_FOR(augments, u) { |
| 5835 | ret = lys_compile_augment(&ctx, augments[u], options, NULL); |
| 5836 | LY_CHECK_GOTO(ret, error); |
| 5837 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5838 | |
| 5839 | /* deviations */ |
| 5840 | ret = lys_compile_deviations(&ctx, sp, options); |
| 5841 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5842 | |
| 5843 | COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error); |
| 5844 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5845 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5846 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 5847 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 5848 | * 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] | 5849 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5850 | 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] | 5851 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 5852 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 5853 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5854 | 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] | 5855 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 5856 | } else if (type->basetype == LY_TYPE_UNION) { |
| 5857 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 5858 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 5859 | /* validate the path */ |
| 5860 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 5861 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 5862 | LY_CHECK_GOTO(ret, error); |
| 5863 | } |
| 5864 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5865 | } |
| 5866 | } |
| 5867 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5868 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5869 | 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] | 5870 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 5871 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 5872 | /* store pointer to the real type */ |
| 5873 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 5874 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 5875 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 5876 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 5877 | } else if (type->basetype == LY_TYPE_UNION) { |
| 5878 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 5879 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 5880 | /* store pointer to the real type */ |
| 5881 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 5882 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 5883 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 5884 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 5885 | } |
| 5886 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5887 | } |
| 5888 | } |
| 5889 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5890 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5891 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5892 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5893 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5894 | if (options & LYSC_OPT_FREE_SP) { |
| 5895 | lysp_module_free(mod->parsed); |
| 5896 | ((struct lys_module*)mod)->parsed = NULL; |
| 5897 | } |
| 5898 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5899 | if (!(options & LYSC_OPT_INTERNAL)) { |
| 5900 | /* remove flag of the modules implemented by dependency */ |
| 5901 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 5902 | m = ctx.ctx->list.objs[u]; |
| 5903 | if (m->implemented == 2) { |
| 5904 | m->implemented = 1; |
| 5905 | } |
| 5906 | } |
| 5907 | } |
| 5908 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5909 | ((struct lys_module*)mod)->compiled = mod_c; |
| 5910 | return LY_SUCCESS; |
| 5911 | |
| 5912 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5913 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5914 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5915 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5916 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5917 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5918 | mod->compiled = NULL; |
| 5919 | |
| 5920 | /* revert compilation of modules implemented by dependency */ |
| 5921 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 5922 | m = ctx.ctx->list.objs[u]; |
| 5923 | if (m->implemented == 2) { |
| 5924 | /* revert features list to the precompiled state */ |
| 5925 | lys_feature_precompile_revert(&ctx, m); |
| 5926 | /* mark module as imported-only / not-implemented */ |
| 5927 | m->implemented = 0; |
| 5928 | /* free the compiled version of the module */ |
| 5929 | lysc_module_free(m->compiled, NULL); |
| 5930 | m->compiled = NULL; |
| 5931 | } |
| 5932 | } |
| 5933 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5934 | return ret; |
| 5935 | } |