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 | |
| 44 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, 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, ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 51 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 52 | } \ |
| 53 | } |
| 54 | |
| 55 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \ |
| 56 | if (MEMBER_P) { \ |
| 57 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 58 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
| 59 | RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \ |
| 60 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 61 | } |
| 62 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 63 | #define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, OPTIONS, FUNC, RET, GOTO) \ |
| 64 | if (MEMBER_P) { \ |
| 65 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
| 66 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 67 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
| 68 | RET = FUNC(CTX, MEMBER_P, OPTIONS, &(ARRAY_C)[__array_offset]); \ |
| 69 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 70 | } |
| 71 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 72 | #define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 73 | if (ARRAY) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 74 | for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
| 75 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 76 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 77 | return LY_EVALID; \ |
| 78 | } \ |
| 79 | } \ |
| 80 | } |
| 81 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 82 | static struct lysc_ext_instance * |
| 83 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 84 | { |
| 85 | /* TODO */ |
| 86 | (void) ctx; |
| 87 | (void) orig; |
| 88 | return NULL; |
| 89 | } |
| 90 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 91 | /** |
| 92 | * @brief Duplicate the compiled pattern structure. |
| 93 | * |
| 94 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 95 | * |
| 96 | * @param[in] orig The pattern structure to duplicate. |
| 97 | * @return The duplicated structure to use. |
| 98 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 99 | static struct lysc_pattern* |
| 100 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 101 | { |
| 102 | ++orig->refcount; |
| 103 | return orig; |
| 104 | } |
| 105 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 106 | /** |
| 107 | * @brief Duplicate the array of compiled patterns. |
| 108 | * |
| 109 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 110 | * |
| 111 | * @param[in] ctx Libyang context for logging. |
| 112 | * @param[in] orig The patterns sized array to duplicate. |
| 113 | * @return New sized array as a copy of @p orig. |
| 114 | * @return NULL in case of memory allocation error. |
| 115 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 116 | static struct lysc_pattern** |
| 117 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 118 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 119 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 120 | unsigned int u; |
| 121 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 122 | assert(orig); |
| 123 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 124 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 125 | LY_ARRAY_FOR(orig, u) { |
| 126 | dup[u] = lysc_pattern_dup(orig[u]); |
| 127 | LY_ARRAY_INCREMENT(dup); |
| 128 | } |
| 129 | return dup; |
| 130 | } |
| 131 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 132 | /** |
| 133 | * @brief Duplicate compiled range structure. |
| 134 | * |
| 135 | * @param[in] ctx Libyang context for logging. |
| 136 | * @param[in] orig The range structure to be duplicated. |
| 137 | * @return New compiled range structure as a copy of @p orig. |
| 138 | * @return NULL in case of memory allocation error. |
| 139 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 140 | struct lysc_range* |
| 141 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 142 | { |
| 143 | struct lysc_range *dup; |
| 144 | LY_ERR ret; |
| 145 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 146 | assert(orig); |
| 147 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 148 | dup = calloc(1, sizeof *dup); |
| 149 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 150 | if (orig->parts) { |
| 151 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 152 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 153 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 154 | } |
| 155 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 156 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 157 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 158 | |
| 159 | return dup; |
| 160 | cleanup: |
| 161 | free(dup); |
| 162 | (void) ret; /* set but not used due to the return type */ |
| 163 | return NULL; |
| 164 | } |
| 165 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 166 | /** |
| 167 | * @brief Stack for processing if-feature expressions. |
| 168 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 169 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 170 | int size; /**< number of items in the stack */ |
| 171 | int index; /**< first empty item */ |
| 172 | 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] | 173 | }; |
| 174 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 175 | /** |
| 176 | * @brief Add @ref ifftokens into the stack. |
| 177 | * @param[in] stack The if-feature stack to use. |
| 178 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 179 | * @return LY_EMEM in case of memory allocation error |
| 180 | * @return LY_ESUCCESS if the value successfully stored. |
| 181 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 182 | static LY_ERR |
| 183 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 184 | { |
| 185 | if (stack->index == stack->size) { |
| 186 | stack->size += 4; |
| 187 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 188 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 189 | } |
| 190 | stack->stack[stack->index++] = value; |
| 191 | return LY_SUCCESS; |
| 192 | } |
| 193 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 194 | /** |
| 195 | * @brief Get (and remove) the last item form the stack. |
| 196 | * @param[in] stack The if-feature stack to use. |
| 197 | * @return The value from the top of the stack. |
| 198 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 199 | static uint8_t |
| 200 | iff_stack_pop(struct iff_stack *stack) |
| 201 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 202 | assert(stack && stack->index); |
| 203 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 204 | stack->index--; |
| 205 | return stack->stack[stack->index]; |
| 206 | } |
| 207 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 208 | /** |
| 209 | * @brief Clean up the stack. |
| 210 | * @param[in] stack The if-feature stack to use. |
| 211 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 212 | static void |
| 213 | iff_stack_clean(struct iff_stack *stack) |
| 214 | { |
| 215 | stack->size = 0; |
| 216 | free(stack->stack); |
| 217 | } |
| 218 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 219 | /** |
| 220 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 221 | * (libyang format of the if-feature expression). |
| 222 | * @param[in,out] list The 2bits array to modify. |
| 223 | * @param[in] op The operand (@ref ifftokens) to store. |
| 224 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 225 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 226 | static void |
| 227 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 228 | { |
| 229 | uint8_t *item; |
| 230 | uint8_t mask = 3; |
| 231 | |
| 232 | assert(pos >= 0); |
| 233 | assert(op <= 3); /* max 2 bits */ |
| 234 | |
| 235 | item = &list[pos / 4]; |
| 236 | mask = mask << 2 * (pos % 4); |
| 237 | *item = (*item) & ~mask; |
| 238 | *item = (*item) | (op << 2 * (pos % 4)); |
| 239 | } |
| 240 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 241 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 242 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 243 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 244 | /** |
| 245 | * @brief Find a feature of the given name and referenced in the given module. |
| 246 | * |
| 247 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 248 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 249 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 250 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 251 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 252 | * assigned till that time will be still valid. |
| 253 | * |
| 254 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 255 | * @param[in] name Name of the feature including possible prefix. |
| 256 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 257 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 258 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 259 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 260 | 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] | 261 | { |
| 262 | size_t i; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 263 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 264 | |
| 265 | for (i = 0; i < len; ++i) { |
| 266 | if (name[i] == ':') { |
| 267 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 268 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 269 | LY_CHECK_RET(!mod, NULL); |
| 270 | |
| 271 | name = &name[i + 1]; |
| 272 | len = len - i - 1; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 277 | if (mod->implemented) { |
| 278 | /* module is implemented so there is already the compiled schema */ |
| 279 | flist = mod->compiled->features; |
| 280 | } else { |
| 281 | flist = mod->off_features; |
| 282 | } |
| 283 | LY_ARRAY_FOR(flist, i) { |
| 284 | f = &flist[i]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 285 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 286 | return f; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | static LY_ERR |
| 294 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext) |
| 295 | { |
| 296 | const char *name; |
| 297 | unsigned int u; |
| 298 | const struct lys_module *mod; |
| 299 | struct lysp_ext *edef = NULL; |
| 300 | |
| 301 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 302 | ext->insubstmt = ext_p->insubstmt; |
| 303 | ext->insubstmt_index = ext_p->insubstmt_index; |
| 304 | |
| 305 | /* get module where the extension definition should be placed */ |
| 306 | for (u = 0; ext_p->name[u] != ':'; ++u); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 307 | mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 308 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 309 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 310 | LY_EVALID); |
| 311 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 312 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 313 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 314 | ext_p->name, mod->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 315 | LY_EVALID); |
| 316 | name = &ext_p->name[u + 1]; |
| 317 | /* find the extension definition there */ |
| 318 | for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) { |
| 319 | if (!strcmp(name, mod->parsed->extensions[u].name)) { |
| 320 | edef = &mod->parsed->extensions[u]; |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 325 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
| 326 | LY_EVALID); |
| 327 | /* TODO plugins */ |
| 328 | |
| 329 | return LY_SUCCESS; |
| 330 | } |
| 331 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 332 | /** |
| 333 | * @brief Compile information from the if-feature statement |
| 334 | * @param[in] ctx Compile context. |
| 335 | * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions. |
| 336 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 337 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 338 | * @return LY_ERR value. |
| 339 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 340 | static LY_ERR |
| 341 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff) |
| 342 | { |
| 343 | const char *c = *value; |
| 344 | int r, rc = EXIT_FAILURE; |
| 345 | int i, j, last_not, checkversion = 0; |
| 346 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 347 | uint8_t op; |
| 348 | struct iff_stack stack = {0, 0, NULL}; |
| 349 | struct lysc_feature *f; |
| 350 | |
| 351 | assert(c); |
| 352 | |
| 353 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 354 | for (i = j = last_not = 0; c[i]; i++) { |
| 355 | if (c[i] == '(') { |
| 356 | j++; |
| 357 | checkversion = 1; |
| 358 | continue; |
| 359 | } else if (c[i] == ')') { |
| 360 | j--; |
| 361 | continue; |
| 362 | } else if (isspace(c[i])) { |
| 363 | checkversion = 1; |
| 364 | continue; |
| 365 | } |
| 366 | |
| 367 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 368 | if (c[i + r] == '\0') { |
| 369 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 370 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 371 | return LY_EVALID; |
| 372 | } else if (!isspace(c[i + r])) { |
| 373 | /* feature name starting with the not/and/or */ |
| 374 | last_not = 0; |
| 375 | f_size++; |
| 376 | } else if (c[i] == 'n') { /* not operation */ |
| 377 | if (last_not) { |
| 378 | /* double not */ |
| 379 | expr_size = expr_size - 2; |
| 380 | last_not = 0; |
| 381 | } else { |
| 382 | last_not = 1; |
| 383 | } |
| 384 | } else { /* and, or */ |
| 385 | f_exp++; |
| 386 | /* not a not operation */ |
| 387 | last_not = 0; |
| 388 | } |
| 389 | i += r; |
| 390 | } else { |
| 391 | f_size++; |
| 392 | last_not = 0; |
| 393 | } |
| 394 | expr_size++; |
| 395 | |
| 396 | while (!isspace(c[i])) { |
| 397 | if (!c[i] || c[i] == ')') { |
| 398 | i--; |
| 399 | break; |
| 400 | } |
| 401 | i++; |
| 402 | } |
| 403 | } |
| 404 | if (j || f_exp != f_size) { |
| 405 | /* not matching count of ( and ) */ |
| 406 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 407 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 408 | return LY_EVALID; |
| 409 | } |
| 410 | |
| 411 | if (checkversion || expr_size > 1) { |
| 412 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 413 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 414 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 415 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 416 | return LY_EVALID; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /* allocate the memory */ |
| 421 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 422 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 423 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 424 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 425 | |
| 426 | stack.size = expr_size; |
| 427 | f_size--; expr_size--; /* used as indexes from now */ |
| 428 | |
| 429 | for (i--; i >= 0; i--) { |
| 430 | if (c[i] == ')') { |
| 431 | /* push it on stack */ |
| 432 | iff_stack_push(&stack, LYS_IFF_RP); |
| 433 | continue; |
| 434 | } else if (c[i] == '(') { |
| 435 | /* pop from the stack into result all operators until ) */ |
| 436 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 437 | iff_setop(iff->expr, op, expr_size--); |
| 438 | } |
| 439 | continue; |
| 440 | } else if (isspace(c[i])) { |
| 441 | continue; |
| 442 | } |
| 443 | |
| 444 | /* end of operator or operand -> find beginning and get what is it */ |
| 445 | j = i + 1; |
| 446 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 447 | i--; |
| 448 | } |
| 449 | i++; /* go back by one step */ |
| 450 | |
| 451 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 452 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 453 | /* double not */ |
| 454 | iff_stack_pop(&stack); |
| 455 | } else { |
| 456 | /* not has the highest priority, so do not pop from the stack |
| 457 | * as in case of AND and OR */ |
| 458 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 459 | } |
| 460 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 461 | /* as for OR - pop from the stack all operators with the same or higher |
| 462 | * priority and store them to the result, then push the AND to the stack */ |
| 463 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 464 | op = iff_stack_pop(&stack); |
| 465 | iff_setop(iff->expr, op, expr_size--); |
| 466 | } |
| 467 | iff_stack_push(&stack, LYS_IFF_AND); |
| 468 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 469 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 470 | op = iff_stack_pop(&stack); |
| 471 | iff_setop(iff->expr, op, expr_size--); |
| 472 | } |
| 473 | iff_stack_push(&stack, LYS_IFF_OR); |
| 474 | } else { |
| 475 | /* feature name, length is j - i */ |
| 476 | |
| 477 | /* add it to the expression */ |
| 478 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 479 | |
| 480 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 481 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 482 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 483 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 484 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 485 | iff->features[f_size] = f; |
| 486 | LY_ARRAY_INCREMENT(iff->features); |
| 487 | f_size--; |
| 488 | } |
| 489 | } |
| 490 | while (stack.index) { |
| 491 | op = iff_stack_pop(&stack); |
| 492 | iff_setop(iff->expr, op, expr_size--); |
| 493 | } |
| 494 | |
| 495 | if (++expr_size || ++f_size) { |
| 496 | /* not all expected operators and operands found */ |
| 497 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 498 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 499 | rc = LY_EINT; |
| 500 | } else { |
| 501 | rc = LY_SUCCESS; |
| 502 | } |
| 503 | |
| 504 | error: |
| 505 | /* cleanup */ |
| 506 | iff_stack_clean(&stack); |
| 507 | |
| 508 | return rc; |
| 509 | } |
| 510 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 511 | /** |
| 512 | * @brief Compile information from the when statement |
| 513 | * @param[in] ctx Compile context. |
| 514 | * @param[in] when_p The parsed when statement structure. |
| 515 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 516 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 517 | * @return LY_ERR value. |
| 518 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 519 | static LY_ERR |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 520 | 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] | 521 | { |
| 522 | unsigned int u; |
| 523 | LY_ERR ret = LY_SUCCESS; |
| 524 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 525 | *when = calloc(1, sizeof **when); |
| 526 | (*when)->refcount = 1; |
| 527 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
| 528 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 529 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 530 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
| 531 | 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] | 532 | |
| 533 | done: |
| 534 | return ret; |
| 535 | } |
| 536 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 537 | /** |
| 538 | * @brief Compile information from the must statement |
| 539 | * @param[in] ctx Compile context. |
| 540 | * @param[in] must_p The parsed must statement structure. |
| 541 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 542 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 543 | * @return LY_ERR value. |
| 544 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 545 | static LY_ERR |
| 546 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must) |
| 547 | { |
| 548 | unsigned int u; |
| 549 | LY_ERR ret = LY_SUCCESS; |
| 550 | |
| 551 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 552 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
| 553 | |
| 554 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 555 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 556 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 557 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 558 | COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done); |
| 559 | |
| 560 | done: |
| 561 | return ret; |
| 562 | } |
| 563 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 564 | /** |
| 565 | * @brief Compile information from the import statement |
| 566 | * @param[in] ctx Compile context. |
| 567 | * @param[in] imp_p The parsed import statement structure. |
| 568 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 569 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 570 | * @return LY_ERR value. |
| 571 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 572 | static LY_ERR |
| 573 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp) |
| 574 | { |
| 575 | unsigned int u; |
| 576 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 577 | LY_ERR ret = LY_SUCCESS; |
| 578 | |
| 579 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
| 580 | COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done); |
| 581 | imp->module = imp_p->module; |
| 582 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 583 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 584 | * 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] | 585 | * 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] | 586 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 587 | /* try to use filepath if present */ |
| 588 | if (imp->module->filepath) { |
| 589 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 590 | !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] | 591 | if (mod != imp->module) { |
| 592 | 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] | 593 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 594 | mod = NULL; |
| 595 | } |
| 596 | } |
| 597 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 598 | 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] | 599 | 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] | 600 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 601 | return LY_ENOTFOUND; |
| 602 | } |
| 603 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | done: |
| 607 | return ret; |
| 608 | } |
| 609 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 610 | /** |
| 611 | * @brief Compile information from the identity statement |
| 612 | * |
| 613 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 614 | * |
| 615 | * @param[in] ctx Compile context. |
| 616 | * @param[in] ident_p The parsed identity statement structure. |
| 617 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 618 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 619 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 620 | * @return LY_ERR value. |
| 621 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 622 | static LY_ERR |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 623 | 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] | 624 | { |
| 625 | unsigned int u; |
| 626 | LY_ERR ret = LY_SUCCESS; |
| 627 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 628 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 629 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 630 | DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc); |
| 631 | DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 632 | COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 633 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
| 634 | COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done); |
| 635 | ident->flags = ident_p->flags; |
| 636 | |
| 637 | done: |
| 638 | return ret; |
| 639 | } |
| 640 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 641 | /** |
| 642 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 643 | * |
| 644 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 645 | * |
| 646 | * @param[in] ctx Compile context for logging. |
| 647 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 648 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 649 | * @return LY_SUCCESS if everything is ok. |
| 650 | * @return LY_EVALID if the identity is derived from itself. |
| 651 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 652 | static LY_ERR |
| 653 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 654 | { |
| 655 | LY_ERR ret = LY_EVALID; |
| 656 | unsigned int u, v; |
| 657 | struct ly_set recursion = {0}; |
| 658 | struct lysc_ident *drv; |
| 659 | |
| 660 | if (!derived) { |
| 661 | return LY_SUCCESS; |
| 662 | } |
| 663 | |
| 664 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 665 | if (ident == derived[u]) { |
| 666 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 667 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 668 | goto cleanup; |
| 669 | } |
| 670 | ly_set_add(&recursion, derived[u], 0); |
| 671 | } |
| 672 | |
| 673 | for (v = 0; v < recursion.count; ++v) { |
| 674 | drv = recursion.objs[v]; |
| 675 | if (!drv->derived) { |
| 676 | continue; |
| 677 | } |
| 678 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 679 | if (ident == drv->derived[u]) { |
| 680 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 681 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 682 | goto cleanup; |
| 683 | } |
| 684 | ly_set_add(&recursion, drv->derived[u], 0); |
| 685 | } |
| 686 | } |
| 687 | ret = LY_SUCCESS; |
| 688 | |
| 689 | cleanup: |
| 690 | ly_set_erase(&recursion, NULL); |
| 691 | return ret; |
| 692 | } |
| 693 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 694 | /** |
| 695 | * @brief Find and process the referenced base identities from another identity or identityref |
| 696 | * |
| 697 | * For bases in identity se backlinks to them from the base identities. For identityref, store |
| 698 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 699 | * to distinguish these two use cases. |
| 700 | * |
| 701 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 702 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 703 | * @param[in] ident Referencing identity to work with. |
| 704 | * @param[in] bases Array of bases of identityref to fill in. |
| 705 | * @return LY_ERR value. |
| 706 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 707 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 708 | 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] | 709 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 710 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 711 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 712 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 713 | struct lysc_ident **idref; |
| 714 | |
| 715 | assert(ident || bases); |
| 716 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 717 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 718 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 719 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 720 | return LY_EVALID; |
| 721 | } |
| 722 | |
| 723 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 724 | s = strchr(bases_p[u], ':'); |
| 725 | if (s) { |
| 726 | /* prefixed identity */ |
| 727 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 728 | 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] | 729 | } else { |
| 730 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 731 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 732 | } |
| 733 | if (!mod) { |
| 734 | if (ident) { |
| 735 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 736 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 737 | } else { |
| 738 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 739 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 740 | } |
| 741 | return LY_EVALID; |
| 742 | } |
| 743 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 744 | if (mod->compiled && mod->compiled->identities) { |
| 745 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 746 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 747 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 748 | if (ident == &mod->compiled->identities[v]) { |
| 749 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 750 | "Identity \"%s\" is derived from itself.", ident->name); |
| 751 | return LY_EVALID; |
| 752 | } |
| 753 | 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] | 754 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 755 | 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] | 756 | *idref = ident; |
| 757 | } else { |
| 758 | /* we have match! store the found identity */ |
| 759 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 760 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 761 | } |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | if (!idref || !(*idref)) { |
| 767 | if (ident) { |
| 768 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 769 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 770 | } else { |
| 771 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 772 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 773 | } |
| 774 | return LY_EVALID; |
| 775 | } |
| 776 | } |
| 777 | return LY_SUCCESS; |
| 778 | } |
| 779 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 780 | /** |
| 781 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 782 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 783 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 784 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 785 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 786 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 787 | static LY_ERR |
| 788 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 789 | { |
| 790 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 791 | |
| 792 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 793 | if (!idents_p[i].bases) { |
| 794 | continue; |
| 795 | } |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 796 | 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] | 797 | } |
| 798 | return LY_SUCCESS; |
| 799 | } |
| 800 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 801 | LY_ERR |
| 802 | lys_feature_precompile(struct ly_ctx *ctx, struct lysp_feature *features_p, struct lysc_feature **features) |
| 803 | { |
| 804 | unsigned int offset = 0, u; |
| 805 | struct lysc_ctx context = {0}; |
| 806 | |
| 807 | assert(ctx); |
| 808 | context.ctx = ctx; |
| 809 | |
| 810 | if (!features_p) { |
| 811 | return LY_SUCCESS; |
| 812 | } |
| 813 | if (*features) { |
| 814 | offset = LY_ARRAY_SIZE(*features); |
| 815 | } |
| 816 | |
| 817 | LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM); |
| 818 | LY_ARRAY_FOR(features_p, u) { |
| 819 | LY_ARRAY_INCREMENT(*features); |
| 820 | COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 821 | DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name); |
| 822 | DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 823 | DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref); |
| 824 | (*features)[offset + u].flags = features_p[u].flags; |
| 825 | } |
| 826 | |
| 827 | return LY_SUCCESS; |
| 828 | } |
| 829 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 830 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 831 | * @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] | 832 | * |
| 833 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 834 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 835 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 836 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 837 | * being currently processed). |
| 838 | * @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] | 839 | * @return LY_SUCCESS if everything is ok. |
| 840 | * @return LY_EVALID if the feature references indirectly itself. |
| 841 | */ |
| 842 | static LY_ERR |
| 843 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 844 | { |
| 845 | LY_ERR ret = LY_EVALID; |
| 846 | unsigned int u, v; |
| 847 | struct ly_set recursion = {0}; |
| 848 | struct lysc_feature *drv; |
| 849 | |
| 850 | if (!depfeatures) { |
| 851 | return LY_SUCCESS; |
| 852 | } |
| 853 | |
| 854 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 855 | if (feature == depfeatures[u]) { |
| 856 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 857 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 858 | goto cleanup; |
| 859 | } |
| 860 | ly_set_add(&recursion, depfeatures[u], 0); |
| 861 | } |
| 862 | |
| 863 | for (v = 0; v < recursion.count; ++v) { |
| 864 | drv = recursion.objs[v]; |
| 865 | if (!drv->depfeatures) { |
| 866 | continue; |
| 867 | } |
| 868 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 869 | if (feature == drv->depfeatures[u]) { |
| 870 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 871 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 872 | goto cleanup; |
| 873 | } |
| 874 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 875 | } |
| 876 | } |
| 877 | ret = LY_SUCCESS; |
| 878 | |
| 879 | cleanup: |
| 880 | ly_set_erase(&recursion, NULL); |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 885 | * @brief Create pre-compiled features array. |
| 886 | * |
| 887 | * See lys_feature_precompile() for more details. |
| 888 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 889 | * @param[in] ctx Compile context. |
| 890 | * @param[in] feature_p Parsed feature definition to compile. |
| 891 | * @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] | 892 | * @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] | 893 | * @return LY_ERR value. |
| 894 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 895 | static LY_ERR |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 896 | 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] | 897 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 898 | unsigned int u, v, x; |
| 899 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 900 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 901 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 902 | /* find the preprecompiled feature */ |
| 903 | LY_ARRAY_FOR(features, x) { |
| 904 | if (strcmp(features[x].name, feature_p->name)) { |
| 905 | continue; |
| 906 | } |
| 907 | feature = &features[x]; |
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 | /* finish compilation started in lys_feature_precompile() */ |
| 910 | COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done); |
| 911 | COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 912 | if (feature->iffeatures) { |
| 913 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 914 | if (feature->iffeatures[u].features) { |
| 915 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 916 | /* check for circular dependency - direct reference first,... */ |
| 917 | if (feature == feature->iffeatures[u].features[v]) { |
| 918 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 919 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 920 | return LY_EVALID; |
| 921 | } |
| 922 | /* ... and indirect circular reference */ |
| 923 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 924 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 925 | /* add itself into the dependants list */ |
| 926 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 927 | *df = feature; |
| 928 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 929 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 930 | } |
| 931 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 932 | done: |
| 933 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 934 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 935 | |
| 936 | LOGINT(ctx->ctx); |
| 937 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 940 | /** |
| 941 | * @brief Revert compiled list of features back to the precompiled state. |
| 942 | * |
| 943 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 944 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 945 | * |
| 946 | * @param[in] ctx Compilation context. |
| 947 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 948 | * and supposed to hold the off_features list. |
| 949 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 950 | static void |
| 951 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 952 | { |
| 953 | unsigned int u, v; |
| 954 | |
| 955 | /* keep the off_features list until the complete lys_module is freed */ |
| 956 | mod->off_features = mod->compiled->features; |
| 957 | mod->compiled->features = NULL; |
| 958 | |
| 959 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 960 | * which may points into the data being freed here */ |
| 961 | LY_ARRAY_FOR(mod->off_features, u) { |
| 962 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 963 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 964 | } |
| 965 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 966 | mod->off_features[u].iffeatures = NULL; |
| 967 | |
| 968 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 969 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 970 | } |
| 971 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 972 | mod->off_features[u].exts = NULL; |
| 973 | } |
| 974 | } |
| 975 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 976 | /** |
| 977 | * @brief Validate and normalize numeric value from a range definition. |
| 978 | * @param[in] ctx Compile context. |
| 979 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 980 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 981 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 982 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 983 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 984 | * @param[in] value String value of the range boundary. |
| 985 | * @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. |
| 986 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 987 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 988 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 989 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 990 | 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] | 991 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 992 | size_t fraction = 0, size; |
| 993 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 994 | *len = 0; |
| 995 | |
| 996 | assert(value); |
| 997 | /* parse value */ |
| 998 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 999 | return LY_EVALID; |
| 1000 | } |
| 1001 | |
| 1002 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1003 | ++(*len); |
| 1004 | } |
| 1005 | |
| 1006 | while (isdigit(value[*len])) { |
| 1007 | ++(*len); |
| 1008 | } |
| 1009 | |
| 1010 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1011 | if (basetype == LY_TYPE_DEC64) { |
| 1012 | goto decimal; |
| 1013 | } else { |
| 1014 | *valcopy = strndup(value, *len); |
| 1015 | return LY_SUCCESS; |
| 1016 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1017 | } |
| 1018 | fraction = *len; |
| 1019 | |
| 1020 | ++(*len); |
| 1021 | while (isdigit(value[*len])) { |
| 1022 | ++(*len); |
| 1023 | } |
| 1024 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1025 | if (basetype == LY_TYPE_DEC64) { |
| 1026 | decimal: |
| 1027 | assert(frdigits); |
| 1028 | if (*len - 1 - fraction > frdigits) { |
| 1029 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1030 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1031 | *len, value, frdigits); |
| 1032 | return LY_EINVAL; |
| 1033 | } |
| 1034 | if (fraction) { |
| 1035 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1036 | } else { |
| 1037 | size = (*len) + frdigits + 1; |
| 1038 | } |
| 1039 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1040 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1041 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1042 | (*valcopy)[size - 1] = '\0'; |
| 1043 | if (fraction) { |
| 1044 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1045 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1046 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1047 | } else { |
| 1048 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1049 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1050 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1051 | } |
| 1052 | return LY_SUCCESS; |
| 1053 | } |
| 1054 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1055 | /** |
| 1056 | * @brief Check that values in range are in ascendant order. |
| 1057 | * @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] | 1058 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1059 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1060 | * @param[in] value Current value to check. |
| 1061 | * @param[in] prev_value The last seen value. |
| 1062 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1063 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1064 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1065 | 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] | 1066 | { |
| 1067 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1068 | 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] | 1069 | return LY_EEXIST; |
| 1070 | } |
| 1071 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1072 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1073 | return LY_EEXIST; |
| 1074 | } |
| 1075 | } |
| 1076 | return LY_SUCCESS; |
| 1077 | } |
| 1078 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1079 | /** |
| 1080 | * @brief Set min/max value of the range part. |
| 1081 | * @param[in] ctx Compile context. |
| 1082 | * @param[in] part Range part structure to fill. |
| 1083 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1084 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1085 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1086 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1087 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1088 | * @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] | 1089 | * @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] | 1090 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1091 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1092 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1093 | * frdigits value), LY_EMEM. |
| 1094 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1095 | static LY_ERR |
| 1096 | 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] | 1097 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1098 | { |
| 1099 | LY_ERR ret = LY_SUCCESS; |
| 1100 | char *valcopy = NULL; |
| 1101 | size_t len; |
| 1102 | |
| 1103 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1104 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1105 | LY_CHECK_GOTO(ret, finalize); |
| 1106 | } |
| 1107 | if (!valcopy && base_range) { |
| 1108 | if (max) { |
| 1109 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1110 | } else { |
| 1111 | part->min_64 = base_range->parts[0].min_64; |
| 1112 | } |
| 1113 | if (!first) { |
| 1114 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1115 | } |
| 1116 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1120 | case LY_TYPE_INT8: /* range */ |
| 1121 | if (valcopy) { |
| 1122 | ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64); |
| 1123 | } else if (max) { |
| 1124 | part->max_64 = INT64_C(127); |
| 1125 | } else { |
| 1126 | part->min_64 = INT64_C(-128); |
| 1127 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1128 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1129 | 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] | 1130 | } |
| 1131 | break; |
| 1132 | case LY_TYPE_INT16: /* range */ |
| 1133 | if (valcopy) { |
| 1134 | ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
| 1135 | } else if (max) { |
| 1136 | part->max_64 = INT64_C(32767); |
| 1137 | } else { |
| 1138 | part->min_64 = INT64_C(-32768); |
| 1139 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1140 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1141 | 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] | 1142 | } |
| 1143 | break; |
| 1144 | case LY_TYPE_INT32: /* range */ |
| 1145 | if (valcopy) { |
| 1146 | ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
| 1147 | } else if (max) { |
| 1148 | part->max_64 = INT64_C(2147483647); |
| 1149 | } else { |
| 1150 | part->min_64 = INT64_C(-2147483648); |
| 1151 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1152 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1153 | 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] | 1154 | } |
| 1155 | break; |
| 1156 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1157 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1158 | if (valcopy) { |
| 1159 | ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
| 1160 | max ? &part->max_64 : &part->min_64); |
| 1161 | } else if (max) { |
| 1162 | part->max_64 = INT64_C(9223372036854775807); |
| 1163 | } else { |
| 1164 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1165 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1166 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1167 | 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] | 1168 | } |
| 1169 | break; |
| 1170 | case LY_TYPE_UINT8: /* range */ |
| 1171 | if (valcopy) { |
| 1172 | ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64); |
| 1173 | } else if (max) { |
| 1174 | part->max_u64 = UINT64_C(255); |
| 1175 | } else { |
| 1176 | part->min_u64 = UINT64_C(0); |
| 1177 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1178 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1179 | 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] | 1180 | } |
| 1181 | break; |
| 1182 | case LY_TYPE_UINT16: /* range */ |
| 1183 | if (valcopy) { |
| 1184 | ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
| 1185 | } else if (max) { |
| 1186 | part->max_u64 = UINT64_C(65535); |
| 1187 | } else { |
| 1188 | part->min_u64 = UINT64_C(0); |
| 1189 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1190 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1191 | 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] | 1192 | } |
| 1193 | break; |
| 1194 | case LY_TYPE_UINT32: /* range */ |
| 1195 | if (valcopy) { |
| 1196 | ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64); |
| 1197 | } else if (max) { |
| 1198 | part->max_u64 = UINT64_C(4294967295); |
| 1199 | } else { |
| 1200 | part->min_u64 = UINT64_C(0); |
| 1201 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1202 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1203 | 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] | 1204 | } |
| 1205 | break; |
| 1206 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1207 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1208 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1209 | if (valcopy) { |
| 1210 | ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
| 1211 | } else if (max) { |
| 1212 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1213 | } else { |
| 1214 | part->min_u64 = UINT64_C(0); |
| 1215 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1216 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1217 | 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] | 1218 | } |
| 1219 | break; |
| 1220 | default: |
| 1221 | LOGINT(ctx->ctx); |
| 1222 | ret = LY_EINT; |
| 1223 | } |
| 1224 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1225 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1226 | if (ret == LY_EDENIED) { |
| 1227 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1228 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1229 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1230 | } else if (ret == LY_EVALID) { |
| 1231 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1232 | "Invalid %s restriction - invalid value \"%s\".", |
| 1233 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1234 | } else if (ret == LY_EEXIST) { |
| 1235 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1236 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1237 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1238 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1239 | } else if (!ret && value) { |
| 1240 | *value = *value + len; |
| 1241 | } |
| 1242 | free(valcopy); |
| 1243 | return ret; |
| 1244 | } |
| 1245 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1246 | /** |
| 1247 | * @brief Compile the parsed range restriction. |
| 1248 | * @param[in] ctx Compile context. |
| 1249 | * @param[in] range_p Parsed range structure to compile. |
| 1250 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1251 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1252 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1253 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1254 | * range restriction must be more restrictive than the base_range. |
| 1255 | * @param[in,out] range Pointer to the created current range structure. |
| 1256 | * @return LY_ERR value. |
| 1257 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1258 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1259 | 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] | 1260 | struct lysc_range *base_range, struct lysc_range **range) |
| 1261 | { |
| 1262 | LY_ERR ret = LY_EVALID; |
| 1263 | const char *expr; |
| 1264 | struct lysc_range_part *parts = NULL, *part; |
| 1265 | int range_expected = 0, uns; |
| 1266 | unsigned int parts_done = 0, u, v; |
| 1267 | |
| 1268 | assert(range); |
| 1269 | assert(range_p); |
| 1270 | |
| 1271 | expr = range_p->arg; |
| 1272 | while(1) { |
| 1273 | if (isspace(*expr)) { |
| 1274 | ++expr; |
| 1275 | } else if (*expr == '\0') { |
| 1276 | if (range_expected) { |
| 1277 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1278 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1279 | length_restr ? "length" : "range", range_p->arg); |
| 1280 | goto cleanup; |
| 1281 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1282 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1283 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1284 | length_restr ? "length" : "range", range_p->arg); |
| 1285 | goto cleanup; |
| 1286 | } |
| 1287 | parts_done++; |
| 1288 | break; |
| 1289 | } else if (!strncmp(expr, "min", 3)) { |
| 1290 | if (parts) { |
| 1291 | /* min cannot be used elsewhere than in the first part */ |
| 1292 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1293 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1294 | expr - range_p->arg, range_p->arg); |
| 1295 | goto cleanup; |
| 1296 | } |
| 1297 | expr += 3; |
| 1298 | |
| 1299 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1300 | 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] | 1301 | part->max_64 = part->min_64; |
| 1302 | } else if (*expr == '|') { |
| 1303 | if (!parts || range_expected) { |
| 1304 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1305 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1306 | goto cleanup; |
| 1307 | } |
| 1308 | expr++; |
| 1309 | parts_done++; |
| 1310 | /* process next part of the expression */ |
| 1311 | } else if (!strncmp(expr, "..", 2)) { |
| 1312 | expr += 2; |
| 1313 | while (isspace(*expr)) { |
| 1314 | expr++; |
| 1315 | } |
| 1316 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1317 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1318 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1319 | goto cleanup; |
| 1320 | } |
| 1321 | /* continue expecting the upper boundary */ |
| 1322 | range_expected = 1; |
| 1323 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1324 | /* number */ |
| 1325 | if (range_expected) { |
| 1326 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1327 | 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] | 1328 | range_expected = 0; |
| 1329 | } else { |
| 1330 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1331 | 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] | 1332 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1333 | part->max_64 = part->min_64; |
| 1334 | } |
| 1335 | |
| 1336 | /* continue with possible another expression part */ |
| 1337 | } else if (!strncmp(expr, "max", 3)) { |
| 1338 | expr += 3; |
| 1339 | while (isspace(*expr)) { |
| 1340 | expr++; |
| 1341 | } |
| 1342 | if (*expr != '\0') { |
| 1343 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1344 | length_restr ? "length" : "range", expr); |
| 1345 | goto cleanup; |
| 1346 | } |
| 1347 | if (range_expected) { |
| 1348 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1349 | 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] | 1350 | range_expected = 0; |
| 1351 | } else { |
| 1352 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1353 | 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] | 1354 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1355 | part->min_64 = part->max_64; |
| 1356 | } |
| 1357 | } else { |
| 1358 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1359 | length_restr ? "length" : "range", expr); |
| 1360 | goto cleanup; |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | /* check with the previous range/length restriction */ |
| 1365 | if (base_range) { |
| 1366 | switch (basetype) { |
| 1367 | case LY_TYPE_BINARY: |
| 1368 | case LY_TYPE_UINT8: |
| 1369 | case LY_TYPE_UINT16: |
| 1370 | case LY_TYPE_UINT32: |
| 1371 | case LY_TYPE_UINT64: |
| 1372 | case LY_TYPE_STRING: |
| 1373 | uns = 1; |
| 1374 | break; |
| 1375 | case LY_TYPE_DEC64: |
| 1376 | case LY_TYPE_INT8: |
| 1377 | case LY_TYPE_INT16: |
| 1378 | case LY_TYPE_INT32: |
| 1379 | case LY_TYPE_INT64: |
| 1380 | uns = 0; |
| 1381 | break; |
| 1382 | default: |
| 1383 | LOGINT(ctx->ctx); |
| 1384 | ret = LY_EINT; |
| 1385 | goto cleanup; |
| 1386 | } |
| 1387 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1388 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1389 | goto baseerror; |
| 1390 | } |
| 1391 | /* current lower bound is not lower than the base */ |
| 1392 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1393 | /* base has single value */ |
| 1394 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1395 | /* both lower bounds are the same */ |
| 1396 | if (parts[u].min_64 != parts[u].max_64) { |
| 1397 | /* current continues with a range */ |
| 1398 | goto baseerror; |
| 1399 | } else { |
| 1400 | /* equal single values, move both forward */ |
| 1401 | ++v; |
| 1402 | continue; |
| 1403 | } |
| 1404 | } else { |
| 1405 | /* base is single value lower than current range, so the |
| 1406 | * value from base range is removed in the current, |
| 1407 | * move only base and repeat checking */ |
| 1408 | ++v; |
| 1409 | --u; |
| 1410 | continue; |
| 1411 | } |
| 1412 | } else { |
| 1413 | /* base is the range */ |
| 1414 | if (parts[u].min_64 == parts[u].max_64) { |
| 1415 | /* current is a single value */ |
| 1416 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1417 | /* current is behind the base range, so base range is omitted, |
| 1418 | * move the base and keep the current for further check */ |
| 1419 | ++v; |
| 1420 | --u; |
| 1421 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1422 | continue; |
| 1423 | } else { |
| 1424 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1425 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1426 | /* higher bound is higher than the current higher bound */ |
| 1427 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1428 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1429 | * continue with the same current, but move the base */ |
| 1430 | --u; |
| 1431 | ++v; |
| 1432 | continue; |
| 1433 | } |
| 1434 | /* current range starts within the base range but end behind it */ |
| 1435 | goto baseerror; |
| 1436 | } else { |
| 1437 | /* current range is smaller than the base, |
| 1438 | * move current, but stay with the base */ |
| 1439 | continue; |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | if (u != parts_done) { |
| 1445 | baseerror: |
| 1446 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1447 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1448 | length_restr ? "length" : "range", range_p->arg); |
| 1449 | goto cleanup; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | if (!(*range)) { |
| 1454 | *range = calloc(1, sizeof **range); |
| 1455 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1456 | } |
| 1457 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1458 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1459 | if (range_p->eapptag) { |
| 1460 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1461 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1462 | } |
| 1463 | if (range_p->emsg) { |
| 1464 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1465 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1466 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1467 | if (range_p->dsc) { |
| 1468 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1469 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1470 | } |
| 1471 | if (range_p->ref) { |
| 1472 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1473 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1474 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1475 | /* extensions are taken only from the last range by the caller */ |
| 1476 | |
| 1477 | (*range)->parts = parts; |
| 1478 | parts = NULL; |
| 1479 | ret = LY_SUCCESS; |
| 1480 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1481 | LY_ARRAY_FREE(parts); |
| 1482 | |
| 1483 | return ret; |
| 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * @brief Checks pattern syntax. |
| 1488 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1489 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1490 | * @param[in] pattern Pattern to check. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1491 | * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed. |
| 1492 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1493 | */ |
| 1494 | static LY_ERR |
| 1495 | lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp) |
| 1496 | { |
| 1497 | int idx, idx2, start, end, err_offset, count; |
| 1498 | char *perl_regex, *ptr; |
| 1499 | const char *err_msg, *orig_ptr; |
| 1500 | pcre *precomp; |
| 1501 | #define URANGE_LEN 19 |
| 1502 | char *ublock2urange[][2] = { |
| 1503 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1504 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1505 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1506 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1507 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1508 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1509 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1510 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1511 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1512 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1513 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1514 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1515 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1516 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1517 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1518 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1519 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1520 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1521 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1522 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1523 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1524 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1525 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1526 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1527 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1528 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1529 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1530 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1531 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1532 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1533 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1534 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1535 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1536 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1537 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1538 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1539 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1540 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1541 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1542 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1543 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1544 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1545 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1546 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1547 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1548 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1549 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1550 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1551 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1552 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1553 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1554 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1555 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1556 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1557 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1558 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1559 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1560 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1561 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1562 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1563 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1564 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1565 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1566 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1567 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1568 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1569 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1570 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1571 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1572 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1573 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1574 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1575 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1576 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1577 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1578 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1579 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1580 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1581 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1582 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1583 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1584 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1585 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1586 | {NULL, NULL} |
| 1587 | }; |
| 1588 | |
| 1589 | /* adjust the expression to a Perl equivalent |
| 1590 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1591 | |
| 1592 | /* we need to replace all "$" with "\$", count them now */ |
| 1593 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1594 | |
| 1595 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1596 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1597 | perl_regex[0] = '\0'; |
| 1598 | |
| 1599 | ptr = perl_regex; |
| 1600 | |
| 1601 | if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) { |
| 1602 | /* we will add line-end anchoring */ |
| 1603 | ptr[0] = '('; |
| 1604 | ++ptr; |
| 1605 | } |
| 1606 | |
| 1607 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1608 | if (orig_ptr[0] == '$') { |
| 1609 | ptr += sprintf(ptr, "\\$"); |
| 1610 | } else if (orig_ptr[0] == '^') { |
| 1611 | ptr += sprintf(ptr, "\\^"); |
| 1612 | } else { |
| 1613 | ptr[0] = orig_ptr[0]; |
| 1614 | ++ptr; |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) { |
| 1619 | ptr += sprintf(ptr, ")$"); |
| 1620 | } else { |
| 1621 | ptr[0] = '\0'; |
| 1622 | ++ptr; |
| 1623 | } |
| 1624 | |
| 1625 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1626 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1627 | start = ptr - perl_regex; |
| 1628 | |
| 1629 | ptr = strchr(ptr, '}'); |
| 1630 | if (!ptr) { |
| 1631 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1632 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1633 | free(perl_regex); |
| 1634 | return LY_EVALID; |
| 1635 | } |
| 1636 | end = (ptr - perl_regex) + 1; |
| 1637 | |
| 1638 | /* need more space */ |
| 1639 | if (end - start < URANGE_LEN) { |
| 1640 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1641 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1642 | } |
| 1643 | |
| 1644 | /* find our range */ |
| 1645 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1646 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1647 | break; |
| 1648 | } |
| 1649 | } |
| 1650 | if (!ublock2urange[idx][0]) { |
| 1651 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1652 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1653 | free(perl_regex); |
| 1654 | return LY_EVALID; |
| 1655 | } |
| 1656 | |
| 1657 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1658 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1659 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1660 | ++count; |
| 1661 | } |
| 1662 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1663 | --count; |
| 1664 | } |
| 1665 | } |
| 1666 | if (count) { |
| 1667 | /* skip brackets */ |
| 1668 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1669 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1670 | } else { |
| 1671 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1672 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | /* must return 0, already checked during parsing */ |
| 1677 | precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE, |
| 1678 | &err_msg, &err_offset, NULL); |
| 1679 | if (!precomp) { |
| 1680 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1681 | free(perl_regex); |
| 1682 | return LY_EVALID; |
| 1683 | } |
| 1684 | free(perl_regex); |
| 1685 | |
| 1686 | if (pcre_precomp) { |
| 1687 | *pcre_precomp = precomp; |
| 1688 | } else { |
| 1689 | free(precomp); |
| 1690 | } |
| 1691 | |
| 1692 | return LY_SUCCESS; |
| 1693 | |
| 1694 | #undef URANGE_LEN |
| 1695 | } |
| 1696 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1697 | /** |
| 1698 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1699 | * @param[in] ctx Compile context. |
| 1700 | * @param[in] patterns_p Array of parsed patterns from the current type to compile. |
| 1701 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 1702 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1703 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1704 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1705 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1706 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1707 | static LY_ERR |
| 1708 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options, |
| 1709 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1710 | { |
| 1711 | struct lysc_pattern **pattern; |
| 1712 | unsigned int u, v; |
| 1713 | const char *err_msg; |
| 1714 | LY_ERR ret = LY_SUCCESS; |
| 1715 | |
| 1716 | /* first, copy the patterns from the base type */ |
| 1717 | if (base_patterns) { |
| 1718 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1719 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1720 | } |
| 1721 | |
| 1722 | LY_ARRAY_FOR(patterns_p, u) { |
| 1723 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1724 | *pattern = calloc(1, sizeof **pattern); |
| 1725 | ++(*pattern)->refcount; |
| 1726 | |
| 1727 | ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr); |
| 1728 | LY_CHECK_RET(ret); |
| 1729 | (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg); |
| 1730 | if (err_msg) { |
| 1731 | LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg); |
| 1732 | } |
| 1733 | |
| 1734 | if (patterns_p[u].arg[0] == 0x15) { |
| 1735 | (*pattern)->inverted = 1; |
| 1736 | } |
| 1737 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1738 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1739 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 1740 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1741 | COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, |
| 1742 | options, v, lys_compile_ext, ret, done); |
| 1743 | } |
| 1744 | done: |
| 1745 | return ret; |
| 1746 | } |
| 1747 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1748 | /** |
| 1749 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1750 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1751 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1752 | 0 /* LY_TYPE_UNKNOWN */, |
| 1753 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1754 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1755 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1756 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1757 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 1758 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1759 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1760 | 0 /* LY_TYPE_BOOL */, |
| 1761 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 1762 | 0 /* LY_TYPE_EMPTY */, |
| 1763 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 1764 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 1765 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 1766 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1767 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 1768 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1769 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1770 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1771 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 1772 | }; |
| 1773 | |
| 1774 | /** |
| 1775 | * @brief stringification of the YANG built-in data types |
| 1776 | */ |
| 1777 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 1778 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 1779 | "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] | 1780 | }; |
| 1781 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1782 | /** |
| 1783 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 1784 | * @param[in] ctx Compile context. |
| 1785 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 1786 | * @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. |
| 1787 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 1788 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 1789 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 1790 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1791 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1792 | static LY_ERR |
| 1793 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options, |
| 1794 | struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums) |
| 1795 | { |
| 1796 | LY_ERR ret = LY_SUCCESS; |
| 1797 | unsigned int u, v, match; |
| 1798 | int32_t value = 0; |
| 1799 | uint32_t position = 0; |
| 1800 | struct lysc_type_enum_item *e, storage; |
| 1801 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1802 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1803 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 1804 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 1805 | return LY_EVALID; |
| 1806 | } |
| 1807 | |
| 1808 | LY_ARRAY_FOR(enums_p, u) { |
| 1809 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 1810 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1811 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 1812 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1813 | if (base_enums) { |
| 1814 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 1815 | LY_ARRAY_FOR(base_enums, v) { |
| 1816 | if (!strcmp(e->name, base_enums[v].name)) { |
| 1817 | break; |
| 1818 | } |
| 1819 | } |
| 1820 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 1821 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1822 | "Invalid %s - derived type adds new item \"%s\".", |
| 1823 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 1824 | return LY_EVALID; |
| 1825 | } |
| 1826 | match = v; |
| 1827 | } |
| 1828 | |
| 1829 | if (basetype == LY_TYPE_ENUM) { |
| 1830 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1831 | e->value = (int32_t)enums_p[u].value; |
| 1832 | if (!u || e->value >= value) { |
| 1833 | value = e->value + 1; |
| 1834 | } |
| 1835 | /* check collision with other values */ |
| 1836 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1837 | if (e->value == (*enums)[v].value) { |
| 1838 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1839 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 1840 | e->value, e->name, (*enums)[v].name); |
| 1841 | return LY_EVALID; |
| 1842 | } |
| 1843 | } |
| 1844 | } else if (base_enums) { |
| 1845 | /* inherit the assigned value */ |
| 1846 | e->value = base_enums[match].value; |
| 1847 | if (!u || e->value >= value) { |
| 1848 | value = e->value + 1; |
| 1849 | } |
| 1850 | } else { |
| 1851 | /* assign value automatically */ |
| 1852 | if (u && value == INT32_MIN) { |
| 1853 | /* counter overflow */ |
| 1854 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1855 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 1856 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 1857 | return LY_EVALID; |
| 1858 | } |
| 1859 | e->value = value++; |
| 1860 | } |
| 1861 | } else { /* LY_TYPE_BITS */ |
| 1862 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1863 | e->value = (int32_t)enums_p[u].value; |
| 1864 | if (!u || (uint32_t)e->value >= position) { |
| 1865 | position = (uint32_t)e->value + 1; |
| 1866 | } |
| 1867 | /* check collision with other values */ |
| 1868 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1869 | if (e->value == (*enums)[v].value) { |
| 1870 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1871 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 1872 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 1873 | return LY_EVALID; |
| 1874 | } |
| 1875 | } |
| 1876 | } else if (base_enums) { |
| 1877 | /* inherit the assigned value */ |
| 1878 | e->value = base_enums[match].value; |
| 1879 | if (!u || (uint32_t)e->value >= position) { |
| 1880 | position = (uint32_t)e->value + 1; |
| 1881 | } |
| 1882 | } else { |
| 1883 | /* assign value automatically */ |
| 1884 | if (u && position == 0) { |
| 1885 | /* counter overflow */ |
| 1886 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1887 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 1888 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 1889 | return LY_EVALID; |
| 1890 | } |
| 1891 | e->value = position++; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | if (base_enums) { |
| 1896 | /* the assigned values must not change from the derived type */ |
| 1897 | if (e->value != base_enums[match].value) { |
| 1898 | if (basetype == LY_TYPE_ENUM) { |
| 1899 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1900 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 1901 | e->name, base_enums[match].value, e->value); |
| 1902 | } else { |
| 1903 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1904 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 1905 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 1906 | } |
| 1907 | return LY_EVALID; |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done); |
| 1912 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done); |
| 1913 | |
| 1914 | if (basetype == LY_TYPE_BITS) { |
| 1915 | /* keep bits ordered by position */ |
| 1916 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 1917 | if (v != u) { |
| 1918 | memcpy(&storage, e, sizeof *e); |
| 1919 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 1920 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 1921 | } |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | done: |
| 1926 | return ret; |
| 1927 | } |
| 1928 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1929 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 1930 | for ((NODE) = (NODE)->parent; \ |
| 1931 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 1932 | (NODE) = (NODE)->parent); \ |
| 1933 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 1934 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 1935 | TERM; \ |
| 1936 | } |
| 1937 | |
| 1938 | /** |
| 1939 | * @brief Validate the predicate(s) from the leafref path. |
| 1940 | * @param[in] ctx Compile context |
| 1941 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 1942 | * 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] | 1943 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1944 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1945 | * @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] | 1946 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1947 | */ |
| 1948 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1949 | 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] | 1950 | 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] | 1951 | { |
| 1952 | LY_ERR ret = LY_EVALID; |
| 1953 | const struct lys_module *mod; |
| 1954 | const struct lysc_node *src_node, *dst_node; |
| 1955 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 1956 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1957 | unsigned int dest_parent_times, c, u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1958 | const char *start, *end, *pke_end; |
| 1959 | struct ly_set keys = {0}; |
| 1960 | int i; |
| 1961 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1962 | assert(path_context); |
| 1963 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1964 | while (**predicate == '[') { |
| 1965 | start = (*predicate)++; |
| 1966 | |
| 1967 | while (isspace(**predicate)) { |
| 1968 | ++(*predicate); |
| 1969 | } |
| 1970 | LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup); |
| 1971 | while (isspace(**predicate)) { |
| 1972 | ++(*predicate); |
| 1973 | } |
| 1974 | if (**predicate != '=') { |
| 1975 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1976 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 1977 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1978 | goto cleanup; |
| 1979 | } |
| 1980 | ++(*predicate); |
| 1981 | while (isspace(**predicate)) { |
| 1982 | ++(*predicate); |
| 1983 | } |
| 1984 | |
| 1985 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 1986 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1987 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 1988 | goto cleanup; |
| 1989 | } |
| 1990 | --pke_end; |
| 1991 | while (isspace(*pke_end)) { |
| 1992 | --pke_end; |
| 1993 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 1994 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1995 | /* localize path-key-expr */ |
| 1996 | pke_start = path_key_expr = *predicate; |
| 1997 | /* move after the current predicate */ |
| 1998 | *predicate = end + 1; |
| 1999 | |
| 2000 | /* source (must be leaf or leaf-list) */ |
| 2001 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2002 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2003 | if (!mod) { |
| 2004 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2005 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2006 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2007 | goto cleanup; |
| 2008 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2009 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2010 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2011 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2012 | src_node = NULL; |
| 2013 | if (context_node->keys) { |
| 2014 | for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) { |
| 2015 | if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') { |
| 2016 | src_node = (const struct lysc_node*)context_node->keys[u]; |
| 2017 | break; |
| 2018 | } |
| 2019 | } |
| 2020 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2021 | if (!src_node) { |
| 2022 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2023 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2024 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2025 | goto cleanup; |
| 2026 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2027 | |
| 2028 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2029 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2030 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2031 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2032 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 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\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2035 | *predicate - start, start, src_node->name); |
| 2036 | goto cleanup; |
| 2037 | } |
| 2038 | |
| 2039 | /* destination */ |
| 2040 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2041 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2042 | |
| 2043 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 2044 | if (strncmp(path_key_expr, "current()", 9)) { |
| 2045 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2046 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2047 | *predicate - start, start); |
| 2048 | goto cleanup; |
| 2049 | } |
| 2050 | path_key_expr += 9; |
| 2051 | while (isspace(*path_key_expr)) { |
| 2052 | ++path_key_expr; |
| 2053 | } |
| 2054 | |
| 2055 | if (*path_key_expr != '/') { |
| 2056 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2057 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2058 | *predicate - start, start); |
| 2059 | goto cleanup; |
| 2060 | } |
| 2061 | ++path_key_expr; |
| 2062 | while (isspace(*path_key_expr)) { |
| 2063 | ++path_key_expr; |
| 2064 | } |
| 2065 | |
| 2066 | /* rel-path-keyexpr: |
| 2067 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2068 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2069 | ++dest_parent_times; |
| 2070 | path_key_expr += 2; |
| 2071 | while (isspace(*path_key_expr)) { |
| 2072 | ++path_key_expr; |
| 2073 | } |
| 2074 | if (*path_key_expr != '/') { |
| 2075 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2076 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2077 | *predicate - start, start); |
| 2078 | goto cleanup; |
| 2079 | } |
| 2080 | ++path_key_expr; |
| 2081 | while (isspace(*path_key_expr)) { |
| 2082 | ++path_key_expr; |
| 2083 | } |
| 2084 | |
| 2085 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2086 | * all schema nodes that cannot be instantiated in data tree */ |
| 2087 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2088 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2089 | *predicate - start, start); |
| 2090 | } |
| 2091 | if (!dest_parent_times) { |
| 2092 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2093 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2094 | *predicate - start, start); |
| 2095 | goto cleanup; |
| 2096 | } |
| 2097 | if (path_key_expr == pke_end) { |
| 2098 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2099 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2100 | *predicate - start, start); |
| 2101 | goto cleanup; |
| 2102 | } |
| 2103 | |
| 2104 | while(path_key_expr != pke_end) { |
| 2105 | if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) { |
| 2106 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2107 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2108 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2109 | goto cleanup; |
| 2110 | } |
| 2111 | |
| 2112 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2113 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2114 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2115 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2116 | } |
| 2117 | if (!mod) { |
| 2118 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2119 | "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.", |
| 2120 | *predicate - start, start, dst_len, dst); |
| 2121 | goto cleanup; |
| 2122 | } |
| 2123 | |
| 2124 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2125 | if (!dst_node) { |
| 2126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2127 | "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.", |
| 2128 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2129 | goto cleanup; |
| 2130 | } |
| 2131 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2132 | 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] | 2133 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2134 | "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] | 2135 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2136 | goto cleanup; |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | ret = LY_SUCCESS; |
| 2141 | cleanup: |
| 2142 | ly_set_erase(&keys, NULL); |
| 2143 | return ret; |
| 2144 | } |
| 2145 | |
| 2146 | /** |
| 2147 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2148 | * |
| 2149 | * path-arg = absolute-path / relative-path |
| 2150 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2151 | * relative-path = 1*(".." "/") descendant-path |
| 2152 | * |
| 2153 | * @param[in,out] path Path to parse. |
| 2154 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2155 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2156 | * @param[out] name Name of the token. |
| 2157 | * @param[out] nam_len Length of the name. |
| 2158 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2159 | * must not be changed between consecutive calls. -1 if the |
| 2160 | * path is absolute. |
| 2161 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2162 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2163 | */ |
| 2164 | static LY_ERR |
| 2165 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2166 | int *parent_times, int *has_predicate) |
| 2167 | { |
| 2168 | int par_times = 0; |
| 2169 | |
| 2170 | assert(path && *path); |
| 2171 | assert(parent_times); |
| 2172 | assert(prefix); |
| 2173 | assert(prefix_len); |
| 2174 | assert(name); |
| 2175 | assert(name_len); |
| 2176 | assert(has_predicate); |
| 2177 | |
| 2178 | *prefix = NULL; |
| 2179 | *prefix_len = 0; |
| 2180 | *name = NULL; |
| 2181 | *name_len = 0; |
| 2182 | *has_predicate = 0; |
| 2183 | |
| 2184 | if (!*parent_times) { |
| 2185 | if (!strncmp(*path, "..", 2)) { |
| 2186 | *path += 2; |
| 2187 | ++par_times; |
| 2188 | while (!strncmp(*path, "/..", 3)) { |
| 2189 | *path += 3; |
| 2190 | ++par_times; |
| 2191 | } |
| 2192 | } |
| 2193 | if (par_times) { |
| 2194 | *parent_times = par_times; |
| 2195 | } else { |
| 2196 | *parent_times = -1; |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | if (**path != '/') { |
| 2201 | return LY_EINVAL; |
| 2202 | } |
| 2203 | /* skip '/' */ |
| 2204 | ++(*path); |
| 2205 | |
| 2206 | /* node-identifier ([prefix:]name) */ |
| 2207 | LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len)); |
| 2208 | |
| 2209 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2210 | /* path continues by another token or this is the last token */ |
| 2211 | return LY_SUCCESS; |
| 2212 | } else if ((*path)[0] != '[') { |
| 2213 | /* unexpected character */ |
| 2214 | return LY_EINVAL; |
| 2215 | } else { |
| 2216 | /* predicate starting with [ */ |
| 2217 | *has_predicate = 1; |
| 2218 | return LY_SUCCESS; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2223 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2224 | * |
| 2225 | * The set of features used for target must be a subset of features used for the leafref. |
| 2226 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2227 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2228 | * |
| 2229 | * @param[in] refnode The leafref node. |
| 2230 | * @param[in] target Tha target node of the leafref. |
| 2231 | * @return LY_SUCCESS or LY_EVALID; |
| 2232 | */ |
| 2233 | static LY_ERR |
| 2234 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2235 | { |
| 2236 | LY_ERR ret = LY_EVALID; |
| 2237 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2238 | unsigned int u, v, count; |
| 2239 | struct ly_set features = {0}; |
| 2240 | |
| 2241 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2242 | if (iter->iffeatures) { |
| 2243 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2244 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2245 | 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] | 2246 | } |
| 2247 | } |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2252 | * So when adding features applicable to the target into the features set, we should not be |
| 2253 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2254 | * to the leafref itself. */ |
| 2255 | count = features.count; |
| 2256 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2257 | if (iter->iffeatures) { |
| 2258 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2259 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2260 | 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] | 2261 | /* new feature was added (or LY_EMEM) */ |
| 2262 | goto cleanup; |
| 2263 | } |
| 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | ret = LY_SUCCESS; |
| 2269 | |
| 2270 | cleanup: |
| 2271 | ly_set_erase(&features, NULL); |
| 2272 | return ret; |
| 2273 | } |
| 2274 | |
| 2275 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2276 | * @brief Validate the leafref path. |
| 2277 | * @param[in] ctx Compile context |
| 2278 | * @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] | 2279 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2280 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2281 | */ |
| 2282 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2283 | 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] | 2284 | { |
| 2285 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2286 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2287 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2288 | const char *id, *prefix, *name; |
| 2289 | size_t prefix_len, name_len; |
| 2290 | int parent_times = 0, has_predicate; |
| 2291 | unsigned int iter, u; |
| 2292 | LY_ERR ret = LY_SUCCESS; |
| 2293 | |
| 2294 | assert(ctx); |
| 2295 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2296 | assert(leafref); |
| 2297 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2298 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2299 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2300 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2301 | if (!iter) { /* first iteration */ |
| 2302 | /* precess ".." in relative paths */ |
| 2303 | if (parent_times > 0) { |
| 2304 | /* move from the context node */ |
| 2305 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2306 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2307 | * all schema nodes that cannot be instantiated in data tree */ |
| 2308 | 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] | 2309 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2315 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2316 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2317 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2318 | } |
| 2319 | if (!mod) { |
| 2320 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2321 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2322 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2323 | return LY_EVALID; |
| 2324 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame^] | 2325 | if (!mod->implemented) { |
| 2326 | /* make the module implemented */ |
| 2327 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2328 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2329 | |
| 2330 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2331 | if (!node) { |
| 2332 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2333 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2334 | return LY_EVALID; |
| 2335 | } |
| 2336 | parent = node; |
| 2337 | |
| 2338 | if (has_predicate) { |
| 2339 | /* we have predicate, so the current result must be list */ |
| 2340 | if (node->nodetype != LYS_LIST) { |
| 2341 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2342 | "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] | 2343 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2344 | return LY_EVALID; |
| 2345 | } |
| 2346 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2347 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2348 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | ++iter; |
| 2352 | } |
| 2353 | if (ret) { |
| 2354 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2355 | "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] | 2356 | return LY_EVALID; |
| 2357 | } |
| 2358 | |
| 2359 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2360 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2361 | "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] | 2362 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2363 | return LY_EVALID; |
| 2364 | } |
| 2365 | |
| 2366 | /* check status */ |
| 2367 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2368 | return LY_EVALID; |
| 2369 | } |
| 2370 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2371 | /* check config */ |
| 2372 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2373 | if (node->flags & LYS_CONFIG_R) { |
| 2374 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2375 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2376 | leafref->path); |
| 2377 | return LY_EVALID; |
| 2378 | } |
| 2379 | } |
| 2380 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2381 | /* store the target's type and check for circular chain of leafrefs */ |
| 2382 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2383 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2384 | if (type == (struct lysc_type*)leafref) { |
| 2385 | /* circular chain detected */ |
| 2386 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2387 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2388 | return LY_EVALID; |
| 2389 | } |
| 2390 | } |
| 2391 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2392 | /* check if leafref and its target are under common if-features */ |
| 2393 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2394 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2395 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2396 | leafref->path); |
| 2397 | return LY_EVALID; |
| 2398 | } |
| 2399 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2400 | return LY_SUCCESS; |
| 2401 | } |
| 2402 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2403 | 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] | 2404 | 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] | 2405 | /** |
| 2406 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2407 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2408 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2409 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2410 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2411 | * @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] | 2412 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2413 | * @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] | 2414 | * @param[in] basetype Base YANG built-in type of the type to compile. |
| 2415 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2416 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2417 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2418 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2419 | * @return LY_ERR value. |
| 2420 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2421 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2422 | 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, |
| 2423 | struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname, |
| 2424 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2425 | { |
| 2426 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2427 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2428 | struct lysc_type_bin *bin; |
| 2429 | struct lysc_type_num *num; |
| 2430 | struct lysc_type_str *str; |
| 2431 | struct lysc_type_bits *bits; |
| 2432 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2433 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2434 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2435 | struct lysc_type_union *un, *un_aux; |
| 2436 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2437 | |
| 2438 | switch (basetype) { |
| 2439 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2440 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2441 | |
| 2442 | /* 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] | 2443 | if (type_p->length) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2444 | ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2445 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length); |
| 2446 | LY_CHECK_RET(ret); |
| 2447 | if (!tpdfname) { |
| 2448 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, |
| 2449 | options, u, lys_compile_ext, ret, done); |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | if (tpdfname) { |
| 2454 | type_p->compiled = *type; |
| 2455 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2456 | } |
| 2457 | break; |
| 2458 | case LY_TYPE_BITS: |
| 2459 | /* RFC 7950 9.7 - bits */ |
| 2460 | bits = (struct lysc_type_bits*)(*type); |
| 2461 | if (type_p->bits) { |
| 2462 | ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options, |
| 2463 | base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2464 | (struct lysc_type_enum_item**)&bits->bits); |
| 2465 | LY_CHECK_RET(ret); |
| 2466 | } |
| 2467 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2468 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2469 | /* 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] | 2470 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2471 | 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] | 2472 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2473 | 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] | 2474 | free(*type); |
| 2475 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2476 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2477 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | if (tpdfname) { |
| 2481 | type_p->compiled = *type; |
| 2482 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2483 | } |
| 2484 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2485 | case LY_TYPE_DEC64: |
| 2486 | dec = (struct lysc_type_dec*)(*type); |
| 2487 | |
| 2488 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2489 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2490 | if (!type_p->fraction_digits) { |
| 2491 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2492 | 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] | 2493 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2494 | 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] | 2495 | free(*type); |
| 2496 | *type = NULL; |
| 2497 | } |
| 2498 | return LY_EVALID; |
| 2499 | } |
| 2500 | } else if (type_p->fraction_digits) { |
| 2501 | /* 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] | 2502 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2503 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2504 | "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] | 2505 | tpdfname); |
| 2506 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2507 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2508 | "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] | 2509 | free(*type); |
| 2510 | *type = NULL; |
| 2511 | } |
| 2512 | return LY_EVALID; |
| 2513 | } |
| 2514 | dec->fraction_digits = type_p->fraction_digits; |
| 2515 | |
| 2516 | /* RFC 7950 9.2.4 - range */ |
| 2517 | if (type_p->range) { |
| 2518 | ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2519 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range); |
| 2520 | LY_CHECK_RET(ret); |
| 2521 | if (!tpdfname) { |
| 2522 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, |
| 2523 | options, u, lys_compile_ext, ret, done); |
| 2524 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | if (tpdfname) { |
| 2528 | type_p->compiled = *type; |
| 2529 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2530 | } |
| 2531 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2532 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2533 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2534 | |
| 2535 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2536 | if (type_p->length) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2537 | ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2538 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length); |
| 2539 | LY_CHECK_RET(ret); |
| 2540 | if (!tpdfname) { |
| 2541 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, |
| 2542 | options, u, lys_compile_ext, ret, done); |
| 2543 | } |
| 2544 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2545 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2546 | } |
| 2547 | |
| 2548 | /* RFC 7950 9.4.5 - pattern */ |
| 2549 | if (type_p->patterns) { |
| 2550 | ret = lys_compile_type_patterns(ctx, type_p->patterns, options, |
| 2551 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns); |
| 2552 | LY_CHECK_RET(ret); |
| 2553 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2554 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2555 | } |
| 2556 | |
| 2557 | if (tpdfname) { |
| 2558 | type_p->compiled = *type; |
| 2559 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2560 | } |
| 2561 | break; |
| 2562 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2563 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2564 | |
| 2565 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2566 | if (type_p->enums) { |
| 2567 | ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options, |
| 2568 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums); |
| 2569 | LY_CHECK_RET(ret); |
| 2570 | } |
| 2571 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2572 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2573 | /* 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] | 2574 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2575 | 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] | 2576 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2577 | 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] | 2578 | free(*type); |
| 2579 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2580 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2581 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | if (tpdfname) { |
| 2585 | type_p->compiled = *type; |
| 2586 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2587 | } |
| 2588 | break; |
| 2589 | case LY_TYPE_INT8: |
| 2590 | case LY_TYPE_UINT8: |
| 2591 | case LY_TYPE_INT16: |
| 2592 | case LY_TYPE_UINT16: |
| 2593 | case LY_TYPE_INT32: |
| 2594 | case LY_TYPE_UINT32: |
| 2595 | case LY_TYPE_INT64: |
| 2596 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2597 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2598 | |
| 2599 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2600 | if (type_p->range) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2601 | ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2602 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range); |
| 2603 | LY_CHECK_RET(ret); |
| 2604 | if (!tpdfname) { |
| 2605 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, |
| 2606 | options, u, lys_compile_ext, ret, done); |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | if (tpdfname) { |
| 2611 | type_p->compiled = *type; |
| 2612 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2613 | } |
| 2614 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2615 | case LY_TYPE_IDENT: |
| 2616 | idref = (struct lysc_type_identityref*)(*type); |
| 2617 | |
| 2618 | /* RFC 7950 9.10.2 - base */ |
| 2619 | if (type_p->bases) { |
| 2620 | if (base) { |
| 2621 | /* only the directly derived identityrefs can contain base specification */ |
| 2622 | if (tpdfname) { |
| 2623 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2624 | "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] | 2625 | tpdfname); |
| 2626 | } else { |
| 2627 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2628 | "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] | 2629 | free(*type); |
| 2630 | *type = NULL; |
| 2631 | } |
| 2632 | return LY_EVALID; |
| 2633 | } |
| 2634 | ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases); |
| 2635 | LY_CHECK_RET(ret); |
| 2636 | } |
| 2637 | |
| 2638 | if (!base && !type_p->flags) { |
| 2639 | /* type derived from identityref built-in type must contain at least one base */ |
| 2640 | if (tpdfname) { |
| 2641 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2642 | } else { |
| 2643 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2644 | free(*type); |
| 2645 | *type = NULL; |
| 2646 | } |
| 2647 | return LY_EVALID; |
| 2648 | } |
| 2649 | |
| 2650 | if (tpdfname) { |
| 2651 | type_p->compiled = *type; |
| 2652 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2653 | } |
| 2654 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2655 | case LY_TYPE_LEAFREF: |
| 2656 | /* RFC 7950 9.9.3 - require-instance */ |
| 2657 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2658 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2659 | if (tpdfname) { |
| 2660 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2661 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2662 | } else { |
| 2663 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2664 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2665 | free(*type); |
| 2666 | *type = NULL; |
| 2667 | } |
| 2668 | return LY_EVALID; |
| 2669 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2670 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2671 | } else if (base) { |
| 2672 | /* inherit */ |
| 2673 | ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2674 | } else { |
| 2675 | /* default is true */ |
| 2676 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2677 | } |
| 2678 | if (type_p->path) { |
| 2679 | DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2680 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2681 | } else if (base) { |
| 2682 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2683 | ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2684 | } else if (tpdfname) { |
| 2685 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2686 | return LY_EVALID; |
| 2687 | } else { |
| 2688 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2689 | free(*type); |
| 2690 | *type = NULL; |
| 2691 | return LY_EVALID; |
| 2692 | } |
| 2693 | if (tpdfname) { |
| 2694 | type_p->compiled = *type; |
| 2695 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2696 | } |
| 2697 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2698 | case LY_TYPE_INST: |
| 2699 | /* RFC 7950 9.9.3 - require-instance */ |
| 2700 | if (type_p->flags & LYS_SET_REQINST) { |
| 2701 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2702 | } else { |
| 2703 | /* default is true */ |
| 2704 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2705 | } |
| 2706 | |
| 2707 | if (tpdfname) { |
| 2708 | type_p->compiled = *type; |
| 2709 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2710 | } |
| 2711 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2712 | case LY_TYPE_UNION: |
| 2713 | un = (struct lysc_type_union*)(*type); |
| 2714 | |
| 2715 | /* RFC 7950 7.4 - type */ |
| 2716 | if (type_p->types) { |
| 2717 | if (base) { |
| 2718 | /* only the directly derived union can contain types specification */ |
| 2719 | if (tpdfname) { |
| 2720 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2721 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2722 | tpdfname); |
| 2723 | } else { |
| 2724 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2725 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2726 | free(*type); |
| 2727 | *type = NULL; |
| 2728 | } |
| 2729 | return LY_EVALID; |
| 2730 | } |
| 2731 | /* compile the type */ |
| 2732 | additional = 0; |
| 2733 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 2734 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2735 | 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] | 2736 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2737 | /* add space for additional types from the union subtype */ |
| 2738 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 2739 | p = ly_realloc(((uint32_t*)(un->types) - 1), sizeof(uint32_t) + ((LY_ARRAY_SIZE(type_p->types) + additional + LY_ARRAY_SIZE(un_aux->types) - 1) * sizeof *(un->types))); |
| 2740 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2741 | un->types = (void*)((uint32_t*)(p) + 1); |
| 2742 | |
| 2743 | /* copy subtypes of the subtype union */ |
| 2744 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 2745 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2746 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2747 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2748 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2749 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 2750 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 2751 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 2752 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2753 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 2754 | /* TODO extensions */ |
| 2755 | |
| 2756 | } else { |
| 2757 | un->types[u + additional] = un_aux->types[v]; |
| 2758 | ++un_aux->types[v]->refcount; |
| 2759 | } |
| 2760 | ++additional; |
| 2761 | LY_ARRAY_INCREMENT(un->types); |
| 2762 | } |
| 2763 | /* compensate u increment in main loop */ |
| 2764 | --additional; |
| 2765 | |
| 2766 | /* free the replaced union subtype */ |
| 2767 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2768 | } else { |
| 2769 | LY_ARRAY_INCREMENT(un->types); |
| 2770 | } |
| 2771 | LY_CHECK_RET(ret); |
| 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | if (!base && !type_p->flags) { |
| 2776 | /* type derived from union built-in type must contain at least one type */ |
| 2777 | if (tpdfname) { |
| 2778 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2779 | } else { |
| 2780 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2781 | free(*type); |
| 2782 | *type = NULL; |
| 2783 | } |
| 2784 | return LY_EVALID; |
| 2785 | } |
| 2786 | |
| 2787 | if (tpdfname) { |
| 2788 | type_p->compiled = *type; |
| 2789 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2790 | } |
| 2791 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2792 | case LY_TYPE_BOOL: |
| 2793 | case LY_TYPE_EMPTY: |
| 2794 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2795 | break; |
| 2796 | } |
| 2797 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2798 | done: |
| 2799 | return ret; |
| 2800 | } |
| 2801 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2802 | /** |
| 2803 | * @brief Compile information about the leaf/leaf-list's type. |
| 2804 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2805 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2806 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2807 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2808 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2809 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2810 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2811 | * @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] | 2812 | * @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] | 2813 | * @return LY_ERR value. |
| 2814 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2815 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2816 | 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] | 2817 | 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] | 2818 | { |
| 2819 | LY_ERR ret = LY_SUCCESS; |
| 2820 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2821 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2822 | struct type_context { |
| 2823 | const struct lysp_tpdf *tpdf; |
| 2824 | struct lysp_node *node; |
| 2825 | struct lysp_module *mod; |
| 2826 | } *tctx, *tctx_prev = NULL; |
| 2827 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2828 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2829 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2830 | const char *dflt = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2831 | |
| 2832 | (*type) = NULL; |
| 2833 | |
| 2834 | tctx = calloc(1, sizeof *tctx); |
| 2835 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2836 | 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] | 2837 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2838 | ret == LY_SUCCESS; |
| 2839 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2840 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2841 | if (basetype) { |
| 2842 | break; |
| 2843 | } |
| 2844 | |
| 2845 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2846 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2847 | 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] | 2848 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2849 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2850 | if (units && !*units) { |
| 2851 | /* inherit units */ |
| 2852 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2853 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2854 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2855 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2856 | dflt = tctx->tpdf->dflt; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2857 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2858 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2859 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2860 | break; |
| 2861 | } |
| 2862 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2863 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2864 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2865 | * 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] | 2866 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2867 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2868 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2869 | dummyloops = 1; |
| 2870 | goto preparenext; |
| 2871 | } else { |
| 2872 | tctx = NULL; |
| 2873 | break; |
| 2874 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | /* store information for the following processing */ |
| 2878 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2879 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2880 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2881 | /* prepare next loop */ |
| 2882 | tctx_prev = tctx; |
| 2883 | tctx = calloc(1, sizeof *tctx); |
| 2884 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2885 | } |
| 2886 | free(tctx); |
| 2887 | |
| 2888 | /* allocate type according to the basetype */ |
| 2889 | switch (basetype) { |
| 2890 | case LY_TYPE_BINARY: |
| 2891 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2892 | break; |
| 2893 | case LY_TYPE_BITS: |
| 2894 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2895 | break; |
| 2896 | case LY_TYPE_BOOL: |
| 2897 | case LY_TYPE_EMPTY: |
| 2898 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2899 | break; |
| 2900 | case LY_TYPE_DEC64: |
| 2901 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2902 | break; |
| 2903 | case LY_TYPE_ENUM: |
| 2904 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2905 | break; |
| 2906 | case LY_TYPE_IDENT: |
| 2907 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2908 | break; |
| 2909 | case LY_TYPE_INST: |
| 2910 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2911 | break; |
| 2912 | case LY_TYPE_LEAFREF: |
| 2913 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2914 | break; |
| 2915 | case LY_TYPE_STRING: |
| 2916 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2917 | break; |
| 2918 | case LY_TYPE_UNION: |
| 2919 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2920 | break; |
| 2921 | case LY_TYPE_INT8: |
| 2922 | case LY_TYPE_UINT8: |
| 2923 | case LY_TYPE_INT16: |
| 2924 | case LY_TYPE_UINT16: |
| 2925 | case LY_TYPE_INT32: |
| 2926 | case LY_TYPE_UINT32: |
| 2927 | case LY_TYPE_INT64: |
| 2928 | case LY_TYPE_UINT64: |
| 2929 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2930 | break; |
| 2931 | case LY_TYPE_UNKNOWN: |
| 2932 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2933 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 2934 | ret = LY_EVALID; |
| 2935 | goto cleanup; |
| 2936 | } |
| 2937 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2938 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2939 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 2940 | ly_data_type2str[basetype]); |
| 2941 | free(*type); |
| 2942 | (*type) = NULL; |
| 2943 | ret = LY_EVALID; |
| 2944 | goto cleanup; |
| 2945 | } |
| 2946 | |
| 2947 | /* get restrictions from the referred typedefs */ |
| 2948 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 2949 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2950 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2951 | base = tctx->tpdf->type.compiled; |
| 2952 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2953 | } 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] | 2954 | /* no change, just use the type information from the base */ |
| 2955 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 2956 | ++base->refcount; |
| 2957 | continue; |
| 2958 | } |
| 2959 | |
| 2960 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2961 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 2962 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 2963 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 2964 | ret = LY_EVALID; |
| 2965 | goto cleanup; |
| 2966 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 2967 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2968 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 2969 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 2970 | ret = LY_EVALID; |
| 2971 | goto cleanup; |
| 2972 | } |
| 2973 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2974 | (*type)->basetype = basetype; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2975 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2976 | 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] | 2977 | basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL, |
| 2978 | basetype, options, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2979 | LY_CHECK_GOTO(ret, cleanup); |
| 2980 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2981 | } |
| 2982 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2983 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2984 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2985 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2986 | (*type)->basetype = basetype; |
| 2987 | ++(*type)->refcount; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2988 | 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] | 2989 | LY_CHECK_GOTO(ret, cleanup); |
| 2990 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2991 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 2992 | free(*type); |
| 2993 | (*type) = base; |
| 2994 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2995 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2996 | if (!(*type)->dflt) { |
| 2997 | DUP_STRING(ctx->ctx, dflt, (*type)->dflt); |
| 2998 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2999 | |
| 3000 | COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup); |
| 3001 | |
| 3002 | cleanup: |
| 3003 | ly_set_erase(&tpdf_chain, free); |
| 3004 | return ret; |
| 3005 | } |
| 3006 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3007 | 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] | 3008 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3009 | /** |
| 3010 | * @brief Compile parsed container node information. |
| 3011 | * @param[in] ctx Compile context |
| 3012 | * @param[in] node_p Parsed container node. |
| 3013 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3014 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3015 | * is enriched with the container-specific information. |
| 3016 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3017 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3018 | static LY_ERR |
| 3019 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3020 | { |
| 3021 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3022 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3023 | struct lysp_node *child_p; |
| 3024 | unsigned int u; |
| 3025 | LY_ERR ret = LY_SUCCESS; |
| 3026 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3027 | if (cont_p->presence) { |
| 3028 | cont->flags |= LYS_PRESENCE; |
| 3029 | } |
| 3030 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3031 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3032 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done); |
| 3036 | //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done); |
| 3037 | //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done); |
| 3038 | |
| 3039 | done: |
| 3040 | return ret; |
| 3041 | } |
| 3042 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3043 | /* |
| 3044 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3045 | * @param[in] ctx Compile context. |
| 3046 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3047 | * @param[in] type_p Parsed type to compile. |
| 3048 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3049 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3050 | * @return LY_ERR value. |
| 3051 | */ |
| 3052 | static LY_ERR |
| 3053 | 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) |
| 3054 | { |
| 3055 | unsigned int u, v; |
| 3056 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3057 | |
| 3058 | LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, options, &leaf->type, |
| 3059 | leaf->units ? NULL : &leaf->units)); |
| 3060 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3061 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
| 3062 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
| 3063 | DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3064 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3065 | } |
| 3066 | } else { |
| 3067 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
| 3068 | DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3069 | } |
| 3070 | } |
| 3071 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3072 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3073 | ly_set_add(&ctx->unres, leaf, 0); |
| 3074 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3075 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3076 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3077 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3078 | ly_set_add(&ctx->unres, leaf, 0); |
| 3079 | } |
| 3080 | } |
| 3081 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
| 3082 | if (leaf->flags & LYS_SET_DFLT) { |
| 3083 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).", |
| 3084 | leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt); |
| 3085 | return LY_EVALID; |
| 3086 | } |
| 3087 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3088 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3089 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3090 | return LY_EVALID; |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3095 | /* configuration data values must be unique - so check the default values */ |
| 3096 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3097 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3098 | if (!strcmp(llist->dflts[u], llist->dflts[v])) { |
| 3099 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3100 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]); |
| 3101 | return LY_EVALID; |
| 3102 | } |
| 3103 | } |
| 3104 | } |
| 3105 | } |
| 3106 | |
| 3107 | /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */ |
| 3108 | |
| 3109 | return LY_SUCCESS; |
| 3110 | } |
| 3111 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3112 | /** |
| 3113 | * @brief Compile parsed leaf node information. |
| 3114 | * @param[in] ctx Compile context |
| 3115 | * @param[in] node_p Parsed leaf node. |
| 3116 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3117 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3118 | * is enriched with the leaf-specific information. |
| 3119 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3120 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3121 | static LY_ERR |
| 3122 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3123 | { |
| 3124 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3125 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3126 | unsigned int u; |
| 3127 | LY_ERR ret = LY_SUCCESS; |
| 3128 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3129 | 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] | 3130 | if (leaf_p->units) { |
| 3131 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3132 | leaf->flags |= LYS_SET_UNITS; |
| 3133 | } |
| 3134 | if (leaf_p->dflt) { |
| 3135 | leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3136 | leaf->flags |= LYS_SET_DFLT; |
| 3137 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3138 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3139 | 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] | 3140 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3141 | done: |
| 3142 | return ret; |
| 3143 | } |
| 3144 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3145 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3146 | * @brief Compile parsed leaf-list node information. |
| 3147 | * @param[in] ctx Compile context |
| 3148 | * @param[in] node_p Parsed leaf-list node. |
| 3149 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3150 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3151 | * is enriched with the leaf-list-specific information. |
| 3152 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3153 | */ |
| 3154 | static LY_ERR |
| 3155 | lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3156 | { |
| 3157 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3158 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3159 | unsigned int u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3160 | LY_ERR ret = LY_SUCCESS; |
| 3161 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3162 | 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] | 3163 | if (llist_p->units) { |
| 3164 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3165 | llist->flags |= LYS_SET_UNITS; |
| 3166 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3167 | |
| 3168 | if (llist_p->dflts) { |
| 3169 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3170 | LY_ARRAY_FOR(llist_p->dflts, u) { |
| 3171 | DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]); |
| 3172 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3173 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3174 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3178 | if (llist->min) { |
| 3179 | llist->flags |= LYS_MAND_TRUE; |
| 3180 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3181 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3182 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3183 | 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] | 3184 | |
| 3185 | done: |
| 3186 | return ret; |
| 3187 | } |
| 3188 | |
| 3189 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3190 | * @brief Compile information about list's uniques. |
| 3191 | * @param[in] ctx Compile context. |
| 3192 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3193 | * @param[in] uniques Sized array list of unique statements. |
| 3194 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3195 | * @return LY_ERR value. |
| 3196 | */ |
| 3197 | static LY_ERR |
| 3198 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3199 | { |
| 3200 | LY_ERR ret = LY_SUCCESS; |
| 3201 | struct lysc_node_leaf **key, ***unique; |
| 3202 | const char *keystr, *delim; |
| 3203 | size_t len; |
| 3204 | unsigned int v; |
| 3205 | int config; |
| 3206 | |
| 3207 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3208 | config = -1; |
| 3209 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3210 | keystr = uniques[v]; |
| 3211 | while (keystr) { |
| 3212 | delim = strpbrk(keystr, " \t\n"); |
| 3213 | if (delim) { |
| 3214 | len = delim - keystr; |
| 3215 | while (isspace(*delim)) { |
| 3216 | ++delim; |
| 3217 | } |
| 3218 | } else { |
| 3219 | len = strlen(keystr); |
| 3220 | } |
| 3221 | |
| 3222 | /* unique node must be present */ |
| 3223 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
| 3224 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, (const struct lysc_node**)key); |
| 3225 | if (ret != LY_SUCCESS) { |
| 3226 | if (ret == LY_EDENIED) { |
| 3227 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3228 | "Unique's descendant-schema-nodeid \"%.*s\" refers to a %s node instead of a leaf.", |
| 3229 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3230 | } |
| 3231 | return LY_EVALID; |
| 3232 | } |
| 3233 | |
| 3234 | /* all referenced leafs must be of the same config type */ |
| 3235 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3236 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3237 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3238 | return LY_EVALID; |
| 3239 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3240 | config = 1; |
| 3241 | } else { /* LYS_CONFIG_R */ |
| 3242 | config = 0; |
| 3243 | } |
| 3244 | |
| 3245 | /* check status */ |
| 3246 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3247 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3248 | |
| 3249 | /* mark leaf as unique */ |
| 3250 | (*key)->flags |= LYS_UNIQUE; |
| 3251 | |
| 3252 | /* next unique value in line */ |
| 3253 | keystr = delim; |
| 3254 | } |
| 3255 | /* next unique definition */ |
| 3256 | } |
| 3257 | |
| 3258 | return LY_SUCCESS; |
| 3259 | } |
| 3260 | |
| 3261 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3262 | * @brief Compile parsed list node information. |
| 3263 | * @param[in] ctx Compile context |
| 3264 | * @param[in] node_p Parsed list node. |
| 3265 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3266 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3267 | * is enriched with the list-specific information. |
| 3268 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3269 | */ |
| 3270 | static LY_ERR |
| 3271 | lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3272 | { |
| 3273 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3274 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3275 | struct lysp_node *child_p; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3276 | struct lysc_node_leaf **key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3277 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3278 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3279 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3280 | LY_ERR ret = LY_SUCCESS; |
| 3281 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3282 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3283 | if (list->min) { |
| 3284 | list->flags |= LYS_MAND_TRUE; |
| 3285 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3286 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3287 | |
| 3288 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3289 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done); |
| 3293 | |
| 3294 | /* keys */ |
| 3295 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3296 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3297 | return LY_EVALID; |
| 3298 | } |
| 3299 | |
| 3300 | /* find all the keys (must be direct children) */ |
| 3301 | keystr = list_p->key; |
| 3302 | while (keystr) { |
| 3303 | delim = strpbrk(keystr, " \t\n"); |
| 3304 | if (delim) { |
| 3305 | len = delim - keystr; |
| 3306 | while (isspace(*delim)) { |
| 3307 | ++delim; |
| 3308 | } |
| 3309 | } else { |
| 3310 | len = strlen(keystr); |
| 3311 | } |
| 3312 | |
| 3313 | /* key node must be present */ |
| 3314 | LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM); |
| 3315 | *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 3316 | if (!(*key)) { |
| 3317 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3318 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3319 | return LY_EVALID; |
| 3320 | } |
| 3321 | /* keys must be unique */ |
| 3322 | for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) { |
| 3323 | if (*key == list->keys[u]) { |
| 3324 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3325 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3326 | return LY_EVALID; |
| 3327 | } |
| 3328 | } |
| 3329 | /* key must have the same config flag as the list itself */ |
| 3330 | if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) { |
| 3331 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3332 | return LY_EVALID; |
| 3333 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3334 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3335 | /* YANG 1.0 denies key to be of empty type */ |
| 3336 | if ((*key)->type->basetype == LY_TYPE_EMPTY) { |
| 3337 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3338 | "Key of a list can be of type \"empty\" only in YANG 1.1 modules."); |
| 3339 | return LY_EVALID; |
| 3340 | } |
| 3341 | } else { |
| 3342 | /* when and if-feature are illegal on list keys */ |
| 3343 | if ((*key)->when) { |
| 3344 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3345 | "List's key \"%s\" must not have any \"when\" statement.", (*key)->name); |
| 3346 | return LY_EVALID; |
| 3347 | } |
| 3348 | if ((*key)->iffeatures) { |
| 3349 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3350 | "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name); |
| 3351 | return LY_EVALID; |
| 3352 | } |
| 3353 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3354 | |
| 3355 | /* check status */ |
| 3356 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3357 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3358 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3359 | /* ignore default values of the key */ |
| 3360 | if ((*key)->dflt) { |
| 3361 | lydict_remove(ctx->ctx, (*key)->dflt); |
| 3362 | (*key)->dflt = NULL; |
| 3363 | } |
| 3364 | /* mark leaf as key */ |
| 3365 | (*key)->flags |= LYS_KEY; |
| 3366 | |
| 3367 | /* next key value */ |
| 3368 | keystr = delim; |
| 3369 | } |
| 3370 | |
| 3371 | /* uniques */ |
| 3372 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3373 | 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] | 3374 | } |
| 3375 | |
| 3376 | //COMPILE_ARRAY_GOTO(ctx, list_p->actions, list->actions, options, u, lys_compile_action, ret, done); |
| 3377 | //COMPILE_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, options, u, lys_compile_notif, ret, done); |
| 3378 | |
| 3379 | done: |
| 3380 | return ret; |
| 3381 | } |
| 3382 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3383 | /** |
| 3384 | * @brief Do some checks and set the default choice's case. |
| 3385 | * |
| 3386 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3387 | * |
| 3388 | * @param[in] ctx Compile context. |
| 3389 | * @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, |
| 3390 | * not the reference to the imported module. |
| 3391 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3392 | * @return LY_ERR value. |
| 3393 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3394 | static LY_ERR |
| 3395 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3396 | { |
| 3397 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3398 | const char *prefix = NULL, *name; |
| 3399 | size_t prefix_len = 0; |
| 3400 | |
| 3401 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3402 | name = strchr(dflt, ':'); |
| 3403 | if (name) { |
| 3404 | prefix = dflt; |
| 3405 | prefix_len = name - prefix; |
| 3406 | ++name; |
| 3407 | } else { |
| 3408 | name = dflt; |
| 3409 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3410 | 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] | 3411 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3412 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3413 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3414 | prefix_len, prefix); |
| 3415 | return LY_EVALID; |
| 3416 | } |
| 3417 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3418 | if (!ch->dflt) { |
| 3419 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3420 | "Default case \"%s\" not found.", dflt); |
| 3421 | return LY_EVALID; |
| 3422 | } |
| 3423 | /* no mandatory nodes directly under the default case */ |
| 3424 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3425 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3426 | break; |
| 3427 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3428 | if (iter->flags & LYS_MAND_TRUE) { |
| 3429 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3430 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3431 | return LY_EVALID; |
| 3432 | } |
| 3433 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3434 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3435 | return LY_SUCCESS; |
| 3436 | } |
| 3437 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3438 | static LY_ERR |
| 3439 | lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch) |
| 3440 | { |
| 3441 | struct lys_module *mod; |
| 3442 | const char *prefix = NULL, *name; |
| 3443 | size_t prefix_len = 0; |
| 3444 | struct lysc_node_case *cs; |
| 3445 | struct lysc_node *node; |
| 3446 | |
| 3447 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3448 | name = strchr(dflt, ':'); |
| 3449 | if (name) { |
| 3450 | prefix = dflt; |
| 3451 | prefix_len = name - prefix; |
| 3452 | ++name; |
| 3453 | } else { |
| 3454 | name = dflt; |
| 3455 | } |
| 3456 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3457 | if (prefix) { |
| 3458 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3459 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3460 | "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. " |
| 3461 | "The prefix does not match any imported module of the deviation module.", |
| 3462 | devnodeid, dflt); |
| 3463 | return LY_EVALID; |
| 3464 | } |
| 3465 | } else { |
| 3466 | mod = ctx->mod; |
| 3467 | } |
| 3468 | /* get the default case */ |
| 3469 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3470 | if (!cs) { |
| 3471 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3472 | "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.", |
| 3473 | devnodeid, dflt); |
| 3474 | return LY_EVALID; |
| 3475 | } |
| 3476 | |
| 3477 | /* check that there is no mandatory node */ |
| 3478 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3479 | if (node->parent != (struct lysc_node*)cs) { |
| 3480 | break; |
| 3481 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3482 | if (node->flags & LYS_MAND_TRUE) { |
| 3483 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3484 | "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - " |
| 3485 | "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name); |
| 3486 | return LY_EVALID; |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | /* set the default case in choice */ |
| 3491 | ch->dflt = cs; |
| 3492 | cs->flags |= LYS_SET_DFLT; |
| 3493 | |
| 3494 | return LY_SUCCESS; |
| 3495 | } |
| 3496 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3497 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3498 | * @brief Compile parsed choice node information. |
| 3499 | * @param[in] ctx Compile context |
| 3500 | * @param[in] node_p Parsed choice node. |
| 3501 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3502 | * @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] | 3503 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3504 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3505 | */ |
| 3506 | static LY_ERR |
| 3507 | lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3508 | { |
| 3509 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 3510 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 3511 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3512 | struct lys_module; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3513 | LY_ERR ret = LY_SUCCESS; |
| 3514 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3515 | LY_LIST_FOR(ch_p->child, child_p) { |
| 3516 | if (child_p->nodetype == LYS_CASE) { |
| 3517 | 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] | 3518 | 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] | 3519 | } |
| 3520 | } else { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3521 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3526 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3527 | 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] | 3528 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3529 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3530 | return ret; |
| 3531 | } |
| 3532 | |
| 3533 | /** |
| 3534 | * @brief Compile parsed anydata or anyxml node information. |
| 3535 | * @param[in] ctx Compile context |
| 3536 | * @param[in] node_p Parsed anydata or anyxml node. |
| 3537 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3538 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3539 | * is enriched with the any-specific information. |
| 3540 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3541 | */ |
| 3542 | static LY_ERR |
| 3543 | lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3544 | { |
| 3545 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 3546 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 3547 | unsigned int u; |
| 3548 | LY_ERR ret = LY_SUCCESS; |
| 3549 | |
| 3550 | COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done); |
| 3551 | |
| 3552 | if (any->flags & LYS_CONFIG_W) { |
| 3553 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
| 3554 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML)); |
| 3555 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3556 | done: |
| 3557 | return ret; |
| 3558 | } |
| 3559 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3560 | /** |
| 3561 | * @brief Compile status information of the given node. |
| 3562 | * |
| 3563 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3564 | * has the status correctly set during the compilation. |
| 3565 | * |
| 3566 | * @param[in] ctx Compile context |
| 3567 | * @param[in,out] node Compiled node which status is supposed to be resolved. If the status was set explicitely on the node, it is already set in the |
| 3568 | * flags value and we just check the compatibility with the parent's status value. |
| 3569 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3570 | * @return LY_ERR value. |
| 3571 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3572 | static LY_ERR |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3573 | lys_compile_status(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t parent_flags) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3574 | { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3575 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3576 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3577 | if (!(node->flags & LYS_STATUS_MASK)) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3578 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3579 | if ((parent_flags & 0x3) != 0x3) { |
| 3580 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3581 | * combination of bits (0x3) which marks the uses_status value */ |
| 3582 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3583 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3584 | } |
| 3585 | node->flags |= parent_flags & LYS_STATUS_MASK; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3586 | } else { |
| 3587 | node->flags |= LYS_STATUS_CURR; |
| 3588 | } |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3589 | } else if (parent_flags & LYS_STATUS_MASK) { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3590 | /* check status compatibility with the parent */ |
| 3591 | if ((parent_flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) { |
| 3592 | if (node->flags & LYS_STATUS_CURR) { |
| 3593 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3594 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3595 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3596 | } else { /* LYS_STATUS_DEPRC */ |
| 3597 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3598 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3599 | } |
| 3600 | return LY_EVALID; |
| 3601 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3602 | } |
| 3603 | return LY_SUCCESS; |
| 3604 | } |
| 3605 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3606 | /** |
| 3607 | * @brief Check uniqness of the node/action/notification name. |
| 3608 | * |
| 3609 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3610 | * structures, but they share the namespace so we need to check their name collisions. |
| 3611 | * |
| 3612 | * @param[in] ctx Compile context. |
| 3613 | * @param[in] children List (linked list) of data nodes to go through. |
| 3614 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3615 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3616 | * @param[in] name Name of the item to find in the given lists. |
| 3617 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3618 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3619 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3620 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3621 | static LY_ERR |
| 3622 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3623 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3624 | const char *name, void *exclude) |
| 3625 | { |
| 3626 | const struct lysc_node *iter; |
| 3627 | unsigned int u; |
| 3628 | |
| 3629 | LY_LIST_FOR(children, iter) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3630 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3631 | goto error; |
| 3632 | } |
| 3633 | } |
| 3634 | LY_ARRAY_FOR(actions, u) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3635 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3636 | goto error; |
| 3637 | } |
| 3638 | } |
| 3639 | LY_ARRAY_FOR(notifs, u) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3640 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3641 | goto error; |
| 3642 | } |
| 3643 | } |
| 3644 | return LY_SUCCESS; |
| 3645 | error: |
| 3646 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition"); |
| 3647 | return LY_EEXIST; |
| 3648 | } |
| 3649 | |
| 3650 | /** |
| 3651 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 3652 | * |
| 3653 | * @param[in] ctx Compile context |
| 3654 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 3655 | * the choice itself is expected instead of a specific case node. |
| 3656 | * @param[in] node Schema node to connect into the list. |
| 3657 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 3658 | */ |
| 3659 | static LY_ERR |
| 3660 | lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node) |
| 3661 | { |
| 3662 | struct lysc_node **children; |
| 3663 | |
| 3664 | if (node->nodetype == LYS_CASE) { |
| 3665 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 3666 | } else { |
| 3667 | children = lysc_node_children_p(parent); |
| 3668 | } |
| 3669 | if (children) { |
| 3670 | if (!(*children)) { |
| 3671 | /* first child */ |
| 3672 | *children = node; |
| 3673 | } else if (*children != node) { |
| 3674 | /* by the condition in previous branch we cover the choice/case children |
| 3675 | * - the children list is shared by the choice and the the first case, in addition |
| 3676 | * the first child of each case must be referenced from the case node. So the node is |
| 3677 | * actually always already inserted in case it is the first children - so here such |
| 3678 | * a situation actually corresponds to the first branch */ |
| 3679 | /* insert at the end of the parent's children list */ |
| 3680 | (*children)->prev->next = node; |
| 3681 | node->prev = (*children)->prev; |
| 3682 | (*children)->prev = node; |
| 3683 | |
| 3684 | /* check the name uniqueness */ |
| 3685 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 3686 | lysc_node_notifs(parent), node->name, node)) { |
| 3687 | return LY_EEXIST; |
| 3688 | } |
| 3689 | } |
| 3690 | } |
| 3691 | return LY_SUCCESS; |
| 3692 | } |
| 3693 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3694 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3695 | * @brief Get the XPath context node for the given schema node. |
| 3696 | * @param[in] start The schema node where the XPath expression appears. |
| 3697 | * @return The context node to evaluate XPath expression in given schema node. |
| 3698 | * @return NULL in case the context node is the root node. |
| 3699 | */ |
| 3700 | static struct lysc_node * |
| 3701 | lysc_xpath_context(struct lysc_node *start) |
| 3702 | { |
| 3703 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 3704 | start = start->parent); |
| 3705 | return start; |
| 3706 | } |
| 3707 | |
| 3708 | /** |
| 3709 | * @brief Prepare the case structure in choice node for the new data node. |
| 3710 | * |
| 3711 | * 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 |
| 3712 | * created in the choice when the first child was processed. |
| 3713 | * |
| 3714 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3715 | * @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, |
| 3716 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3717 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3718 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 3719 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 3720 | * @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, |
| 3721 | * 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] | 3722 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3723 | static struct lysc_node_case* |
| 3724 | lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child) |
| 3725 | { |
| 3726 | struct lysc_node *iter; |
| 3727 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 3728 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3729 | unsigned int u; |
| 3730 | LY_ERR ret; |
| 3731 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3732 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3733 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3734 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3735 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 3736 | return NULL; \ |
| 3737 | } \ |
| 3738 | } |
| 3739 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3740 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 3741 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3742 | |
| 3743 | /* we have to add an implicit case node into the parent choice */ |
| 3744 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 3745 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 3746 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3747 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3748 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 3749 | /* the case is already present since the child is not its first children */ |
| 3750 | return (struct lysc_node_case*)ch->cases->prev; |
| 3751 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3752 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3753 | |
| 3754 | /* explicit parent case is not present (this is its first child) */ |
| 3755 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 3756 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 3757 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 3758 | cs->sp = node_p; |
| 3759 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3760 | /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */ |
| 3761 | LY_CHECK_RET(lys_compile_status(ctx, (struct lysc_node*)cs, ch->flags), NULL); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 3762 | |
| 3763 | if (node_p->when) { |
| 3764 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
| 3765 | ret = lys_compile_when(ctx, node_p->when, options, when); |
| 3766 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3767 | (*when)->context = lysc_xpath_context(ch->parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 3768 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3769 | 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] | 3770 | } else { |
| 3771 | LOGINT(ctx->ctx); |
| 3772 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3773 | } |
| 3774 | cs->module = ctx->mod; |
| 3775 | cs->prev = (struct lysc_node*)cs; |
| 3776 | cs->nodetype = LYS_CASE; |
| 3777 | lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs); |
| 3778 | cs->parent = (struct lysc_node*)ch; |
| 3779 | cs->child = child; |
| 3780 | |
| 3781 | return cs; |
| 3782 | error: |
| 3783 | return NULL; |
| 3784 | |
| 3785 | #undef UNIQUE_CHECK |
| 3786 | } |
| 3787 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3788 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3789 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3790 | * |
| 3791 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3792 | * @param[in] node Target node where the config is supposed to be changed. |
| 3793 | * @param[in] config_flag Node's config flag to be applied to the @p node. |
| 3794 | * @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] | 3795 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 3796 | * 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] | 3797 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 3798 | * @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] | 3799 | * @return LY_ERR value. |
| 3800 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3801 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3802 | lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag, |
| 3803 | const char *nodeid, int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3804 | { |
| 3805 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3806 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3807 | |
| 3808 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 3809 | /* nothing to do */ |
| 3810 | return LY_SUCCESS; |
| 3811 | } |
| 3812 | |
| 3813 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3814 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3815 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 3816 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3817 | "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.", |
| 3818 | refine_flag ? "refine" : "deviation", nodeid); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3819 | return LY_EVALID; |
| 3820 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3821 | node->flags |= LYS_SET_CONFIG; |
| 3822 | } else { |
| 3823 | if (node->flags & LYS_SET_CONFIG) { |
| 3824 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 3825 | /* setting config flags, but have node with explicit config true */ |
| 3826 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3827 | "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.", |
| 3828 | refine_flag ? "refine" : "deviation", nodeid); |
| 3829 | return LY_EVALID; |
| 3830 | } |
| 3831 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 3832 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 3833 | return LY_SUCCESS; |
| 3834 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3835 | } |
| 3836 | node->flags &= ~LYS_CONFIG_MASK; |
| 3837 | node->flags |= config; |
| 3838 | |
| 3839 | /* inherit the change into the children */ |
| 3840 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node), child) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3841 | 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] | 3842 | } |
| 3843 | |
| 3844 | /* TODO actions and notifications */ |
| 3845 | |
| 3846 | return LY_SUCCESS; |
| 3847 | } |
| 3848 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3849 | /** |
| 3850 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 3851 | * |
| 3852 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 3853 | * the flag to such parents from a mandatory children. |
| 3854 | * |
| 3855 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 3856 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 3857 | * (mandatory children was removed). |
| 3858 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3859 | void |
| 3860 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 3861 | { |
| 3862 | struct lysc_node *iter; |
| 3863 | |
| 3864 | if (add) { /* set flag */ |
| 3865 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 3866 | parent = parent->parent) { |
| 3867 | parent->flags |= LYS_MAND_TRUE; |
| 3868 | } |
| 3869 | } else { /* unset flag */ |
| 3870 | for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) { |
| 3871 | for (iter = (struct lysc_node*)lysc_node_children(parent); iter; iter = iter->next) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 3872 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3873 | /* there is another mandatory node */ |
| 3874 | return; |
| 3875 | } |
| 3876 | } |
| 3877 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 3878 | parent->flags &= ~LYS_MAND_TRUE; |
| 3879 | } |
| 3880 | } |
| 3881 | } |
| 3882 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3883 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 3884 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 3885 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 3886 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 3887 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 3888 | */ |
| 3889 | static void |
| 3890 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 3891 | { |
| 3892 | unsigned int v; |
| 3893 | size_t len; |
| 3894 | |
| 3895 | len = strlen(aug_p->nodeid); |
| 3896 | LY_ARRAY_FOR(result, v) { |
| 3897 | if (strlen(result[v]->nodeid) <= len) { |
| 3898 | continue; |
| 3899 | } |
| 3900 | if (v < LY_ARRAY_SIZE(result)) { |
| 3901 | /* move the rest of array */ |
| 3902 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 3903 | break; |
| 3904 | } |
| 3905 | } |
| 3906 | result[v] = aug_p; |
| 3907 | LY_ARRAY_INCREMENT(result); |
| 3908 | } |
| 3909 | |
| 3910 | /** |
| 3911 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 3912 | * |
| 3913 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 3914 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 3915 | * |
| 3916 | * @param[in] ctx Compile context. |
| 3917 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 3918 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 3919 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 3920 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 3921 | * @return LY_ERR value. |
| 3922 | */ |
| 3923 | LY_ERR |
| 3924 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 3925 | { |
| 3926 | struct lysp_augment **result = NULL; |
| 3927 | unsigned int u, v; |
| 3928 | size_t count = 0; |
| 3929 | |
| 3930 | assert(augments); |
| 3931 | |
| 3932 | /* get count of the augments in module and all its submodules */ |
| 3933 | if (aug_p) { |
| 3934 | count += LY_ARRAY_SIZE(aug_p); |
| 3935 | } |
| 3936 | LY_ARRAY_FOR(inc_p, u) { |
| 3937 | if (inc_p[u].submodule->augments) { |
| 3938 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 3939 | } |
| 3940 | } |
| 3941 | |
| 3942 | if (!count) { |
| 3943 | *augments = NULL; |
| 3944 | return LY_SUCCESS; |
| 3945 | } |
| 3946 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 3947 | |
| 3948 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 3949 | * together, so there can be even /z/y betwwen them. */ |
| 3950 | LY_ARRAY_FOR(aug_p, u) { |
| 3951 | lys_compile_augment_sort_(&aug_p[u], result); |
| 3952 | } |
| 3953 | LY_ARRAY_FOR(inc_p, u) { |
| 3954 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 3955 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | *augments = result; |
| 3960 | return LY_SUCCESS; |
| 3961 | } |
| 3962 | |
| 3963 | /** |
| 3964 | * @brief Compile the parsed augment connecting it into its target. |
| 3965 | * |
| 3966 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 3967 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 3968 | * are already implemented and compiled. |
| 3969 | * |
| 3970 | * @param[in] ctx Compile context. |
| 3971 | * @param[in] aug_p Parsed augment to compile. |
| 3972 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3973 | * @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 |
| 3974 | * children in case of the augmenting uses data. |
| 3975 | * @return LY_SUCCESS on success. |
| 3976 | * @return LY_EVALID on failure. |
| 3977 | */ |
| 3978 | LY_ERR |
| 3979 | lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent) |
| 3980 | { |
| 3981 | LY_ERR ret = LY_SUCCESS; |
| 3982 | struct lysp_node *node_p, *case_node_p; |
| 3983 | struct lysc_node *target; /* target target of the augment */ |
| 3984 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 3985 | struct lysc_when **when, *when_shared; |
| 3986 | int allow_mandatory = 0; |
| 3987 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3988 | 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] | 3989 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
| 3990 | 1, (const struct lysc_node**)&target); |
| 3991 | if (ret != LY_SUCCESS) { |
| 3992 | if (ret == LY_EDENIED) { |
| 3993 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3994 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 3995 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 3996 | } |
| 3997 | return LY_EVALID; |
| 3998 | } |
| 3999 | |
| 4000 | /* check for mandatory nodes |
| 4001 | * - new cases augmenting some choice can have mandatory nodes |
| 4002 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4003 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4004 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4005 | allow_mandatory = 1; |
| 4006 | } |
| 4007 | |
| 4008 | when_shared = NULL; |
| 4009 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4010 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4011 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4012 | && !((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] | 4013 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4014 | && !(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] | 4015 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4016 | "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".", |
| 4017 | aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
| 4018 | return LY_EVALID; |
| 4019 | } |
| 4020 | |
| 4021 | /* compile the children */ |
| 4022 | if (node_p->nodetype != LYS_CASE) { |
| 4023 | LY_CHECK_RET(lys_compile_node(ctx, node_p, options, target, 0)); |
| 4024 | } else { |
| 4025 | LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) { |
| 4026 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options, target, 0)); |
| 4027 | } |
| 4028 | } |
| 4029 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4030 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4031 | * 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] | 4032 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4033 | /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */ |
| 4034 | for (node = (struct lysc_node*)lysc_node_children(target); node->next && node->next->parent == node->parent; node = node->next); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4035 | } else if (target->nodetype == LYS_CHOICE) { |
| 4036 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4037 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4038 | } else { |
| 4039 | /* the compiled node is the last child of the target */ |
| 4040 | node = lysc_node_children(target)->prev; |
| 4041 | } |
| 4042 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4043 | 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] | 4044 | node->flags &= ~LYS_MAND_TRUE; |
| 4045 | lys_compile_mandatory_parents(target, 0); |
| 4046 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4047 | "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.", |
| 4048 | aug_p->nodeid, node->name); |
| 4049 | return LY_EVALID; |
| 4050 | } |
| 4051 | |
| 4052 | /* pass augment's when to all the children */ |
| 4053 | if (aug_p->when) { |
| 4054 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4055 | if (!when_shared) { |
| 4056 | ret = lys_compile_when(ctx, aug_p->when, options, when); |
| 4057 | LY_CHECK_GOTO(ret, error); |
| 4058 | (*when)->context = lysc_xpath_context(target); |
| 4059 | when_shared = *when; |
| 4060 | } else { |
| 4061 | ++when_shared->refcount; |
| 4062 | (*when) = when_shared; |
| 4063 | } |
| 4064 | } |
| 4065 | } |
| 4066 | /* TODO actions, notifications */ |
| 4067 | |
| 4068 | error: |
| 4069 | return ret; |
| 4070 | } |
| 4071 | |
| 4072 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4073 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4074 | * |
| 4075 | * @param[in] ctx Compile context. |
| 4076 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4077 | * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node. |
| 4078 | * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging). |
| 4079 | * @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] | 4080 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4081 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4082 | * 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] | 4083 | * @return LY_ERR value. |
| 4084 | */ |
| 4085 | static LY_ERR |
| 4086 | lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag) |
| 4087 | { |
| 4088 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4089 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4090 | "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.", |
| 4091 | refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype)); |
| 4092 | return LY_EVALID; |
| 4093 | } |
| 4094 | |
| 4095 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4096 | /* check if node has default value */ |
| 4097 | if (node->nodetype & LYS_LEAF) { |
| 4098 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4099 | if (refine_flag) { |
| 4100 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4101 | "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid); |
| 4102 | return LY_EVALID; |
| 4103 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4104 | } else { |
| 4105 | /* remove the default value taken from the leaf's type */ |
| 4106 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt); |
| 4107 | ((struct lysc_node_leaf*)node)->dflt = NULL; |
| 4108 | } |
| 4109 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4110 | if (refine_flag) { |
| 4111 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4112 | "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid); |
| 4113 | return LY_EVALID; |
| 4114 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4115 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4116 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4117 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4118 | "Invalid refine of mandatory in \"%s\" under the default case.", nodeid); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4119 | return LY_EVALID; |
| 4120 | } |
| 4121 | |
| 4122 | node->flags &= ~LYS_MAND_FALSE; |
| 4123 | node->flags |= LYS_MAND_TRUE; |
| 4124 | lys_compile_mandatory_parents(node->parent, 1); |
| 4125 | } else { |
| 4126 | /* make mandatory false */ |
| 4127 | node->flags &= ~LYS_MAND_TRUE; |
| 4128 | node->flags |= LYS_MAND_FALSE; |
| 4129 | lys_compile_mandatory_parents(node->parent, 0); |
| 4130 | if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) { |
| 4131 | /* get the type's default value if any */ |
| 4132 | DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt); |
| 4133 | } |
| 4134 | } |
| 4135 | return LY_SUCCESS; |
| 4136 | } |
| 4137 | |
| 4138 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4139 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4140 | * If present, also apply uses's modificators. |
| 4141 | * |
| 4142 | * @param[in] ctx Compile context |
| 4143 | * @param[in] uses_p Parsed uses schema node. |
| 4144 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 4145 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4146 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4147 | * the compile context. |
| 4148 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4149 | */ |
| 4150 | static LY_ERR |
| 4151 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent) |
| 4152 | { |
| 4153 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4154 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4155 | /* 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] | 4156 | struct lysc_node_container context_node_fake = |
| 4157 | {.nodetype = LYS_CONTAINER, |
| 4158 | .module = ctx->mod, |
| 4159 | .flags = parent ? parent->flags : 0, |
| 4160 | .child = NULL, .next = NULL, |
| 4161 | .prev = (struct lysc_node*)&context_node_fake}; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4162 | const struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4163 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4164 | int found; |
| 4165 | const char *id, *name, *prefix; |
| 4166 | size_t prefix_len, name_len; |
| 4167 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4168 | struct lysp_refine *rfn; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4169 | LY_ERR ret = LY_EVALID; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4170 | uint32_t min, max; |
| 4171 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4172 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4173 | struct lysp_augment **augments = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4174 | |
| 4175 | /* search for the grouping definition */ |
| 4176 | found = 0; |
| 4177 | id = uses_p->name; |
| 4178 | lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len); |
| 4179 | if (prefix) { |
| 4180 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4181 | if (!mod) { |
| 4182 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4183 | "Invalid prefix used for grouping reference (%s).", uses_p->name); |
| 4184 | return LY_EVALID; |
| 4185 | } |
| 4186 | } else { |
| 4187 | mod = ctx->mod_def; |
| 4188 | } |
| 4189 | if (mod == ctx->mod_def) { |
| 4190 | for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) { |
| 4191 | grp = lysp_node_groupings(node_p); |
| 4192 | LY_ARRAY_FOR(grp, u) { |
| 4193 | if (!strcmp(grp[u].name, name)) { |
| 4194 | grp = &grp[u]; |
| 4195 | found = 1; |
| 4196 | break; |
| 4197 | } |
| 4198 | } |
| 4199 | } |
| 4200 | } |
| 4201 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4202 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4203 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4204 | if (grp) { |
| 4205 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4206 | if (!strcmp(grp[u].name, name)) { |
| 4207 | grp = &grp[u]; |
| 4208 | found = 1; |
| 4209 | } |
| 4210 | } |
| 4211 | } |
| 4212 | if (!found && mod->parsed->includes) { |
| 4213 | /* ... and all the submodules */ |
| 4214 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4215 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4216 | if (grp) { |
| 4217 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4218 | if (!strcmp(grp[v].name, name)) { |
| 4219 | grp = &grp[v]; |
| 4220 | found = 1; |
| 4221 | } |
| 4222 | } |
| 4223 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4224 | } |
| 4225 | } |
| 4226 | } |
| 4227 | if (!found) { |
| 4228 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4229 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4230 | return LY_EVALID; |
| 4231 | } |
| 4232 | |
| 4233 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4234 | grp_stack_count = ctx->groupings.count; |
| 4235 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4236 | if (grp_stack_count == ctx->groupings.count) { |
| 4237 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4238 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4239 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4240 | return LY_EVALID; |
| 4241 | } |
| 4242 | |
| 4243 | /* switch context's mod_def */ |
| 4244 | mod_old = ctx->mod_def; |
| 4245 | ctx->mod_def = mod; |
| 4246 | |
| 4247 | /* check status */ |
| 4248 | LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), error); |
| 4249 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4250 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4251 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
| 4252 | LY_CHECK_GOTO(lys_compile_node(ctx, node_p, options, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), error); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4253 | child = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4254 | |
| 4255 | /* some preparation for applying refines */ |
| 4256 | if (grp->data == node_p) { |
| 4257 | /* remember the first child */ |
| 4258 | context_node_fake.child = child; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4259 | } |
| 4260 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4261 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4262 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4263 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4264 | |
| 4265 | /* pass uses's when to all the children */ |
| 4266 | if (uses_p->when) { |
| 4267 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, error); |
| 4268 | if (!when_shared) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4269 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, options, when), error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4270 | (*when)->context = lysc_xpath_context(parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4271 | when_shared = *when; |
| 4272 | } else { |
| 4273 | ++when_shared->refcount; |
| 4274 | (*when) = when_shared; |
| 4275 | } |
| 4276 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4277 | } |
| 4278 | if (context_node_fake.child) { |
| 4279 | child = context_node_fake.child->prev; |
| 4280 | context_node_fake.child->prev = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4281 | } |
| 4282 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4283 | /* sort and apply augments */ |
| 4284 | LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), error); |
| 4285 | LY_ARRAY_FOR(augments, u) { |
| 4286 | LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], options, (struct lysc_node*)&context_node_fake), error); |
| 4287 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4288 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4289 | /* reload previous context's mod_def */ |
| 4290 | ctx->mod_def = mod_old; |
| 4291 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4292 | /* apply refine */ |
| 4293 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4294 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, ctx->mod, |
| 4295 | 0, 0, (const struct lysc_node**)&node), |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4296 | error); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4297 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4298 | |
| 4299 | /* default value */ |
| 4300 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4301 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4302 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4303 | "Invalid refine of default in \"%s\" - %s cannot hold %d default values.", |
| 4304 | rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
| 4305 | goto error; |
| 4306 | } |
| 4307 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4308 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4309 | "Invalid refine of default in \"%s\" - %s cannot hold default value(s).", |
| 4310 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
| 4311 | goto error; |
| 4312 | } |
| 4313 | if (node->nodetype == LYS_LEAF) { |
| 4314 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt); |
| 4315 | 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] | 4316 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4317 | /* TODO check the default value according to type */ |
| 4318 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4319 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4320 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4321 | "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
| 4322 | goto error; |
| 4323 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4324 | LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) { |
| 4325 | lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]); |
| 4326 | } |
| 4327 | LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4328 | ((struct lysc_node_leaflist*)node)->dflts = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4329 | LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, error); |
| 4330 | LY_ARRAY_FOR(rfn->dflts, u) { |
| 4331 | LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts); |
| 4332 | DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]); |
| 4333 | } |
| 4334 | /* TODO check the default values according to type */ |
| 4335 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4336 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4337 | /* unset LYS_SET_DFLT from the current default case */ |
| 4338 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4339 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4340 | LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), error); |
| 4341 | } |
| 4342 | } |
| 4343 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4344 | /* description */ |
| 4345 | if (rfn->dsc) { |
| 4346 | FREE_STRING(ctx->ctx, node->dsc); |
| 4347 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4348 | } |
| 4349 | |
| 4350 | /* reference */ |
| 4351 | if (rfn->ref) { |
| 4352 | FREE_STRING(ctx->ctx, node->ref); |
| 4353 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4354 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4355 | |
| 4356 | /* config */ |
| 4357 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4358 | LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), error); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | /* mandatory */ |
| 4362 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4363 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), error); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4364 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4365 | |
| 4366 | /* presence */ |
| 4367 | if (rfn->presence) { |
| 4368 | if (node->nodetype != LYS_CONTAINER) { |
| 4369 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4370 | "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.", |
| 4371 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
| 4372 | goto error; |
| 4373 | } |
| 4374 | node->flags |= LYS_PRESENCE; |
| 4375 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4376 | |
| 4377 | /* must */ |
| 4378 | if (rfn->musts) { |
| 4379 | switch (node->nodetype) { |
| 4380 | case LYS_LEAF: |
| 4381 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, options, u, lys_compile_must, ret, error); |
| 4382 | break; |
| 4383 | case LYS_LEAFLIST: |
| 4384 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, options, u, lys_compile_must, ret, error); |
| 4385 | break; |
| 4386 | case LYS_LIST: |
| 4387 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, options, u, lys_compile_must, ret, error); |
| 4388 | break; |
| 4389 | case LYS_CONTAINER: |
| 4390 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, options, u, lys_compile_must, ret, error); |
| 4391 | break; |
| 4392 | case LYS_ANYXML: |
| 4393 | case LYS_ANYDATA: |
| 4394 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, options, u, lys_compile_must, ret, error); |
| 4395 | break; |
| 4396 | default: |
| 4397 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4398 | "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.", |
| 4399 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
| 4400 | goto error; |
| 4401 | } |
| 4402 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4403 | |
| 4404 | /* min/max-elements */ |
| 4405 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4406 | switch (node->nodetype) { |
| 4407 | case LYS_LEAFLIST: |
| 4408 | if (rfn->flags & LYS_SET_MAX) { |
| 4409 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4410 | } |
| 4411 | if (rfn->flags & LYS_SET_MIN) { |
| 4412 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4413 | if (rfn->min) { |
| 4414 | node->flags |= LYS_MAND_TRUE; |
| 4415 | lys_compile_mandatory_parents(node->parent, 1); |
| 4416 | } else { |
| 4417 | node->flags &= ~LYS_MAND_TRUE; |
| 4418 | lys_compile_mandatory_parents(node->parent, 0); |
| 4419 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4420 | } |
| 4421 | break; |
| 4422 | case LYS_LIST: |
| 4423 | if (rfn->flags & LYS_SET_MAX) { |
| 4424 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4425 | } |
| 4426 | if (rfn->flags & LYS_SET_MIN) { |
| 4427 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4428 | if (rfn->min) { |
| 4429 | node->flags |= LYS_MAND_TRUE; |
| 4430 | lys_compile_mandatory_parents(node->parent, 1); |
| 4431 | } else { |
| 4432 | node->flags &= ~LYS_MAND_TRUE; |
| 4433 | lys_compile_mandatory_parents(node->parent, 0); |
| 4434 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4435 | } |
| 4436 | break; |
| 4437 | default: |
| 4438 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4439 | "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.", |
| 4440 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype)); |
| 4441 | goto error; |
| 4442 | } |
| 4443 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4444 | |
| 4445 | /* if-feature */ |
| 4446 | if (rfn->iffeatures) { |
| 4447 | /* any node in compiled tree can get additional if-feature, so do not check nodetype */ |
| 4448 | COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error); |
| 4449 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4450 | } |
| 4451 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 4452 | if (context_node_fake.child) { |
| 4453 | context_node_fake.child->prev = child; |
| 4454 | } |
| 4455 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4456 | child->parent = parent; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4457 | } |
| 4458 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4459 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 4460 | for (u = 0; u < refined.count; ++u) { |
| 4461 | node = (struct lysc_node*)refined.objs[u]; |
| 4462 | rfn = &uses_p->refines[u]; |
| 4463 | |
| 4464 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 4465 | if ((node->flags & LYS_MAND_TRUE) && |
| 4466 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 4467 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 4468 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4469 | "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid); |
| 4470 | goto error; |
| 4471 | } |
| 4472 | |
| 4473 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4474 | if (node->nodetype == LYS_LIST) { |
| 4475 | min = ((struct lysc_node_list*)node)->min; |
| 4476 | max = ((struct lysc_node_list*)node)->max; |
| 4477 | } else { |
| 4478 | min = ((struct lysc_node_leaflist*)node)->min; |
| 4479 | max = ((struct lysc_node_leaflist*)node)->max; |
| 4480 | } |
| 4481 | if (min > max) { |
| 4482 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4483 | "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".", |
| 4484 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid); |
| 4485 | goto error; |
| 4486 | } |
| 4487 | } |
| 4488 | } |
| 4489 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4490 | ret = LY_SUCCESS; |
| 4491 | error: |
| 4492 | /* reload previous context's mod_def */ |
| 4493 | ctx->mod_def = mod_old; |
| 4494 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 4495 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 4496 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4497 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4498 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4499 | |
| 4500 | return ret; |
| 4501 | } |
| 4502 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4503 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4504 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4505 | * @brief Compile parsed schema node information. |
| 4506 | * @param[in] ctx Compile context |
| 4507 | * @param[in] node_p Parsed schema node. |
| 4508 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 4509 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 4510 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4511 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4512 | * @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). |
| 4513 | * 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] | 4514 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4515 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4516 | static LY_ERR |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4517 | 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] | 4518 | { |
| 4519 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4520 | struct lysc_node *node; |
| 4521 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4522 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4523 | unsigned int u; |
| 4524 | LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*); |
| 4525 | |
| 4526 | switch (node_p->nodetype) { |
| 4527 | case LYS_CONTAINER: |
| 4528 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 4529 | node_compile_spec = lys_compile_node_container; |
| 4530 | break; |
| 4531 | case LYS_LEAF: |
| 4532 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 4533 | node_compile_spec = lys_compile_node_leaf; |
| 4534 | break; |
| 4535 | case LYS_LIST: |
| 4536 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4537 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4538 | break; |
| 4539 | case LYS_LEAFLIST: |
| 4540 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 4541 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4542 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4543 | case LYS_CHOICE: |
| 4544 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4545 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4546 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4547 | case LYS_ANYXML: |
| 4548 | case LYS_ANYDATA: |
| 4549 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4550 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4551 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4552 | case LYS_USES: |
| 4553 | 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] | 4554 | default: |
| 4555 | LOGINT(ctx->ctx); |
| 4556 | return LY_EINT; |
| 4557 | } |
| 4558 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 4559 | node->nodetype = node_p->nodetype; |
| 4560 | node->module = ctx->mod; |
| 4561 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 4562 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4563 | |
| 4564 | /* config */ |
| 4565 | if (!(node->flags & LYS_CONFIG_MASK)) { |
| 4566 | /* config not explicitely set, inherit it from parent */ |
| 4567 | if (parent) { |
| 4568 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 4569 | } else { |
| 4570 | /* default is config true */ |
| 4571 | node->flags |= LYS_CONFIG_W; |
| 4572 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4573 | } else { |
| 4574 | /* config set explicitely */ |
| 4575 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4576 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4577 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4578 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4579 | "Configuration node cannot be child of any state data node."); |
| 4580 | goto error; |
| 4581 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4582 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 4583 | /* *list ordering */ |
| 4584 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 4585 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
| 4586 | LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, " |
| 4587 | "RPC/action output parameters or notification content (%s).", ctx->path); |
| 4588 | node->flags &= ~LYS_ORDBY_MASK; |
| 4589 | node->flags |= LYS_ORDBY_SYSTEM; |
| 4590 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 4591 | /* default ordering is system */ |
| 4592 | node->flags |= LYS_ORDBY_SYSTEM; |
| 4593 | } |
| 4594 | } |
| 4595 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4596 | /* status - it is not inherited by specification, but it does not make sense to have |
| 4597 | * 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] | 4598 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 4599 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 4600 | * the parent is choice (parent here) or some case (so we have to get its flags to check) */ |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4601 | LY_CHECK_GOTO(lys_compile_status(ctx, node, uses_status ? uses_status : (parent ? parent->flags : 0)), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | if (!(options & LYSC_OPT_FREE_SP)) { |
| 4605 | node->sp = node_p; |
| 4606 | } |
| 4607 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4608 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 4609 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4610 | if (node_p->when) { |
| 4611 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4612 | ret = lys_compile_when(ctx, node_p->when, options, when); |
| 4613 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4614 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4615 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4616 | 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] | 4617 | COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error); |
| 4618 | |
| 4619 | /* nodetype-specific part */ |
| 4620 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error); |
| 4621 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4622 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 4623 | if (node->flags & LYS_MAND_TRUE) { |
| 4624 | lys_compile_mandatory_parents(parent, 1); |
| 4625 | } |
| 4626 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4627 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4628 | if (parent) { |
| 4629 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4630 | cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node); |
| 4631 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4632 | if (uses_status) { |
| 4633 | |
| 4634 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4635 | /* 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] | 4636 | * it directly gets the same status flags as the choice; |
| 4637 | * uses_status cannot be applied here since uses cannot be child statement of choice */ |
| 4638 | LY_CHECK_GOTO(lys_compile_status(ctx, node, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4639 | node->parent = (struct lysc_node*)cs; |
| 4640 | } else { /* other than choice */ |
| 4641 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4642 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4643 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4644 | } else { |
| 4645 | /* top-level element */ |
| 4646 | if (!ctx->mod->compiled->data) { |
| 4647 | ctx->mod->compiled->data = node; |
| 4648 | } else { |
| 4649 | /* insert at the end of the module's top-level nodes list */ |
| 4650 | ctx->mod->compiled->data->prev->next = node; |
| 4651 | node->prev = ctx->mod->compiled->data->prev; |
| 4652 | ctx->mod->compiled->data->prev = node; |
| 4653 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4654 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 4655 | ctx->mod->compiled->notifs, node->name, node)) { |
| 4656 | return LY_EVALID; |
| 4657 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | return LY_SUCCESS; |
| 4661 | |
| 4662 | error: |
| 4663 | lysc_node_free(ctx->ctx, node); |
| 4664 | return ret; |
| 4665 | } |
| 4666 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4667 | static void |
| 4668 | lysc_disconnect(struct lysc_node *node) |
| 4669 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4670 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 4671 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4672 | int remove_cs = 0; |
| 4673 | |
| 4674 | parent = node->parent; |
| 4675 | |
| 4676 | /* parent's first child */ |
| 4677 | if (!parent) { |
| 4678 | return; |
| 4679 | } |
| 4680 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4681 | cs = (struct lysc_node_case*)node; |
| 4682 | } else if (parent->nodetype == LYS_CASE) { |
| 4683 | /* disconnecting some node in a case */ |
| 4684 | cs = (struct lysc_node_case*)parent; |
| 4685 | parent = cs->parent; |
| 4686 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 4687 | if (child == node) { |
| 4688 | if (cs->child == child) { |
| 4689 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 4690 | /* case with a single child -> remove also the case */ |
| 4691 | child->parent = NULL; |
| 4692 | remove_cs = 1; |
| 4693 | } else { |
| 4694 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4695 | } |
| 4696 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4697 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4698 | } |
| 4699 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4700 | if (!remove_cs) { |
| 4701 | cs = NULL; |
| 4702 | } |
| 4703 | } else if (lysc_node_children(parent) == node) { |
| 4704 | *lysc_node_children_p(parent) = node->next; |
| 4705 | } |
| 4706 | |
| 4707 | if (cs) { |
| 4708 | if (remove_cs) { |
| 4709 | /* cs has only one child which is being also removed */ |
| 4710 | lysc_disconnect((struct lysc_node*)cs); |
| 4711 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 4712 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4713 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 4714 | /* default case removed */ |
| 4715 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 4716 | } |
| 4717 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 4718 | /* first case removed */ |
| 4719 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 4720 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4721 | if (cs->child) { |
| 4722 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 4723 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 4724 | prev = cs->child->prev; |
| 4725 | } /* else all the children are under a single case */ |
| 4726 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 4727 | if (child->parent != (struct lysc_node*)cs) { |
| 4728 | break; |
| 4729 | } |
| 4730 | lysc_node_free(node->module->ctx, child); |
| 4731 | } |
| 4732 | if (prev) { |
| 4733 | if (prev->next) { |
| 4734 | prev->next = child; |
| 4735 | } |
| 4736 | if (child) { |
| 4737 | child->prev = prev; |
| 4738 | } else { |
| 4739 | /* link from the first child under the cases */ |
| 4740 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 4741 | } |
| 4742 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4743 | } |
| 4744 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4748 | if (node->prev->next) { |
| 4749 | node->prev->next = node->next; |
| 4750 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4751 | if (node->next) { |
| 4752 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 4753 | } else if (node->nodetype != LYS_CASE) { |
| 4754 | child = (struct lysc_node*)lysc_node_children(parent); |
| 4755 | if (child) { |
| 4756 | child->prev = node->prev; |
| 4757 | } |
| 4758 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 4759 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4760 | } |
| 4761 | } |
| 4762 | |
| 4763 | LY_ERR |
| 4764 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p, int options) |
| 4765 | { |
| 4766 | LY_ERR ret = LY_EVALID; |
| 4767 | struct ly_set devs_p = {0}; |
| 4768 | struct ly_set targets = {0}; |
| 4769 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4770 | struct lysc_node_list *list; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4771 | struct lysp_deviation *dev; |
| 4772 | struct lysp_deviate *d, **dp_new; |
| 4773 | struct lysp_deviate_add *d_add; |
| 4774 | struct lysp_deviate_del *d_del; |
| 4775 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4776 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4777 | struct lysc_deviation { |
| 4778 | const char *nodeid; |
| 4779 | struct lysc_node *target; /* target node of the deviation */ |
| 4780 | struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */ |
| 4781 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 4782 | } **devs = NULL; |
| 4783 | int i; |
| 4784 | size_t prefix_len, name_len; |
| 4785 | const char *prefix, *name, *nodeid; |
| 4786 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4787 | uint32_t min, max; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4788 | |
| 4789 | /* get all deviations from the module and all its submodules ... */ |
| 4790 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 4791 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 4792 | } |
| 4793 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 4794 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 4795 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 4796 | } |
| 4797 | } |
| 4798 | if (!devs_p.count) { |
| 4799 | /* nothing to do */ |
| 4800 | return LY_SUCCESS; |
| 4801 | } |
| 4802 | |
| 4803 | /* ... and group them by the target node */ |
| 4804 | devs = calloc(devs_p.count, sizeof *devs); |
| 4805 | for (u = 0; u < devs_p.count; ++u) { |
| 4806 | dev = devs_p.objs[u]; |
| 4807 | |
| 4808 | /* resolve the target */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4809 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, (const struct lysc_node**)&target), cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4810 | |
| 4811 | /* insert into the set of targets with duplicity detection */ |
| 4812 | i = ly_set_add(&targets, target, 0); |
| 4813 | if (!devs[i]) { |
| 4814 | /* new record */ |
| 4815 | devs[i] = calloc(1, sizeof **devs); |
| 4816 | devs[i]->target = target; |
| 4817 | devs[i]->nodeid = dev->nodeid; |
| 4818 | } |
| 4819 | /* add deviates into the deviation's list of deviates */ |
| 4820 | for (d = dev->deviates; d; d = d->next) { |
| 4821 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 4822 | *dp_new = d; |
| 4823 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 4824 | devs[i]->not_supported = 1; |
| 4825 | } |
| 4826 | } |
| 4827 | } |
| 4828 | |
| 4829 | /* MACROS for deviates checking */ |
| 4830 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 4831 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
| 4832 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\ |
| 4833 | goto cleanup; \ |
| 4834 | } |
| 4835 | |
| 4836 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 4837 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
| 4838 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \ |
| 4839 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
| 4840 | goto cleanup; \ |
| 4841 | } |
| 4842 | |
| 4843 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4844 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4845 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 4846 | "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \ |
| 4847 | devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 4848 | goto cleanup; \ |
| 4849 | } |
| 4850 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4851 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 4852 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 4853 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 4854 | "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \ |
| 4855 | devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
| 4856 | goto cleanup; \ |
| 4857 | } |
| 4858 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4859 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 4860 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
| 4861 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \ |
| 4862 | goto cleanup; \ |
| 4863 | } |
| 4864 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4865 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 4866 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 4867 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 4868 | "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \ |
| 4869 | devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \ |
| 4870 | goto cleanup; \ |
| 4871 | } |
| 4872 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4873 | #define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \ |
| 4874 | DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \ |
| 4875 | if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \ |
| 4876 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 4877 | "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \ |
| 4878 | devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \ |
| 4879 | goto cleanup; \ |
| 4880 | } \ |
| 4881 | DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \ |
| 4882 | ((TYPE)devs[u]->target)->MEMBER_TRG = NULL; |
| 4883 | |
| 4884 | #define DEV_DEL_ARRAY(TYPE, ARRAY, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 4885 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY, "deleting", PROPERTY, d_del->ARRAY[0]VALMEMBER); \ |
| 4886 | LY_ARRAY_FOR(d_del->ARRAY, x) { \ |
| 4887 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY, y) { \ |
| 4888 | if (!strcmp(((TYPE)devs[u]->target)->ARRAY[y]VALMEMBER_CMP, d_del->ARRAY[x]VALMEMBER)) { break; } \ |
| 4889 | } \ |
| 4890 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY)) { \ |
| 4891 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 4892 | "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 4893 | devs[u]->nodeid, PROPERTY, d_del->ARRAY[x]VALMEMBER); \ |
| 4894 | goto cleanup; \ |
| 4895 | } \ |
| 4896 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY); \ |
| 4897 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY[y]); \ |
| 4898 | memmove(&((TYPE)devs[u]->target)->ARRAY[y], \ |
| 4899 | &((TYPE)devs[u]->target)->ARRAY[y + 1], \ |
| 4900 | (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY)); \ |
| 4901 | } \ |
| 4902 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY)) { \ |
| 4903 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY); \ |
| 4904 | ((TYPE)devs[u]->target)->ARRAY = NULL; \ |
| 4905 | } |
| 4906 | |
| 4907 | /* apply deviations */ |
| 4908 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 4909 | /* not-supported */ |
| 4910 | if (devs[u]->not_supported) { |
| 4911 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 4912 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 4913 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 4914 | } |
| 4915 | /* remove the target node */ |
| 4916 | lysc_disconnect(devs[u]->target); |
| 4917 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 4918 | |
| 4919 | continue; |
| 4920 | } |
| 4921 | |
| 4922 | /* list of deviates (not-supported is not present in the list) */ |
| 4923 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 4924 | d = devs[u]->deviates[v]; |
| 4925 | |
| 4926 | switch (d->mod) { |
| 4927 | case LYS_DEV_ADD: |
| 4928 | d_add = (struct lysp_deviate_add*)d; |
| 4929 | /* [units-stmt] */ |
| 4930 | if (d_add->units) { |
| 4931 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 4932 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 4933 | |
| 4934 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 4935 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 4936 | } |
| 4937 | |
| 4938 | /* *must-stmt */ |
| 4939 | if (d_add->musts) { |
| 4940 | switch (devs[u]->target->nodetype) { |
| 4941 | case LYS_CONTAINER: |
| 4942 | case LYS_LIST: |
| 4943 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 4944 | options, x, lys_compile_must, ret, cleanup); |
| 4945 | break; |
| 4946 | case LYS_LEAF: |
| 4947 | case LYS_LEAFLIST: |
| 4948 | case LYS_ANYDATA: |
| 4949 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 4950 | options, x, lys_compile_must, ret, cleanup); |
| 4951 | break; |
| 4952 | case LYS_NOTIF: |
| 4953 | case LYS_INOUT: |
| 4954 | /* TODO */ |
| 4955 | default: |
| 4956 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4957 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4958 | goto cleanup; |
| 4959 | } |
| 4960 | } |
| 4961 | |
| 4962 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4963 | if (d_add->uniques) { |
| 4964 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 4965 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 4966 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4967 | |
| 4968 | /* *default-stmt */ |
| 4969 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4970 | switch (devs[u]->target->nodetype) { |
| 4971 | case LYS_LEAF: |
| 4972 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 4973 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt); |
| 4974 | if (((struct lysc_node_leaf*)devs[u]->target)->dflt) { |
| 4975 | /* first, remove the default value taken from the type */ |
| 4976 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 4977 | ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL; |
| 4978 | } |
| 4979 | 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] | 4980 | /* mark the new default values as leaf's own */ |
| 4981 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4982 | break; |
| 4983 | case LYS_LEAFLIST: |
| 4984 | if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 4985 | /* first, remove the default value taken from the type */ |
| 4986 | LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) { |
| 4987 | lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]); |
| 4988 | } |
| 4989 | LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 4990 | ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL; |
| 4991 | } |
| 4992 | /* add new default value(s) */ |
| 4993 | LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts, |
| 4994 | LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 4995 | for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 4996 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
| 4997 | DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]); |
| 4998 | LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts); |
| 4999 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5000 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5001 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5002 | break; |
| 5003 | case LYS_CHOICE: |
| 5004 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5005 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5006 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5007 | * to allow making the default case even the augmented case from the deviating module */ |
| 5008 | if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0], |
| 5009 | (struct lysc_node_choice*)devs[u]->target)) { |
| 5010 | goto cleanup; |
| 5011 | } |
| 5012 | break; |
| 5013 | default: |
| 5014 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5015 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5016 | goto cleanup; |
| 5017 | } |
| 5018 | } |
| 5019 | |
| 5020 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5021 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 5022 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) { |
| 5023 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5024 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5025 | goto cleanup; |
| 5026 | } |
| 5027 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5028 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5029 | "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").", |
| 5030 | devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5031 | goto cleanup; |
| 5032 | } |
| 5033 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup); |
| 5034 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5035 | |
| 5036 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5037 | if (d_add->flags & LYS_MAND_MASK) { |
| 5038 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5039 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5040 | "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5041 | devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5042 | goto cleanup; |
| 5043 | } |
| 5044 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup); |
| 5045 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5046 | |
| 5047 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5048 | if (d_add->flags & LYS_SET_MIN) { |
| 5049 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5050 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5051 | /* change value */ |
| 5052 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5053 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5054 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5055 | /* change value */ |
| 5056 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5057 | } else { |
| 5058 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5059 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5060 | goto cleanup; |
| 5061 | } |
| 5062 | if (d_add->min) { |
| 5063 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5064 | } |
| 5065 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5066 | |
| 5067 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5068 | if (d_add->flags & LYS_SET_MAX) { |
| 5069 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5070 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5071 | /* change value */ |
| 5072 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5073 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5074 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5075 | /* change value */ |
| 5076 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5077 | } else { |
| 5078 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5079 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 5080 | goto cleanup; |
| 5081 | } |
| 5082 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5083 | |
| 5084 | break; |
| 5085 | case LYS_DEV_DELETE: |
| 5086 | d_del = (struct lysp_deviate_del*)d; |
| 5087 | |
| 5088 | /* [units-stmt] */ |
| 5089 | if (d_del->units) { |
| 5090 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
| 5091 | DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units"); |
| 5092 | } |
| 5093 | |
| 5094 | /* *must-stmt */ |
| 5095 | if (d_del->musts) { |
| 5096 | switch (devs[u]->target->nodetype) { |
| 5097 | case LYS_CONTAINER: |
| 5098 | case LYS_LIST: |
| 5099 | DEV_DEL_ARRAY(struct lysc_node_container*, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5100 | break; |
| 5101 | case LYS_LEAF: |
| 5102 | case LYS_LEAFLIST: |
| 5103 | case LYS_ANYDATA: |
| 5104 | DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5105 | break; |
| 5106 | case LYS_NOTIF: |
| 5107 | case LYS_INOUT: |
| 5108 | /* TODO */ |
| 5109 | default: |
| 5110 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5111 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5112 | goto cleanup; |
| 5113 | } |
| 5114 | } |
| 5115 | |
| 5116 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5117 | if (d_del->uniques) { |
| 5118 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5119 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 5120 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 5121 | LY_ARRAY_FOR(list->uniques, z) { |
| 5122 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 5123 | nodeid = strpbrk(name, " \t\n"); |
| 5124 | if (nodeid) { |
| 5125 | if (strncmp(name, list->uniques[z][y]->name, nodeid - name) |
| 5126 | || list->uniques[z][y]->name[nodeid - name] != '\0') { |
| 5127 | break; |
| 5128 | } |
| 5129 | while (isspace(*nodeid)) { |
| 5130 | ++nodeid; |
| 5131 | } |
| 5132 | } else { |
| 5133 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 5134 | break; |
| 5135 | } |
| 5136 | } |
| 5137 | } |
| 5138 | if (!name) { |
| 5139 | /* complete match - remove the unique */ |
| 5140 | LY_ARRAY_DECREMENT(list->uniques); |
| 5141 | LY_ARRAY_FREE(list->uniques[z]); |
| 5142 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 5143 | --z; |
| 5144 | break; |
| 5145 | } |
| 5146 | } |
| 5147 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 5148 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5149 | "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 5150 | devs[u]->nodeid, d_del->uniques[x]); |
| 5151 | goto cleanup; |
| 5152 | } |
| 5153 | } |
| 5154 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 5155 | LY_ARRAY_FREE(list->uniques); |
| 5156 | list->uniques = NULL; |
| 5157 | } |
| 5158 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5159 | |
| 5160 | /* *default-stmt */ |
| 5161 | if (d_del->dflts) { |
| 5162 | switch (devs[u]->target->nodetype) { |
| 5163 | case LYS_LEAF: |
| 5164 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 5165 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 5166 | dflt, "deleting", "default", d_del->dflts[0]); |
| 5167 | |
| 5168 | 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] | 5169 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5170 | break; |
| 5171 | case LYS_LEAFLIST: |
| 5172 | DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, , , , lydict_remove, "default"); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5173 | if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) { |
| 5174 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
| 5175 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5176 | break; |
| 5177 | case LYS_CHOICE: |
| 5178 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 5179 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 5180 | nodeid = d_del->dflts[0]; |
| 5181 | LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 5182 | if (prefix) { |
| 5183 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 5184 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 5185 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
| 5186 | "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. " |
| 5187 | "The prefix does not match any imported module of the deviation module.", |
| 5188 | devs[u]->nodeid, d_del->dflts[0]); |
| 5189 | goto cleanup; |
| 5190 | } |
| 5191 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 5192 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
| 5193 | "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. " |
| 5194 | "The prefix does not match the default case's module.", |
| 5195 | devs[u]->nodeid, d_del->dflts[0]); |
| 5196 | goto cleanup; |
| 5197 | } |
| 5198 | } |
| 5199 | /* else { |
| 5200 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 5201 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 5202 | * the module of the default case, just matching its name */ |
| 5203 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 5204 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5205 | "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 5206 | devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name); |
| 5207 | goto cleanup; |
| 5208 | } |
| 5209 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 5210 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 5211 | break; |
| 5212 | default: |
| 5213 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5214 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5215 | goto cleanup; |
| 5216 | } |
| 5217 | } |
| 5218 | |
| 5219 | break; |
| 5220 | case LYS_DEV_REPLACE: |
| 5221 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 5222 | |
| 5223 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5224 | if (d_rpl->type) { |
| 5225 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 5226 | /* type is mandatory, so checking for its presence is not necessary */ |
| 5227 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type); |
| 5228 | LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, options, (struct lysc_node_leaf*)devs[u]->target), cleanup); |
| 5229 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5230 | |
| 5231 | /* [units-stmt] */ |
| 5232 | if (d_rpl->units) { |
| 5233 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 5234 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 5235 | units, "replacing", "units", d_rpl->units); |
| 5236 | |
| 5237 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5238 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5239 | } |
| 5240 | |
| 5241 | /* [default-stmt] */ |
| 5242 | if (d_rpl->dflt) { |
| 5243 | switch (devs[u]->target->nodetype) { |
| 5244 | case LYS_LEAF: |
| 5245 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 5246 | dflt, "replacing", "default", d_rpl->dflt); |
| 5247 | |
| 5248 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 5249 | DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt); |
| 5250 | break; |
| 5251 | case LYS_CHOICE: |
| 5252 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt); |
| 5253 | if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt, |
| 5254 | (struct lysc_node_choice*)devs[u]->target)) { |
| 5255 | goto cleanup; |
| 5256 | } |
| 5257 | break; |
| 5258 | default: |
| 5259 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5260 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5261 | goto cleanup; |
| 5262 | } |
| 5263 | } |
| 5264 | |
| 5265 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5266 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 5267 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) { |
| 5268 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5269 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 5270 | goto cleanup; |
| 5271 | } |
| 5272 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
| 5273 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, |
| 5274 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 5275 | goto cleanup; |
| 5276 | } |
| 5277 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup); |
| 5278 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5279 | |
| 5280 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5281 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 5282 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
| 5283 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, |
| 5284 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 5285 | goto cleanup; |
| 5286 | } |
| 5287 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup); |
| 5288 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5289 | |
| 5290 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5291 | if (d_rpl->flags & LYS_SET_MIN) { |
| 5292 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5293 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5294 | /* change value */ |
| 5295 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 5296 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5297 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5298 | /* change value */ |
| 5299 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 5300 | } else { |
| 5301 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5302 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 5303 | goto cleanup; |
| 5304 | } |
| 5305 | if (d_rpl->min) { |
| 5306 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5307 | } |
| 5308 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5309 | |
| 5310 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5311 | if (d_rpl->flags & LYS_SET_MAX) { |
| 5312 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5313 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5314 | /* change value */ |
| 5315 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 5316 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5317 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5318 | /* change value */ |
| 5319 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 5320 | } else { |
| 5321 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, |
| 5322 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 5323 | goto cleanup; |
| 5324 | } |
| 5325 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5326 | |
| 5327 | break; |
| 5328 | default: |
| 5329 | LOGINT(ctx->ctx); |
| 5330 | goto cleanup; |
| 5331 | } |
| 5332 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5333 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5334 | /* final check when all deviations of a single target node are applied */ |
| 5335 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5336 | /* check min-max compatibility */ |
| 5337 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5338 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 5339 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 5340 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5341 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 5342 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 5343 | } else { |
| 5344 | min = max = 0; |
| 5345 | } |
| 5346 | if (min > max) { |
| 5347 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
| 5348 | "after deviation (%s): min value %u is bigger than max value %u.", |
| 5349 | devs[u]->nodeid, min, max); |
| 5350 | goto cleanup; |
| 5351 | } |
| 5352 | |
| 5353 | /* check mandatory - default compatibility */ |
| 5354 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 5355 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 5356 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 5357 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5358 | "Invalid deviation (%s) combining default value and mandatory %s.", |
| 5359 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype)); |
| 5360 | goto cleanup; |
| 5361 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 5362 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 5363 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 5364 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5365 | "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid); |
| 5366 | goto cleanup; |
| 5367 | } |
| 5368 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 5369 | /* mandatory node under a default case */ |
| 5370 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5371 | "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 5372 | devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name); |
| 5373 | goto cleanup; |
| 5374 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 5375 | |
| 5376 | /* TODO check default value(s) according to the type */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5377 | } |
| 5378 | |
| 5379 | ret = LY_SUCCESS; |
| 5380 | |
| 5381 | cleanup: |
| 5382 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 5383 | LY_ARRAY_FREE(devs[u]->deviates); |
| 5384 | free(devs[u]); |
| 5385 | } |
| 5386 | free(devs); |
| 5387 | ly_set_erase(&targets, NULL); |
| 5388 | ly_set_erase(&devs_p, NULL); |
| 5389 | |
| 5390 | return ret; |
| 5391 | } |
| 5392 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 5393 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5394 | * @brief Compile the given YANG submodule into the main module. |
| 5395 | * @param[in] ctx Compile context |
| 5396 | * @param[in] inc Include structure from the main module defining the submodule. |
| 5397 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 5398 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5399 | */ |
| 5400 | LY_ERR |
| 5401 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options) |
| 5402 | { |
| 5403 | unsigned int u; |
| 5404 | LY_ERR ret = LY_SUCCESS; |
| 5405 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5406 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5407 | struct lysc_module *mainmod = ctx->mod->compiled; |
| 5408 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5409 | if (!mainmod->mod->off_features) { |
| 5410 | /* features are compiled directly into the compiled module structure, |
| 5411 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 5412 | * The features compilation is finished in the main module (lys_compile()). */ |
| 5413 | ret = lys_feature_precompile(ctx->ctx, submod->features, |
| 5414 | mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features); |
| 5415 | LY_CHECK_GOTO(ret, error); |
| 5416 | } |
| 5417 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5418 | COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error); |
| 5419 | |
| 5420 | error: |
| 5421 | return ret; |
| 5422 | } |
| 5423 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5424 | LY_ERR |
| 5425 | lys_compile(struct lys_module *mod, int options) |
| 5426 | { |
| 5427 | struct lysc_ctx ctx = {0}; |
| 5428 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5429 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5430 | struct lysp_module *sp; |
| 5431 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5432 | struct lysp_augment **augments = NULL; |
| 5433 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 5434 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5435 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5436 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5437 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 5438 | |
| 5439 | if (!mod->implemented) { |
| 5440 | /* just imported modules are not compiled */ |
| 5441 | return LY_SUCCESS; |
| 5442 | } |
| 5443 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5444 | sp = mod->parsed; |
| 5445 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5446 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5447 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5448 | ctx.mod_def = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5449 | |
| 5450 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 5451 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 5452 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5453 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5454 | 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] | 5455 | LY_ARRAY_FOR(sp->includes, u) { |
| 5456 | ret = lys_compile_submodule(&ctx, &sp->includes[u], options); |
| 5457 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 5458 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5459 | if (mod->off_features) { |
| 5460 | /* there is already precompiled array of features */ |
| 5461 | mod_c->features = mod->off_features; |
| 5462 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 5463 | } else { |
| 5464 | /* features are compiled directly into the compiled module structure, |
| 5465 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
| 5466 | ret = lys_feature_precompile(ctx.ctx, sp->features, &mod_c->features); |
| 5467 | LY_CHECK_GOTO(ret, error); |
| 5468 | } |
| 5469 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 5470 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 5471 | * are present. */ |
| 5472 | LY_ARRAY_FOR(sp->features, u) { |
| 5473 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features); |
| 5474 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 5475 | } |
| 5476 | LY_ARRAY_FOR(sp->includes, v) { |
| 5477 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
| 5478 | ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features); |
| 5479 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 5480 | } |
| 5481 | } |
| 5482 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 5483 | 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] | 5484 | if (sp->identities) { |
| 5485 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 5486 | } |
| 5487 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5488 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5489 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5490 | ret = lys_compile_node(&ctx, node_p, options, NULL, 0); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5491 | LY_CHECK_GOTO(ret, error); |
| 5492 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5493 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5494 | //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error); |
| 5495 | //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error); |
| 5496 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5497 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5498 | ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5499 | LY_CHECK_GOTO(ret, error); |
| 5500 | LY_ARRAY_FOR(augments, u) { |
| 5501 | ret = lys_compile_augment(&ctx, augments[u], options, NULL); |
| 5502 | LY_CHECK_GOTO(ret, error); |
| 5503 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5504 | |
| 5505 | /* deviations */ |
| 5506 | ret = lys_compile_deviations(&ctx, sp, options); |
| 5507 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5508 | |
| 5509 | COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error); |
| 5510 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5511 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5512 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 5513 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 5514 | * 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] | 5515 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5516 | 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] | 5517 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 5518 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 5519 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5520 | 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] | 5521 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 5522 | } else if (type->basetype == LY_TYPE_UNION) { |
| 5523 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 5524 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 5525 | /* validate the path */ |
| 5526 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 5527 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 5528 | LY_CHECK_GOTO(ret, error); |
| 5529 | } |
| 5530 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5531 | } |
| 5532 | } |
| 5533 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5534 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5535 | 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] | 5536 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 5537 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 5538 | /* store pointer to the real type */ |
| 5539 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 5540 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 5541 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 5542 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 5543 | } else if (type->basetype == LY_TYPE_UNION) { |
| 5544 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 5545 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 5546 | /* store pointer to the real type */ |
| 5547 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 5548 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 5549 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 5550 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 5551 | } |
| 5552 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 5553 | } |
| 5554 | } |
| 5555 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5556 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5557 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5558 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5559 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5560 | if (options & LYSC_OPT_FREE_SP) { |
| 5561 | lysp_module_free(mod->parsed); |
| 5562 | ((struct lys_module*)mod)->parsed = NULL; |
| 5563 | } |
| 5564 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5565 | if (!(options & LYSC_OPT_INTERNAL)) { |
| 5566 | /* remove flag of the modules implemented by dependency */ |
| 5567 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 5568 | m = ctx.ctx->list.objs[u]; |
| 5569 | if (m->implemented == 2) { |
| 5570 | m->implemented = 1; |
| 5571 | } |
| 5572 | } |
| 5573 | } |
| 5574 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5575 | ((struct lys_module*)mod)->compiled = mod_c; |
| 5576 | return LY_SUCCESS; |
| 5577 | |
| 5578 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5579 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5580 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5581 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5582 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5583 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 5584 | mod->compiled = NULL; |
| 5585 | |
| 5586 | /* revert compilation of modules implemented by dependency */ |
| 5587 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 5588 | m = ctx.ctx->list.objs[u]; |
| 5589 | if (m->implemented == 2) { |
| 5590 | /* revert features list to the precompiled state */ |
| 5591 | lys_feature_precompile_revert(&ctx, m); |
| 5592 | /* mark module as imported-only / not-implemented */ |
| 5593 | m->implemented = 0; |
| 5594 | /* free the compiled version of the module */ |
| 5595 | lysc_module_free(m->compiled, NULL); |
| 5596 | m->compiled = NULL; |
| 5597 | } |
| 5598 | } |
| 5599 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5600 | return ret; |
| 5601 | } |