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 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 24 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 26 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "dict.h" |
| 29 | #include "log.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 30 | #include "path.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 31 | #include "parser.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 32 | #include "parser_schema.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 33 | #include "plugins_exts.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "plugins_types.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 35 | #include "plugins_exts_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "set.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | #include "tree.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 38 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 39 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 40 | #include "tree_schema_internal.h" |
| 41 | #include "xpath.h" |
| 42 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 43 | static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, |
| 44 | void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod); |
| 45 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 46 | /** |
| 47 | * @brief Duplicate string into dictionary |
| 48 | * @param[in] CTX libyang context of the dictionary. |
| 49 | * @param[in] ORIG String to duplicate. |
| 50 | * @param[out] DUP Where to store the result. |
| 51 | */ |
| 52 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 53 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 54 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 55 | if (ARRAY_P) { \ |
| 56 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 57 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 58 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 59 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 60 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 61 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 62 | } \ |
| 63 | } |
| 64 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 65 | #define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 66 | if (ARRAY_P) { \ |
| 67 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 68 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 69 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 70 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 71 | RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 72 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 73 | } \ |
| 74 | } |
| 75 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 76 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 77 | if (EXTS_P) { \ |
| 78 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 79 | for (LY_ARRAY_SIZE_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 80 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 81 | RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 82 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 83 | } \ |
| 84 | } |
| 85 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 86 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 87 | if (ARRAY_P) { \ |
| 88 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 89 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 90 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 91 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 92 | RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 93 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 94 | } \ |
| 95 | } |
| 96 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 97 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 98 | if (MEMBER_P) { \ |
| 99 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 100 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 101 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 102 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 103 | } |
| 104 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 105 | #define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 106 | if (MEMBER_P) { \ |
| 107 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 108 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 109 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 110 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 111 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 112 | } |
| 113 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 114 | #define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 115 | if (ARRAY) { \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 116 | for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 117 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 118 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 119 | return LY_EVALID; \ |
| 120 | } \ |
| 121 | } \ |
| 122 | } |
| 123 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 124 | #define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 125 | if (ARRAY) { \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 126 | for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 127 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \ |
| 128 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 129 | return LY_EVALID; \ |
| 130 | } \ |
| 131 | } \ |
| 132 | } |
| 133 | |
| 134 | struct lysc_ext * |
| 135 | lysc_ext_dup(struct lysc_ext *orig) |
| 136 | { |
| 137 | ++orig->refcount; |
| 138 | return orig; |
| 139 | } |
| 140 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 141 | static struct lysc_ext_instance * |
| 142 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 143 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 144 | /* TODO - extensions, increase refcount */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 145 | (void) ctx; |
| 146 | (void) orig; |
| 147 | return NULL; |
| 148 | } |
| 149 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 150 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 151 | * @brief Add record into the compile context's list of incomplete default values. |
| 152 | * @param[in] ctx Compile context with the incomplete default values list. |
| 153 | * @param[in] context_node Context schema node to store in the record. |
| 154 | * @param[in] dflt Incomplete default value to store in the record. |
| 155 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 156 | * @return LY_EMEM in case of memory allocation failure. |
| 157 | * @return LY_SUCCESS |
| 158 | */ |
| 159 | static LY_ERR |
| 160 | lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod) |
| 161 | { |
| 162 | struct lysc_incomplete_dflt *r; |
| 163 | r = malloc(sizeof *r); |
| 164 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 165 | r->context_node = context_node; |
| 166 | r->dflt = dflt; |
| 167 | r->dflt_mod = dflt_mod; |
| 168 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 169 | |
| 170 | return LY_SUCCESS; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 175 | * @param[in] ctx Compile context with the incomplete default values list. |
| 176 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 177 | */ |
| 178 | static void |
| 179 | lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt) |
| 180 | { |
| 181 | unsigned int u; |
| 182 | struct lysc_incomplete_dflt *r; |
| 183 | |
| 184 | for (u = 0; u < ctx->dflts.count; ++u) { |
| 185 | r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u]; |
| 186 | if (r->dflt == dflt) { |
| 187 | free(ctx->dflts.objs[u]); |
| 188 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 189 | --ctx->dflts.count; |
| 190 | return; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 195 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 196 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 197 | { |
| 198 | int len; |
| 199 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 200 | |
| 201 | if (!name) { |
| 202 | /* removing last path segment */ |
| 203 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 204 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 205 | if (ctx->path[ctx->path_len] == '=') { |
| 206 | ctx->path[ctx->path_len++] = '}'; |
| 207 | } else { |
| 208 | /* not a top-level special tag, remove also preceiding '/' */ |
| 209 | goto remove_nodelevel; |
| 210 | } |
| 211 | } else { |
| 212 | remove_nodelevel: |
| 213 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 214 | if (ctx->path_len == 0) { |
| 215 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 216 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | /* set new terminating NULL-byte */ |
| 220 | ctx->path[ctx->path_len] = '\0'; |
| 221 | } else { |
| 222 | if (ctx->path_len > 1) { |
| 223 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 224 | /* extension of the special tag */ |
| 225 | nextlevel = 2; |
| 226 | --ctx->path_len; |
| 227 | } else { |
| 228 | /* there is already some path, so add next level */ |
| 229 | nextlevel = 1; |
| 230 | } |
| 231 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 232 | |
| 233 | if (nextlevel != 2) { |
| 234 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 235 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 236 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 237 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 238 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 239 | } |
| 240 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 241 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 242 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 243 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 244 | /* output truncated */ |
| 245 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 246 | } else { |
| 247 | ctx->path_len += len; |
| 248 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 253 | * @brief Duplicate the compiled pattern structure. |
| 254 | * |
| 255 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 256 | * |
| 257 | * @param[in] orig The pattern structure to duplicate. |
| 258 | * @return The duplicated structure to use. |
| 259 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 260 | static struct lysc_pattern* |
| 261 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 262 | { |
| 263 | ++orig->refcount; |
| 264 | return orig; |
| 265 | } |
| 266 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 267 | /** |
| 268 | * @brief Duplicate the array of compiled patterns. |
| 269 | * |
| 270 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 271 | * |
| 272 | * @param[in] ctx Libyang context for logging. |
| 273 | * @param[in] orig The patterns sized array to duplicate. |
| 274 | * @return New sized array as a copy of @p orig. |
| 275 | * @return NULL in case of memory allocation error. |
| 276 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 277 | static struct lysc_pattern** |
| 278 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 279 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 280 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 281 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 282 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 283 | assert(orig); |
| 284 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 285 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 286 | LY_ARRAY_FOR(orig, u) { |
| 287 | dup[u] = lysc_pattern_dup(orig[u]); |
| 288 | LY_ARRAY_INCREMENT(dup); |
| 289 | } |
| 290 | return dup; |
| 291 | } |
| 292 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 293 | /** |
| 294 | * @brief Duplicate compiled range structure. |
| 295 | * |
| 296 | * @param[in] ctx Libyang context for logging. |
| 297 | * @param[in] orig The range structure to be duplicated. |
| 298 | * @return New compiled range structure as a copy of @p orig. |
| 299 | * @return NULL in case of memory allocation error. |
| 300 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 301 | struct lysc_range* |
| 302 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 303 | { |
| 304 | struct lysc_range *dup; |
| 305 | LY_ERR ret; |
| 306 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 307 | assert(orig); |
| 308 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 309 | dup = calloc(1, sizeof *dup); |
| 310 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 311 | if (orig->parts) { |
| 312 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 313 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 314 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 315 | } |
| 316 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 317 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 318 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 319 | |
| 320 | return dup; |
| 321 | cleanup: |
| 322 | free(dup); |
| 323 | (void) ret; /* set but not used due to the return type */ |
| 324 | return NULL; |
| 325 | } |
| 326 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 327 | /** |
| 328 | * @brief Stack for processing if-feature expressions. |
| 329 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 330 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 331 | int size; /**< number of items in the stack */ |
| 332 | int index; /**< first empty item */ |
| 333 | 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] | 334 | }; |
| 335 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 336 | /** |
| 337 | * @brief Add @ref ifftokens into the stack. |
| 338 | * @param[in] stack The if-feature stack to use. |
| 339 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 340 | * @return LY_EMEM in case of memory allocation error |
| 341 | * @return LY_ESUCCESS if the value successfully stored. |
| 342 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 343 | static LY_ERR |
| 344 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 345 | { |
| 346 | if (stack->index == stack->size) { |
| 347 | stack->size += 4; |
| 348 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 349 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 350 | } |
| 351 | stack->stack[stack->index++] = value; |
| 352 | return LY_SUCCESS; |
| 353 | } |
| 354 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 355 | /** |
| 356 | * @brief Get (and remove) the last item form the stack. |
| 357 | * @param[in] stack The if-feature stack to use. |
| 358 | * @return The value from the top of the stack. |
| 359 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 360 | static uint8_t |
| 361 | iff_stack_pop(struct iff_stack *stack) |
| 362 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 363 | assert(stack && stack->index); |
| 364 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 365 | stack->index--; |
| 366 | return stack->stack[stack->index]; |
| 367 | } |
| 368 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 369 | /** |
| 370 | * @brief Clean up the stack. |
| 371 | * @param[in] stack The if-feature stack to use. |
| 372 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 373 | static void |
| 374 | iff_stack_clean(struct iff_stack *stack) |
| 375 | { |
| 376 | stack->size = 0; |
| 377 | free(stack->stack); |
| 378 | } |
| 379 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 380 | /** |
| 381 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 382 | * (libyang format of the if-feature expression). |
| 383 | * @param[in,out] list The 2bits array to modify. |
| 384 | * @param[in] op The operand (@ref ifftokens) to store. |
| 385 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 386 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 387 | static void |
| 388 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 389 | { |
| 390 | uint8_t *item; |
| 391 | uint8_t mask = 3; |
| 392 | |
| 393 | assert(pos >= 0); |
| 394 | assert(op <= 3); /* max 2 bits */ |
| 395 | |
| 396 | item = &list[pos / 4]; |
| 397 | mask = mask << 2 * (pos % 4); |
| 398 | *item = (*item) & ~mask; |
| 399 | *item = (*item) | (op << 2 * (pos % 4)); |
| 400 | } |
| 401 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 402 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 403 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 404 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 405 | /** |
| 406 | * @brief Find a feature of the given name and referenced in the given module. |
| 407 | * |
| 408 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 409 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 410 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 411 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 412 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 413 | * assigned till that time will be still valid. |
| 414 | * |
| 415 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 416 | * @param[in] name Name of the feature including possible prefix. |
| 417 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 418 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 419 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 420 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 421 | 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] | 422 | { |
| 423 | size_t i; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 424 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 425 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 426 | |
| 427 | for (i = 0; i < len; ++i) { |
| 428 | if (name[i] == ':') { |
| 429 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 430 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 431 | LY_CHECK_RET(!mod, NULL); |
| 432 | |
| 433 | name = &name[i + 1]; |
| 434 | len = len - i - 1; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 439 | if (mod->implemented) { |
| 440 | /* module is implemented so there is already the compiled schema */ |
| 441 | flist = mod->compiled->features; |
| 442 | } else { |
| 443 | flist = mod->off_features; |
| 444 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 445 | LY_ARRAY_FOR(flist, u) { |
| 446 | f = &flist[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 447 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 448 | return f; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | return NULL; |
| 453 | } |
| 454 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 455 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 456 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 457 | */ |
| 458 | static LY_ERR |
| 459 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 460 | { |
| 461 | LY_ERR ret = LY_SUCCESS; |
| 462 | |
| 463 | if (!ext_p->compiled) { |
| 464 | lysc_update_path(ctx, NULL, "{extension}"); |
| 465 | lysc_update_path(ctx, NULL, ext_p->name); |
| 466 | |
| 467 | /* compile the extension definition */ |
| 468 | ext_p->compiled = calloc(1, sizeof **ext); |
| 469 | ext_p->compiled->refcount = 1; |
| 470 | DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name); |
| 471 | DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument); |
| 472 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 473 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 474 | |
| 475 | lysc_update_path(ctx, NULL, NULL); |
| 476 | lysc_update_path(ctx, NULL, NULL); |
| 477 | |
| 478 | /* find extension definition plugin */ |
| 479 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 480 | } |
| 481 | |
| 482 | *ext = lysc_ext_dup(ext_p->compiled); |
| 483 | |
| 484 | done: |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 489 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 490 | * |
| 491 | * @param[in] ctx Compilation context. |
| 492 | * @param[in] ext_p Parsed extension instance. |
| 493 | * @param[in,out] ext Prepared compiled extension instance. |
| 494 | * @param[in] parent Extension instance parent. |
| 495 | * @param[in] parent_type Extension instance parent type. |
| 496 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 497 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 498 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 499 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
| 500 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 501 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 502 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 503 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 504 | size_t u; |
| 505 | LY_ARRAY_SIZE_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 506 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 507 | |
| 508 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 509 | ext->insubstmt = ext_p->insubstmt; |
| 510 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 511 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 512 | ext->parent = parent; |
| 513 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 514 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 515 | lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}"); |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 516 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 517 | /* get module where the extension definition should be placed */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 518 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u); |
| 519 | if (ext_p->yin) { |
| 520 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 521 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 522 | * YANG (import) prefix must be done here. */ |
| 523 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 524 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 525 | u = 0; |
| 526 | } else if (ctx->mod_def->compiled) { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 527 | LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) { |
| 528 | if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) { |
| 529 | char *s; |
| 530 | asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]); |
| 531 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
| 532 | u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */ |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | if (!prefixed_name) { |
| 538 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 539 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 540 | goto cleanup; |
| 541 | } |
| 542 | } else { |
| 543 | prefixed_name = ext_p->name; |
| 544 | } |
| 545 | lysc_update_path(ctx, NULL, prefixed_name); |
| 546 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 547 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 548 | ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 549 | if (!ext_mod) { |
| 550 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 551 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 552 | goto cleanup; |
| 553 | } else if (!ext_mod->parsed->extensions) { |
| 554 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 555 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 556 | prefixed_name, ext_mod->name); |
| 557 | goto cleanup; |
| 558 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 559 | } |
| 560 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 561 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 562 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 563 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 564 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 565 | /* compile extension definition and assign it */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 566 | LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 570 | if (!ext->def) { |
| 571 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 572 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 573 | goto cleanup; |
| 574 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 575 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 576 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 577 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 578 | * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 579 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 580 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 581 | struct lysp_stmt *stmt = NULL; |
| 582 | |
| 583 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 584 | /* ... argument was the first XML child element */ |
| 585 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 586 | /* TODO check namespace of the statement */ |
| 587 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 588 | stmt = ext_p->child; |
| 589 | } |
| 590 | } |
| 591 | } else { |
| 592 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 593 | * with LYS_YIN_ATTR flag */ |
| 594 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 595 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 596 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 597 | break; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | if (!stmt) { |
| 602 | /* missing extension's argument */ |
| 603 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 604 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 605 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 606 | |
| 607 | } |
| 608 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 609 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 610 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 611 | if (prefixed_name != ext_p->name) { |
| 612 | lydict_remove(ctx->ctx, ext_p->name); |
| 613 | ext_p->name = prefixed_name; |
| 614 | if (!ext_p->argument && ext->argument) { |
| 615 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 616 | } |
| 617 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 618 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 619 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 620 | if (ext->argument) { |
| 621 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 622 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 623 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 624 | if (ext->argument) { |
| 625 | lysc_update_path(ctx, NULL, NULL); |
| 626 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 627 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 628 | ext_p->compiled = ext; |
| 629 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 630 | ret = LY_SUCCESS; |
| 631 | cleanup: |
| 632 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 633 | lydict_remove(ctx->ctx, prefixed_name); |
| 634 | } |
| 635 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 636 | lysc_update_path(ctx, NULL, NULL); |
| 637 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 638 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 639 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 643 | * @brief Compile information from the if-feature statement |
| 644 | * @param[in] ctx Compile context. |
| 645 | * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 646 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 647 | * @return LY_ERR value. |
| 648 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 649 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 650 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 651 | { |
| 652 | const char *c = *value; |
| 653 | int r, rc = EXIT_FAILURE; |
| 654 | int i, j, last_not, checkversion = 0; |
| 655 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 656 | uint8_t op; |
| 657 | struct iff_stack stack = {0, 0, NULL}; |
| 658 | struct lysc_feature *f; |
| 659 | |
| 660 | assert(c); |
| 661 | |
| 662 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 663 | for (i = j = last_not = 0; c[i]; i++) { |
| 664 | if (c[i] == '(') { |
| 665 | j++; |
| 666 | checkversion = 1; |
| 667 | continue; |
| 668 | } else if (c[i] == ')') { |
| 669 | j--; |
| 670 | continue; |
| 671 | } else if (isspace(c[i])) { |
| 672 | checkversion = 1; |
| 673 | continue; |
| 674 | } |
| 675 | |
| 676 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 677 | int sp; |
| 678 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 679 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 680 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 681 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 682 | return LY_EVALID; |
| 683 | } else if (!isspace(c[i + r])) { |
| 684 | /* feature name starting with the not/and/or */ |
| 685 | last_not = 0; |
| 686 | f_size++; |
| 687 | } else if (c[i] == 'n') { /* not operation */ |
| 688 | if (last_not) { |
| 689 | /* double not */ |
| 690 | expr_size = expr_size - 2; |
| 691 | last_not = 0; |
| 692 | } else { |
| 693 | last_not = 1; |
| 694 | } |
| 695 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 696 | if (f_exp != f_size) { |
| 697 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 698 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 699 | return LY_EVALID; |
| 700 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 701 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 702 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 703 | /* not a not operation */ |
| 704 | last_not = 0; |
| 705 | } |
| 706 | i += r; |
| 707 | } else { |
| 708 | f_size++; |
| 709 | last_not = 0; |
| 710 | } |
| 711 | expr_size++; |
| 712 | |
| 713 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 714 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 715 | i--; |
| 716 | break; |
| 717 | } |
| 718 | i++; |
| 719 | } |
| 720 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 721 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 722 | /* not matching count of ( and ) */ |
| 723 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 724 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 725 | return LY_EVALID; |
| 726 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 727 | if (f_exp != f_size) { |
| 728 | /* features do not match the needed arguments for the logical operations */ |
| 729 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 730 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 731 | "the required number of operands for the operations.", *value); |
| 732 | return LY_EVALID; |
| 733 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 734 | |
| 735 | if (checkversion || expr_size > 1) { |
| 736 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 737 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 738 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 739 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 740 | return LY_EVALID; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | /* allocate the memory */ |
| 745 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 746 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 747 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 748 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 749 | |
| 750 | stack.size = expr_size; |
| 751 | f_size--; expr_size--; /* used as indexes from now */ |
| 752 | |
| 753 | for (i--; i >= 0; i--) { |
| 754 | if (c[i] == ')') { |
| 755 | /* push it on stack */ |
| 756 | iff_stack_push(&stack, LYS_IFF_RP); |
| 757 | continue; |
| 758 | } else if (c[i] == '(') { |
| 759 | /* pop from the stack into result all operators until ) */ |
| 760 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 761 | iff_setop(iff->expr, op, expr_size--); |
| 762 | } |
| 763 | continue; |
| 764 | } else if (isspace(c[i])) { |
| 765 | continue; |
| 766 | } |
| 767 | |
| 768 | /* end of operator or operand -> find beginning and get what is it */ |
| 769 | j = i + 1; |
| 770 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 771 | i--; |
| 772 | } |
| 773 | i++; /* go back by one step */ |
| 774 | |
| 775 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 776 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 777 | /* double not */ |
| 778 | iff_stack_pop(&stack); |
| 779 | } else { |
| 780 | /* not has the highest priority, so do not pop from the stack |
| 781 | * as in case of AND and OR */ |
| 782 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 783 | } |
| 784 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 785 | /* as for OR - pop from the stack all operators with the same or higher |
| 786 | * priority and store them to the result, then push the AND to the stack */ |
| 787 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 788 | op = iff_stack_pop(&stack); |
| 789 | iff_setop(iff->expr, op, expr_size--); |
| 790 | } |
| 791 | iff_stack_push(&stack, LYS_IFF_AND); |
| 792 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 793 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 794 | op = iff_stack_pop(&stack); |
| 795 | iff_setop(iff->expr, op, expr_size--); |
| 796 | } |
| 797 | iff_stack_push(&stack, LYS_IFF_OR); |
| 798 | } else { |
| 799 | /* feature name, length is j - i */ |
| 800 | |
| 801 | /* add it to the expression */ |
| 802 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 803 | |
| 804 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 805 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 806 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 807 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 808 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 809 | iff->features[f_size] = f; |
| 810 | LY_ARRAY_INCREMENT(iff->features); |
| 811 | f_size--; |
| 812 | } |
| 813 | } |
| 814 | while (stack.index) { |
| 815 | op = iff_stack_pop(&stack); |
| 816 | iff_setop(iff->expr, op, expr_size--); |
| 817 | } |
| 818 | |
| 819 | if (++expr_size || ++f_size) { |
| 820 | /* not all expected operators and operands found */ |
| 821 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 822 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 823 | rc = LY_EINT; |
| 824 | } else { |
| 825 | rc = LY_SUCCESS; |
| 826 | } |
| 827 | |
| 828 | error: |
| 829 | /* cleanup */ |
| 830 | iff_stack_clean(&stack); |
| 831 | |
| 832 | return rc; |
| 833 | } |
| 834 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 835 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 836 | * @brief Get the XPath context node for the given schema node. |
| 837 | * @param[in] start The schema node where the XPath expression appears. |
| 838 | * @return The context node to evaluate XPath expression in given schema node. |
| 839 | * @return NULL in case the context node is the root node. |
| 840 | */ |
| 841 | static struct lysc_node * |
| 842 | lysc_xpath_context(struct lysc_node *start) |
| 843 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 844 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 845 | start = start->parent); |
| 846 | return start; |
| 847 | } |
| 848 | |
| 849 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 850 | * @brief Compile information from the when statement |
| 851 | * @param[in] ctx Compile context. |
| 852 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 853 | * @param[in] flags Flags of the node with the "when" defiition. |
| 854 | * @param[in] node Node that inherited the "when" definition, must be connected to parents. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 855 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 856 | * @return LY_ERR value. |
| 857 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 858 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 859 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 860 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 861 | LY_ERR ret = LY_SUCCESS; |
| 862 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 863 | *when = calloc(1, sizeof **when); |
| 864 | (*when)->refcount = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 865 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 866 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 867 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 868 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 869 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 870 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 871 | COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 872 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 873 | |
| 874 | done: |
| 875 | return ret; |
| 876 | } |
| 877 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 878 | /** |
| 879 | * @brief Compile information from the must statement |
| 880 | * @param[in] ctx Compile context. |
| 881 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 882 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 883 | * @return LY_ERR value. |
| 884 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 885 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 886 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 887 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 888 | LY_ERR ret = LY_SUCCESS; |
| 889 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 890 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 891 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 892 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 893 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 894 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 895 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 896 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 897 | COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 898 | |
| 899 | done: |
| 900 | return ret; |
| 901 | } |
| 902 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 903 | /** |
| 904 | * @brief Compile information from the import statement |
| 905 | * @param[in] ctx Compile context. |
| 906 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 907 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 908 | * @return LY_ERR value. |
| 909 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 910 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 911 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 912 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 913 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 914 | LY_ERR ret = LY_SUCCESS; |
| 915 | |
| 916 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 917 | COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 918 | imp->module = imp_p->module; |
| 919 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 920 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 921 | * 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] | 922 | * 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] | 923 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 924 | /* try to use filepath if present */ |
| 925 | if (imp->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 926 | struct ly_in *in; |
| 927 | if (ly_in_new_filepath(imp->module->filepath, 0, &in)) { |
| 928 | LOGINT(ctx->ctx); |
| 929 | } else { |
| 930 | mod = lys_parse(ctx->ctx, in, !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG); |
| 931 | if (mod != imp->module) { |
| 932 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", imp->module->filepath, imp->module->name); |
| 933 | mod = NULL; |
| 934 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 935 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 936 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 937 | } |
| 938 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 939 | 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] | 940 | 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] | 941 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 942 | return LY_ENOTFOUND; |
| 943 | } |
| 944 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | done: |
| 948 | return ret; |
| 949 | } |
| 950 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 951 | /** |
| 952 | * @brief Compile information from the identity statement |
| 953 | * |
| 954 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 955 | * |
| 956 | * @param[in] ctx Compile context. |
| 957 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 958 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 959 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 960 | * @return LY_ERR value. |
| 961 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 962 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 963 | lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, struct lysc_ident *idents, struct lysc_ident *ident) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 964 | { |
| 965 | unsigned int u; |
| 966 | LY_ERR ret = LY_SUCCESS; |
| 967 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 968 | lysc_update_path(ctx, NULL, ident_p->name); |
| 969 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 970 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 971 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 972 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 973 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 974 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 975 | COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 976 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 977 | COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 978 | ident->flags = ident_p->flags; |
| 979 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 980 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 981 | done: |
| 982 | return ret; |
| 983 | } |
| 984 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 985 | /** |
| 986 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 987 | * |
| 988 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 989 | * |
| 990 | * @param[in] ctx Compile context for logging. |
| 991 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 992 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 993 | * @return LY_SUCCESS if everything is ok. |
| 994 | * @return LY_EVALID if the identity is derived from itself. |
| 995 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 996 | static LY_ERR |
| 997 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 998 | { |
| 999 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1000 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1001 | struct ly_set recursion = {0}; |
| 1002 | struct lysc_ident *drv; |
| 1003 | |
| 1004 | if (!derived) { |
| 1005 | return LY_SUCCESS; |
| 1006 | } |
| 1007 | |
| 1008 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 1009 | if (ident == derived[u]) { |
| 1010 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1011 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1012 | goto cleanup; |
| 1013 | } |
| 1014 | ly_set_add(&recursion, derived[u], 0); |
| 1015 | } |
| 1016 | |
| 1017 | for (v = 0; v < recursion.count; ++v) { |
| 1018 | drv = recursion.objs[v]; |
| 1019 | if (!drv->derived) { |
| 1020 | continue; |
| 1021 | } |
| 1022 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 1023 | if (ident == drv->derived[u]) { |
| 1024 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1025 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1026 | goto cleanup; |
| 1027 | } |
| 1028 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1029 | } |
| 1030 | } |
| 1031 | ret = LY_SUCCESS; |
| 1032 | |
| 1033 | cleanup: |
| 1034 | ly_set_erase(&recursion, NULL); |
| 1035 | return ret; |
| 1036 | } |
| 1037 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1038 | /** |
| 1039 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1040 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1041 | * For bases in identity set backlinks to them from the base identities. For identityref, store |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1042 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1043 | * to distinguish these two use cases. |
| 1044 | * |
| 1045 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1046 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 1047 | * @param[in] ident Referencing identity to work with. |
| 1048 | * @param[in] bases Array of bases of identityref to fill in. |
| 1049 | * @return LY_ERR value. |
| 1050 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1051 | static LY_ERR |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1052 | lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1053 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1054 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1055 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1056 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1057 | struct lysc_ident **idref; |
| 1058 | |
| 1059 | assert(ident || bases); |
| 1060 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1061 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1062 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1063 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1064 | return LY_EVALID; |
| 1065 | } |
| 1066 | |
| 1067 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 1068 | s = strchr(bases_p[u], ':'); |
| 1069 | if (s) { |
| 1070 | /* prefixed identity */ |
| 1071 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1072 | mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1073 | } else { |
| 1074 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1075 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1076 | } |
| 1077 | if (!mod) { |
| 1078 | if (ident) { |
| 1079 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1080 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1081 | } else { |
| 1082 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1083 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1084 | } |
| 1085 | return LY_EVALID; |
| 1086 | } |
| 1087 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1088 | if (mod->compiled && mod->compiled->identities) { |
| 1089 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 1090 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1091 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1092 | if (ident == &mod->compiled->identities[v]) { |
| 1093 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1094 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1095 | return LY_EVALID; |
| 1096 | } |
| 1097 | 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] | 1098 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1099 | 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] | 1100 | *idref = ident; |
| 1101 | } else { |
| 1102 | /* we have match! store the found identity */ |
| 1103 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1104 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1105 | } |
| 1106 | break; |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | if (!idref || !(*idref)) { |
| 1111 | if (ident) { |
| 1112 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1113 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1114 | } else { |
| 1115 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1116 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1117 | } |
| 1118 | return LY_EVALID; |
| 1119 | } |
| 1120 | } |
| 1121 | return LY_SUCCESS; |
| 1122 | } |
| 1123 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1124 | /** |
| 1125 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1126 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1127 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1128 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1129 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1130 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1131 | static LY_ERR |
| 1132 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1133 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1134 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1135 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1136 | for (u = 0; u < LY_ARRAY_SIZE(idents_p); ++u) { |
| 1137 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1138 | continue; |
| 1139 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1140 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1141 | LY_CHECK_RET(lys_compile_identity_bases(ctx, idents[u].module, idents_p[u].bases, &idents[u], NULL)); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1142 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1143 | } |
| 1144 | return LY_SUCCESS; |
| 1145 | } |
| 1146 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1147 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1148 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1149 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1150 | LY_ARRAY_SIZE_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1151 | struct lysc_ctx context = {0}; |
| 1152 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1153 | assert(ctx_sc || ctx); |
| 1154 | |
| 1155 | if (!ctx_sc) { |
| 1156 | context.ctx = ctx; |
| 1157 | context.mod = module; |
| 1158 | context.path_len = 1; |
| 1159 | context.path[0] = '/'; |
| 1160 | ctx_sc = &context; |
| 1161 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1162 | |
| 1163 | if (!features_p) { |
| 1164 | return LY_SUCCESS; |
| 1165 | } |
| 1166 | if (*features) { |
| 1167 | offset = LY_ARRAY_SIZE(*features); |
| 1168 | } |
| 1169 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1170 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 1171 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1172 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1173 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1174 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1175 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1176 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1177 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1178 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1179 | DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1180 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1181 | (*features)[offset + u].module = ctx_sc->mod; |
| 1182 | |
| 1183 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1184 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1185 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1186 | |
| 1187 | return LY_SUCCESS; |
| 1188 | } |
| 1189 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1190 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1191 | * @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] | 1192 | * |
| 1193 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1194 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1195 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1196 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1197 | * being currently processed). |
| 1198 | * @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] | 1199 | * @return LY_SUCCESS if everything is ok. |
| 1200 | * @return LY_EVALID if the feature references indirectly itself. |
| 1201 | */ |
| 1202 | static LY_ERR |
| 1203 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1204 | { |
| 1205 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1206 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1207 | struct ly_set recursion = {0}; |
| 1208 | struct lysc_feature *drv; |
| 1209 | |
| 1210 | if (!depfeatures) { |
| 1211 | return LY_SUCCESS; |
| 1212 | } |
| 1213 | |
| 1214 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1215 | if (feature == depfeatures[u]) { |
| 1216 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1217 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1218 | goto cleanup; |
| 1219 | } |
| 1220 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1221 | } |
| 1222 | |
| 1223 | for (v = 0; v < recursion.count; ++v) { |
| 1224 | drv = recursion.objs[v]; |
| 1225 | if (!drv->depfeatures) { |
| 1226 | continue; |
| 1227 | } |
| 1228 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1229 | if (feature == drv->depfeatures[u]) { |
| 1230 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1231 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1232 | goto cleanup; |
| 1233 | } |
| 1234 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1235 | } |
| 1236 | } |
| 1237 | ret = LY_SUCCESS; |
| 1238 | |
| 1239 | cleanup: |
| 1240 | ly_set_erase(&recursion, NULL); |
| 1241 | return ret; |
| 1242 | } |
| 1243 | |
| 1244 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1245 | * @brief Create pre-compiled features array. |
| 1246 | * |
| 1247 | * See lys_feature_precompile() for more details. |
| 1248 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1249 | * @param[in] ctx Compile context. |
| 1250 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1251 | * @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] | 1252 | * @return LY_ERR value. |
| 1253 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1254 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1255 | lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1256 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1257 | LY_ARRAY_SIZE_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1258 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1259 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1260 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1261 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1262 | /* find the preprecompiled feature */ |
| 1263 | LY_ARRAY_FOR(features, x) { |
| 1264 | if (strcmp(features[x].name, feature_p->name)) { |
| 1265 | continue; |
| 1266 | } |
| 1267 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1268 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1269 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1270 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1271 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1272 | COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1273 | COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1274 | if (feature->iffeatures) { |
| 1275 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1276 | if (feature->iffeatures[u].features) { |
| 1277 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1278 | /* check for circular dependency - direct reference first,... */ |
| 1279 | if (feature == feature->iffeatures[u].features[v]) { |
| 1280 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1281 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1282 | return LY_EVALID; |
| 1283 | } |
| 1284 | /* ... and indirect circular reference */ |
| 1285 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1286 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1287 | /* add itself into the dependants list */ |
| 1288 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1289 | *df = feature; |
| 1290 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1291 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1292 | } |
| 1293 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1294 | lysc_update_path(ctx, NULL, NULL); |
| 1295 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1296 | done: |
| 1297 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1298 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1299 | |
| 1300 | LOGINT(ctx->ctx); |
| 1301 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1302 | } |
| 1303 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1304 | /** |
| 1305 | * @brief Revert compiled list of features back to the precompiled state. |
| 1306 | * |
| 1307 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1308 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1309 | * |
| 1310 | * @param[in] ctx Compilation context. |
| 1311 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1312 | * and supposed to hold the off_features list. |
| 1313 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1314 | static void |
| 1315 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1316 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1317 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1318 | |
| 1319 | /* keep the off_features list until the complete lys_module is freed */ |
| 1320 | mod->off_features = mod->compiled->features; |
| 1321 | mod->compiled->features = NULL; |
| 1322 | |
| 1323 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1324 | * which may points into the data being freed here */ |
| 1325 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1326 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1327 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1328 | } |
| 1329 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1330 | mod->off_features[u].iffeatures = NULL; |
| 1331 | |
| 1332 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1333 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1334 | } |
| 1335 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1336 | mod->off_features[u].exts = NULL; |
| 1337 | } |
| 1338 | } |
| 1339 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1340 | /** |
| 1341 | * @brief Validate and normalize numeric value from a range definition. |
| 1342 | * @param[in] ctx Compile context. |
| 1343 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1344 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1345 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1346 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1347 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1348 | * @param[in] value String value of the range boundary. |
| 1349 | * @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. |
| 1350 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1351 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1352 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1353 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1354 | 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] | 1355 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1356 | size_t fraction = 0, size; |
| 1357 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1358 | *len = 0; |
| 1359 | |
| 1360 | assert(value); |
| 1361 | /* parse value */ |
| 1362 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1363 | return LY_EVALID; |
| 1364 | } |
| 1365 | |
| 1366 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1367 | ++(*len); |
| 1368 | } |
| 1369 | |
| 1370 | while (isdigit(value[*len])) { |
| 1371 | ++(*len); |
| 1372 | } |
| 1373 | |
| 1374 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1375 | if (basetype == LY_TYPE_DEC64) { |
| 1376 | goto decimal; |
| 1377 | } else { |
| 1378 | *valcopy = strndup(value, *len); |
| 1379 | return LY_SUCCESS; |
| 1380 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1381 | } |
| 1382 | fraction = *len; |
| 1383 | |
| 1384 | ++(*len); |
| 1385 | while (isdigit(value[*len])) { |
| 1386 | ++(*len); |
| 1387 | } |
| 1388 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1389 | if (basetype == LY_TYPE_DEC64) { |
| 1390 | decimal: |
| 1391 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1392 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1393 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1394 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1395 | *len, value, frdigits); |
| 1396 | return LY_EINVAL; |
| 1397 | } |
| 1398 | if (fraction) { |
| 1399 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1400 | } else { |
| 1401 | size = (*len) + frdigits + 1; |
| 1402 | } |
| 1403 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1404 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1405 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1406 | (*valcopy)[size - 1] = '\0'; |
| 1407 | if (fraction) { |
| 1408 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1409 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1410 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1411 | } else { |
| 1412 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1413 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1414 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1415 | } |
| 1416 | return LY_SUCCESS; |
| 1417 | } |
| 1418 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1419 | /** |
| 1420 | * @brief Check that values in range are in ascendant order. |
| 1421 | * @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] | 1422 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1423 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1424 | * @param[in] value Current value to check. |
| 1425 | * @param[in] prev_value The last seen value. |
| 1426 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1427 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1428 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1429 | 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] | 1430 | { |
| 1431 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1432 | 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] | 1433 | return LY_EEXIST; |
| 1434 | } |
| 1435 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1436 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1437 | return LY_EEXIST; |
| 1438 | } |
| 1439 | } |
| 1440 | return LY_SUCCESS; |
| 1441 | } |
| 1442 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1443 | /** |
| 1444 | * @brief Set min/max value of the range part. |
| 1445 | * @param[in] ctx Compile context. |
| 1446 | * @param[in] part Range part structure to fill. |
| 1447 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1448 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1449 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1450 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1451 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1452 | * @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] | 1453 | * @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] | 1454 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1455 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1456 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1457 | * frdigits value), LY_EMEM. |
| 1458 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1459 | static LY_ERR |
| 1460 | 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] | 1461 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1462 | { |
| 1463 | LY_ERR ret = LY_SUCCESS; |
| 1464 | char *valcopy = NULL; |
| 1465 | size_t len; |
| 1466 | |
| 1467 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1468 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1469 | LY_CHECK_GOTO(ret, finalize); |
| 1470 | } |
| 1471 | if (!valcopy && base_range) { |
| 1472 | if (max) { |
| 1473 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1474 | } else { |
| 1475 | part->min_64 = base_range->parts[0].min_64; |
| 1476 | } |
| 1477 | if (!first) { |
| 1478 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1479 | } |
| 1480 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1484 | case LY_TYPE_INT8: /* range */ |
| 1485 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1486 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1487 | } else if (max) { |
| 1488 | part->max_64 = INT64_C(127); |
| 1489 | } else { |
| 1490 | part->min_64 = INT64_C(-128); |
| 1491 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1492 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1493 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1494 | } |
| 1495 | break; |
| 1496 | case LY_TYPE_INT16: /* range */ |
| 1497 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1498 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1499 | } else if (max) { |
| 1500 | part->max_64 = INT64_C(32767); |
| 1501 | } else { |
| 1502 | part->min_64 = INT64_C(-32768); |
| 1503 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1504 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1505 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1506 | } |
| 1507 | break; |
| 1508 | case LY_TYPE_INT32: /* range */ |
| 1509 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1510 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1511 | } else if (max) { |
| 1512 | part->max_64 = INT64_C(2147483647); |
| 1513 | } else { |
| 1514 | part->min_64 = INT64_C(-2147483648); |
| 1515 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1516 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1517 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1518 | } |
| 1519 | break; |
| 1520 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1521 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1522 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1523 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1524 | max ? &part->max_64 : &part->min_64); |
| 1525 | } else if (max) { |
| 1526 | part->max_64 = INT64_C(9223372036854775807); |
| 1527 | } else { |
| 1528 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1529 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1530 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1531 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1532 | } |
| 1533 | break; |
| 1534 | case LY_TYPE_UINT8: /* range */ |
| 1535 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1536 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1537 | } else if (max) { |
| 1538 | part->max_u64 = UINT64_C(255); |
| 1539 | } else { |
| 1540 | part->min_u64 = UINT64_C(0); |
| 1541 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1542 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1543 | 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] | 1544 | } |
| 1545 | break; |
| 1546 | case LY_TYPE_UINT16: /* range */ |
| 1547 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1548 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1549 | } else if (max) { |
| 1550 | part->max_u64 = UINT64_C(65535); |
| 1551 | } else { |
| 1552 | part->min_u64 = UINT64_C(0); |
| 1553 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1554 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1555 | 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] | 1556 | } |
| 1557 | break; |
| 1558 | case LY_TYPE_UINT32: /* range */ |
| 1559 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1560 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1561 | } else if (max) { |
| 1562 | part->max_u64 = UINT64_C(4294967295); |
| 1563 | } else { |
| 1564 | part->min_u64 = UINT64_C(0); |
| 1565 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1566 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1567 | 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] | 1568 | } |
| 1569 | break; |
| 1570 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1571 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1572 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1573 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1574 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1575 | } else if (max) { |
| 1576 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1577 | } else { |
| 1578 | part->min_u64 = UINT64_C(0); |
| 1579 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1580 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1581 | 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] | 1582 | } |
| 1583 | break; |
| 1584 | default: |
| 1585 | LOGINT(ctx->ctx); |
| 1586 | ret = LY_EINT; |
| 1587 | } |
| 1588 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1589 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1590 | if (ret == LY_EDENIED) { |
| 1591 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1592 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1593 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1594 | } else if (ret == LY_EVALID) { |
| 1595 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1596 | "Invalid %s restriction - invalid value \"%s\".", |
| 1597 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1598 | } else if (ret == LY_EEXIST) { |
| 1599 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1600 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1601 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1602 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1603 | } else if (!ret && value) { |
| 1604 | *value = *value + len; |
| 1605 | } |
| 1606 | free(valcopy); |
| 1607 | return ret; |
| 1608 | } |
| 1609 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1610 | /** |
| 1611 | * @brief Compile the parsed range restriction. |
| 1612 | * @param[in] ctx Compile context. |
| 1613 | * @param[in] range_p Parsed range structure to compile. |
| 1614 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1615 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1616 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1617 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1618 | * range restriction must be more restrictive than the base_range. |
| 1619 | * @param[in,out] range Pointer to the created current range structure. |
| 1620 | * @return LY_ERR value. |
| 1621 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1622 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1623 | 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] | 1624 | struct lysc_range *base_range, struct lysc_range **range) |
| 1625 | { |
| 1626 | LY_ERR ret = LY_EVALID; |
| 1627 | const char *expr; |
| 1628 | struct lysc_range_part *parts = NULL, *part; |
| 1629 | int range_expected = 0, uns; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1630 | LY_ARRAY_SIZE_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1631 | |
| 1632 | assert(range); |
| 1633 | assert(range_p); |
| 1634 | |
| 1635 | expr = range_p->arg; |
| 1636 | while(1) { |
| 1637 | if (isspace(*expr)) { |
| 1638 | ++expr; |
| 1639 | } else if (*expr == '\0') { |
| 1640 | if (range_expected) { |
| 1641 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1642 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1643 | length_restr ? "length" : "range", range_p->arg); |
| 1644 | goto cleanup; |
| 1645 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1646 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1647 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1648 | length_restr ? "length" : "range", range_p->arg); |
| 1649 | goto cleanup; |
| 1650 | } |
| 1651 | parts_done++; |
| 1652 | break; |
| 1653 | } else if (!strncmp(expr, "min", 3)) { |
| 1654 | if (parts) { |
| 1655 | /* min cannot be used elsewhere than in the first part */ |
| 1656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1657 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1658 | expr - range_p->arg, range_p->arg); |
| 1659 | goto cleanup; |
| 1660 | } |
| 1661 | expr += 3; |
| 1662 | |
| 1663 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1664 | 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] | 1665 | part->max_64 = part->min_64; |
| 1666 | } else if (*expr == '|') { |
| 1667 | if (!parts || range_expected) { |
| 1668 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1669 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1670 | goto cleanup; |
| 1671 | } |
| 1672 | expr++; |
| 1673 | parts_done++; |
| 1674 | /* process next part of the expression */ |
| 1675 | } else if (!strncmp(expr, "..", 2)) { |
| 1676 | expr += 2; |
| 1677 | while (isspace(*expr)) { |
| 1678 | expr++; |
| 1679 | } |
| 1680 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1682 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1683 | goto cleanup; |
| 1684 | } |
| 1685 | /* continue expecting the upper boundary */ |
| 1686 | range_expected = 1; |
| 1687 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1688 | /* number */ |
| 1689 | if (range_expected) { |
| 1690 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1691 | 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] | 1692 | range_expected = 0; |
| 1693 | } else { |
| 1694 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1695 | 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] | 1696 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1697 | part->max_64 = part->min_64; |
| 1698 | } |
| 1699 | |
| 1700 | /* continue with possible another expression part */ |
| 1701 | } else if (!strncmp(expr, "max", 3)) { |
| 1702 | expr += 3; |
| 1703 | while (isspace(*expr)) { |
| 1704 | expr++; |
| 1705 | } |
| 1706 | if (*expr != '\0') { |
| 1707 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1708 | length_restr ? "length" : "range", expr); |
| 1709 | goto cleanup; |
| 1710 | } |
| 1711 | if (range_expected) { |
| 1712 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1713 | 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] | 1714 | range_expected = 0; |
| 1715 | } else { |
| 1716 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1717 | 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] | 1718 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1719 | part->min_64 = part->max_64; |
| 1720 | } |
| 1721 | } else { |
| 1722 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1723 | length_restr ? "length" : "range", expr); |
| 1724 | goto cleanup; |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | /* check with the previous range/length restriction */ |
| 1729 | if (base_range) { |
| 1730 | switch (basetype) { |
| 1731 | case LY_TYPE_BINARY: |
| 1732 | case LY_TYPE_UINT8: |
| 1733 | case LY_TYPE_UINT16: |
| 1734 | case LY_TYPE_UINT32: |
| 1735 | case LY_TYPE_UINT64: |
| 1736 | case LY_TYPE_STRING: |
| 1737 | uns = 1; |
| 1738 | break; |
| 1739 | case LY_TYPE_DEC64: |
| 1740 | case LY_TYPE_INT8: |
| 1741 | case LY_TYPE_INT16: |
| 1742 | case LY_TYPE_INT32: |
| 1743 | case LY_TYPE_INT64: |
| 1744 | uns = 0; |
| 1745 | break; |
| 1746 | default: |
| 1747 | LOGINT(ctx->ctx); |
| 1748 | ret = LY_EINT; |
| 1749 | goto cleanup; |
| 1750 | } |
| 1751 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1752 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1753 | goto baseerror; |
| 1754 | } |
| 1755 | /* current lower bound is not lower than the base */ |
| 1756 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1757 | /* base has single value */ |
| 1758 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1759 | /* both lower bounds are the same */ |
| 1760 | if (parts[u].min_64 != parts[u].max_64) { |
| 1761 | /* current continues with a range */ |
| 1762 | goto baseerror; |
| 1763 | } else { |
| 1764 | /* equal single values, move both forward */ |
| 1765 | ++v; |
| 1766 | continue; |
| 1767 | } |
| 1768 | } else { |
| 1769 | /* base is single value lower than current range, so the |
| 1770 | * value from base range is removed in the current, |
| 1771 | * move only base and repeat checking */ |
| 1772 | ++v; |
| 1773 | --u; |
| 1774 | continue; |
| 1775 | } |
| 1776 | } else { |
| 1777 | /* base is the range */ |
| 1778 | if (parts[u].min_64 == parts[u].max_64) { |
| 1779 | /* current is a single value */ |
| 1780 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1781 | /* current is behind the base range, so base range is omitted, |
| 1782 | * move the base and keep the current for further check */ |
| 1783 | ++v; |
| 1784 | --u; |
| 1785 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1786 | continue; |
| 1787 | } else { |
| 1788 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1789 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1790 | /* higher bound is higher than the current higher bound */ |
| 1791 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1792 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1793 | * continue with the same current, but move the base */ |
| 1794 | --u; |
| 1795 | ++v; |
| 1796 | continue; |
| 1797 | } |
| 1798 | /* current range starts within the base range but end behind it */ |
| 1799 | goto baseerror; |
| 1800 | } else { |
| 1801 | /* current range is smaller than the base, |
| 1802 | * move current, but stay with the base */ |
| 1803 | continue; |
| 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | if (u != parts_done) { |
| 1809 | baseerror: |
| 1810 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1811 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1812 | length_restr ? "length" : "range", range_p->arg); |
| 1813 | goto cleanup; |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | if (!(*range)) { |
| 1818 | *range = calloc(1, sizeof **range); |
| 1819 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1820 | } |
| 1821 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1822 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1823 | if (range_p->eapptag) { |
| 1824 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1825 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1826 | } |
| 1827 | if (range_p->emsg) { |
| 1828 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1829 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1830 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1831 | if (range_p->dsc) { |
| 1832 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1833 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1834 | } |
| 1835 | if (range_p->ref) { |
| 1836 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1837 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1838 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1839 | /* extensions are taken only from the last range by the caller */ |
| 1840 | |
| 1841 | (*range)->parts = parts; |
| 1842 | parts = NULL; |
| 1843 | ret = LY_SUCCESS; |
| 1844 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1845 | LY_ARRAY_FREE(parts); |
| 1846 | |
| 1847 | return ret; |
| 1848 | } |
| 1849 | |
| 1850 | /** |
| 1851 | * @brief Checks pattern syntax. |
| 1852 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1853 | * @param[in] ctx Context. |
| 1854 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1855 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1856 | * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1857 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1858 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1859 | LY_ERR |
| 1860 | lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1861 | { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1862 | int idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1863 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1864 | int err_code; |
| 1865 | const char *orig_ptr; |
| 1866 | PCRE2_SIZE err_offset; |
| 1867 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1868 | #define URANGE_LEN 19 |
| 1869 | char *ublock2urange[][2] = { |
| 1870 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1871 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1872 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1873 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1874 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1875 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1876 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1877 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1878 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1879 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1880 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1881 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1882 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1883 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1884 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1885 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1886 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1887 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1888 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1889 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1890 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1891 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1892 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1893 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1894 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1895 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1896 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1897 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1898 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1899 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1900 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1901 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1902 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1903 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1904 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1905 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1906 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1907 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1908 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1909 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1910 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1911 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1912 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1913 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1914 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1915 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1916 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1917 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1918 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1919 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1920 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1921 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1922 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1923 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1924 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1925 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1926 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1927 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1928 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1929 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1930 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1931 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1932 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1933 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1934 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1935 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1936 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1937 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1938 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1939 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1940 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1941 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1942 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1943 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1944 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1945 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1946 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1947 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1948 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1949 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1950 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1951 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1952 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1953 | {NULL, NULL} |
| 1954 | }; |
| 1955 | |
| 1956 | /* adjust the expression to a Perl equivalent |
| 1957 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1958 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1959 | /* allocate space for the transformed pattern */ |
| 1960 | size = strlen(pattern) + 1; |
| 1961 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1962 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1963 | perl_regex[0] = '\0'; |
| 1964 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1965 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 1966 | brack = 0; |
| 1967 | idx = 0; |
| 1968 | orig_ptr = pattern; |
| 1969 | while (orig_ptr[0]) { |
| 1970 | switch (orig_ptr[0]) { |
| 1971 | case '$': |
| 1972 | case '^': |
| 1973 | if (!brack) { |
| 1974 | /* make space for the extra character */ |
| 1975 | ++size; |
| 1976 | perl_regex = ly_realloc(perl_regex, size); |
| 1977 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1978 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1979 | /* print escape slash */ |
| 1980 | perl_regex[idx] = '\\'; |
| 1981 | ++idx; |
| 1982 | } |
| 1983 | break; |
| 1984 | case '[': |
| 1985 | /* must not be escaped */ |
| 1986 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 1987 | ++brack; |
| 1988 | } |
| 1989 | break; |
| 1990 | case ']': |
| 1991 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 1992 | /* pattern was checked and compiled already */ |
| 1993 | assert(brack); |
| 1994 | --brack; |
| 1995 | } |
| 1996 | break; |
| 1997 | default: |
| 1998 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1999 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2000 | |
| 2001 | /* copy char */ |
| 2002 | perl_regex[idx] = orig_ptr[0]; |
| 2003 | |
| 2004 | ++idx; |
| 2005 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2006 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2007 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2008 | |
| 2009 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2010 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2011 | start = ptr - perl_regex; |
| 2012 | |
| 2013 | ptr = strchr(ptr, '}'); |
| 2014 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2015 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2016 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2017 | free(perl_regex); |
| 2018 | return LY_EVALID; |
| 2019 | } |
| 2020 | end = (ptr - perl_regex) + 1; |
| 2021 | |
| 2022 | /* need more space */ |
| 2023 | if (end - start < URANGE_LEN) { |
| 2024 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2025 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | /* find our range */ |
| 2029 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2030 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2031 | break; |
| 2032 | } |
| 2033 | } |
| 2034 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2035 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2036 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2037 | free(perl_regex); |
| 2038 | return LY_EVALID; |
| 2039 | } |
| 2040 | |
| 2041 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2042 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2043 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2044 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2045 | } |
| 2046 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2047 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2048 | } |
| 2049 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2050 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2051 | /* skip brackets */ |
| 2052 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2053 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2054 | } else { |
| 2055 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2056 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2061 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2062 | PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE, |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2063 | &err_code, &err_offset, NULL); |
| 2064 | if (!code_local) { |
| 2065 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2066 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2067 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2068 | free(perl_regex); |
| 2069 | return LY_EVALID; |
| 2070 | } |
| 2071 | free(perl_regex); |
| 2072 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2073 | if (code) { |
| 2074 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2075 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2076 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | return LY_SUCCESS; |
| 2080 | |
| 2081 | #undef URANGE_LEN |
| 2082 | } |
| 2083 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2084 | /** |
| 2085 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2086 | * @param[in] ctx Compile context. |
| 2087 | * @param[in] patterns_p Array of parsed patterns from the current type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2088 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2089 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2090 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2091 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2092 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2093 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2094 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2095 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2096 | { |
| 2097 | struct lysc_pattern **pattern; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2098 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2099 | LY_ERR ret = LY_SUCCESS; |
| 2100 | |
| 2101 | /* first, copy the patterns from the base type */ |
| 2102 | if (base_patterns) { |
| 2103 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2104 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2105 | } |
| 2106 | |
| 2107 | LY_ARRAY_FOR(patterns_p, u) { |
| 2108 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2109 | *pattern = calloc(1, sizeof **pattern); |
| 2110 | ++(*pattern)->refcount; |
| 2111 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2112 | ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2113 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2114 | |
| 2115 | if (patterns_p[u].arg[0] == 0x15) { |
| 2116 | (*pattern)->inverted = 1; |
| 2117 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2118 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2119 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2120 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2121 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2122 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2123 | COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2124 | } |
| 2125 | done: |
| 2126 | return ret; |
| 2127 | } |
| 2128 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2129 | /** |
| 2130 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2131 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2132 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2133 | 0 /* LY_TYPE_UNKNOWN */, |
| 2134 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2135 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2136 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2137 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2138 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2139 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2140 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2141 | 0 /* LY_TYPE_BOOL */, |
| 2142 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2143 | 0 /* LY_TYPE_EMPTY */, |
| 2144 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2145 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2146 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2147 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2148 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2149 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2150 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2151 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2152 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2153 | }; |
| 2154 | |
| 2155 | /** |
| 2156 | * @brief stringification of the YANG built-in data types |
| 2157 | */ |
| 2158 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2159 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2160 | "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] | 2161 | }; |
| 2162 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2163 | /** |
| 2164 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2165 | * @param[in] ctx Compile context. |
| 2166 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2167 | * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2168 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2169 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2170 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2171 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2172 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2173 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2174 | struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2175 | { |
| 2176 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2177 | LY_ARRAY_SIZE_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2178 | int32_t value = 0; |
| 2179 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2180 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2181 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2182 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2183 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2184 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2185 | return LY_EVALID; |
| 2186 | } |
| 2187 | |
| 2188 | LY_ARRAY_FOR(enums_p, u) { |
| 2189 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2190 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2191 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2192 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2193 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2194 | if (base_enums) { |
| 2195 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2196 | LY_ARRAY_FOR(base_enums, v) { |
| 2197 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2198 | break; |
| 2199 | } |
| 2200 | } |
| 2201 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 2202 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2203 | "Invalid %s - derived type adds new item \"%s\".", |
| 2204 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2205 | return LY_EVALID; |
| 2206 | } |
| 2207 | match = v; |
| 2208 | } |
| 2209 | |
| 2210 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2211 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2212 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2213 | e->value = (int32_t)enums_p[u].value; |
| 2214 | if (!u || e->value >= value) { |
| 2215 | value = e->value + 1; |
| 2216 | } |
| 2217 | /* check collision with other values */ |
| 2218 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2219 | if (e->value == (*enums)[v].value) { |
| 2220 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2221 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2222 | e->value, e->name, (*enums)[v].name); |
| 2223 | return LY_EVALID; |
| 2224 | } |
| 2225 | } |
| 2226 | } else if (base_enums) { |
| 2227 | /* inherit the assigned value */ |
| 2228 | e->value = base_enums[match].value; |
| 2229 | if (!u || e->value >= value) { |
| 2230 | value = e->value + 1; |
| 2231 | } |
| 2232 | } else { |
| 2233 | /* assign value automatically */ |
| 2234 | if (u && value == INT32_MIN) { |
| 2235 | /* counter overflow */ |
| 2236 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2237 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2238 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2239 | return LY_EVALID; |
| 2240 | } |
| 2241 | e->value = value++; |
| 2242 | } |
| 2243 | } else { /* LY_TYPE_BITS */ |
| 2244 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2245 | e->value = (int32_t)enums_p[u].value; |
| 2246 | if (!u || (uint32_t)e->value >= position) { |
| 2247 | position = (uint32_t)e->value + 1; |
| 2248 | } |
| 2249 | /* check collision with other values */ |
| 2250 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2251 | if (e->value == (*enums)[v].value) { |
| 2252 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2253 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2254 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2255 | return LY_EVALID; |
| 2256 | } |
| 2257 | } |
| 2258 | } else if (base_enums) { |
| 2259 | /* inherit the assigned value */ |
| 2260 | e->value = base_enums[match].value; |
| 2261 | if (!u || (uint32_t)e->value >= position) { |
| 2262 | position = (uint32_t)e->value + 1; |
| 2263 | } |
| 2264 | } else { |
| 2265 | /* assign value automatically */ |
| 2266 | if (u && position == 0) { |
| 2267 | /* counter overflow */ |
| 2268 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2269 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2270 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2271 | return LY_EVALID; |
| 2272 | } |
| 2273 | e->value = position++; |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | if (base_enums) { |
| 2278 | /* the assigned values must not change from the derived type */ |
| 2279 | if (e->value != base_enums[match].value) { |
| 2280 | if (basetype == LY_TYPE_ENUM) { |
| 2281 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2282 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2283 | e->name, base_enums[match].value, e->value); |
| 2284 | } else { |
| 2285 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2286 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2287 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2288 | } |
| 2289 | return LY_EVALID; |
| 2290 | } |
| 2291 | } |
| 2292 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2293 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2294 | COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2295 | |
| 2296 | if (basetype == LY_TYPE_BITS) { |
| 2297 | /* keep bits ordered by position */ |
| 2298 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2299 | if (v != u) { |
| 2300 | memcpy(&storage, e, sizeof *e); |
| 2301 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2302 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2303 | } |
| 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | done: |
| 2308 | return ret; |
| 2309 | } |
| 2310 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2311 | /** |
| 2312 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2313 | * |
| 2314 | * path-arg = absolute-path / relative-path |
| 2315 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2316 | * relative-path = 1*(".." "/") descendant-path |
| 2317 | * |
| 2318 | * @param[in,out] path Path to parse. |
| 2319 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2320 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2321 | * @param[out] name Name of the token. |
| 2322 | * @param[out] nam_len Length of the name. |
| 2323 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2324 | * must not be changed between consecutive calls. -1 if the |
| 2325 | * path is absolute. |
| 2326 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2327 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2328 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2329 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2330 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2331 | int *parent_times, int *has_predicate) |
| 2332 | { |
| 2333 | int par_times = 0; |
| 2334 | |
| 2335 | assert(path && *path); |
| 2336 | assert(parent_times); |
| 2337 | assert(prefix); |
| 2338 | assert(prefix_len); |
| 2339 | assert(name); |
| 2340 | assert(name_len); |
| 2341 | assert(has_predicate); |
| 2342 | |
| 2343 | *prefix = NULL; |
| 2344 | *prefix_len = 0; |
| 2345 | *name = NULL; |
| 2346 | *name_len = 0; |
| 2347 | *has_predicate = 0; |
| 2348 | |
| 2349 | if (!*parent_times) { |
| 2350 | if (!strncmp(*path, "..", 2)) { |
| 2351 | *path += 2; |
| 2352 | ++par_times; |
| 2353 | while (!strncmp(*path, "/..", 3)) { |
| 2354 | *path += 3; |
| 2355 | ++par_times; |
| 2356 | } |
| 2357 | } |
| 2358 | if (par_times) { |
| 2359 | *parent_times = par_times; |
| 2360 | } else { |
| 2361 | *parent_times = -1; |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | if (**path != '/') { |
| 2366 | return LY_EINVAL; |
| 2367 | } |
| 2368 | /* skip '/' */ |
| 2369 | ++(*path); |
| 2370 | |
| 2371 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2372 | LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2373 | |
| 2374 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2375 | /* path continues by another token or this is the last token */ |
| 2376 | return LY_SUCCESS; |
| 2377 | } else if ((*path)[0] != '[') { |
| 2378 | /* unexpected character */ |
| 2379 | return LY_EINVAL; |
| 2380 | } else { |
| 2381 | /* predicate starting with [ */ |
| 2382 | *has_predicate = 1; |
| 2383 | return LY_SUCCESS; |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2388 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2389 | * |
| 2390 | * The set of features used for target must be a subset of features used for the leafref. |
| 2391 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2392 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2393 | * |
| 2394 | * @param[in] refnode The leafref node. |
| 2395 | * @param[in] target Tha target node of the leafref. |
| 2396 | * @return LY_SUCCESS or LY_EVALID; |
| 2397 | */ |
| 2398 | static LY_ERR |
| 2399 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2400 | { |
| 2401 | LY_ERR ret = LY_EVALID; |
| 2402 | const struct lysc_node *iter; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2403 | LY_ARRAY_SIZE_TYPE u, v, count; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2404 | struct ly_set features = {0}; |
| 2405 | |
| 2406 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2407 | if (iter->iffeatures) { |
| 2408 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2409 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2410 | 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] | 2411 | } |
| 2412 | } |
| 2413 | } |
| 2414 | } |
| 2415 | |
| 2416 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2417 | * So when adding features applicable to the target into the features set, we should not be |
| 2418 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2419 | * to the leafref itself. */ |
| 2420 | count = features.count; |
| 2421 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2422 | if (iter->iffeatures) { |
| 2423 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2424 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2425 | 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] | 2426 | /* new feature was added (or LY_EMEM) */ |
| 2427 | goto cleanup; |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | } |
| 2432 | } |
| 2433 | ret = LY_SUCCESS; |
| 2434 | |
| 2435 | cleanup: |
| 2436 | ly_set_erase(&features, NULL); |
| 2437 | return ret; |
| 2438 | } |
| 2439 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2440 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2441 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2442 | struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2443 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2444 | /** |
| 2445 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2446 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2447 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2448 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2449 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2450 | * @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] | 2451 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2452 | * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2453 | * @param[in] basetype Base YANG built-in type of the type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2454 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2455 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2456 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2457 | * @return LY_ERR value. |
| 2458 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2459 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2460 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2461 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2462 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2463 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2464 | { |
| 2465 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2466 | struct lysc_type_bin *bin; |
| 2467 | struct lysc_type_num *num; |
| 2468 | struct lysc_type_str *str; |
| 2469 | struct lysc_type_bits *bits; |
| 2470 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2471 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2472 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2473 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2474 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2475 | |
| 2476 | switch (basetype) { |
| 2477 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2478 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2479 | |
| 2480 | /* 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] | 2481 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2482 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2483 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2484 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2485 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | if (tpdfname) { |
| 2490 | type_p->compiled = *type; |
| 2491 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2492 | } |
| 2493 | break; |
| 2494 | case LY_TYPE_BITS: |
| 2495 | /* RFC 7950 9.7 - bits */ |
| 2496 | bits = (struct lysc_type_bits*)(*type); |
| 2497 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2498 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2499 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2500 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2501 | } |
| 2502 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2503 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2504 | /* 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] | 2505 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2506 | 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] | 2507 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2508 | 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] | 2509 | free(*type); |
| 2510 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2511 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2512 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | if (tpdfname) { |
| 2516 | type_p->compiled = *type; |
| 2517 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2518 | } |
| 2519 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2520 | case LY_TYPE_DEC64: |
| 2521 | dec = (struct lysc_type_dec*)(*type); |
| 2522 | |
| 2523 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2524 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2525 | if (!type_p->fraction_digits) { |
| 2526 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2527 | 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] | 2528 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2529 | 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] | 2530 | free(*type); |
| 2531 | *type = NULL; |
| 2532 | } |
| 2533 | return LY_EVALID; |
| 2534 | } |
| 2535 | } else if (type_p->fraction_digits) { |
| 2536 | /* 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] | 2537 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2538 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2539 | "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] | 2540 | tpdfname); |
| 2541 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2542 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2543 | "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] | 2544 | free(*type); |
| 2545 | *type = NULL; |
| 2546 | } |
| 2547 | return LY_EVALID; |
| 2548 | } |
| 2549 | dec->fraction_digits = type_p->fraction_digits; |
| 2550 | |
| 2551 | /* RFC 7950 9.2.4 - range */ |
| 2552 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2553 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2554 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2555 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2556 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2557 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | if (tpdfname) { |
| 2561 | type_p->compiled = *type; |
| 2562 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2563 | } |
| 2564 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2565 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2566 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2567 | |
| 2568 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2569 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2570 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2571 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2572 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2573 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2574 | } |
| 2575 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2576 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2577 | } |
| 2578 | |
| 2579 | /* RFC 7950 9.4.5 - pattern */ |
| 2580 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2581 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2582 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2583 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2584 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2585 | } |
| 2586 | |
| 2587 | if (tpdfname) { |
| 2588 | type_p->compiled = *type; |
| 2589 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2590 | } |
| 2591 | break; |
| 2592 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2593 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2594 | |
| 2595 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2596 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2597 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2598 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2599 | } |
| 2600 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2601 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2602 | /* 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] | 2603 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2604 | 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] | 2605 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2606 | 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] | 2607 | free(*type); |
| 2608 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2609 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2610 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | if (tpdfname) { |
| 2614 | type_p->compiled = *type; |
| 2615 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2616 | } |
| 2617 | break; |
| 2618 | case LY_TYPE_INT8: |
| 2619 | case LY_TYPE_UINT8: |
| 2620 | case LY_TYPE_INT16: |
| 2621 | case LY_TYPE_UINT16: |
| 2622 | case LY_TYPE_INT32: |
| 2623 | case LY_TYPE_UINT32: |
| 2624 | case LY_TYPE_INT64: |
| 2625 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2626 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2627 | |
| 2628 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2629 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2630 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2631 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2632 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2633 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | if (tpdfname) { |
| 2638 | type_p->compiled = *type; |
| 2639 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2640 | } |
| 2641 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2642 | case LY_TYPE_IDENT: |
| 2643 | idref = (struct lysc_type_identityref*)(*type); |
| 2644 | |
| 2645 | /* RFC 7950 9.10.2 - base */ |
| 2646 | if (type_p->bases) { |
| 2647 | if (base) { |
| 2648 | /* only the directly derived identityrefs can contain base specification */ |
| 2649 | if (tpdfname) { |
| 2650 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2651 | "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] | 2652 | tpdfname); |
| 2653 | } else { |
| 2654 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2655 | "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] | 2656 | free(*type); |
| 2657 | *type = NULL; |
| 2658 | } |
| 2659 | return LY_EVALID; |
| 2660 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2661 | LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | if (!base && !type_p->flags) { |
| 2665 | /* type derived from identityref built-in type must contain at least one base */ |
| 2666 | if (tpdfname) { |
| 2667 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2668 | } else { |
| 2669 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2670 | free(*type); |
| 2671 | *type = NULL; |
| 2672 | } |
| 2673 | return LY_EVALID; |
| 2674 | } |
| 2675 | |
| 2676 | if (tpdfname) { |
| 2677 | type_p->compiled = *type; |
| 2678 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2679 | } |
| 2680 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2681 | case LY_TYPE_LEAFREF: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2682 | lref = (struct lysc_type_leafref*)*type; |
| 2683 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2684 | /* RFC 7950 9.9.3 - require-instance */ |
| 2685 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2686 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2687 | if (tpdfname) { |
| 2688 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2689 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2690 | } else { |
| 2691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2692 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2693 | free(*type); |
| 2694 | *type = NULL; |
| 2695 | } |
| 2696 | return LY_EVALID; |
| 2697 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2698 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2699 | } else if (base) { |
| 2700 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2701 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2702 | } else { |
| 2703 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2704 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2705 | } |
| 2706 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2707 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2708 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2709 | } else if (base) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2710 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path); |
| 2711 | lref->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2712 | } else if (tpdfname) { |
| 2713 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2714 | return LY_EVALID; |
| 2715 | } else { |
| 2716 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2717 | free(*type); |
| 2718 | *type = NULL; |
| 2719 | return LY_EVALID; |
| 2720 | } |
| 2721 | if (tpdfname) { |
| 2722 | type_p->compiled = *type; |
| 2723 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2724 | } |
| 2725 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2726 | case LY_TYPE_INST: |
| 2727 | /* RFC 7950 9.9.3 - require-instance */ |
| 2728 | if (type_p->flags & LYS_SET_REQINST) { |
| 2729 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2730 | } else { |
| 2731 | /* default is true */ |
| 2732 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2733 | } |
| 2734 | |
| 2735 | if (tpdfname) { |
| 2736 | type_p->compiled = *type; |
| 2737 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2738 | } |
| 2739 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2740 | case LY_TYPE_UNION: |
| 2741 | un = (struct lysc_type_union*)(*type); |
| 2742 | |
| 2743 | /* RFC 7950 7.4 - type */ |
| 2744 | if (type_p->types) { |
| 2745 | if (base) { |
| 2746 | /* only the directly derived union can contain types specification */ |
| 2747 | if (tpdfname) { |
| 2748 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2749 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2750 | tpdfname); |
| 2751 | } else { |
| 2752 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2753 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2754 | free(*type); |
| 2755 | *type = NULL; |
| 2756 | } |
| 2757 | return LY_EVALID; |
| 2758 | } |
| 2759 | /* compile the type */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2760 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2761 | for (LY_ARRAY_SIZE_TYPE u = 0, additional = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2762 | LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, |
| 2763 | &type_p->types[u], &un->types[u + additional], NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2764 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2765 | /* add space for additional types from the union subtype */ |
| 2766 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Radek Krejci | c4fa029 | 2020-05-14 10:54:49 +0200 | [diff] [blame] | 2767 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_SIZE(un_aux->types) - 1, |
| 2768 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2769 | |
| 2770 | /* copy subtypes of the subtype union */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2771 | for (LY_ARRAY_SIZE_TYPE v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2772 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2773 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2774 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2775 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2776 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2777 | |
| 2778 | lref->basetype = LY_TYPE_LEAFREF; |
| 2779 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path); |
| 2780 | lref->refcount = 1; |
| 2781 | lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2782 | lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2783 | /* TODO extensions */ |
| 2784 | |
| 2785 | } else { |
| 2786 | un->types[u + additional] = un_aux->types[v]; |
| 2787 | ++un_aux->types[v]->refcount; |
| 2788 | } |
| 2789 | ++additional; |
| 2790 | LY_ARRAY_INCREMENT(un->types); |
| 2791 | } |
| 2792 | /* compensate u increment in main loop */ |
| 2793 | --additional; |
| 2794 | |
| 2795 | /* free the replaced union subtype */ |
| 2796 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2797 | } else { |
| 2798 | LY_ARRAY_INCREMENT(un->types); |
| 2799 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | if (!base && !type_p->flags) { |
| 2804 | /* type derived from union built-in type must contain at least one type */ |
| 2805 | if (tpdfname) { |
| 2806 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2807 | } else { |
| 2808 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2809 | free(*type); |
| 2810 | *type = NULL; |
| 2811 | } |
| 2812 | return LY_EVALID; |
| 2813 | } |
| 2814 | |
| 2815 | if (tpdfname) { |
| 2816 | type_p->compiled = *type; |
| 2817 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2818 | } |
| 2819 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2820 | case LY_TYPE_BOOL: |
| 2821 | case LY_TYPE_EMPTY: |
| 2822 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2823 | break; |
| 2824 | } |
| 2825 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2826 | done: |
| 2827 | return ret; |
| 2828 | } |
| 2829 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2830 | /** |
| 2831 | * @brief Compile information about the leaf/leaf-list's type. |
| 2832 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2833 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2834 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2835 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2836 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2837 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2838 | * @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] | 2839 | * @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] | 2840 | * @return LY_ERR value. |
| 2841 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2842 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2843 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2844 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2845 | struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2846 | { |
| 2847 | LY_ERR ret = LY_SUCCESS; |
| 2848 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2849 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2850 | struct type_context { |
| 2851 | const struct lysp_tpdf *tpdf; |
| 2852 | struct lysp_node *node; |
| 2853 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2854 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2855 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2856 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2857 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2858 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2859 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2860 | |
| 2861 | (*type) = NULL; |
| 2862 | |
| 2863 | tctx = calloc(1, sizeof *tctx); |
| 2864 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2865 | 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] | 2866 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2867 | ret == LY_SUCCESS; |
| 2868 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2869 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2870 | if (basetype) { |
| 2871 | break; |
| 2872 | } |
| 2873 | |
| 2874 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2875 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2876 | 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] | 2877 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2878 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2879 | if (units && !*units) { |
| 2880 | /* inherit units */ |
| 2881 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2882 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2883 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2884 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2885 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2886 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2887 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2888 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2889 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2890 | break; |
| 2891 | } |
| 2892 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2893 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2894 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2895 | * 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] | 2896 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2897 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2898 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2899 | dummyloops = 1; |
| 2900 | goto preparenext; |
| 2901 | } else { |
| 2902 | tctx = NULL; |
| 2903 | break; |
| 2904 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2905 | } |
| 2906 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2907 | /* circular typedef reference detection */ |
| 2908 | for (u = 0; u < tpdf_chain.count; u++) { |
| 2909 | /* local part */ |
| 2910 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 2911 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2912 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2913 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2914 | free(tctx); |
| 2915 | ret = LY_EVALID; |
| 2916 | goto cleanup; |
| 2917 | } |
| 2918 | } |
| 2919 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 2920 | /* global part for unions corner case */ |
| 2921 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 2922 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2923 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2924 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2925 | free(tctx); |
| 2926 | ret = LY_EVALID; |
| 2927 | goto cleanup; |
| 2928 | } |
| 2929 | } |
| 2930 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2931 | /* store information for the following processing */ |
| 2932 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2933 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2934 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2935 | /* prepare next loop */ |
| 2936 | tctx_prev = tctx; |
| 2937 | tctx = calloc(1, sizeof *tctx); |
| 2938 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2939 | } |
| 2940 | free(tctx); |
| 2941 | |
| 2942 | /* allocate type according to the basetype */ |
| 2943 | switch (basetype) { |
| 2944 | case LY_TYPE_BINARY: |
| 2945 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2946 | break; |
| 2947 | case LY_TYPE_BITS: |
| 2948 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2949 | break; |
| 2950 | case LY_TYPE_BOOL: |
| 2951 | case LY_TYPE_EMPTY: |
| 2952 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2953 | break; |
| 2954 | case LY_TYPE_DEC64: |
| 2955 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2956 | break; |
| 2957 | case LY_TYPE_ENUM: |
| 2958 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2959 | break; |
| 2960 | case LY_TYPE_IDENT: |
| 2961 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2962 | break; |
| 2963 | case LY_TYPE_INST: |
| 2964 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2965 | break; |
| 2966 | case LY_TYPE_LEAFREF: |
| 2967 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2968 | break; |
| 2969 | case LY_TYPE_STRING: |
| 2970 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2971 | break; |
| 2972 | case LY_TYPE_UNION: |
| 2973 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2974 | break; |
| 2975 | case LY_TYPE_INT8: |
| 2976 | case LY_TYPE_UINT8: |
| 2977 | case LY_TYPE_INT16: |
| 2978 | case LY_TYPE_UINT16: |
| 2979 | case LY_TYPE_INT32: |
| 2980 | case LY_TYPE_UINT32: |
| 2981 | case LY_TYPE_INT64: |
| 2982 | case LY_TYPE_UINT64: |
| 2983 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2984 | break; |
| 2985 | case LY_TYPE_UNKNOWN: |
| 2986 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2987 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 2988 | ret = LY_EVALID; |
| 2989 | goto cleanup; |
| 2990 | } |
| 2991 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2992 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2993 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 2994 | ly_data_type2str[basetype]); |
| 2995 | free(*type); |
| 2996 | (*type) = NULL; |
| 2997 | ret = LY_EVALID; |
| 2998 | goto cleanup; |
| 2999 | } |
| 3000 | |
| 3001 | /* get restrictions from the referred typedefs */ |
| 3002 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3003 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3004 | |
| 3005 | /* remember the typedef context for circular check */ |
| 3006 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3007 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3008 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3009 | base = tctx->tpdf->type.compiled; |
| 3010 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3011 | } 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] | 3012 | /* no change, just use the type information from the base */ |
| 3013 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3014 | ++base->refcount; |
| 3015 | continue; |
| 3016 | } |
| 3017 | |
| 3018 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3019 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3020 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3021 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3022 | ret = LY_EVALID; |
| 3023 | goto cleanup; |
| 3024 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3025 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3026 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3027 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3028 | ret = LY_EVALID; |
| 3029 | goto cleanup; |
| 3030 | } |
| 3031 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3032 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3033 | /* TODO user type plugins */ |
| 3034 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3035 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3036 | 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] | 3037 | basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3038 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3039 | LY_CHECK_GOTO(ret, cleanup); |
| 3040 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3041 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3042 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3043 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3044 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3045 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3046 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3047 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3048 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3049 | /* TODO user type plugins */ |
| 3050 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3051 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3052 | ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3053 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3054 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3055 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3056 | free(*type); |
| 3057 | (*type) = base; |
| 3058 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3059 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3060 | if (dflt && !(*type)->dflt) { |
| 3061 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3062 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3063 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3064 | (*type)->dflt->realtype = (*type); |
| 3065 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3066 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3067 | lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3068 | if (err) { |
| 3069 | ly_err_print(err); |
| 3070 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3071 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3072 | ly_err_free(err); |
| 3073 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3074 | if (ret == LY_EINCOMPLETE) { |
| 3075 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3076 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3077 | |
| 3078 | /* but in general result is so far ok */ |
| 3079 | ret = LY_SUCCESS; |
| 3080 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3081 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3082 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3083 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3084 | COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3085 | |
| 3086 | cleanup: |
| 3087 | ly_set_erase(&tpdf_chain, free); |
| 3088 | return ret; |
| 3089 | } |
| 3090 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3091 | /** |
| 3092 | * @brief Compile status information of the given node. |
| 3093 | * |
| 3094 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3095 | * has the status correctly set during the compilation. |
| 3096 | * |
| 3097 | * @param[in] ctx Compile context |
| 3098 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3099 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3100 | * the compatibility with the parent's status value. |
| 3101 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3102 | * @return LY_ERR value. |
| 3103 | */ |
| 3104 | static LY_ERR |
| 3105 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3106 | { |
| 3107 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3108 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3109 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3110 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3111 | if ((parent_flags & 0x3) != 0x3) { |
| 3112 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3113 | * combination of bits (0x3) which marks the uses_status value */ |
| 3114 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3115 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3116 | } |
| 3117 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3118 | } else { |
| 3119 | (*node_flags) |= LYS_STATUS_CURR; |
| 3120 | } |
| 3121 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3122 | /* check status compatibility with the parent */ |
| 3123 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3124 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3125 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3126 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3127 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3128 | } else { /* LYS_STATUS_DEPRC */ |
| 3129 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3130 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3131 | } |
| 3132 | return LY_EVALID; |
| 3133 | } |
| 3134 | } |
| 3135 | return LY_SUCCESS; |
| 3136 | } |
| 3137 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3138 | /** |
| 3139 | * @brief Check uniqness of the node/action/notification name. |
| 3140 | * |
| 3141 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3142 | * structures, but they share the namespace so we need to check their name collisions. |
| 3143 | * |
| 3144 | * @param[in] ctx Compile context. |
| 3145 | * @param[in] children List (linked list) of data nodes to go through. |
| 3146 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3147 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3148 | * @param[in] name Name of the item to find in the given lists. |
| 3149 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3150 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3151 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3152 | */ |
| 3153 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3154 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions, |
| 3155 | const struct lysc_notif *notifs, const char *name, void *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3156 | { |
| 3157 | const struct lysc_node *iter; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3158 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3159 | |
| 3160 | LY_LIST_FOR(children, iter) { |
| 3161 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3162 | goto error; |
| 3163 | } |
| 3164 | } |
| 3165 | LY_ARRAY_FOR(actions, u) { |
| 3166 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3167 | goto error; |
| 3168 | } |
| 3169 | } |
| 3170 | LY_ARRAY_FOR(notifs, u) { |
| 3171 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3172 | goto error; |
| 3173 | } |
| 3174 | } |
| 3175 | return LY_SUCCESS; |
| 3176 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3177 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification"); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3178 | return LY_EEXIST; |
| 3179 | } |
| 3180 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3181 | static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3182 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3183 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3184 | * @brief Compile parsed RPC/action schema node information. |
| 3185 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3186 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3187 | * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action) |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3188 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3189 | * @param[in] uses_status If the RPC/action is being placed instead of uses, here we have the uses's status value (as node's flags). |
| 3190 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3191 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3192 | */ |
| 3193 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3194 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3195 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3196 | { |
| 3197 | LY_ERR ret = LY_SUCCESS; |
| 3198 | struct lysp_node *child_p; |
| 3199 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3200 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3201 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3202 | lysc_update_path(ctx, parent, action_p->name); |
| 3203 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3204 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3205 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3206 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3207 | action_p->name, action)) { |
| 3208 | return LY_EVALID; |
| 3209 | } |
| 3210 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3211 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3212 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3213 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3214 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3215 | return LY_EVALID; |
| 3216 | } |
| 3217 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3218 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3219 | action->module = ctx->mod; |
| 3220 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3221 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3222 | action->sp = action_p; |
| 3223 | } |
| 3224 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3225 | |
| 3226 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3227 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 3228 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3229 | |
| 3230 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3231 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3232 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3233 | COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3234 | COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3235 | |
| 3236 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3237 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3238 | COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3239 | COMPILE_EXTS_GOTO(ctx, action_p->input.exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3240 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3241 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3242 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3243 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3244 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3245 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3246 | |
| 3247 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3248 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3249 | COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3250 | COMPILE_EXTS_GOTO(ctx, action_p->output.exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3251 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3252 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3253 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3254 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3255 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3256 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3257 | |
| 3258 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3259 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3260 | ly_set_add(&ctx->xpath, action, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3261 | } |
| 3262 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3263 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3264 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3265 | return ret; |
| 3266 | } |
| 3267 | |
| 3268 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3269 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3270 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3271 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3272 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3273 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3274 | * @param[in] uses_status If the Notification is being placed instead of uses, here we have the uses's status value (as node's flags). |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3275 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3276 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3277 | */ |
| 3278 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3279 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3280 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3281 | { |
| 3282 | LY_ERR ret = LY_SUCCESS; |
| 3283 | struct lysp_node *child_p; |
| 3284 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3285 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3286 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3287 | lysc_update_path(ctx, parent, notif_p->name); |
| 3288 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3289 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3290 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3291 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3292 | notif_p->name, notif)) { |
| 3293 | return LY_EVALID; |
| 3294 | } |
| 3295 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3296 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3297 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3298 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3299 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3300 | return LY_EVALID; |
| 3301 | } |
| 3302 | |
| 3303 | notif->nodetype = LYS_NOTIF; |
| 3304 | notif->module = ctx->mod; |
| 3305 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3306 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3307 | notif->sp = notif_p; |
| 3308 | } |
| 3309 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3310 | |
| 3311 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3312 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3313 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3314 | |
| 3315 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3316 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3317 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3318 | COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3319 | COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3320 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3321 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3322 | ly_set_add(&ctx->xpath, notif, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3323 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3324 | COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3325 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3326 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3327 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3328 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3329 | } |
| 3330 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3331 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3332 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3333 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3334 | return ret; |
| 3335 | } |
| 3336 | |
| 3337 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3338 | * @brief Compile parsed container node information. |
| 3339 | * @param[in] ctx Compile context |
| 3340 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3341 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3342 | * is enriched with the container-specific information. |
| 3343 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3344 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3345 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3346 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3347 | { |
| 3348 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3349 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3350 | struct lysp_node *child_p; |
| 3351 | unsigned int u; |
| 3352 | LY_ERR ret = LY_SUCCESS; |
| 3353 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3354 | if (cont_p->presence) { |
| 3355 | cont->flags |= LYS_PRESENCE; |
| 3356 | } |
| 3357 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3358 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3359 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3360 | } |
| 3361 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3362 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3363 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3364 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3365 | ly_set_add(&ctx->xpath, cont, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3366 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3367 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3368 | COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3369 | |
| 3370 | done: |
| 3371 | return ret; |
| 3372 | } |
| 3373 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3374 | /* |
| 3375 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3376 | * @param[in] ctx Compile context. |
| 3377 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3378 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3379 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3380 | * @return LY_ERR value. |
| 3381 | */ |
| 3382 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3383 | lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, struct lysc_node_leaf *leaf) |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3384 | { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3385 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3386 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3387 | LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type, |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3388 | leaf->units ? NULL : &leaf->units)); |
| 3389 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3390 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3391 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3392 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3393 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3394 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3395 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3396 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3397 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3398 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3399 | } |
| 3400 | } else { |
| 3401 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3402 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3403 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3404 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3405 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3406 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3407 | } |
| 3408 | } |
| 3409 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3410 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3411 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3412 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3413 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3414 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3415 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3416 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3417 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3418 | } |
| 3419 | } |
| 3420 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3421 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3422 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3423 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3424 | return LY_EVALID; |
| 3425 | } |
| 3426 | } |
| 3427 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3428 | return LY_SUCCESS; |
| 3429 | } |
| 3430 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3431 | /** |
| 3432 | * @brief Compile parsed leaf node information. |
| 3433 | * @param[in] ctx Compile context |
| 3434 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3435 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3436 | * is enriched with the leaf-specific information. |
| 3437 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3438 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3439 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3440 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3441 | { |
| 3442 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3443 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3444 | unsigned int u; |
| 3445 | LY_ERR ret = LY_SUCCESS; |
| 3446 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3447 | COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3448 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3449 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3450 | ly_set_add(&ctx->xpath, leaf, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3451 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3452 | if (leaf_p->units) { |
| 3453 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3454 | leaf->flags |= LYS_SET_UNITS; |
| 3455 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3456 | |
| 3457 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3458 | leaf->dflt = (void*)leaf_p->dflt; |
| 3459 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3460 | if (ret) { |
| 3461 | leaf->dflt = NULL; |
| 3462 | return ret; |
| 3463 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3464 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3465 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3466 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3467 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3468 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3469 | leaf->dflt->realtype = leaf->type; |
| 3470 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3471 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3472 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3473 | leaf->dflt->realtype->refcount++; |
| 3474 | if (err) { |
| 3475 | ly_err_print(err); |
| 3476 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3477 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3478 | ly_err_free(err); |
| 3479 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3480 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3481 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3482 | LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod)); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3483 | |
| 3484 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3485 | ret = LY_SUCCESS; |
| 3486 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3487 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3488 | leaf->flags |= LYS_SET_DFLT; |
| 3489 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3490 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3491 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3492 | done: |
| 3493 | return ret; |
| 3494 | } |
| 3495 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3496 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3497 | * @brief Compile parsed leaf-list node information. |
| 3498 | * @param[in] ctx Compile context |
| 3499 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3500 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3501 | * is enriched with the leaf-list-specific information. |
| 3502 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3503 | */ |
| 3504 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3505 | lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3506 | { |
| 3507 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3508 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3509 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3510 | LY_ERR ret = LY_SUCCESS; |
| 3511 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3512 | COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3513 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3514 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3515 | ly_set_add(&ctx->xpath, llist, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3516 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3517 | if (llist_p->units) { |
| 3518 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3519 | llist->flags |= LYS_SET_UNITS; |
| 3520 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3521 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3522 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3523 | llist->dflts = (void*)llist_p->dflts; |
| 3524 | ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist); |
Michal Vasko | 6a044b2 | 2020-01-15 12:25:39 +0100 | [diff] [blame] | 3525 | if (ret != LY_SUCCESS) { |
| 3526 | /* make sure the defaults are freed correctly */ |
| 3527 | if (llist_p->dflts) { |
| 3528 | llist->dflts = NULL; |
| 3529 | } |
| 3530 | return ret; |
| 3531 | } |
| 3532 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3533 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3534 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3535 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3536 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3537 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3538 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3539 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3540 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3541 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3542 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3543 | llist->dflts[u]->realtype = llist->type; |
| 3544 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3545 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3546 | lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3547 | llist->dflts[u]->realtype->refcount++; |
| 3548 | if (err) { |
| 3549 | ly_err_print(err); |
| 3550 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3551 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3552 | ly_err_free(err); |
| 3553 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3554 | if (ret == LY_EINCOMPLETE) { |
| 3555 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3556 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3557 | |
| 3558 | /* but in general result is so far ok */ |
| 3559 | ret = LY_SUCCESS; |
| 3560 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3561 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3562 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3563 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3564 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3565 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3566 | /* configuration data values must be unique - so check the default values */ |
| 3567 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3568 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3569 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3570 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3571 | const char *val = llist->type->plugin->print(llist->dflts[v], LYD_XML, lys_get_prefix, llist->dflts_mods[v], &dynamic); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3572 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3573 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3574 | if (dynamic) { |
| 3575 | free((char*)val); |
| 3576 | } |
| 3577 | return LY_EVALID; |
| 3578 | } |
| 3579 | } |
| 3580 | } |
| 3581 | } |
| 3582 | |
| 3583 | /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3584 | |
| 3585 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3586 | if (llist->min) { |
| 3587 | llist->flags |= LYS_MAND_TRUE; |
| 3588 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3589 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3590 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3591 | done: |
| 3592 | return ret; |
| 3593 | } |
| 3594 | |
| 3595 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3596 | * @brief Compile information about list's uniques. |
| 3597 | * @param[in] ctx Compile context. |
| 3598 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3599 | * @param[in] uniques Sized array list of unique statements. |
| 3600 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3601 | * @return LY_ERR value. |
| 3602 | */ |
| 3603 | static LY_ERR |
| 3604 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3605 | { |
| 3606 | LY_ERR ret = LY_SUCCESS; |
| 3607 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3608 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3609 | const char *keystr, *delim; |
| 3610 | size_t len; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3611 | LY_ARRAY_SIZE_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3612 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3613 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3614 | |
| 3615 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3616 | config = -1; |
| 3617 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3618 | keystr = uniques[v]; |
| 3619 | while (keystr) { |
| 3620 | delim = strpbrk(keystr, " \t\n"); |
| 3621 | if (delim) { |
| 3622 | len = delim - keystr; |
| 3623 | while (isspace(*delim)) { |
| 3624 | ++delim; |
| 3625 | } |
| 3626 | } else { |
| 3627 | len = strlen(keystr); |
| 3628 | } |
| 3629 | |
| 3630 | /* unique node must be present */ |
| 3631 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3632 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3633 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3634 | if (ret != LY_SUCCESS) { |
| 3635 | if (ret == LY_EDENIED) { |
| 3636 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3637 | "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.", |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3638 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3639 | } |
| 3640 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3641 | } else if (flags) { |
| 3642 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3643 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3644 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3645 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3646 | } |
| 3647 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3648 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3649 | /* all referenced leafs must be of the same config type */ |
| 3650 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3651 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3652 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3653 | return LY_EVALID; |
| 3654 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3655 | config = 1; |
| 3656 | } else { /* LYS_CONFIG_R */ |
| 3657 | config = 0; |
| 3658 | } |
| 3659 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3660 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3661 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3662 | if (parent->nodetype == LYS_LIST) { |
| 3663 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3664 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3665 | return LY_EVALID; |
| 3666 | } |
| 3667 | } |
| 3668 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3669 | /* check status */ |
| 3670 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3671 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3672 | |
| 3673 | /* mark leaf as unique */ |
| 3674 | (*key)->flags |= LYS_UNIQUE; |
| 3675 | |
| 3676 | /* next unique value in line */ |
| 3677 | keystr = delim; |
| 3678 | } |
| 3679 | /* next unique definition */ |
| 3680 | } |
| 3681 | |
| 3682 | return LY_SUCCESS; |
| 3683 | } |
| 3684 | |
| 3685 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3686 | * @brief Compile parsed list node information. |
| 3687 | * @param[in] ctx Compile context |
| 3688 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3689 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3690 | * is enriched with the list-specific information. |
| 3691 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3692 | */ |
| 3693 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3694 | lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3695 | { |
| 3696 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3697 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3698 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3699 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3700 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3701 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3702 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3703 | LY_ERR ret = LY_SUCCESS; |
| 3704 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3705 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3706 | if (list->min) { |
| 3707 | list->flags |= LYS_MAND_TRUE; |
| 3708 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3709 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3710 | |
| 3711 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3712 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3713 | } |
| 3714 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3715 | COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3716 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3717 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3718 | ly_set_add(&ctx->xpath, list, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3719 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3720 | |
| 3721 | /* keys */ |
| 3722 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3723 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3724 | return LY_EVALID; |
| 3725 | } |
| 3726 | |
| 3727 | /* find all the keys (must be direct children) */ |
| 3728 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3729 | if (!keystr) { |
| 3730 | /* keyless list */ |
| 3731 | list->flags |= LYS_KEYLESS; |
| 3732 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3733 | while (keystr) { |
| 3734 | delim = strpbrk(keystr, " \t\n"); |
| 3735 | if (delim) { |
| 3736 | len = delim - keystr; |
| 3737 | while (isspace(*delim)) { |
| 3738 | ++delim; |
| 3739 | } |
| 3740 | } else { |
| 3741 | len = strlen(keystr); |
| 3742 | } |
| 3743 | |
| 3744 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3745 | key = (struct lysc_node_leaf*)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3746 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3747 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3748 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3749 | return LY_EVALID; |
| 3750 | } |
| 3751 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3752 | if (key->flags & LYS_KEY) { |
| 3753 | /* the node was already marked as a key */ |
| 3754 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3755 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3756 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3757 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3758 | |
| 3759 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3760 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3761 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3762 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3763 | return LY_EVALID; |
| 3764 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3765 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3766 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3767 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3768 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3769 | "List's key cannot be of \"empty\" type until it is in YANG 1.1 module."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3770 | return LY_EVALID; |
| 3771 | } |
| 3772 | } else { |
| 3773 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3774 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3775 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3776 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3777 | return LY_EVALID; |
| 3778 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3779 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3780 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3781 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3782 | return LY_EVALID; |
| 3783 | } |
| 3784 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3785 | |
| 3786 | /* check status */ |
| 3787 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3788 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3789 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3790 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3791 | if (key->dflt) { |
| 3792 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3793 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3794 | free(key->dflt); |
| 3795 | key->dflt = NULL; |
| 3796 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3797 | } |
| 3798 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3799 | key->flags |= LYS_KEY; |
| 3800 | |
| 3801 | /* move it to the correct position */ |
| 3802 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3803 | /* fix links in closest previous siblings of the key */ |
| 3804 | if (key->next) { |
| 3805 | key->next->prev = key->prev; |
| 3806 | } else { |
| 3807 | /* last child */ |
| 3808 | list->child->prev = key->prev; |
| 3809 | } |
| 3810 | if (key->prev->next) { |
| 3811 | key->prev->next = key->next; |
| 3812 | } |
| 3813 | /* fix links in the key */ |
| 3814 | if (prev_key) { |
| 3815 | key->prev = (struct lysc_node*)prev_key; |
| 3816 | key->next = prev_key->next; |
| 3817 | } else { |
| 3818 | key->prev = list->child->prev; |
| 3819 | key->next = list->child; |
| 3820 | } |
| 3821 | /* fix links in closes future siblings of the key */ |
| 3822 | if (prev_key) { |
| 3823 | if (prev_key->next) { |
| 3824 | prev_key->next->prev = (struct lysc_node*)key; |
| 3825 | } else { |
| 3826 | list->child->prev = (struct lysc_node*)key; |
| 3827 | } |
| 3828 | prev_key->next = (struct lysc_node*)key; |
| 3829 | } else { |
| 3830 | list->child->prev = (struct lysc_node*)key; |
| 3831 | } |
| 3832 | /* fix links in parent */ |
| 3833 | if (!key->prev->next) { |
| 3834 | list->child = (struct lysc_node*)key; |
| 3835 | } |
| 3836 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3837 | |
| 3838 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3839 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3840 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3841 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | /* uniques */ |
| 3845 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3846 | 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] | 3847 | } |
| 3848 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3849 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3850 | COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3851 | |
| 3852 | done: |
| 3853 | return ret; |
| 3854 | } |
| 3855 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3856 | /** |
| 3857 | * @brief Do some checks and set the default choice's case. |
| 3858 | * |
| 3859 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3860 | * |
| 3861 | * @param[in] ctx Compile context. |
| 3862 | * @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, |
| 3863 | * not the reference to the imported module. |
| 3864 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3865 | * @return LY_ERR value. |
| 3866 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3867 | static LY_ERR |
| 3868 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3869 | { |
| 3870 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3871 | const char *prefix = NULL, *name; |
| 3872 | size_t prefix_len = 0; |
| 3873 | |
| 3874 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3875 | name = strchr(dflt, ':'); |
| 3876 | if (name) { |
| 3877 | prefix = dflt; |
| 3878 | prefix_len = name - prefix; |
| 3879 | ++name; |
| 3880 | } else { |
| 3881 | name = dflt; |
| 3882 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3883 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3884 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3885 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3886 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3887 | prefix_len, prefix); |
| 3888 | return LY_EVALID; |
| 3889 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3890 | ch->dflt = (struct lysc_node_case*)lys_find_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3891 | if (!ch->dflt) { |
| 3892 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3893 | "Default case \"%s\" not found.", dflt); |
| 3894 | return LY_EVALID; |
| 3895 | } |
| 3896 | /* no mandatory nodes directly under the default case */ |
| 3897 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3898 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3899 | break; |
| 3900 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3901 | if (iter->flags & LYS_MAND_TRUE) { |
| 3902 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3903 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3904 | return LY_EVALID; |
| 3905 | } |
| 3906 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3907 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3908 | return LY_SUCCESS; |
| 3909 | } |
| 3910 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3911 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3912 | lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3913 | { |
| 3914 | struct lys_module *mod; |
| 3915 | const char *prefix = NULL, *name; |
| 3916 | size_t prefix_len = 0; |
| 3917 | struct lysc_node_case *cs; |
| 3918 | struct lysc_node *node; |
| 3919 | |
| 3920 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3921 | name = strchr(dflt, ':'); |
| 3922 | if (name) { |
| 3923 | prefix = dflt; |
| 3924 | prefix_len = name - prefix; |
| 3925 | ++name; |
| 3926 | } else { |
| 3927 | name = dflt; |
| 3928 | } |
| 3929 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3930 | if (prefix) { |
| 3931 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3932 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3933 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3934 | "The prefix does not match any imported module of the deviation module.", dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3935 | return LY_EVALID; |
| 3936 | } |
| 3937 | } else { |
| 3938 | mod = ctx->mod; |
| 3939 | } |
| 3940 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3941 | cs = (struct lysc_node_case*)lys_find_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3942 | if (!cs) { |
| 3943 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3944 | "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3945 | return LY_EVALID; |
| 3946 | } |
| 3947 | |
| 3948 | /* check that there is no mandatory node */ |
| 3949 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3950 | if (node->parent != (struct lysc_node*)cs) { |
| 3951 | break; |
| 3952 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3953 | if (node->flags & LYS_MAND_TRUE) { |
| 3954 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3955 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3956 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3957 | return LY_EVALID; |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | /* set the default case in choice */ |
| 3962 | ch->dflt = cs; |
| 3963 | cs->flags |= LYS_SET_DFLT; |
| 3964 | |
| 3965 | return LY_SUCCESS; |
| 3966 | } |
| 3967 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3968 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3969 | * @brief Compile parsed choice node information. |
| 3970 | * @param[in] ctx Compile context |
| 3971 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3972 | * @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] | 3973 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3974 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3975 | */ |
| 3976 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3977 | lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3978 | { |
| 3979 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 3980 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 3981 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3982 | LY_ERR ret = LY_SUCCESS; |
| 3983 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3984 | LY_LIST_FOR(ch_p->child, child_p) { |
| 3985 | if (child_p->nodetype == LYS_CASE) { |
| 3986 | LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3987 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3988 | } |
| 3989 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3990 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3995 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3996 | 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] | 3997 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3998 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3999 | return ret; |
| 4000 | } |
| 4001 | |
| 4002 | /** |
| 4003 | * @brief Compile parsed anydata or anyxml node information. |
| 4004 | * @param[in] ctx Compile context |
| 4005 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4006 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4007 | * is enriched with the any-specific information. |
| 4008 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4009 | */ |
| 4010 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4011 | lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4012 | { |
| 4013 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4014 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4015 | unsigned int u; |
| 4016 | LY_ERR ret = LY_SUCCESS; |
| 4017 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4018 | COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4019 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4020 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4021 | ly_set_add(&ctx->xpath, any, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4022 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4023 | |
| 4024 | if (any->flags & LYS_CONFIG_W) { |
| 4025 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4026 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4027 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4028 | done: |
| 4029 | return ret; |
| 4030 | } |
| 4031 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4032 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4033 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4034 | * |
| 4035 | * @param[in] ctx Compile context |
| 4036 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4037 | * the choice itself is expected instead of a specific case node. |
| 4038 | * @param[in] node Schema node to connect into the list. |
| 4039 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4040 | * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4041 | */ |
| 4042 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4043 | lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4044 | { |
| 4045 | struct lysc_node **children; |
| 4046 | |
| 4047 | if (node->nodetype == LYS_CASE) { |
| 4048 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4049 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4050 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4051 | } |
| 4052 | if (children) { |
| 4053 | if (!(*children)) { |
| 4054 | /* first child */ |
| 4055 | *children = node; |
| 4056 | } else if (*children != node) { |
| 4057 | /* by the condition in previous branch we cover the choice/case children |
| 4058 | * - the children list is shared by the choice and the the first case, in addition |
| 4059 | * the first child of each case must be referenced from the case node. So the node is |
| 4060 | * actually always already inserted in case it is the first children - so here such |
| 4061 | * a situation actually corresponds to the first branch */ |
| 4062 | /* insert at the end of the parent's children list */ |
| 4063 | (*children)->prev->next = node; |
| 4064 | node->prev = (*children)->prev; |
| 4065 | (*children)->prev = node; |
| 4066 | |
| 4067 | /* check the name uniqueness */ |
| 4068 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4069 | lysc_node_notifs(parent), node->name, node)) { |
| 4070 | return LY_EEXIST; |
| 4071 | } |
| 4072 | } |
| 4073 | } |
| 4074 | return LY_SUCCESS; |
| 4075 | } |
| 4076 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4077 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4078 | * @brief Prepare the case structure in choice node for the new data node. |
| 4079 | * |
| 4080 | * 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 |
| 4081 | * created in the choice when the first child was processed. |
| 4082 | * |
| 4083 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4084 | * @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, |
| 4085 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4086 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4087 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4088 | * @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, |
| 4089 | * 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] | 4090 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4091 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4092 | lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node_choice *ch, struct lysc_node *child) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4093 | { |
| 4094 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4095 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4096 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4097 | unsigned int u; |
| 4098 | LY_ERR ret; |
| 4099 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4100 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4101 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4102 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4103 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4104 | return NULL; \ |
| 4105 | } \ |
| 4106 | } |
| 4107 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4108 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4109 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4110 | |
| 4111 | /* we have to add an implicit case node into the parent choice */ |
| 4112 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4113 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4114 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4115 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4116 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4117 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4118 | /* the case is already present since the child is not its first children */ |
| 4119 | return (struct lysc_node_case*)ch->cases->prev; |
| 4120 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4121 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4122 | |
| 4123 | /* explicit parent case is not present (this is its first child) */ |
| 4124 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4125 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4126 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4127 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4128 | cs->sp = node_p; |
| 4129 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4130 | /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */ |
Radek Krejci | bae745f | 2019-04-09 16:28:00 +0200 | [diff] [blame] | 4131 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4132 | |
| 4133 | if (node_p->when) { |
| 4134 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4135 | ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4136 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4137 | |
| 4138 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4139 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4140 | ly_set_add(&ctx->xpath, cs, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4141 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4142 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4143 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4144 | } else { |
| 4145 | LOGINT(ctx->ctx); |
| 4146 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4147 | } |
| 4148 | cs->module = ctx->mod; |
| 4149 | cs->prev = (struct lysc_node*)cs; |
| 4150 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4151 | lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4152 | cs->child = child; |
| 4153 | |
| 4154 | return cs; |
| 4155 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4156 | if (cs) { |
| 4157 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4158 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4159 | return NULL; |
| 4160 | |
| 4161 | #undef UNIQUE_CHECK |
| 4162 | } |
| 4163 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4164 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4165 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4166 | * |
| 4167 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4168 | * @param[in] node Target node where the config is supposed to be changed. |
| 4169 | * @param[in] config_flag Node's config flag to be applied to the @p node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4170 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4171 | * 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] | 4172 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4173 | * @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] | 4174 | * @return LY_ERR value. |
| 4175 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4176 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4177 | lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4178 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4179 | { |
| 4180 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4181 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4182 | |
| 4183 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4184 | /* nothing to do */ |
| 4185 | return LY_SUCCESS; |
| 4186 | } |
| 4187 | |
| 4188 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4189 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4190 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4191 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4192 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4193 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4194 | return LY_EVALID; |
| 4195 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4196 | node->flags |= LYS_SET_CONFIG; |
| 4197 | } else { |
| 4198 | if (node->flags & LYS_SET_CONFIG) { |
| 4199 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4200 | /* setting config flags, but have node with explicit config true */ |
| 4201 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4202 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4203 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4204 | return LY_EVALID; |
| 4205 | } |
| 4206 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4207 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4208 | return LY_SUCCESS; |
| 4209 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4210 | } |
| 4211 | node->flags &= ~LYS_CONFIG_MASK; |
| 4212 | node->flags |= config; |
| 4213 | |
| 4214 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4215 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4216 | LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4217 | } |
| 4218 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4219 | return LY_SUCCESS; |
| 4220 | } |
| 4221 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4222 | /** |
| 4223 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4224 | * |
| 4225 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4226 | * the flag to such parents from a mandatory children. |
| 4227 | * |
| 4228 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4229 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4230 | * (mandatory children was removed). |
| 4231 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4232 | void |
| 4233 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4234 | { |
| 4235 | struct lysc_node *iter; |
| 4236 | |
| 4237 | if (add) { /* set flag */ |
| 4238 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4239 | parent = parent->parent) { |
| 4240 | parent->flags |= LYS_MAND_TRUE; |
| 4241 | } |
| 4242 | } else { /* unset flag */ |
| 4243 | for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4244 | for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4245 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4246 | /* there is another mandatory node */ |
| 4247 | return; |
| 4248 | } |
| 4249 | } |
| 4250 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4251 | parent->flags &= ~LYS_MAND_TRUE; |
| 4252 | } |
| 4253 | } |
| 4254 | } |
| 4255 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4256 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4257 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4258 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4259 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4260 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4261 | */ |
| 4262 | static void |
| 4263 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4264 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4265 | LY_ARRAY_SIZE_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4266 | size_t len; |
| 4267 | |
| 4268 | len = strlen(aug_p->nodeid); |
| 4269 | LY_ARRAY_FOR(result, v) { |
| 4270 | if (strlen(result[v]->nodeid) <= len) { |
| 4271 | continue; |
| 4272 | } |
| 4273 | if (v < LY_ARRAY_SIZE(result)) { |
| 4274 | /* move the rest of array */ |
| 4275 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4276 | break; |
| 4277 | } |
| 4278 | } |
| 4279 | result[v] = aug_p; |
| 4280 | LY_ARRAY_INCREMENT(result); |
| 4281 | } |
| 4282 | |
| 4283 | /** |
| 4284 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4285 | * |
| 4286 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4287 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4288 | * |
| 4289 | * @param[in] ctx Compile context. |
| 4290 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4291 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4292 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4293 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4294 | * @return LY_ERR value. |
| 4295 | */ |
| 4296 | LY_ERR |
| 4297 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4298 | { |
| 4299 | struct lysp_augment **result = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4300 | LY_ARRAY_SIZE_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4301 | |
| 4302 | assert(augments); |
| 4303 | |
| 4304 | /* get count of the augments in module and all its submodules */ |
| 4305 | if (aug_p) { |
| 4306 | count += LY_ARRAY_SIZE(aug_p); |
| 4307 | } |
| 4308 | LY_ARRAY_FOR(inc_p, u) { |
| 4309 | if (inc_p[u].submodule->augments) { |
| 4310 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4311 | } |
| 4312 | } |
| 4313 | |
| 4314 | if (!count) { |
| 4315 | *augments = NULL; |
| 4316 | return LY_SUCCESS; |
| 4317 | } |
| 4318 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4319 | |
| 4320 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4321 | * together, so there can be even /z/y betwwen them. */ |
| 4322 | LY_ARRAY_FOR(aug_p, u) { |
| 4323 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4324 | } |
| 4325 | LY_ARRAY_FOR(inc_p, u) { |
| 4326 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4327 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4328 | } |
| 4329 | } |
| 4330 | |
| 4331 | *augments = result; |
| 4332 | return LY_SUCCESS; |
| 4333 | } |
| 4334 | |
| 4335 | /** |
| 4336 | * @brief Compile the parsed augment connecting it into its target. |
| 4337 | * |
| 4338 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4339 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4340 | * are already implemented and compiled. |
| 4341 | * |
| 4342 | * @param[in] ctx Compile context. |
| 4343 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4344 | * @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 |
| 4345 | * children in case of the augmenting uses data. |
| 4346 | * @return LY_SUCCESS on success. |
| 4347 | * @return LY_EVALID on failure. |
| 4348 | */ |
| 4349 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4350 | lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent) |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4351 | { |
| 4352 | LY_ERR ret = LY_SUCCESS; |
| 4353 | struct lysp_node *node_p, *case_node_p; |
| 4354 | struct lysc_node *target; /* target target of the augment */ |
| 4355 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4356 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4357 | struct lys_module **aug_mod; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4358 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4359 | uint16_t flags = 0; |
| 4360 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4361 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4362 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4363 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4364 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4365 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4366 | 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] | 4367 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4368 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4369 | if (ret != LY_SUCCESS) { |
| 4370 | if (ret == LY_EDENIED) { |
| 4371 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4372 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4373 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4374 | } |
| 4375 | return LY_EVALID; |
| 4376 | } |
| 4377 | |
| 4378 | /* check for mandatory nodes |
| 4379 | * - new cases augmenting some choice can have mandatory nodes |
| 4380 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4381 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4382 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4383 | allow_mandatory = 1; |
| 4384 | } |
| 4385 | |
| 4386 | when_shared = NULL; |
| 4387 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4388 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4389 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4390 | && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) |
Radek Krejci | 2d56a89 | 2019-02-19 09:05:26 +0100 | [diff] [blame] | 4391 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4392 | && !(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] | 4393 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4394 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4395 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4396 | return LY_EVALID; |
| 4397 | } |
| 4398 | |
| 4399 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4400 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4401 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4402 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4403 | } else { |
| 4404 | LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4405 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4406 | } |
| 4407 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4408 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4409 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4410 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4411 | * 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] | 4412 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4413 | /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4414 | for (node = (struct lysc_node*)lysc_node_children(target, flags); node->next && node->next->parent == node->parent; node = node->next); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4415 | } else if (target->nodetype == LYS_CHOICE) { |
| 4416 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4417 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4418 | } else { |
| 4419 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4420 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4421 | if (!node) { |
| 4422 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4423 | break; |
| 4424 | } |
| 4425 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4426 | } |
| 4427 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4428 | 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] | 4429 | node->flags &= ~LYS_MAND_TRUE; |
| 4430 | lys_compile_mandatory_parents(target, 0); |
| 4431 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4432 | "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4433 | return LY_EVALID; |
| 4434 | } |
| 4435 | |
| 4436 | /* pass augment's when to all the children */ |
| 4437 | if (aug_p->when) { |
| 4438 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4439 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4440 | ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4441 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4442 | |
| 4443 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4444 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4445 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4446 | } |
| 4447 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4448 | when_shared = *when; |
| 4449 | } else { |
| 4450 | ++when_shared->refcount; |
| 4451 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4452 | |
| 4453 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4454 | /* in this case check "when" again for all children because of dummy node check */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4455 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4456 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4457 | } |
| 4458 | } |
| 4459 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4460 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4461 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4462 | switch (target->nodetype) { |
| 4463 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4464 | COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4465 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4466 | COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4467 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4468 | break; |
| 4469 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4470 | COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4471 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4472 | COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4473 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4474 | break; |
| 4475 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4476 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4477 | if (aug_p->actions) { |
| 4478 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4479 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4480 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4481 | return LY_EVALID; |
| 4482 | } |
| 4483 | if (aug_p->notifs) { |
| 4484 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4485 | "Invalid augment of %s node which is not allowed to contain notification node \"%s\".", |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4486 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4487 | return LY_EVALID; |
| 4488 | } |
| 4489 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4490 | |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4491 | /* add this module into the target module augmented_by */ |
| 4492 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4493 | *aug_mod = ctx->mod; |
| 4494 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4495 | lysc_update_path(ctx, NULL, NULL); |
| 4496 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4497 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4498 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4499 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4500 | return ret; |
| 4501 | } |
| 4502 | |
| 4503 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4504 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4505 | * |
| 4506 | * @param[in] ctx Compile context. |
| 4507 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4508 | * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node. |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4509 | * @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] | 4510 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4511 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4512 | * 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] | 4513 | * @return LY_ERR value. |
| 4514 | */ |
| 4515 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4516 | lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, int refine_flag) |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4517 | { |
| 4518 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4519 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4520 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4521 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4522 | return LY_EVALID; |
| 4523 | } |
| 4524 | |
| 4525 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4526 | /* check if node has default value */ |
| 4527 | if (node->nodetype & LYS_LEAF) { |
| 4528 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4529 | if (refine_flag) { |
| 4530 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4531 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4532 | return LY_EVALID; |
| 4533 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4534 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4535 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4536 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4537 | |
| 4538 | /* update the list of incomplete default values if needed */ |
| 4539 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4540 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4541 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4542 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4543 | free(leaf->dflt); |
| 4544 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4545 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4546 | } |
| 4547 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4548 | if (refine_flag) { |
| 4549 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4550 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4551 | return LY_EVALID; |
| 4552 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4553 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4554 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4555 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid refine of mandatory under the default case."); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4556 | return LY_EVALID; |
| 4557 | } |
| 4558 | |
| 4559 | node->flags &= ~LYS_MAND_FALSE; |
| 4560 | node->flags |= LYS_MAND_TRUE; |
| 4561 | lys_compile_mandatory_parents(node->parent, 1); |
| 4562 | } else { |
| 4563 | /* make mandatory false */ |
| 4564 | node->flags &= ~LYS_MAND_TRUE; |
| 4565 | node->flags |= LYS_MAND_FALSE; |
| 4566 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4567 | if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt && ((struct lysc_node_leaf*)node)->type->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4568 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4569 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4570 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4571 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4572 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4573 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4574 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4575 | } |
| 4576 | } |
| 4577 | return LY_SUCCESS; |
| 4578 | } |
| 4579 | |
| 4580 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4581 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4582 | * If present, also apply uses's modificators. |
| 4583 | * |
| 4584 | * @param[in] ctx Compile context |
| 4585 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4586 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4587 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4588 | * the compile context. |
| 4589 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4590 | */ |
| 4591 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4592 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent) |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4593 | { |
| 4594 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4595 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4596 | /* 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] | 4597 | struct lysc_node_container context_node_fake = |
| 4598 | {.nodetype = LYS_CONTAINER, |
| 4599 | .module = ctx->mod, |
| 4600 | .flags = parent ? parent->flags : 0, |
| 4601 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4602 | .prev = (struct lysc_node*)&context_node_fake, |
| 4603 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4604 | struct lysp_grp *grp = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4605 | LY_ARRAY_SIZE_TYPE u, v; |
| 4606 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4607 | int found; |
| 4608 | const char *id, *name, *prefix; |
| 4609 | size_t prefix_len, name_len; |
| 4610 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4611 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4612 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4613 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4614 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4615 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4616 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4617 | struct lysp_augment **augments = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4618 | LY_ARRAY_SIZE_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4619 | struct lysc_notif **notifs = NULL; |
| 4620 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4621 | |
| 4622 | /* search for the grouping definition */ |
| 4623 | found = 0; |
| 4624 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4625 | LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4626 | if (prefix) { |
| 4627 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4628 | if (!mod) { |
| 4629 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4630 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4631 | return LY_EVALID; |
| 4632 | } |
| 4633 | } else { |
| 4634 | mod = ctx->mod_def; |
| 4635 | } |
| 4636 | if (mod == ctx->mod_def) { |
| 4637 | for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4638 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4639 | LY_ARRAY_FOR(grp, u) { |
| 4640 | if (!strcmp(grp[u].name, name)) { |
| 4641 | grp = &grp[u]; |
| 4642 | found = 1; |
| 4643 | break; |
| 4644 | } |
| 4645 | } |
| 4646 | } |
| 4647 | } |
| 4648 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4649 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4650 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4651 | if (grp) { |
| 4652 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4653 | if (!strcmp(grp[u].name, name)) { |
| 4654 | grp = &grp[u]; |
| 4655 | found = 1; |
| 4656 | } |
| 4657 | } |
| 4658 | } |
| 4659 | if (!found && mod->parsed->includes) { |
| 4660 | /* ... and all the submodules */ |
| 4661 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4662 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4663 | if (grp) { |
| 4664 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4665 | if (!strcmp(grp[v].name, name)) { |
| 4666 | grp = &grp[v]; |
| 4667 | found = 1; |
| 4668 | } |
| 4669 | } |
| 4670 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4671 | } |
| 4672 | } |
| 4673 | } |
| 4674 | if (!found) { |
| 4675 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4676 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4677 | return LY_EVALID; |
| 4678 | } |
| 4679 | |
| 4680 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4681 | grp_stack_count = ctx->groupings.count; |
| 4682 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4683 | if (grp_stack_count == ctx->groupings.count) { |
| 4684 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4685 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4686 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4687 | return LY_EVALID; |
| 4688 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4689 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4690 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4691 | grp->flags |= LYS_USED_GRP; |
| 4692 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4693 | |
| 4694 | /* switch context's mod_def */ |
| 4695 | mod_old = ctx->mod_def; |
| 4696 | ctx->mod_def = mod; |
| 4697 | |
| 4698 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4699 | LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), cleanup); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4700 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4701 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4702 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4703 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4704 | LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4705 | |
| 4706 | /* some preparation for applying refines */ |
| 4707 | if (grp->data == node_p) { |
| 4708 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4709 | if (parent) { |
| 4710 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4711 | } else if (ctx->mod->compiled->data) { |
| 4712 | child = ctx->mod->compiled->data; |
| 4713 | } else { |
| 4714 | child = NULL; |
| 4715 | } |
| 4716 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4717 | } |
| 4718 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4719 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4720 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4721 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4722 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4723 | /* pass uses's when to all the data children, actions and notifications are ignored */ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4724 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4725 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4726 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4727 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 4728 | |
| 4729 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4730 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4731 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4732 | } |
| 4733 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4734 | when_shared = *when; |
| 4735 | } else { |
| 4736 | ++when_shared->refcount; |
| 4737 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4738 | |
| 4739 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4740 | /* in this case check "when" again for all children because of dummy node check */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4741 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4742 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4743 | } |
| 4744 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4745 | } |
| 4746 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4747 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4748 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4749 | /* fix child link of our fake container to point to the first child of the original list */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4750 | context_node_fake.child->prev = parent ? lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4751 | } |
| 4752 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4753 | /* compile actions */ |
| 4754 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4755 | if (actions) { |
| 4756 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4757 | COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4758 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4759 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4760 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4761 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4762 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4763 | } |
| 4764 | } |
| 4765 | |
| 4766 | /* compile notifications */ |
| 4767 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4768 | if (notifs) { |
| 4769 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4770 | COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4771 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4772 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4773 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4774 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4775 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4776 | } |
| 4777 | } |
| 4778 | |
| 4779 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4780 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4781 | LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4782 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4783 | LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4784 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4785 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4786 | /* reload previous context's mod_def */ |
| 4787 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4788 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4789 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4790 | /* apply refine */ |
| 4791 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4792 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4793 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4794 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, ctx->mod, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4795 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4796 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4797 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4798 | |
| 4799 | /* default value */ |
| 4800 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4801 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4802 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4803 | "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_SIZE_TYPE" default values.", |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4804 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4805 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4806 | } |
| 4807 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4808 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4809 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4810 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4811 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4812 | } |
| 4813 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4814 | struct ly_err_item *err = NULL; |
| 4815 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4816 | if (leaf->dflt) { |
| 4817 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4818 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4819 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4820 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4821 | } else { |
| 4822 | /* prepare a new one */ |
| 4823 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4824 | leaf->dflt->realtype = leaf->type; |
| 4825 | } |
| 4826 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4827 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4828 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4829 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4830 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4831 | leaf->dflt->realtype->refcount++; |
| 4832 | if (err) { |
| 4833 | ly_err_print(err); |
| 4834 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4835 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4836 | ly_err_free(err); |
| 4837 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4838 | if (rc == LY_EINCOMPLETE) { |
| 4839 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4840 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4841 | |
| 4842 | /* but in general result is so far ok */ |
| 4843 | rc = LY_SUCCESS; |
| 4844 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4845 | LY_CHECK_GOTO(rc, cleanup); |
| 4846 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4847 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4848 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4849 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4850 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4851 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4852 | "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4853 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4854 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4855 | |
| 4856 | /* remove previous set of default values */ |
| 4857 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4858 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4859 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 4860 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 4861 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4862 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4863 | LY_ARRAY_FREE(llist->dflts); |
| 4864 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4865 | LY_ARRAY_FREE(llist->dflts_mods); |
| 4866 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4867 | |
| 4868 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4869 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4870 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4871 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4872 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4873 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 4874 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4875 | LY_ARRAY_INCREMENT(llist->dflts); |
| 4876 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 4877 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4878 | rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]), |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4879 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4880 | lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4881 | llist->dflts[u]->realtype->refcount++; |
| 4882 | if (err) { |
| 4883 | ly_err_print(err); |
| 4884 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4885 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 4886 | ly_err_free(err); |
| 4887 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4888 | if (rc == LY_EINCOMPLETE) { |
| 4889 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4890 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4891 | |
| 4892 | /* but in general result is so far ok */ |
| 4893 | rc = LY_SUCCESS; |
| 4894 | } |
| 4895 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4896 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4897 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4898 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4899 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4900 | /* unset LYS_SET_DFLT from the current default case */ |
| 4901 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4902 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4903 | LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4904 | } |
| 4905 | } |
| 4906 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4907 | /* description */ |
| 4908 | if (rfn->dsc) { |
| 4909 | FREE_STRING(ctx->ctx, node->dsc); |
| 4910 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4911 | } |
| 4912 | |
| 4913 | /* reference */ |
| 4914 | if (rfn->ref) { |
| 4915 | FREE_STRING(ctx->ctx, node->ref); |
| 4916 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4917 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4918 | |
| 4919 | /* config */ |
| 4920 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4921 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4922 | LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4923 | } else { |
| 4924 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4925 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4926 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | /* mandatory */ |
| 4930 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4931 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4932 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4933 | |
| 4934 | /* presence */ |
| 4935 | if (rfn->presence) { |
| 4936 | if (node->nodetype != LYS_CONTAINER) { |
| 4937 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4938 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4939 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4940 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4941 | } |
| 4942 | node->flags |= LYS_PRESENCE; |
| 4943 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4944 | |
| 4945 | /* must */ |
| 4946 | if (rfn->musts) { |
| 4947 | switch (node->nodetype) { |
| 4948 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4949 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4950 | break; |
| 4951 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4952 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4953 | break; |
| 4954 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4955 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4956 | break; |
| 4957 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4958 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4959 | break; |
| 4960 | case LYS_ANYXML: |
| 4961 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4962 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4963 | break; |
| 4964 | default: |
| 4965 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4966 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 4967 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4968 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4969 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4970 | ly_set_add(&ctx->xpath, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4971 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4972 | |
| 4973 | /* min/max-elements */ |
| 4974 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4975 | switch (node->nodetype) { |
| 4976 | case LYS_LEAFLIST: |
| 4977 | if (rfn->flags & LYS_SET_MAX) { |
| 4978 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4979 | } |
| 4980 | if (rfn->flags & LYS_SET_MIN) { |
| 4981 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4982 | if (rfn->min) { |
| 4983 | node->flags |= LYS_MAND_TRUE; |
| 4984 | lys_compile_mandatory_parents(node->parent, 1); |
| 4985 | } else { |
| 4986 | node->flags &= ~LYS_MAND_TRUE; |
| 4987 | lys_compile_mandatory_parents(node->parent, 0); |
| 4988 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4989 | } |
| 4990 | break; |
| 4991 | case LYS_LIST: |
| 4992 | if (rfn->flags & LYS_SET_MAX) { |
| 4993 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4994 | } |
| 4995 | if (rfn->flags & LYS_SET_MIN) { |
| 4996 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4997 | if (rfn->min) { |
| 4998 | node->flags |= LYS_MAND_TRUE; |
| 4999 | lys_compile_mandatory_parents(node->parent, 1); |
| 5000 | } else { |
| 5001 | node->flags &= ~LYS_MAND_TRUE; |
| 5002 | lys_compile_mandatory_parents(node->parent, 0); |
| 5003 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5004 | } |
| 5005 | break; |
| 5006 | default: |
| 5007 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5008 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5009 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5010 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5011 | } |
| 5012 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5013 | |
| 5014 | /* if-feature */ |
| 5015 | if (rfn->iffeatures) { |
| 5016 | /* any node in compiled tree can get additional if-feature, so do not check nodetype */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5017 | COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5018 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5019 | |
| 5020 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5021 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5022 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5023 | /* do some additional checks of the changed nodes when all the refines are applied */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5024 | for (uint32_t i = 0; i < refined.count; ++i) { |
| 5025 | node = (struct lysc_node*)refined.objs[i]; |
| 5026 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5027 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5028 | |
| 5029 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5030 | if ((node->flags & LYS_MAND_TRUE) && |
| 5031 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5032 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5033 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5034 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5035 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5036 | } |
| 5037 | |
| 5038 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5039 | if (node->nodetype == LYS_LIST) { |
| 5040 | min = ((struct lysc_node_list*)node)->min; |
| 5041 | max = ((struct lysc_node_list*)node)->max; |
| 5042 | } else { |
| 5043 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5044 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5045 | } |
| 5046 | if (min > max) { |
| 5047 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5048 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5049 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5050 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5051 | } |
| 5052 | } |
| 5053 | } |
| 5054 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5055 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5056 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5057 | |
| 5058 | cleanup: |
| 5059 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5060 | if (context_node_fake.child) { |
| 5061 | context_node_fake.child->prev = child; |
| 5062 | } |
| 5063 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5064 | child->parent = parent; |
| 5065 | } |
| 5066 | |
| 5067 | if (uses_p->augments || uses_p->refines) { |
| 5068 | /* return back actions and notifications in case they were separated for augment/refine processing */ |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5069 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5070 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5071 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5072 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5073 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5074 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5075 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5076 | } |
| 5077 | } |
| 5078 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5079 | /* reload previous context's mod_def */ |
| 5080 | ctx->mod_def = mod_old; |
| 5081 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5082 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5083 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5084 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5085 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5086 | |
| 5087 | return ret; |
| 5088 | } |
| 5089 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5090 | static int |
| 5091 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5092 | { |
| 5093 | struct lysp_node *iter; |
| 5094 | int len = 0; |
| 5095 | |
| 5096 | *path = NULL; |
| 5097 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5098 | char *s = *path; |
| 5099 | char *id; |
| 5100 | |
| 5101 | switch (iter->nodetype) { |
| 5102 | case LYS_USES: |
| 5103 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5104 | break; |
| 5105 | case LYS_GROUPING: |
| 5106 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5107 | break; |
| 5108 | case LYS_AUGMENT: |
| 5109 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5110 | break; |
| 5111 | default: |
| 5112 | id = strdup(iter->name); |
| 5113 | break; |
| 5114 | } |
| 5115 | |
| 5116 | if (!iter->parent) { |
| 5117 | /* print prefix */ |
| 5118 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5119 | } else { |
| 5120 | /* prefix is the same as in parent */ |
| 5121 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5122 | } |
| 5123 | free(s); |
| 5124 | free(id); |
| 5125 | } |
| 5126 | |
| 5127 | if (len < 0) { |
| 5128 | free(*path); |
| 5129 | *path = NULL; |
| 5130 | } else if (len == 0) { |
| 5131 | *path = strdup("/"); |
| 5132 | len = 1; |
| 5133 | } |
| 5134 | return len; |
| 5135 | } |
| 5136 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5137 | /** |
| 5138 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5139 | * |
| 5140 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5141 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5142 | */ |
| 5143 | static LY_ERR |
| 5144 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5145 | { |
| 5146 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5147 | char *path; |
| 5148 | int len; |
| 5149 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5150 | struct lysp_node_uses fake_uses = { |
| 5151 | .parent = node_p, |
| 5152 | .nodetype = LYS_USES, |
| 5153 | .flags = 0, .next = NULL, |
| 5154 | .name = grp->name, |
| 5155 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5156 | .refines = NULL, .augments = NULL |
| 5157 | }; |
| 5158 | struct lysc_node_container fake_container = { |
| 5159 | .nodetype = LYS_CONTAINER, |
| 5160 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5161 | .module = ctx->mod, |
| 5162 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5163 | .prev = (struct lysc_node*)&fake_container, |
| 5164 | .name = "fake", |
| 5165 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5166 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5167 | }; |
| 5168 | |
| 5169 | if (grp->parent) { |
| 5170 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5171 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5172 | |
| 5173 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5174 | if (len < 0) { |
| 5175 | LOGMEM(ctx->ctx); |
| 5176 | return LY_EMEM; |
| 5177 | } |
| 5178 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5179 | ctx->path_len = (uint16_t)len; |
| 5180 | free(path); |
| 5181 | |
| 5182 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5183 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5184 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5185 | lysc_update_path(ctx, NULL, NULL); |
| 5186 | lysc_update_path(ctx, NULL, NULL); |
| 5187 | |
| 5188 | ctx->path_len = 1; |
| 5189 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5190 | |
| 5191 | /* cleanup */ |
| 5192 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5193 | |
| 5194 | return ret; |
| 5195 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5196 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5197 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5198 | * @brief Compile parsed schema node information. |
| 5199 | * @param[in] ctx Compile context |
| 5200 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5201 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5202 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5203 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5204 | * @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). |
| 5205 | * 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] | 5206 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5207 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5208 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5209 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5210 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5211 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5212 | struct lysc_node *node; |
| 5213 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5214 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5215 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5216 | LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5217 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5218 | if (node_p->nodetype != LYS_USES) { |
| 5219 | lysc_update_path(ctx, parent, node_p->name); |
| 5220 | } else { |
| 5221 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5222 | lysc_update_path(ctx, NULL, node_p->name); |
| 5223 | } |
| 5224 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5225 | switch (node_p->nodetype) { |
| 5226 | case LYS_CONTAINER: |
| 5227 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5228 | node_compile_spec = lys_compile_node_container; |
| 5229 | break; |
| 5230 | case LYS_LEAF: |
| 5231 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5232 | node_compile_spec = lys_compile_node_leaf; |
| 5233 | break; |
| 5234 | case LYS_LIST: |
| 5235 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5236 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5237 | break; |
| 5238 | case LYS_LEAFLIST: |
| 5239 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5240 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5241 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5242 | case LYS_CHOICE: |
| 5243 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5244 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5245 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5246 | case LYS_ANYXML: |
| 5247 | case LYS_ANYDATA: |
| 5248 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5249 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5250 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5251 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5252 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5253 | lysc_update_path(ctx, NULL, NULL); |
| 5254 | lysc_update_path(ctx, NULL, NULL); |
| 5255 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5256 | default: |
| 5257 | LOGINT(ctx->ctx); |
| 5258 | return LY_EINT; |
| 5259 | } |
| 5260 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5261 | node->nodetype = node_p->nodetype; |
| 5262 | node->module = ctx->mod; |
| 5263 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5264 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5265 | |
| 5266 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5267 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5268 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5269 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5270 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5271 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5272 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5273 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5274 | node->flags |= LYS_CONFIG_R; |
| 5275 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5276 | /* config not explicitely set, inherit it from parent */ |
| 5277 | if (parent) { |
| 5278 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5279 | } else { |
| 5280 | /* default is config true */ |
| 5281 | node->flags |= LYS_CONFIG_W; |
| 5282 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5283 | } else { |
| 5284 | /* config set explicitely */ |
| 5285 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5286 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5287 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5288 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5289 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5290 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5291 | goto error; |
| 5292 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5293 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5294 | /* *list ordering */ |
| 5295 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5296 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5297 | LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).", |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5298 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5299 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5300 | node->flags &= ~LYS_ORDBY_MASK; |
| 5301 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5302 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5303 | /* default ordering is system */ |
| 5304 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5305 | } |
| 5306 | } |
| 5307 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5308 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5309 | * 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] | 5310 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5311 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5312 | * the parent is choice (parent here) or some case (so we have to get its flags to check) */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5313 | LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (parent ? parent->flags : 0)), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5314 | } |
| 5315 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5316 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5317 | node->sp = node_p; |
| 5318 | } |
| 5319 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5320 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5321 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5322 | if (node_p->when) { |
| 5323 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5324 | LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5325 | |
| 5326 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5327 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5328 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5329 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5330 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5331 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5332 | |
| 5333 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5334 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5335 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5336 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5337 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5338 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5339 | if (node->flags & LYS_MAND_TRUE) { |
| 5340 | lys_compile_mandatory_parents(parent, 1); |
| 5341 | } |
| 5342 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5343 | lysc_update_path(ctx, NULL, NULL); |
| 5344 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5345 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5346 | if (parent) { |
| 5347 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5348 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5349 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5350 | } else { |
| 5351 | lysc_update_path(ctx, parent, node->name); |
| 5352 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5353 | cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5354 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5355 | if (uses_status) { |
| 5356 | |
| 5357 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5358 | /* 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] | 5359 | * it directly gets the same status flags as the choice; |
| 5360 | * uses_status cannot be applied here since uses cannot be child statement of choice */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5361 | LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5362 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5363 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5364 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5365 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5366 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5367 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5368 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5369 | |
| 5370 | if (parent->nodetype == LYS_CHOICE) { |
| 5371 | lysc_update_path(ctx, NULL, NULL); |
| 5372 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5373 | } else { |
| 5374 | /* top-level element */ |
| 5375 | if (!ctx->mod->compiled->data) { |
| 5376 | ctx->mod->compiled->data = node; |
| 5377 | } else { |
| 5378 | /* insert at the end of the module's top-level nodes list */ |
| 5379 | ctx->mod->compiled->data->prev->next = node; |
| 5380 | node->prev = ctx->mod->compiled->data->prev; |
| 5381 | ctx->mod->compiled->data->prev = node; |
| 5382 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5383 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5384 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5385 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5386 | return LY_EVALID; |
| 5387 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5388 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5389 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5390 | |
| 5391 | return LY_SUCCESS; |
| 5392 | |
| 5393 | error: |
| 5394 | lysc_node_free(ctx->ctx, node); |
| 5395 | return ret; |
| 5396 | } |
| 5397 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5398 | static void |
| 5399 | lysc_disconnect(struct lysc_node *node) |
| 5400 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5401 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5402 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5403 | int remove_cs = 0; |
| 5404 | |
| 5405 | parent = node->parent; |
| 5406 | |
| 5407 | /* parent's first child */ |
| 5408 | if (!parent) { |
| 5409 | return; |
| 5410 | } |
| 5411 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5412 | cs = (struct lysc_node_case*)node; |
| 5413 | } else if (parent->nodetype == LYS_CASE) { |
| 5414 | /* disconnecting some node in a case */ |
| 5415 | cs = (struct lysc_node_case*)parent; |
| 5416 | parent = cs->parent; |
| 5417 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5418 | if (child == node) { |
| 5419 | if (cs->child == child) { |
| 5420 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5421 | /* case with a single child -> remove also the case */ |
| 5422 | child->parent = NULL; |
| 5423 | remove_cs = 1; |
| 5424 | } else { |
| 5425 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5426 | } |
| 5427 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5428 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5429 | } |
| 5430 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5431 | if (!remove_cs) { |
| 5432 | cs = NULL; |
| 5433 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5434 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5435 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5436 | } |
| 5437 | |
| 5438 | if (cs) { |
| 5439 | if (remove_cs) { |
| 5440 | /* cs has only one child which is being also removed */ |
| 5441 | lysc_disconnect((struct lysc_node*)cs); |
| 5442 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5443 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5444 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5445 | /* default case removed */ |
| 5446 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5447 | } |
| 5448 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5449 | /* first case removed */ |
| 5450 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5451 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5452 | if (cs->child) { |
| 5453 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5454 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5455 | prev = cs->child->prev; |
| 5456 | } /* else all the children are under a single case */ |
| 5457 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5458 | if (child->parent != (struct lysc_node*)cs) { |
| 5459 | break; |
| 5460 | } |
| 5461 | lysc_node_free(node->module->ctx, child); |
| 5462 | } |
| 5463 | if (prev) { |
| 5464 | if (prev->next) { |
| 5465 | prev->next = child; |
| 5466 | } |
| 5467 | if (child) { |
| 5468 | child->prev = prev; |
| 5469 | } else { |
| 5470 | /* link from the first child under the cases */ |
| 5471 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5472 | } |
| 5473 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5474 | } |
| 5475 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5479 | if (node->prev->next) { |
| 5480 | node->prev->next = node->next; |
| 5481 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5482 | if (node->next) { |
| 5483 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5484 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5485 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5486 | if (child) { |
| 5487 | child->prev = node->prev; |
| 5488 | } |
| 5489 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5490 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5491 | } |
| 5492 | } |
| 5493 | |
| 5494 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5495 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5496 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5497 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5498 | struct ly_set devs_p = {0}; |
| 5499 | struct ly_set targets = {0}; |
| 5500 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5501 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5502 | struct lysc_action *rpcs; |
| 5503 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5504 | struct lysp_deviation *dev; |
| 5505 | struct lysp_deviate *d, **dp_new; |
| 5506 | struct lysp_deviate_add *d_add; |
| 5507 | struct lysp_deviate_del *d_del; |
| 5508 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5509 | LY_ARRAY_SIZE_TYPE u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5510 | struct lysc_deviation { |
| 5511 | const char *nodeid; |
| 5512 | struct lysc_node *target; /* target node of the deviation */ |
| 5513 | struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5514 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5515 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5516 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5517 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5518 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5519 | const char *prefix, *name, *nodeid, *dflt; |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 5520 | struct lys_module *mod, **dev_mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5521 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5522 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5523 | |
| 5524 | /* get all deviations from the module and all its submodules ... */ |
| 5525 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5526 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5527 | } |
| 5528 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5529 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5530 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5531 | } |
| 5532 | } |
| 5533 | if (!devs_p.count) { |
| 5534 | /* nothing to do */ |
| 5535 | return LY_SUCCESS; |
| 5536 | } |
| 5537 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5538 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5539 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5540 | /* ... and group them by the target node */ |
| 5541 | devs = calloc(devs_p.count, sizeof *devs); |
| 5542 | for (u = 0; u < devs_p.count; ++u) { |
| 5543 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5544 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5545 | |
| 5546 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5547 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5548 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5549 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5550 | /* move the target pointer to input/output to make them different from the action and |
| 5551 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5552 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5553 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5554 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5555 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5556 | flags |= LYSC_OPT_INTERNAL; |
| 5557 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5558 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5559 | flags |= LYSC_OPT_INTERNAL; |
| 5560 | } |
| 5561 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5562 | /* insert into the set of targets with duplicity detection */ |
| 5563 | i = ly_set_add(&targets, target, 0); |
| 5564 | if (!devs[i]) { |
| 5565 | /* new record */ |
| 5566 | devs[i] = calloc(1, sizeof **devs); |
| 5567 | devs[i]->target = target; |
| 5568 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5569 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5570 | } |
| 5571 | /* add deviates into the deviation's list of deviates */ |
| 5572 | for (d = dev->deviates; d; d = d->next) { |
| 5573 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5574 | *dp_new = d; |
| 5575 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5576 | devs[i]->not_supported = 1; |
| 5577 | } |
| 5578 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5579 | |
| 5580 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | /* MACROS for deviates checking */ |
| 5584 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5585 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5586 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5587 | goto cleanup; \ |
| 5588 | } |
| 5589 | |
| 5590 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5591 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5592 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_SIZE_TYPE") %s properties.", \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5593 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5594 | goto cleanup; \ |
| 5595 | } |
| 5596 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5597 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5598 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5599 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5600 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5601 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5602 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5603 | goto cleanup; \ |
| 5604 | } |
| 5605 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5606 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5607 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5608 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5609 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5610 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5611 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5612 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5613 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5614 | goto cleanup; \ |
| 5615 | } |
| 5616 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5617 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5618 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5619 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5620 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5621 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5622 | goto cleanup; \ |
| 5623 | } |
| 5624 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5625 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5626 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5627 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5628 | goto cleanup; \ |
| 5629 | } |
| 5630 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5631 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5632 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5633 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5634 | "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5635 | goto cleanup; \ |
| 5636 | } |
| 5637 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5638 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5639 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5640 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5641 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5642 | if (!strcmp(((TYPE)devs[u]->target)->ARRAY_TRG[y]VALMEMBER_CMP, d_del->ARRAY_DEV[x]VALMEMBER)) { break; } \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5643 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5644 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5645 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5646 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5647 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5648 | goto cleanup; \ |
| 5649 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5650 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5651 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5652 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5653 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5654 | (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5655 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5656 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5657 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5658 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5659 | } |
| 5660 | |
| 5661 | /* apply deviations */ |
| 5662 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5663 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5664 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5665 | struct ly_err_item *err = NULL; |
| 5666 | |
| 5667 | dflt = NULL; |
| 5668 | changed_type = 0; |
| 5669 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5670 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5671 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5672 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5673 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5674 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5675 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5676 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5677 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5678 | } |
| 5679 | } |
| 5680 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5681 | /* not-supported */ |
| 5682 | if (devs[u]->not_supported) { |
| 5683 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5684 | LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_SIZE_TYPE") deviates on node \"%s\" since the node is not-supported.", |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5685 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5686 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5687 | |
| 5688 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5689 | if (devs[u]->target->parent) { \ |
| 5690 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5691 | } else { \ |
| 5692 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5693 | } \ |
| 5694 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5695 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5696 | } \ |
| 5697 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5698 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5699 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5700 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5701 | } |
| 5702 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5703 | if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5704 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5705 | /* remove RPC's/action's input */ |
| 5706 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5707 | memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5708 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5709 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5710 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5711 | /* remove RPC's/action's output */ |
| 5712 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5713 | memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5714 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5715 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5716 | } else { |
| 5717 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5718 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5719 | } |
| 5720 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5721 | /* remove Notification */ |
| 5722 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5723 | } else { |
| 5724 | /* remove the target node */ |
| 5725 | lysc_disconnect(devs[u]->target); |
| 5726 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5727 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5728 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5729 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5730 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5731 | continue; |
| 5732 | } |
| 5733 | |
| 5734 | /* list of deviates (not-supported is not present in the list) */ |
| 5735 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5736 | d = devs[u]->deviates[v]; |
| 5737 | |
| 5738 | switch (d->mod) { |
| 5739 | case LYS_DEV_ADD: |
| 5740 | d_add = (struct lysp_deviate_add*)d; |
| 5741 | /* [units-stmt] */ |
| 5742 | if (d_add->units) { |
| 5743 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5744 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5745 | |
| 5746 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5747 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5748 | } |
| 5749 | |
| 5750 | /* *must-stmt */ |
| 5751 | if (d_add->musts) { |
| 5752 | switch (devs[u]->target->nodetype) { |
| 5753 | case LYS_CONTAINER: |
| 5754 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5755 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 5756 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5757 | break; |
| 5758 | case LYS_LEAF: |
| 5759 | case LYS_LEAFLIST: |
| 5760 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5761 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 5762 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5763 | break; |
| 5764 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5765 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 5766 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5767 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5768 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5769 | case LYS_ACTION: |
| 5770 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5771 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 5772 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5773 | break; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 5774 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5775 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 5776 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5777 | break; |
| 5778 | } |
| 5779 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5780 | default: |
| 5781 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5782 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5783 | goto cleanup; |
| 5784 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5785 | ly_set_add(&ctx->xpath, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5786 | } |
| 5787 | |
| 5788 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5789 | if (d_add->uniques) { |
| 5790 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5791 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5792 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5793 | |
| 5794 | /* *default-stmt */ |
| 5795 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5796 | switch (devs[u]->target->nodetype) { |
| 5797 | case LYS_LEAF: |
| 5798 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5799 | DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt, dflt_mod); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5800 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5801 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5802 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5803 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5804 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5805 | } else { |
| 5806 | /* prepare new default value storage */ |
| 5807 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5808 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5809 | dflt = d_add->dflts[0]; |
| 5810 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5811 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5812 | /* mark the new default values as leaf's own */ |
| 5813 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5814 | break; |
| 5815 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5816 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5817 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5818 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5819 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5820 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5821 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5822 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5823 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5824 | LY_ARRAY_FREE(llist->dflts); |
| 5825 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5826 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5827 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5828 | } |
| 5829 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5830 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5831 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 5832 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5833 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5834 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5835 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5836 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5837 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5838 | llist->dflts[x]->realtype = llist->type; |
| 5839 | rc = llist->type->plugin->store(ctx->ctx, llist->type, d_add->dflts[x - y], strlen(d_add->dflts[x - y]), |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5840 | LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5841 | (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5842 | llist->dflts[x]->realtype->refcount++; |
| 5843 | if (err) { |
| 5844 | ly_err_print(err); |
| 5845 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5846 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5847 | d_add->dflts[x - y], err->msg); |
| 5848 | ly_err_free(err); |
| 5849 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5850 | if (rc == LY_EINCOMPLETE) { |
| 5851 | /* postpone default compilation when the tree is complete */ |
| 5852 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5853 | |
| 5854 | /* but in general result is so far ok */ |
| 5855 | rc = LY_SUCCESS; |
| 5856 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5857 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5858 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5859 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5860 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5861 | break; |
| 5862 | case LYS_CHOICE: |
| 5863 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5864 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5865 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5866 | * to allow making the default case even the augmented case from the deviating module */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5867 | if (lys_compile_deviation_set_choice_dflt(ctx, d_add->dflts[0], (struct lysc_node_choice*)devs[u]->target)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5868 | goto cleanup; |
| 5869 | } |
| 5870 | break; |
| 5871 | default: |
| 5872 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5873 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5874 | goto cleanup; |
| 5875 | } |
| 5876 | } |
| 5877 | |
| 5878 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5879 | if (d_add->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5880 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5881 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5882 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5883 | goto cleanup; |
| 5884 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5885 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5886 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5887 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5888 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5889 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5890 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5891 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5892 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5893 | goto cleanup; |
| 5894 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5895 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5896 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5897 | |
| 5898 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5899 | if (d_add->flags & LYS_MAND_MASK) { |
| 5900 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5901 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5902 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5903 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5904 | goto cleanup; |
| 5905 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5906 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5907 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5908 | |
| 5909 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5910 | if (d_add->flags & LYS_SET_MIN) { |
| 5911 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5912 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5913 | /* change value */ |
| 5914 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5915 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5916 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5917 | /* change value */ |
| 5918 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5919 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5920 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5921 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5922 | goto cleanup; |
| 5923 | } |
| 5924 | if (d_add->min) { |
| 5925 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5926 | } |
| 5927 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5928 | |
| 5929 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5930 | if (d_add->flags & LYS_SET_MAX) { |
| 5931 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5932 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5933 | /* change value */ |
| 5934 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5935 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5936 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5937 | /* change value */ |
| 5938 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5939 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5940 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5941 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 5942 | goto cleanup; |
| 5943 | } |
| 5944 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5945 | |
| 5946 | break; |
| 5947 | case LYS_DEV_DELETE: |
| 5948 | d_del = (struct lysp_deviate_del*)d; |
| 5949 | |
| 5950 | /* [units-stmt] */ |
| 5951 | if (d_del->units) { |
| 5952 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5953 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 5954 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 5955 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5956 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5957 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5958 | goto cleanup; |
| 5959 | } |
| 5960 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5961 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5962 | } |
| 5963 | |
| 5964 | /* *must-stmt */ |
| 5965 | if (d_del->musts) { |
| 5966 | switch (devs[u]->target->nodetype) { |
| 5967 | case LYS_CONTAINER: |
| 5968 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5969 | DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5970 | break; |
| 5971 | case LYS_LEAF: |
| 5972 | case LYS_LEAFLIST: |
| 5973 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5974 | DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5975 | break; |
| 5976 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5977 | DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5978 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5979 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5980 | case LYS_ACTION: |
| 5981 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5982 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5983 | break; |
| 5984 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5985 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5986 | break; |
| 5987 | } |
| 5988 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5989 | default: |
| 5990 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5991 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5992 | goto cleanup; |
| 5993 | } |
| 5994 | } |
| 5995 | |
| 5996 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5997 | if (d_del->uniques) { |
| 5998 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5999 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6000 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6001 | LY_ARRAY_FOR(list->uniques, z) { |
| 6002 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6003 | nodeid = strpbrk(name, " \t\n"); |
| 6004 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 6005 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6006 | break; |
| 6007 | } |
| 6008 | while (isspace(*nodeid)) { |
| 6009 | ++nodeid; |
| 6010 | } |
| 6011 | } else { |
| 6012 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6013 | break; |
| 6014 | } |
| 6015 | } |
| 6016 | } |
| 6017 | if (!name) { |
| 6018 | /* complete match - remove the unique */ |
| 6019 | LY_ARRAY_DECREMENT(list->uniques); |
| 6020 | LY_ARRAY_FREE(list->uniques[z]); |
| 6021 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6022 | --z; |
| 6023 | break; |
| 6024 | } |
| 6025 | } |
| 6026 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6027 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6028 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6029 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6030 | goto cleanup; |
| 6031 | } |
| 6032 | } |
| 6033 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6034 | LY_ARRAY_FREE(list->uniques); |
| 6035 | list->uniques = NULL; |
| 6036 | } |
| 6037 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6038 | |
| 6039 | /* *default-stmt */ |
| 6040 | if (d_del->dflts) { |
| 6041 | switch (devs[u]->target->nodetype) { |
| 6042 | case LYS_LEAF: |
| 6043 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6044 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6045 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6046 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6047 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6048 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6049 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6050 | if (i) { |
| 6051 | free((char*)dflt); |
| 6052 | } |
| 6053 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6054 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6055 | d_del->dflts[0], dflt); |
| 6056 | goto cleanup; |
| 6057 | } |
| 6058 | if (i) { |
| 6059 | free((char*)dflt); |
| 6060 | } |
| 6061 | dflt = NULL; |
| 6062 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6063 | /* update the list of incomplete default values if needed */ |
| 6064 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6065 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6066 | /* remove the default specification */ |
| 6067 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6068 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6069 | free(leaf->dflt); |
| 6070 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6071 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6072 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6073 | break; |
| 6074 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6075 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6076 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6077 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6078 | dflt = llist->type->plugin->print(llist->dflts[y], LYD_XML, lys_get_prefix, llist->dflts_mods[y], &i); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6079 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6080 | if (i) { |
| 6081 | free((char*)dflt); |
| 6082 | } |
| 6083 | dflt = NULL; |
| 6084 | break; |
| 6085 | } |
| 6086 | if (i) { |
| 6087 | free((char*)dflt); |
| 6088 | } |
| 6089 | dflt = NULL; |
| 6090 | } |
| 6091 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6092 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6093 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6094 | goto cleanup; |
| 6095 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6096 | |
| 6097 | /* update the list of incomplete default values if needed */ |
| 6098 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6099 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6100 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6101 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6102 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6103 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6104 | free(llist->dflts[y]); |
| 6105 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts)); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6106 | memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_SIZE(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods)); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6107 | } |
| 6108 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6109 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6110 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6111 | LY_ARRAY_FREE(llist->dflts); |
| 6112 | llist->dflts = NULL; |
| 6113 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6114 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6115 | break; |
| 6116 | case LYS_CHOICE: |
| 6117 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6118 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6119 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6120 | LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6121 | if (prefix) { |
| 6122 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6123 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6124 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6125 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6126 | "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6127 | goto cleanup; |
| 6128 | } |
| 6129 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6130 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6131 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6132 | "The prefix does not match the default case's module.", d_del->dflts[0]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6133 | goto cleanup; |
| 6134 | } |
| 6135 | } |
| 6136 | /* else { |
| 6137 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6138 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6139 | * the module of the default case, just matching its name */ |
| 6140 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6141 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6142 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6143 | d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6144 | goto cleanup; |
| 6145 | } |
| 6146 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6147 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6148 | break; |
| 6149 | default: |
| 6150 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6151 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6152 | goto cleanup; |
| 6153 | } |
| 6154 | } |
| 6155 | |
| 6156 | break; |
| 6157 | case LYS_DEV_REPLACE: |
| 6158 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6159 | |
| 6160 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6161 | if (d_rpl->type) { |
| 6162 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6163 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6164 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6165 | |
| 6166 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6167 | /* the target has default from the previous type - remove it */ |
| 6168 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6169 | /* update the list of incomplete default values if needed */ |
| 6170 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6171 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6172 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6173 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6174 | free(leaf->dflt); |
| 6175 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6176 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6177 | } else { /* LYS_LEAFLIST */ |
| 6178 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6179 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6180 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6181 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6182 | free(llist->dflts[x]); |
| 6183 | } |
| 6184 | LY_ARRAY_FREE(llist->dflts); |
| 6185 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6186 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6187 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6188 | } |
| 6189 | } |
| 6190 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6191 | /* there is no default value, do not set changed_type after type compilation |
| 6192 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6193 | changed_type = -1; |
| 6194 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6195 | LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, (struct lysc_node_leaf*)devs[u]->target), cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6196 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6197 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6198 | |
| 6199 | /* [units-stmt] */ |
| 6200 | if (d_rpl->units) { |
| 6201 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6202 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6203 | units, "replacing", "units", d_rpl->units); |
| 6204 | |
| 6205 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6206 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6207 | } |
| 6208 | |
| 6209 | /* [default-stmt] */ |
| 6210 | if (d_rpl->dflt) { |
| 6211 | switch (devs[u]->target->nodetype) { |
| 6212 | case LYS_LEAF: |
| 6213 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6214 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6215 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6216 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6217 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6218 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6219 | dflt = d_rpl->dflt; |
| 6220 | /* parsing is done at the end after possible replace of the leaf's type */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6221 | break; |
| 6222 | case LYS_CHOICE: |
| 6223 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6224 | if (lys_compile_deviation_set_choice_dflt(ctx, d_rpl->dflt, (struct lysc_node_choice*)devs[u]->target)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6225 | goto cleanup; |
| 6226 | } |
| 6227 | break; |
| 6228 | default: |
| 6229 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6230 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6231 | goto cleanup; |
| 6232 | } |
| 6233 | } |
| 6234 | |
| 6235 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6236 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6237 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6238 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6239 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6240 | goto cleanup; |
| 6241 | } |
| 6242 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6244 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6245 | goto cleanup; |
| 6246 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6247 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6248 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6249 | |
| 6250 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6251 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6252 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6253 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6254 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6255 | goto cleanup; |
| 6256 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6257 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6258 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6259 | |
| 6260 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6261 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6262 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6263 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6264 | /* change value */ |
| 6265 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6266 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6267 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6268 | /* change value */ |
| 6269 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6270 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6271 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6272 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6273 | goto cleanup; |
| 6274 | } |
| 6275 | if (d_rpl->min) { |
| 6276 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6277 | } |
| 6278 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6279 | |
| 6280 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6281 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6282 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6283 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6284 | /* change value */ |
| 6285 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6286 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6287 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6288 | /* change value */ |
| 6289 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6290 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6291 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6292 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6293 | goto cleanup; |
| 6294 | } |
| 6295 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6296 | |
| 6297 | break; |
| 6298 | default: |
| 6299 | LOGINT(ctx->ctx); |
| 6300 | goto cleanup; |
| 6301 | } |
| 6302 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6303 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6304 | /* final check when all deviations of a single target node are applied */ |
| 6305 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6306 | /* check min-max compatibility */ |
| 6307 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6308 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6309 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6310 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6311 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6312 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6313 | } else { |
| 6314 | min = max = 0; |
| 6315 | } |
| 6316 | if (min > max) { |
| 6317 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6318 | "after deviation: min value %u is bigger than max value %u.", min, max); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6319 | goto cleanup; |
| 6320 | } |
| 6321 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6322 | if (dflt) { |
| 6323 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6324 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6325 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6326 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6327 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6328 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6329 | leaf->dflt->realtype->refcount++; |
| 6330 | if (err) { |
| 6331 | ly_err_print(err); |
| 6332 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6333 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6334 | ly_err_free(err); |
| 6335 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6336 | if (rc == LY_EINCOMPLETE) { |
| 6337 | /* postpone default compilation when the tree is complete */ |
| 6338 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6339 | |
| 6340 | /* but in general result is so far ok */ |
| 6341 | rc = LY_SUCCESS; |
| 6342 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6343 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6344 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6345 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6346 | int dynamic; |
| 6347 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6348 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6349 | |
| 6350 | /* update the list of incomplete default values if needed */ |
| 6351 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6352 | |
| 6353 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6354 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6355 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6356 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6357 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6358 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6359 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6360 | leaf->dflt->realtype->refcount++; |
| 6361 | if (err) { |
| 6362 | ly_err_print(err); |
| 6363 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6364 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6365 | ly_err_free(err); |
| 6366 | } |
| 6367 | if (dynamic) { |
| 6368 | free((void*)dflt); |
| 6369 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6370 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6371 | if (rc == LY_EINCOMPLETE) { |
| 6372 | /* postpone default compilation when the tree is complete */ |
| 6373 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6374 | |
| 6375 | /* but in general result is so far ok */ |
| 6376 | rc = LY_SUCCESS; |
| 6377 | } |
| 6378 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6379 | } else { /* LYS_LEAFLIST */ |
| 6380 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6381 | dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LYD_XML, lys_get_prefix, llist->dflts_mods[x], &dynamic); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6382 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6383 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6384 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6385 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6386 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6387 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6388 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6389 | llist->dflts[x]->realtype->refcount++; |
| 6390 | if (err) { |
| 6391 | ly_err_print(err); |
| 6392 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6393 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6394 | dflt, err->msg); |
| 6395 | ly_err_free(err); |
| 6396 | } |
| 6397 | if (dynamic) { |
| 6398 | free((void*)dflt); |
| 6399 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6400 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6401 | if (rc == LY_EINCOMPLETE) { |
| 6402 | /* postpone default compilation when the tree is complete */ |
| 6403 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6404 | |
| 6405 | /* but in general result is so far ok */ |
| 6406 | rc = LY_SUCCESS; |
| 6407 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6408 | LY_CHECK_GOTO(rc, cleanup); |
| 6409 | } |
| 6410 | } |
| 6411 | } |
| 6412 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6413 | /* check mandatory - default compatibility */ |
| 6414 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6415 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6416 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6417 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6418 | "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype)); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6419 | goto cleanup; |
| 6420 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6421 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6422 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6423 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6424 | goto cleanup; |
| 6425 | } |
| 6426 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6427 | /* mandatory node under a default case */ |
| 6428 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6429 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6430 | lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6431 | goto cleanup; |
| 6432 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6433 | |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 6434 | /* add this module into the target module deviated_by */ |
| 6435 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup); |
| 6436 | *dev_mod = mod_p->mod; |
| 6437 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6438 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6439 | } |
| 6440 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6441 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6442 | ret = LY_SUCCESS; |
| 6443 | |
| 6444 | cleanup: |
| 6445 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6446 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6447 | free(devs[u]); |
| 6448 | } |
| 6449 | free(devs); |
| 6450 | ly_set_erase(&targets, NULL); |
| 6451 | ly_set_erase(&devs_p, NULL); |
| 6452 | |
| 6453 | return ret; |
| 6454 | } |
| 6455 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6456 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6457 | * @brief Compile the given YANG submodule into the main module. |
| 6458 | * @param[in] ctx Compile context |
| 6459 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6460 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6461 | */ |
| 6462 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6463 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6464 | { |
| 6465 | unsigned int u; |
| 6466 | LY_ERR ret = LY_SUCCESS; |
| 6467 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6468 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6469 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6470 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6471 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6472 | if (!mainmod->mod->off_features) { |
| 6473 | /* features are compiled directly into the compiled module structure, |
| 6474 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6475 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6476 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6477 | LY_CHECK_GOTO(ret, error); |
| 6478 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6479 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6480 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6481 | COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6482 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6483 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6484 | /* data nodes */ |
| 6485 | LY_LIST_FOR(submod->data, node_p) { |
| 6486 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6487 | LY_CHECK_GOTO(ret, error); |
| 6488 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6489 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6490 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6491 | COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6492 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6493 | error: |
| 6494 | return ret; |
| 6495 | } |
| 6496 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6497 | static void * |
| 6498 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6499 | { |
| 6500 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6501 | if (substmts[u].stmt == stmt) { |
| 6502 | return substmts[u].storage; |
| 6503 | } |
| 6504 | } |
| 6505 | return NULL; |
| 6506 | } |
| 6507 | |
| 6508 | LY_ERR |
| 6509 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6510 | { |
| 6511 | LY_ERR ret = LY_EVALID, r; |
| 6512 | unsigned int u; |
| 6513 | struct lysp_stmt *stmt; |
| 6514 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6515 | |
| 6516 | /* check for invalid substatements */ |
| 6517 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6518 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6519 | continue; |
| 6520 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6521 | for (u = 0; substmts[u].stmt; ++u) { |
| 6522 | if (substmts[u].stmt == stmt->kw) { |
| 6523 | break; |
| 6524 | } |
| 6525 | } |
| 6526 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6527 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6528 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6529 | goto cleanup; |
| 6530 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6531 | } |
| 6532 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6533 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6534 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6535 | /* keep order of the processing the same as the order in the defined substmts, |
| 6536 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6537 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6538 | int stmt_present = 0; |
| 6539 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6540 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6541 | if (substmts[u].stmt != stmt->kw) { |
| 6542 | continue; |
| 6543 | } |
| 6544 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6545 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6546 | if (substmts[u].storage) { |
| 6547 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6548 | case LY_STMT_STATUS: |
| 6549 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6550 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6551 | break; |
| 6552 | case LY_STMT_UNITS: { |
| 6553 | const char **units; |
| 6554 | |
| 6555 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6556 | /* single item */ |
| 6557 | if (*((const char **)substmts[u].storage)) { |
| 6558 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6559 | goto cleanup; |
| 6560 | } |
| 6561 | units = (const char **)substmts[u].storage; |
| 6562 | } else { |
| 6563 | /* sized array */ |
| 6564 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6565 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6566 | } |
| 6567 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6568 | break; |
| 6569 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6570 | case LY_STMT_TYPE: { |
| 6571 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6572 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6573 | |
| 6574 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6575 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6576 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6577 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6578 | goto cleanup; |
| 6579 | } |
| 6580 | compiled = substmts[u].storage; |
| 6581 | } else { |
| 6582 | /* sized array */ |
| 6583 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6584 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6585 | compiled = (void*)type; |
| 6586 | } |
| 6587 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6588 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6589 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6590 | flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled, |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 6591 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6592 | lysp_type_free(ctx->ctx, parsed); |
| 6593 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6594 | break; |
| 6595 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6596 | case LY_STMT_IF_FEATURE: { |
| 6597 | struct lysc_iffeature *iff = NULL; |
| 6598 | |
| 6599 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6600 | /* single item */ |
| 6601 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6602 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6603 | goto cleanup; |
| 6604 | } |
| 6605 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6606 | } else { |
| 6607 | /* sized array */ |
| 6608 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6609 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6610 | } |
| 6611 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6612 | break; |
| 6613 | } |
| 6614 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6615 | * also note that in many statements their extensions are not taken into account */ |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6616 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6617 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.", |
| 6618 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6619 | goto cleanup; |
| 6620 | } |
| 6621 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6622 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6623 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6624 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6625 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6626 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6627 | goto cleanup; |
| 6628 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6629 | } |
| 6630 | |
| 6631 | ret = LY_SUCCESS; |
| 6632 | |
| 6633 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6634 | return ret; |
| 6635 | } |
| 6636 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6637 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6638 | * @brief Check when for cyclic dependencies. |
| 6639 | * @param[in] set Set with all the referenced nodes. |
| 6640 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6641 | * @return LY_ERR value |
| 6642 | */ |
| 6643 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6644 | lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node) |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6645 | { |
| 6646 | struct lyxp_set tmp_set; |
| 6647 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6648 | uint32_t i, j; |
| 6649 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6650 | int idx; |
| 6651 | struct lysc_when *when; |
| 6652 | LY_ERR ret = LY_SUCCESS; |
| 6653 | |
| 6654 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6655 | |
| 6656 | /* prepare in_ctx of the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6657 | for ( i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6658 | xp_scnode = &set->val.scnodes[i]; |
| 6659 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6660 | if (xp_scnode->in_ctx != -1) { |
| 6661 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6662 | xp_scnode->in_ctx = 1; |
| 6663 | } |
| 6664 | } |
| 6665 | |
| 6666 | for (i = 0; i < set->used; ++i) { |
| 6667 | xp_scnode = &set->val.scnodes[i]; |
| 6668 | if (xp_scnode->in_ctx != 1) { |
| 6669 | /* already checked */ |
| 6670 | continue; |
| 6671 | } |
| 6672 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6673 | if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
Michal Vasko | 2ff7efe | 2019-12-10 14:50:59 +0100 | [diff] [blame] | 6674 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6675 | /* no when to check */ |
| 6676 | xp_scnode->in_ctx = 0; |
| 6677 | continue; |
| 6678 | } |
| 6679 | |
| 6680 | node = xp_scnode->scnode; |
| 6681 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6682 | LY_ARRAY_FOR(node->when, u) { |
| 6683 | when = node->when[u]; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 6684 | ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6685 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6686 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6687 | LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6688 | goto cleanup; |
| 6689 | } |
| 6690 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6691 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6692 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6693 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6694 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6695 | idx = lyxp_set_scnode_dup_node_check(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6696 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6697 | LOGVAL(set->ctx, LY_VLOG_LYSC, node, LY_VCODE_CIRC_WHEN, node->name, set->val.scnodes[idx].scnode->name); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6698 | ret = LY_EVALID; |
| 6699 | goto cleanup; |
| 6700 | } |
| 6701 | |
| 6702 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6703 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6704 | } else { |
| 6705 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6706 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6707 | } |
| 6708 | } |
| 6709 | |
| 6710 | /* merge this set into the global when set */ |
| 6711 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6712 | } |
| 6713 | |
| 6714 | /* check when of non-data parents as well */ |
| 6715 | node = node->parent; |
| 6716 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6717 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6718 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6719 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6720 | } |
| 6721 | |
| 6722 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6723 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6724 | return ret; |
| 6725 | } |
| 6726 | |
| 6727 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6728 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6729 | * @param[in] ctx Compile context. |
| 6730 | * @param[in] node Node to check. |
| 6731 | * @return LY_ERR value |
| 6732 | */ |
| 6733 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6734 | lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node) |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6735 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6736 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6737 | uint32_t i; |
| 6738 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6739 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6740 | struct lysc_when **when = NULL; |
| 6741 | struct lysc_must *musts = NULL; |
| 6742 | LY_ERR ret = LY_SUCCESS; |
| 6743 | |
| 6744 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6745 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6746 | |
| 6747 | switch (node->nodetype) { |
| 6748 | case LYS_CONTAINER: |
| 6749 | when = ((struct lysc_node_container *)node)->when; |
| 6750 | musts = ((struct lysc_node_container *)node)->musts; |
| 6751 | break; |
| 6752 | case LYS_CHOICE: |
| 6753 | when = ((struct lysc_node_choice *)node)->when; |
| 6754 | break; |
| 6755 | case LYS_LEAF: |
| 6756 | when = ((struct lysc_node_leaf *)node)->when; |
| 6757 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6758 | break; |
| 6759 | case LYS_LEAFLIST: |
| 6760 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6761 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6762 | break; |
| 6763 | case LYS_LIST: |
| 6764 | when = ((struct lysc_node_list *)node)->when; |
| 6765 | musts = ((struct lysc_node_list *)node)->musts; |
| 6766 | break; |
| 6767 | case LYS_ANYXML: |
| 6768 | case LYS_ANYDATA: |
| 6769 | when = ((struct lysc_node_anydata *)node)->when; |
| 6770 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6771 | break; |
| 6772 | case LYS_CASE: |
| 6773 | when = ((struct lysc_node_case *)node)->when; |
| 6774 | break; |
| 6775 | case LYS_NOTIF: |
| 6776 | musts = ((struct lysc_notif *)node)->musts; |
| 6777 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6778 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6779 | case LYS_ACTION: |
| 6780 | /* first process input musts */ |
| 6781 | musts = ((struct lysc_action *)node)->input.musts; |
| 6782 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6783 | default: |
| 6784 | /* nothing to check */ |
| 6785 | break; |
| 6786 | } |
| 6787 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6788 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6789 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6790 | ret = lyxp_atomize(when[u]->cond, LYD_SCHEMA, when[u]->module, when[u]->context ? when[u]->context : node, |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6791 | when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6792 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6793 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6794 | goto cleanup; |
| 6795 | } |
| 6796 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6797 | ctx->path[0] = '\0'; |
| 6798 | lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6799 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6800 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6801 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6802 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6803 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6804 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6805 | ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6806 | schema->name); |
| 6807 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6808 | |
| 6809 | /* check dummy node accessing */ |
| 6810 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6811 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6812 | ret = LY_EVALID; |
| 6813 | goto cleanup; |
| 6814 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6815 | } |
| 6816 | } |
| 6817 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6818 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6819 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6820 | LY_CHECK_GOTO(ret, cleanup); |
| 6821 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6822 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6823 | } |
| 6824 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6825 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6826 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6827 | LY_ARRAY_FOR(musts, u) { |
| 6828 | ret = lyxp_atomize(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, &tmp_set, opts); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6829 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6830 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6831 | goto cleanup; |
| 6832 | } |
| 6833 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6834 | ctx->path[0] = '\0'; |
| 6835 | lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6836 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6837 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6838 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6839 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6840 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6841 | tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6842 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6843 | } |
| 6844 | } |
| 6845 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6846 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6847 | } |
| 6848 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6849 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6850 | /* now check output musts */ |
| 6851 | input_done = 1; |
| 6852 | musts = ((struct lysc_action *)node)->output.musts; |
| 6853 | opts = LYXP_SCNODE_OUTPUT; |
| 6854 | goto check_musts; |
| 6855 | } |
| 6856 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6857 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6858 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6859 | return ret; |
| 6860 | } |
| 6861 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6862 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6863 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 6864 | { |
| 6865 | const struct lysc_node *target = NULL; |
| 6866 | struct ly_path *p; |
| 6867 | struct lysc_type *type; |
| 6868 | |
| 6869 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6870 | |
| 6871 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6872 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 6873 | lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, |
| 6874 | lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6875 | |
| 6876 | /* get the target node */ |
| 6877 | target = p[LY_ARRAY_SIZE(p) - 1].node; |
| 6878 | ly_path_free(node->module->ctx, p); |
| 6879 | |
| 6880 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6881 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6882 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 6883 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 6884 | return LY_EVALID; |
| 6885 | } |
| 6886 | |
| 6887 | /* check status */ |
| 6888 | ctx->path[0] = '\0'; |
| 6889 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6890 | ctx->path_len = strlen(ctx->path); |
| 6891 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 6892 | return LY_EVALID; |
| 6893 | } |
| 6894 | ctx->path_len = 1; |
| 6895 | ctx->path[1] = '\0'; |
| 6896 | |
| 6897 | /* check config */ |
| 6898 | if (lref->require_instance && (node->flags & LYS_CONFIG_W)) { |
| 6899 | if (target->flags & LYS_CONFIG_R) { |
| 6900 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 6901 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 6902 | return LY_EVALID; |
| 6903 | } |
| 6904 | } |
| 6905 | |
| 6906 | /* store the target's type and check for circular chain of leafrefs */ |
| 6907 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 6908 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 6909 | if (type == (struct lysc_type *)lref) { |
| 6910 | /* circular chain detected */ |
| 6911 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6912 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 6913 | return LY_EVALID; |
| 6914 | } |
| 6915 | } |
| 6916 | |
| 6917 | /* check if leafref and its target are under common if-features */ |
| 6918 | if (lys_compile_leafref_features_validate(node, target)) { |
| 6919 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6920 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 6921 | " features applicable to the leafref itself.", lref->path->expr); |
| 6922 | return LY_EVALID; |
| 6923 | } |
| 6924 | |
| 6925 | return LY_SUCCESS; |
| 6926 | } |
| 6927 | |
| 6928 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6929 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 6930 | { |
| 6931 | struct lysc_ext_instance *ext; |
| 6932 | struct lysp_ext_instance *ext_p = NULL; |
| 6933 | struct lysp_stmt *stmt; |
| 6934 | const struct lys_module *ext_mod; |
| 6935 | LY_ERR ret = LY_SUCCESS; |
| 6936 | |
| 6937 | /* create the parsed extension instance manually */ |
| 6938 | ext_p = calloc(1, sizeof *ext_p); |
| 6939 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 6940 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 6941 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 6942 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 6943 | ext_p->insubstmt_index = 0; |
| 6944 | |
| 6945 | stmt = calloc(1, sizeof *ext_p->child); |
| 6946 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 6947 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 6948 | stmt->kw = LY_STMT_TYPE; |
| 6949 | ext_p->child = stmt; |
| 6950 | |
| 6951 | /* allocate new extension instance */ |
| 6952 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 6953 | |
| 6954 | /* manually get extension definition module */ |
| 6955 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 6956 | |
| 6957 | /* compile the extension instance */ |
| 6958 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 6959 | |
| 6960 | cleanup: |
| 6961 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 6962 | free(ext_p); |
| 6963 | return ret; |
| 6964 | } |
| 6965 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6966 | static LY_ERR |
| 6967 | lys_compile_unres(struct lysc_ctx *ctx) |
| 6968 | { |
| 6969 | struct lysc_node *node; |
| 6970 | struct lysc_type *type, *typeiter; |
| 6971 | struct lysc_type_leafref *lref; |
| 6972 | LY_ARRAY_SIZE_TYPE v; |
| 6973 | uint32_t i; |
| 6974 | |
| 6975 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 6976 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 6977 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 6978 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 6979 | node = ctx->leafrefs.objs[i]; |
| 6980 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6981 | type = ((struct lysc_node_leaf *)node)->type; |
| 6982 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6983 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 6984 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6985 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 6986 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6987 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 6988 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 6989 | } |
| 6990 | } |
| 6991 | } |
| 6992 | } |
| 6993 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 6994 | /* store pointer to the real type */ |
| 6995 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 6996 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6997 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 6998 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6999 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7000 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
| 7001 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7002 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7003 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7004 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7005 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7006 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7007 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7008 | } |
| 7009 | } |
| 7010 | } |
| 7011 | } |
| 7012 | |
| 7013 | /* check xpath */ |
| 7014 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7015 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7016 | } |
| 7017 | |
| 7018 | /* finish incomplete default values compilation */ |
| 7019 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 7020 | struct ly_err_item *err = NULL; |
| 7021 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
| 7022 | LY_ERR ret; |
| 7023 | ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original), |
| 7024 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7025 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7026 | if (err) { |
| 7027 | ly_err_print(err); |
| 7028 | ctx->path[0] = '\0'; |
| 7029 | lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7030 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7031 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7032 | ly_err_free(err); |
| 7033 | } |
| 7034 | LY_CHECK_RET(ret); |
| 7035 | } |
| 7036 | |
| 7037 | return LY_SUCCESS; |
| 7038 | } |
| 7039 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7040 | LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7041 | lys_compile(struct lys_module **mod, int options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7042 | { |
| 7043 | struct lysc_ctx ctx = {0}; |
| 7044 | struct lysc_module *mod_c; |
| 7045 | struct lysp_module *sp; |
| 7046 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7047 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7048 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7049 | struct lys_module *m; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7050 | LY_ARRAY_SIZE_TYPE u, v; |
| 7051 | uint32_t i; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7052 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7053 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7054 | LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7055 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7056 | if (!(*mod)->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7057 | /* just imported modules are not compiled */ |
| 7058 | return LY_SUCCESS; |
| 7059 | } |
| 7060 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7061 | sp = (*mod)->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7062 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7063 | ctx.ctx = (*mod)->ctx; |
| 7064 | ctx.mod = *mod; |
| 7065 | ctx.mod_def = *mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7066 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7067 | ctx.path_len = 1; |
| 7068 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7069 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7070 | (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7071 | LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM); |
| 7072 | mod_c->mod = *mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7073 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7074 | COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7075 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7076 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7077 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7078 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7079 | |
| 7080 | /* features */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7081 | if ((*mod)->off_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7082 | /* there is already precompiled array of features */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7083 | mod_c->features = (*mod)->off_features; |
| 7084 | (*mod)->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7085 | } else { |
| 7086 | /* features are compiled directly into the compiled module structure, |
| 7087 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7088 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7089 | LY_CHECK_GOTO(ret, error); |
| 7090 | } |
| 7091 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7092 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7093 | * are present. */ |
| 7094 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7095 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7096 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7097 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7098 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7099 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7100 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7101 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7102 | ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7103 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7104 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7105 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7106 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7107 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7108 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7109 | /* identities */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7110 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7111 | COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7112 | if (sp->identities) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7113 | LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7114 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7115 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7116 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7117 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7118 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7119 | LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7120 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7121 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7122 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7123 | COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7124 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7125 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7126 | LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7127 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7128 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7129 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7130 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7131 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7132 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7133 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7134 | /* extension instances TODO cover extension instances from submodules */ |
| 7135 | COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7136 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7137 | /* finish compilation for all unresolved items in the context */ |
| 7138 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7139 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7140 | /* validate non-instantiated groupings from the parsed schema, |
| 7141 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7142 | ctx.options |= LYSC_OPT_GROUPING; |
| 7143 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7144 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7145 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7146 | } |
| 7147 | } |
| 7148 | LY_LIST_FOR(sp->data, node_p) { |
| 7149 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7150 | LY_ARRAY_FOR(grps, u) { |
| 7151 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7152 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7153 | } |
| 7154 | } |
| 7155 | } |
| 7156 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7157 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7158 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7159 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7160 | } |
| 7161 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7162 | #if 0 |
| 7163 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7164 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7165 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7166 | * the anotation definitions available in the internal schema structure. */ |
| 7167 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7168 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7169 | lys_free(mod, NULL, 1, 1); |
| 7170 | return NULL; |
| 7171 | } |
| 7172 | } |
| 7173 | #endif |
| 7174 | |
| 7175 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7176 | if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) { |
| 7177 | LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7178 | } |
| 7179 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7180 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7181 | ly_set_erase(&ctx.xpath, NULL); |
| 7182 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7183 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7184 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7185 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7186 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7187 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7188 | lysp_module_free((*mod)->parsed); |
| 7189 | (*mod)->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7190 | } |
| 7191 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7192 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7193 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7194 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7195 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7196 | if (m->implemented == 2) { |
| 7197 | m->implemented = 1; |
| 7198 | } |
| 7199 | } |
| 7200 | } |
| 7201 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7202 | (*mod)->compiled = mod_c; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7203 | return LY_SUCCESS; |
| 7204 | |
| 7205 | error: |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7206 | lys_feature_precompile_revert(&ctx, *mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7207 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7208 | ly_set_erase(&ctx.xpath, NULL); |
| 7209 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7210 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7211 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7212 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7213 | lysc_module_free(mod_c, NULL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7214 | (*mod)->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7215 | |
| 7216 | /* revert compilation of modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7217 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7218 | m = ctx.ctx->list.objs[i]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7219 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7220 | /* revert features list to the precompiled state */ |
| 7221 | lys_feature_precompile_revert(&ctx, m); |
| 7222 | /* mark module as imported-only / not-implemented */ |
| 7223 | m->implemented = 0; |
| 7224 | /* free the compiled version of the module */ |
| 7225 | lysc_module_free(m->compiled, NULL); |
| 7226 | m->compiled = NULL; |
| 7227 | } |
| 7228 | } |
| 7229 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7230 | /* remove the module itself from the context and free it */ |
| 7231 | ly_set_rm(&ctx.ctx->list, *mod, NULL); |
| 7232 | lys_module_free(*mod, NULL); |
| 7233 | *mod = NULL; |
| 7234 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7235 | return ret; |
| 7236 | } |