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) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 56 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 57 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 58 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 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) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 67 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 68 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 69 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 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) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 78 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \ |
| 79 | for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(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) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 88 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 89 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 90 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 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); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 108 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(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) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 116 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(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) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 126 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(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; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 281 | LY_ARRAY_COUNT_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 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 285 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 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) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 312 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup); |
| 313 | LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts); |
| 314 | memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 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; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 424 | LY_ARRAY_COUNT_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 { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 443 | flist = mod->dis_features; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 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; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 505 | LY_ARRAY_COUNT_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; |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 530 | LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]) == -1, |
| 531 | ret = LY_EMEM, cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 532 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
| 533 | u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */ |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | if (!prefixed_name) { |
| 539 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 540 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 541 | goto cleanup; |
| 542 | } |
| 543 | } else { |
| 544 | prefixed_name = ext_p->name; |
| 545 | } |
| 546 | lysc_update_path(ctx, NULL, prefixed_name); |
| 547 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 548 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 549 | 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] | 550 | if (!ext_mod) { |
| 551 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 552 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 553 | goto cleanup; |
| 554 | } else if (!ext_mod->parsed->extensions) { |
| 555 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 556 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 557 | prefixed_name, ext_mod->name); |
| 558 | goto cleanup; |
| 559 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 560 | } |
| 561 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 562 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 563 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 564 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 565 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 566 | /* compile extension definition and assign it */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 567 | 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] | 568 | break; |
| 569 | } |
| 570 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 571 | if (!ext->def) { |
| 572 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 573 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 574 | goto cleanup; |
| 575 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 576 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 577 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 578 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 579 | * 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] | 580 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 581 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 582 | struct lysp_stmt *stmt = NULL; |
| 583 | |
| 584 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 585 | /* ... argument was the first XML child element */ |
| 586 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 587 | /* TODO check namespace of the statement */ |
| 588 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 589 | stmt = ext_p->child; |
| 590 | } |
| 591 | } |
| 592 | } else { |
| 593 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 594 | * with LYS_YIN_ATTR flag */ |
| 595 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 596 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 597 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 598 | break; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | if (!stmt) { |
| 603 | /* missing extension's argument */ |
| 604 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 605 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 606 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 607 | |
| 608 | } |
| 609 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 610 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 611 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 612 | if (prefixed_name != ext_p->name) { |
| 613 | lydict_remove(ctx->ctx, ext_p->name); |
| 614 | ext_p->name = prefixed_name; |
| 615 | if (!ext_p->argument && ext->argument) { |
| 616 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 617 | } |
| 618 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 619 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 620 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 621 | if (ext->argument) { |
| 622 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 623 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 624 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 625 | if (ext->argument) { |
| 626 | lysc_update_path(ctx, NULL, NULL); |
| 627 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 628 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 629 | ext_p->compiled = ext; |
| 630 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 631 | ret = LY_SUCCESS; |
| 632 | cleanup: |
| 633 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 634 | lydict_remove(ctx->ctx, prefixed_name); |
| 635 | } |
| 636 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 637 | lysc_update_path(ctx, NULL, NULL); |
| 638 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 639 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 640 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 644 | * @brief Compile information from the if-feature statement |
| 645 | * @param[in] ctx Compile context. |
| 646 | * @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] | 647 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 648 | * @return LY_ERR value. |
| 649 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 650 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 651 | 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] | 652 | { |
| 653 | const char *c = *value; |
| 654 | int r, rc = EXIT_FAILURE; |
| 655 | int i, j, last_not, checkversion = 0; |
| 656 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 657 | uint8_t op; |
| 658 | struct iff_stack stack = {0, 0, NULL}; |
| 659 | struct lysc_feature *f; |
| 660 | |
| 661 | assert(c); |
| 662 | |
| 663 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 664 | for (i = j = last_not = 0; c[i]; i++) { |
| 665 | if (c[i] == '(') { |
| 666 | j++; |
| 667 | checkversion = 1; |
| 668 | continue; |
| 669 | } else if (c[i] == ')') { |
| 670 | j--; |
| 671 | continue; |
| 672 | } else if (isspace(c[i])) { |
| 673 | checkversion = 1; |
| 674 | continue; |
| 675 | } |
| 676 | |
| 677 | 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] | 678 | int sp; |
| 679 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 680 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 682 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 683 | return LY_EVALID; |
| 684 | } else if (!isspace(c[i + r])) { |
| 685 | /* feature name starting with the not/and/or */ |
| 686 | last_not = 0; |
| 687 | f_size++; |
| 688 | } else if (c[i] == 'n') { /* not operation */ |
| 689 | if (last_not) { |
| 690 | /* double not */ |
| 691 | expr_size = expr_size - 2; |
| 692 | last_not = 0; |
| 693 | } else { |
| 694 | last_not = 1; |
| 695 | } |
| 696 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 697 | if (f_exp != f_size) { |
| 698 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 699 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 700 | return LY_EVALID; |
| 701 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 702 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 703 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 704 | /* not a not operation */ |
| 705 | last_not = 0; |
| 706 | } |
| 707 | i += r; |
| 708 | } else { |
| 709 | f_size++; |
| 710 | last_not = 0; |
| 711 | } |
| 712 | expr_size++; |
| 713 | |
| 714 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 715 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 716 | i--; |
| 717 | break; |
| 718 | } |
| 719 | i++; |
| 720 | } |
| 721 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 722 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 723 | /* not matching count of ( and ) */ |
| 724 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 725 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 726 | return LY_EVALID; |
| 727 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 728 | if (f_exp != f_size) { |
| 729 | /* features do not match the needed arguments for the logical operations */ |
| 730 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 731 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 732 | "the required number of operands for the operations.", *value); |
| 733 | return LY_EVALID; |
| 734 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 735 | |
| 736 | if (checkversion || expr_size > 1) { |
| 737 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 738 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 739 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 740 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 741 | return LY_EVALID; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /* allocate the memory */ |
| 746 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 747 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 748 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 749 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 750 | |
| 751 | stack.size = expr_size; |
| 752 | f_size--; expr_size--; /* used as indexes from now */ |
| 753 | |
| 754 | for (i--; i >= 0; i--) { |
| 755 | if (c[i] == ')') { |
| 756 | /* push it on stack */ |
| 757 | iff_stack_push(&stack, LYS_IFF_RP); |
| 758 | continue; |
| 759 | } else if (c[i] == '(') { |
| 760 | /* pop from the stack into result all operators until ) */ |
| 761 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 762 | iff_setop(iff->expr, op, expr_size--); |
| 763 | } |
| 764 | continue; |
| 765 | } else if (isspace(c[i])) { |
| 766 | continue; |
| 767 | } |
| 768 | |
| 769 | /* end of operator or operand -> find beginning and get what is it */ |
| 770 | j = i + 1; |
| 771 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 772 | i--; |
| 773 | } |
| 774 | i++; /* go back by one step */ |
| 775 | |
| 776 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 777 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 778 | /* double not */ |
| 779 | iff_stack_pop(&stack); |
| 780 | } else { |
| 781 | /* not has the highest priority, so do not pop from the stack |
| 782 | * as in case of AND and OR */ |
| 783 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 784 | } |
| 785 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 786 | /* as for OR - pop from the stack all operators with the same or higher |
| 787 | * priority and store them to the result, then push the AND to the stack */ |
| 788 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 789 | op = iff_stack_pop(&stack); |
| 790 | iff_setop(iff->expr, op, expr_size--); |
| 791 | } |
| 792 | iff_stack_push(&stack, LYS_IFF_AND); |
| 793 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 794 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 795 | op = iff_stack_pop(&stack); |
| 796 | iff_setop(iff->expr, op, expr_size--); |
| 797 | } |
| 798 | iff_stack_push(&stack, LYS_IFF_OR); |
| 799 | } else { |
| 800 | /* feature name, length is j - i */ |
| 801 | |
| 802 | /* add it to the expression */ |
| 803 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 804 | |
| 805 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 806 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 807 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 808 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 809 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 810 | iff->features[f_size] = f; |
| 811 | LY_ARRAY_INCREMENT(iff->features); |
| 812 | f_size--; |
| 813 | } |
| 814 | } |
| 815 | while (stack.index) { |
| 816 | op = iff_stack_pop(&stack); |
| 817 | iff_setop(iff->expr, op, expr_size--); |
| 818 | } |
| 819 | |
| 820 | if (++expr_size || ++f_size) { |
| 821 | /* not all expected operators and operands found */ |
| 822 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 823 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 824 | rc = LY_EINT; |
| 825 | } else { |
| 826 | rc = LY_SUCCESS; |
| 827 | } |
| 828 | |
| 829 | error: |
| 830 | /* cleanup */ |
| 831 | iff_stack_clean(&stack); |
| 832 | |
| 833 | return rc; |
| 834 | } |
| 835 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 836 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 837 | * @brief Get the XPath context node for the given schema node. |
| 838 | * @param[in] start The schema node where the XPath expression appears. |
| 839 | * @return The context node to evaluate XPath expression in given schema node. |
| 840 | * @return NULL in case the context node is the root node. |
| 841 | */ |
| 842 | static struct lysc_node * |
| 843 | lysc_xpath_context(struct lysc_node *start) |
| 844 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 845 | 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] | 846 | start = start->parent); |
| 847 | return start; |
| 848 | } |
| 849 | |
| 850 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 851 | * @brief Compile information from the when statement |
| 852 | * @param[in] ctx Compile context. |
| 853 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 854 | * @param[in] flags Flags of the node with the "when" defiition. |
| 855 | * @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] | 856 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 857 | * @return LY_ERR value. |
| 858 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 859 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 860 | 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] | 861 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 862 | LY_ERR ret = LY_SUCCESS; |
| 863 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 864 | *when = calloc(1, sizeof **when); |
| 865 | (*when)->refcount = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 866 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 867 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 868 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 869 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 870 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 871 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 872 | 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] | 873 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 874 | |
| 875 | done: |
| 876 | return ret; |
| 877 | } |
| 878 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 879 | /** |
| 880 | * @brief Compile information from the must statement |
| 881 | * @param[in] ctx Compile context. |
| 882 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 883 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 884 | * @return LY_ERR value. |
| 885 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 886 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 887 | 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] | 888 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 889 | LY_ERR ret = LY_SUCCESS; |
| 890 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 891 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 892 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 893 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 894 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 895 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 896 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 897 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 898 | 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] | 899 | |
| 900 | done: |
| 901 | return ret; |
| 902 | } |
| 903 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 904 | /** |
| 905 | * @brief Compile information from the import statement |
| 906 | * @param[in] ctx Compile context. |
| 907 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 908 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 909 | * @return LY_ERR value. |
| 910 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 911 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 912 | lys_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] | 913 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 914 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 915 | LY_ERR ret = LY_SUCCESS; |
| 916 | |
| 917 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 918 | 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] | 919 | imp->module = imp_p->module; |
| 920 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 921 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 922 | * 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] | 923 | * 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] | 924 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 925 | /* try to use filepath if present */ |
| 926 | if (imp->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 927 | struct ly_in *in; |
| 928 | if (ly_in_new_filepath(imp->module->filepath, 0, &in)) { |
| 929 | LOGINT(ctx->ctx); |
| 930 | } else { |
| 931 | mod = lys_parse(ctx->ctx, in, !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG); |
| 932 | if (mod != imp->module) { |
| 933 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", imp->module->filepath, imp->module->name); |
| 934 | mod = NULL; |
| 935 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 936 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 937 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 938 | } |
| 939 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 940 | 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] | 941 | 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] | 942 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 943 | return LY_ENOTFOUND; |
| 944 | } |
| 945 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | done: |
| 949 | return ret; |
| 950 | } |
| 951 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 952 | LY_ERR |
| 953 | lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 954 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 955 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 956 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 957 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 958 | LY_ERR ret = LY_SUCCESS; |
| 959 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 960 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 961 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 962 | if (!ctx_sc) { |
| 963 | context.ctx = ctx; |
| 964 | context.mod = module; |
| 965 | context.path_len = 1; |
| 966 | context.path[0] = '/'; |
| 967 | ctx_sc = &context; |
| 968 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 969 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 970 | if (!identities_p) { |
| 971 | return LY_SUCCESS; |
| 972 | } |
| 973 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 974 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 978 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 979 | LY_ARRAY_FOR(identities_p, u) { |
| 980 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 981 | |
| 982 | LY_ARRAY_INCREMENT(*identities); |
| 983 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name); |
| 984 | DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name); |
| 985 | DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc); |
| 986 | DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref); |
| 987 | (*identities)[offset + u].module = ctx_sc->mod; |
| 988 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
| 989 | lys_compile_iffeature, ret, done); |
| 990 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 991 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
| 992 | LYEXT_PAR_IDENT, ret, done); |
| 993 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 994 | |
| 995 | lysc_update_path(ctx_sc, NULL, NULL); |
| 996 | } |
| 997 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 998 | done: |
| 999 | return ret; |
| 1000 | } |
| 1001 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1002 | /** |
| 1003 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1004 | * |
| 1005 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1006 | * |
| 1007 | * @param[in] ctx Compile context for logging. |
| 1008 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1009 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1010 | * @return LY_SUCCESS if everything is ok. |
| 1011 | * @return LY_EVALID if the identity is derived from itself. |
| 1012 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1013 | static LY_ERR |
| 1014 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1015 | { |
| 1016 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1017 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1018 | struct ly_set recursion = {0}; |
| 1019 | struct lysc_ident *drv; |
| 1020 | |
| 1021 | if (!derived) { |
| 1022 | return LY_SUCCESS; |
| 1023 | } |
| 1024 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1025 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1026 | if (ident == derived[u]) { |
| 1027 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1028 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1029 | goto cleanup; |
| 1030 | } |
| 1031 | ly_set_add(&recursion, derived[u], 0); |
| 1032 | } |
| 1033 | |
| 1034 | for (v = 0; v < recursion.count; ++v) { |
| 1035 | drv = recursion.objs[v]; |
| 1036 | if (!drv->derived) { |
| 1037 | continue; |
| 1038 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1039 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1040 | if (ident == drv->derived[u]) { |
| 1041 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1042 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1043 | goto cleanup; |
| 1044 | } |
| 1045 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1046 | } |
| 1047 | } |
| 1048 | ret = LY_SUCCESS; |
| 1049 | |
| 1050 | cleanup: |
| 1051 | ly_set_erase(&recursion, NULL); |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1055 | /** |
| 1056 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1057 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1058 | * 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] | 1059 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1060 | * to distinguish these two use cases. |
| 1061 | * |
| 1062 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1063 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1064 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1065 | * @param[in] bases Array of bases of identityref to fill in. |
| 1066 | * @return LY_ERR value. |
| 1067 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1068 | static LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1069 | lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, |
| 1070 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1071 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1072 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1073 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1074 | struct lys_module *mod; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1075 | struct lysc_ident **idref, *identities; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1076 | |
| 1077 | assert(ident || bases); |
| 1078 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1079 | if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1080 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1081 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1082 | return LY_EVALID; |
| 1083 | } |
| 1084 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1085 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1086 | s = strchr(bases_p[u], ':'); |
| 1087 | if (s) { |
| 1088 | /* prefixed identity */ |
| 1089 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1090 | 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] | 1091 | } else { |
| 1092 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1093 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1094 | } |
| 1095 | if (!mod) { |
| 1096 | if (ident) { |
| 1097 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1098 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1099 | } else { |
| 1100 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1101 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1102 | } |
| 1103 | return LY_EVALID; |
| 1104 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1105 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1106 | idref = NULL; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1107 | if (mod->compiled) { |
| 1108 | identities = mod->compiled->identities; |
| 1109 | } else { |
| 1110 | identities = mod->dis_identities; |
| 1111 | } |
| 1112 | LY_ARRAY_FOR(identities, v) { |
| 1113 | if (!strcmp(name, identities[v].name)) { |
| 1114 | if (ident) { |
| 1115 | if (ident == &identities[v]) { |
| 1116 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1117 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1118 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1119 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1120 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived)); |
| 1121 | /* we have match! store the backlink */ |
| 1122 | LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM); |
| 1123 | *idref = ident; |
| 1124 | } else { |
| 1125 | /* we have match! store the found identity */ |
| 1126 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 1127 | *idref = &identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1128 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1129 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | if (!idref || !(*idref)) { |
| 1133 | if (ident) { |
| 1134 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1135 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1136 | } else { |
| 1137 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1138 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1139 | } |
| 1140 | return LY_EVALID; |
| 1141 | } |
| 1142 | } |
| 1143 | return LY_SUCCESS; |
| 1144 | } |
| 1145 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1146 | /** |
| 1147 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1148 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1149 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1150 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1151 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1152 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1153 | static LY_ERR |
| 1154 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1155 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1156 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1157 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1158 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1159 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1160 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1161 | continue; |
| 1162 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1163 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1164 | 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] | 1165 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1166 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1167 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1168 | return LY_SUCCESS; |
| 1169 | } |
| 1170 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1171 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1172 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 1173 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1174 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1175 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1176 | struct lysc_ctx context = {0}; |
| 1177 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1178 | assert(ctx_sc || ctx); |
| 1179 | |
| 1180 | if (!ctx_sc) { |
| 1181 | context.ctx = ctx; |
| 1182 | context.mod = module; |
| 1183 | context.path_len = 1; |
| 1184 | context.path[0] = '/'; |
| 1185 | ctx_sc = &context; |
| 1186 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1187 | |
| 1188 | if (!features_p) { |
| 1189 | return LY_SUCCESS; |
| 1190 | } |
| 1191 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1192 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1193 | } |
| 1194 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1195 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1196 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1197 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1198 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1199 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1200 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1201 | 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] | 1202 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1203 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1204 | 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] | 1205 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1206 | (*features)[offset + u].module = ctx_sc->mod; |
| 1207 | |
| 1208 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1209 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1210 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1211 | |
| 1212 | return LY_SUCCESS; |
| 1213 | } |
| 1214 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1215 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1216 | * @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] | 1217 | * |
| 1218 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1219 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1220 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1221 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1222 | * being currently processed). |
| 1223 | * @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] | 1224 | * @return LY_SUCCESS if everything is ok. |
| 1225 | * @return LY_EVALID if the feature references indirectly itself. |
| 1226 | */ |
| 1227 | static LY_ERR |
| 1228 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1229 | { |
| 1230 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1231 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1232 | struct ly_set recursion = {0}; |
| 1233 | struct lysc_feature *drv; |
| 1234 | |
| 1235 | if (!depfeatures) { |
| 1236 | return LY_SUCCESS; |
| 1237 | } |
| 1238 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1239 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1240 | if (feature == depfeatures[u]) { |
| 1241 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1242 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1243 | goto cleanup; |
| 1244 | } |
| 1245 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1246 | } |
| 1247 | |
| 1248 | for (v = 0; v < recursion.count; ++v) { |
| 1249 | drv = recursion.objs[v]; |
| 1250 | if (!drv->depfeatures) { |
| 1251 | continue; |
| 1252 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1253 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1254 | if (feature == drv->depfeatures[u]) { |
| 1255 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1256 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1257 | goto cleanup; |
| 1258 | } |
| 1259 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1260 | } |
| 1261 | } |
| 1262 | ret = LY_SUCCESS; |
| 1263 | |
| 1264 | cleanup: |
| 1265 | ly_set_erase(&recursion, NULL); |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
| 1269 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1270 | * @brief Create pre-compiled features array. |
| 1271 | * |
| 1272 | * See lys_feature_precompile() for more details. |
| 1273 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1274 | * @param[in] ctx Compile context. |
| 1275 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1276 | * @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] | 1277 | * @return LY_ERR value. |
| 1278 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1279 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1280 | 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] | 1281 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1282 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1283 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1284 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1285 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1286 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1287 | /* find the preprecompiled feature */ |
| 1288 | LY_ARRAY_FOR(features, x) { |
| 1289 | if (strcmp(features[x].name, feature_p->name)) { |
| 1290 | continue; |
| 1291 | } |
| 1292 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1293 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1294 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1295 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1296 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1297 | 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] | 1298 | 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] | 1299 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1300 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1301 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1302 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1303 | /* check for circular dependency - direct reference first,... */ |
| 1304 | if (feature == feature->iffeatures[u].features[v]) { |
| 1305 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1306 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1307 | return LY_EVALID; |
| 1308 | } |
| 1309 | /* ... and indirect circular reference */ |
| 1310 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1311 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1312 | /* add itself into the dependants list */ |
| 1313 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1314 | *df = feature; |
| 1315 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1316 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1317 | } |
| 1318 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1319 | lysc_update_path(ctx, NULL, NULL); |
| 1320 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1321 | done: |
| 1322 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1323 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1324 | |
| 1325 | LOGINT(ctx->ctx); |
| 1326 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1327 | } |
| 1328 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1329 | /** |
| 1330 | * @brief Revert compiled list of features back to the precompiled state. |
| 1331 | * |
| 1332 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1333 | * The features are supposed to be stored again as dis_features in ::lys_module structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1334 | * |
| 1335 | * @param[in] ctx Compilation context. |
| 1336 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1337 | * and supposed to hold the dis_features list. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1338 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1339 | static void |
| 1340 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1341 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1342 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1343 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1344 | /* keep the dis_features list until the complete lys_module is freed */ |
| 1345 | mod->dis_features = mod->compiled->features; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1346 | mod->compiled->features = NULL; |
| 1347 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1348 | /* in the dis_features list, remove all the parts (from finished compiling process) |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1349 | * which may points into the data being freed here */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1350 | LY_ARRAY_FOR(mod->dis_features, u) { |
| 1351 | LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) { |
| 1352 | lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1353 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1354 | LY_ARRAY_FREE(mod->dis_features[u].iffeatures); |
| 1355 | mod->dis_features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1356 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1357 | LY_ARRAY_FOR(mod->dis_features[u].exts, v) { |
| 1358 | lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1359 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1360 | LY_ARRAY_FREE(mod->dis_features[u].exts); |
| 1361 | mod->dis_features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1362 | } |
| 1363 | } |
| 1364 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1365 | /** |
| 1366 | * @brief Validate and normalize numeric value from a range definition. |
| 1367 | * @param[in] ctx Compile context. |
| 1368 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1369 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1370 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1371 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1372 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1373 | * @param[in] value String value of the range boundary. |
| 1374 | * @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. |
| 1375 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1376 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1377 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1378 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1379 | 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] | 1380 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1381 | size_t fraction = 0, size; |
| 1382 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1383 | *len = 0; |
| 1384 | |
| 1385 | assert(value); |
| 1386 | /* parse value */ |
| 1387 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1388 | return LY_EVALID; |
| 1389 | } |
| 1390 | |
| 1391 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1392 | ++(*len); |
| 1393 | } |
| 1394 | |
| 1395 | while (isdigit(value[*len])) { |
| 1396 | ++(*len); |
| 1397 | } |
| 1398 | |
| 1399 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1400 | if (basetype == LY_TYPE_DEC64) { |
| 1401 | goto decimal; |
| 1402 | } else { |
| 1403 | *valcopy = strndup(value, *len); |
| 1404 | return LY_SUCCESS; |
| 1405 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1406 | } |
| 1407 | fraction = *len; |
| 1408 | |
| 1409 | ++(*len); |
| 1410 | while (isdigit(value[*len])) { |
| 1411 | ++(*len); |
| 1412 | } |
| 1413 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1414 | if (basetype == LY_TYPE_DEC64) { |
| 1415 | decimal: |
| 1416 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1417 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1418 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1419 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1420 | *len, value, frdigits); |
| 1421 | return LY_EINVAL; |
| 1422 | } |
| 1423 | if (fraction) { |
| 1424 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1425 | } else { |
| 1426 | size = (*len) + frdigits + 1; |
| 1427 | } |
| 1428 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1429 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1430 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1431 | (*valcopy)[size - 1] = '\0'; |
| 1432 | if (fraction) { |
| 1433 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1434 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1435 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1436 | } else { |
| 1437 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1438 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1439 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1440 | } |
| 1441 | return LY_SUCCESS; |
| 1442 | } |
| 1443 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1444 | /** |
| 1445 | * @brief Check that values in range are in ascendant order. |
| 1446 | * @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] | 1447 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1448 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1449 | * @param[in] value Current value to check. |
| 1450 | * @param[in] prev_value The last seen value. |
| 1451 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1452 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1453 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1454 | 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] | 1455 | { |
| 1456 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1457 | 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] | 1458 | return LY_EEXIST; |
| 1459 | } |
| 1460 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1461 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1462 | return LY_EEXIST; |
| 1463 | } |
| 1464 | } |
| 1465 | return LY_SUCCESS; |
| 1466 | } |
| 1467 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1468 | /** |
| 1469 | * @brief Set min/max value of the range part. |
| 1470 | * @param[in] ctx Compile context. |
| 1471 | * @param[in] part Range part structure to fill. |
| 1472 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1473 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1474 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1475 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1476 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1477 | * @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] | 1478 | * @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] | 1479 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1480 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1481 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1482 | * frdigits value), LY_EMEM. |
| 1483 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1484 | static LY_ERR |
| 1485 | 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] | 1486 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1487 | { |
| 1488 | LY_ERR ret = LY_SUCCESS; |
| 1489 | char *valcopy = NULL; |
| 1490 | size_t len; |
| 1491 | |
| 1492 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1493 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1494 | LY_CHECK_GOTO(ret, finalize); |
| 1495 | } |
| 1496 | if (!valcopy && base_range) { |
| 1497 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1498 | part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1499 | } else { |
| 1500 | part->min_64 = base_range->parts[0].min_64; |
| 1501 | } |
| 1502 | if (!first) { |
| 1503 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1504 | } |
| 1505 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1509 | case LY_TYPE_INT8: /* range */ |
| 1510 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1511 | 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] | 1512 | } else if (max) { |
| 1513 | part->max_64 = INT64_C(127); |
| 1514 | } else { |
| 1515 | part->min_64 = INT64_C(-128); |
| 1516 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1517 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1518 | 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] | 1519 | } |
| 1520 | break; |
| 1521 | case LY_TYPE_INT16: /* range */ |
| 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(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1524 | } else if (max) { |
| 1525 | part->max_64 = INT64_C(32767); |
| 1526 | } else { |
| 1527 | part->min_64 = INT64_C(-32768); |
| 1528 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1529 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1530 | 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] | 1531 | } |
| 1532 | break; |
| 1533 | case LY_TYPE_INT32: /* range */ |
| 1534 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1535 | 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] | 1536 | } else if (max) { |
| 1537 | part->max_64 = INT64_C(2147483647); |
| 1538 | } else { |
| 1539 | part->min_64 = INT64_C(-2147483648); |
| 1540 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1541 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1542 | 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] | 1543 | } |
| 1544 | break; |
| 1545 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1546 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1547 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1548 | 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] | 1549 | max ? &part->max_64 : &part->min_64); |
| 1550 | } else if (max) { |
| 1551 | part->max_64 = INT64_C(9223372036854775807); |
| 1552 | } else { |
| 1553 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1554 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1555 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1556 | 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] | 1557 | } |
| 1558 | break; |
| 1559 | case LY_TYPE_UINT8: /* range */ |
| 1560 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1561 | 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] | 1562 | } else if (max) { |
| 1563 | part->max_u64 = UINT64_C(255); |
| 1564 | } else { |
| 1565 | part->min_u64 = UINT64_C(0); |
| 1566 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1567 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1568 | 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] | 1569 | } |
| 1570 | break; |
| 1571 | case LY_TYPE_UINT16: /* range */ |
| 1572 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1573 | 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] | 1574 | } else if (max) { |
| 1575 | part->max_u64 = UINT64_C(65535); |
| 1576 | } else { |
| 1577 | part->min_u64 = UINT64_C(0); |
| 1578 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1579 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1580 | 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] | 1581 | } |
| 1582 | break; |
| 1583 | case LY_TYPE_UINT32: /* range */ |
| 1584 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1585 | 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] | 1586 | } else if (max) { |
| 1587 | part->max_u64 = UINT64_C(4294967295); |
| 1588 | } else { |
| 1589 | part->min_u64 = UINT64_C(0); |
| 1590 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1591 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1592 | 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] | 1593 | } |
| 1594 | break; |
| 1595 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1596 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1597 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1598 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1599 | 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] | 1600 | } else if (max) { |
| 1601 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1602 | } else { |
| 1603 | part->min_u64 = UINT64_C(0); |
| 1604 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1605 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1606 | 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] | 1607 | } |
| 1608 | break; |
| 1609 | default: |
| 1610 | LOGINT(ctx->ctx); |
| 1611 | ret = LY_EINT; |
| 1612 | } |
| 1613 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1614 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1615 | if (ret == LY_EDENIED) { |
| 1616 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1617 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1618 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1619 | } else if (ret == LY_EVALID) { |
| 1620 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1621 | "Invalid %s restriction - invalid value \"%s\".", |
| 1622 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1623 | } else if (ret == LY_EEXIST) { |
| 1624 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1625 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1626 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1627 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1628 | } else if (!ret && value) { |
| 1629 | *value = *value + len; |
| 1630 | } |
| 1631 | free(valcopy); |
| 1632 | return ret; |
| 1633 | } |
| 1634 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1635 | /** |
| 1636 | * @brief Compile the parsed range restriction. |
| 1637 | * @param[in] ctx Compile context. |
| 1638 | * @param[in] range_p Parsed range structure to compile. |
| 1639 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1640 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1641 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1642 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1643 | * range restriction must be more restrictive than the base_range. |
| 1644 | * @param[in,out] range Pointer to the created current range structure. |
| 1645 | * @return LY_ERR value. |
| 1646 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1647 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1648 | 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] | 1649 | struct lysc_range *base_range, struct lysc_range **range) |
| 1650 | { |
| 1651 | LY_ERR ret = LY_EVALID; |
| 1652 | const char *expr; |
| 1653 | struct lysc_range_part *parts = NULL, *part; |
| 1654 | int range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1655 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1656 | |
| 1657 | assert(range); |
| 1658 | assert(range_p); |
| 1659 | |
| 1660 | expr = range_p->arg; |
| 1661 | while(1) { |
| 1662 | if (isspace(*expr)) { |
| 1663 | ++expr; |
| 1664 | } else if (*expr == '\0') { |
| 1665 | if (range_expected) { |
| 1666 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1667 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1668 | length_restr ? "length" : "range", range_p->arg); |
| 1669 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1670 | } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1671 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1672 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1673 | length_restr ? "length" : "range", range_p->arg); |
| 1674 | goto cleanup; |
| 1675 | } |
| 1676 | parts_done++; |
| 1677 | break; |
| 1678 | } else if (!strncmp(expr, "min", 3)) { |
| 1679 | if (parts) { |
| 1680 | /* min cannot be used elsewhere than in the first part */ |
| 1681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1682 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1683 | expr - range_p->arg, range_p->arg); |
| 1684 | goto cleanup; |
| 1685 | } |
| 1686 | expr += 3; |
| 1687 | |
| 1688 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1689 | 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] | 1690 | part->max_64 = part->min_64; |
| 1691 | } else if (*expr == '|') { |
| 1692 | if (!parts || range_expected) { |
| 1693 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1694 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1695 | goto cleanup; |
| 1696 | } |
| 1697 | expr++; |
| 1698 | parts_done++; |
| 1699 | /* process next part of the expression */ |
| 1700 | } else if (!strncmp(expr, "..", 2)) { |
| 1701 | expr += 2; |
| 1702 | while (isspace(*expr)) { |
| 1703 | expr++; |
| 1704 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1705 | if (!parts || LY_ARRAY_COUNT(parts) == parts_done) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1706 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1707 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1708 | goto cleanup; |
| 1709 | } |
| 1710 | /* continue expecting the upper boundary */ |
| 1711 | range_expected = 1; |
| 1712 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1713 | /* number */ |
| 1714 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1715 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1716 | 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] | 1717 | range_expected = 0; |
| 1718 | } else { |
| 1719 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1720 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1721 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1722 | part->max_64 = part->min_64; |
| 1723 | } |
| 1724 | |
| 1725 | /* continue with possible another expression part */ |
| 1726 | } else if (!strncmp(expr, "max", 3)) { |
| 1727 | expr += 3; |
| 1728 | while (isspace(*expr)) { |
| 1729 | expr++; |
| 1730 | } |
| 1731 | if (*expr != '\0') { |
| 1732 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1733 | length_restr ? "length" : "range", expr); |
| 1734 | goto cleanup; |
| 1735 | } |
| 1736 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1737 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1738 | 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] | 1739 | range_expected = 0; |
| 1740 | } else { |
| 1741 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1742 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1743 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1744 | part->min_64 = part->max_64; |
| 1745 | } |
| 1746 | } else { |
| 1747 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1748 | length_restr ? "length" : "range", expr); |
| 1749 | goto cleanup; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | /* check with the previous range/length restriction */ |
| 1754 | if (base_range) { |
| 1755 | switch (basetype) { |
| 1756 | case LY_TYPE_BINARY: |
| 1757 | case LY_TYPE_UINT8: |
| 1758 | case LY_TYPE_UINT16: |
| 1759 | case LY_TYPE_UINT32: |
| 1760 | case LY_TYPE_UINT64: |
| 1761 | case LY_TYPE_STRING: |
| 1762 | uns = 1; |
| 1763 | break; |
| 1764 | case LY_TYPE_DEC64: |
| 1765 | case LY_TYPE_INT8: |
| 1766 | case LY_TYPE_INT16: |
| 1767 | case LY_TYPE_INT32: |
| 1768 | case LY_TYPE_INT64: |
| 1769 | uns = 0; |
| 1770 | break; |
| 1771 | default: |
| 1772 | LOGINT(ctx->ctx); |
| 1773 | ret = LY_EINT; |
| 1774 | goto cleanup; |
| 1775 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1776 | for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1777 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1778 | goto baseerror; |
| 1779 | } |
| 1780 | /* current lower bound is not lower than the base */ |
| 1781 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1782 | /* base has single value */ |
| 1783 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1784 | /* both lower bounds are the same */ |
| 1785 | if (parts[u].min_64 != parts[u].max_64) { |
| 1786 | /* current continues with a range */ |
| 1787 | goto baseerror; |
| 1788 | } else { |
| 1789 | /* equal single values, move both forward */ |
| 1790 | ++v; |
| 1791 | continue; |
| 1792 | } |
| 1793 | } else { |
| 1794 | /* base is single value lower than current range, so the |
| 1795 | * value from base range is removed in the current, |
| 1796 | * move only base and repeat checking */ |
| 1797 | ++v; |
| 1798 | --u; |
| 1799 | continue; |
| 1800 | } |
| 1801 | } else { |
| 1802 | /* base is the range */ |
| 1803 | if (parts[u].min_64 == parts[u].max_64) { |
| 1804 | /* current is a single value */ |
| 1805 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1806 | /* current is behind the base range, so base range is omitted, |
| 1807 | * move the base and keep the current for further check */ |
| 1808 | ++v; |
| 1809 | --u; |
| 1810 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1811 | continue; |
| 1812 | } else { |
| 1813 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1814 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1815 | /* higher bound is higher than the current higher bound */ |
| 1816 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1817 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1818 | * continue with the same current, but move the base */ |
| 1819 | --u; |
| 1820 | ++v; |
| 1821 | continue; |
| 1822 | } |
| 1823 | /* current range starts within the base range but end behind it */ |
| 1824 | goto baseerror; |
| 1825 | } else { |
| 1826 | /* current range is smaller than the base, |
| 1827 | * move current, but stay with the base */ |
| 1828 | continue; |
| 1829 | } |
| 1830 | } |
| 1831 | } |
| 1832 | } |
| 1833 | if (u != parts_done) { |
| 1834 | baseerror: |
| 1835 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1836 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1837 | length_restr ? "length" : "range", range_p->arg); |
| 1838 | goto cleanup; |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | if (!(*range)) { |
| 1843 | *range = calloc(1, sizeof **range); |
| 1844 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1845 | } |
| 1846 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1847 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1848 | if (range_p->eapptag) { |
| 1849 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1850 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1851 | } |
| 1852 | if (range_p->emsg) { |
| 1853 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1854 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1855 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1856 | if (range_p->dsc) { |
| 1857 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1858 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1859 | } |
| 1860 | if (range_p->ref) { |
| 1861 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1862 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1863 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1864 | /* extensions are taken only from the last range by the caller */ |
| 1865 | |
| 1866 | (*range)->parts = parts; |
| 1867 | parts = NULL; |
| 1868 | ret = LY_SUCCESS; |
| 1869 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1870 | LY_ARRAY_FREE(parts); |
| 1871 | |
| 1872 | return ret; |
| 1873 | } |
| 1874 | |
| 1875 | /** |
| 1876 | * @brief Checks pattern syntax. |
| 1877 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1878 | * @param[in] ctx Context. |
| 1879 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1880 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1881 | * @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] | 1882 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1883 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1884 | LY_ERR |
| 1885 | 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] | 1886 | { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1887 | int idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1888 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1889 | int err_code; |
| 1890 | const char *orig_ptr; |
| 1891 | PCRE2_SIZE err_offset; |
| 1892 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1893 | #define URANGE_LEN 19 |
| 1894 | char *ublock2urange[][2] = { |
| 1895 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1896 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1897 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1898 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1899 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1900 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1901 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1902 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1903 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1904 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1905 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1906 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1907 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1908 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1909 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1910 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1911 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1912 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1913 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1914 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1915 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1916 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1917 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1918 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1919 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1920 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1921 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1922 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1923 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1924 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1925 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1926 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1927 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1928 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1929 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1930 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1931 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1932 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1933 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1934 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1935 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1936 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1937 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1938 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1939 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1940 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1941 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1942 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1943 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1944 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1945 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1946 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1947 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1948 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1949 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1950 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1951 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1952 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1953 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1954 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1955 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1956 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1957 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1958 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1959 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1960 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1961 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1962 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1963 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1964 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1965 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1966 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1967 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1968 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1969 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1970 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1971 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1972 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1973 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1974 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1975 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1976 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1977 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1978 | {NULL, NULL} |
| 1979 | }; |
| 1980 | |
| 1981 | /* adjust the expression to a Perl equivalent |
| 1982 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1983 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1984 | /* allocate space for the transformed pattern */ |
| 1985 | size = strlen(pattern) + 1; |
| 1986 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1987 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1988 | perl_regex[0] = '\0'; |
| 1989 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1990 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 1991 | brack = 0; |
| 1992 | idx = 0; |
| 1993 | orig_ptr = pattern; |
| 1994 | while (orig_ptr[0]) { |
| 1995 | switch (orig_ptr[0]) { |
| 1996 | case '$': |
| 1997 | case '^': |
| 1998 | if (!brack) { |
| 1999 | /* make space for the extra character */ |
| 2000 | ++size; |
| 2001 | perl_regex = ly_realloc(perl_regex, size); |
| 2002 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2003 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2004 | /* print escape slash */ |
| 2005 | perl_regex[idx] = '\\'; |
| 2006 | ++idx; |
| 2007 | } |
| 2008 | break; |
| 2009 | case '[': |
| 2010 | /* must not be escaped */ |
| 2011 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2012 | ++brack; |
| 2013 | } |
| 2014 | break; |
| 2015 | case ']': |
| 2016 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2017 | /* pattern was checked and compiled already */ |
| 2018 | assert(brack); |
| 2019 | --brack; |
| 2020 | } |
| 2021 | break; |
| 2022 | default: |
| 2023 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2024 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2025 | |
| 2026 | /* copy char */ |
| 2027 | perl_regex[idx] = orig_ptr[0]; |
| 2028 | |
| 2029 | ++idx; |
| 2030 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2031 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2032 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2033 | |
| 2034 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2035 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2036 | start = ptr - perl_regex; |
| 2037 | |
| 2038 | ptr = strchr(ptr, '}'); |
| 2039 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2040 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2041 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2042 | free(perl_regex); |
| 2043 | return LY_EVALID; |
| 2044 | } |
| 2045 | end = (ptr - perl_regex) + 1; |
| 2046 | |
| 2047 | /* need more space */ |
| 2048 | if (end - start < URANGE_LEN) { |
| 2049 | 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] | 2050 | 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] | 2051 | } |
| 2052 | |
| 2053 | /* find our range */ |
| 2054 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2055 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2056 | break; |
| 2057 | } |
| 2058 | } |
| 2059 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2060 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2061 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2062 | free(perl_regex); |
| 2063 | return LY_EVALID; |
| 2064 | } |
| 2065 | |
| 2066 | /* 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] | 2067 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2068 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2069 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2070 | } |
| 2071 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2072 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2073 | } |
| 2074 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2075 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2076 | /* skip brackets */ |
| 2077 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2078 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2079 | } else { |
| 2080 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2081 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2086 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2087 | 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] | 2088 | &err_code, &err_offset, NULL); |
| 2089 | if (!code_local) { |
| 2090 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2091 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2092 | 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] | 2093 | free(perl_regex); |
| 2094 | return LY_EVALID; |
| 2095 | } |
| 2096 | free(perl_regex); |
| 2097 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2098 | if (code) { |
| 2099 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2100 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2101 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | return LY_SUCCESS; |
| 2105 | |
| 2106 | #undef URANGE_LEN |
| 2107 | } |
| 2108 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2109 | /** |
| 2110 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2111 | * @param[in] ctx Compile context. |
| 2112 | * @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] | 2113 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2114 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2115 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2116 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2117 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2118 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2119 | 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] | 2120 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2121 | { |
| 2122 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2123 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2124 | LY_ERR ret = LY_SUCCESS; |
| 2125 | |
| 2126 | /* first, copy the patterns from the base type */ |
| 2127 | if (base_patterns) { |
| 2128 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2129 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2130 | } |
| 2131 | |
| 2132 | LY_ARRAY_FOR(patterns_p, u) { |
| 2133 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2134 | *pattern = calloc(1, sizeof **pattern); |
| 2135 | ++(*pattern)->refcount; |
| 2136 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | 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] | 2138 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2139 | |
| 2140 | if (patterns_p[u].arg[0] == 0x15) { |
| 2141 | (*pattern)->inverted = 1; |
| 2142 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2143 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2144 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2145 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2146 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2147 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2148 | 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] | 2149 | } |
| 2150 | done: |
| 2151 | return ret; |
| 2152 | } |
| 2153 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2154 | /** |
| 2155 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2156 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2157 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2158 | 0 /* LY_TYPE_UNKNOWN */, |
| 2159 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2160 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2161 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2162 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2163 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2164 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2165 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2166 | 0 /* LY_TYPE_BOOL */, |
| 2167 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2168 | 0 /* LY_TYPE_EMPTY */, |
| 2169 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2170 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2171 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2172 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2173 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2174 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2175 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2176 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2177 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2178 | }; |
| 2179 | |
| 2180 | /** |
| 2181 | * @brief stringification of the YANG built-in data types |
| 2182 | */ |
| 2183 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2184 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2185 | "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] | 2186 | }; |
| 2187 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2188 | /** |
| 2189 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2190 | * @param[in] ctx Compile context. |
| 2191 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2192 | * @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] | 2193 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2194 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2195 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2196 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2197 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2198 | 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] | 2199 | 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] | 2200 | { |
| 2201 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2202 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2203 | int32_t value = 0; |
| 2204 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2205 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2206 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2207 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2208 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2209 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2210 | return LY_EVALID; |
| 2211 | } |
| 2212 | |
| 2213 | LY_ARRAY_FOR(enums_p, u) { |
| 2214 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2215 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2216 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2217 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2218 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2219 | if (base_enums) { |
| 2220 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2221 | LY_ARRAY_FOR(base_enums, v) { |
| 2222 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2223 | break; |
| 2224 | } |
| 2225 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2226 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2227 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2228 | "Invalid %s - derived type adds new item \"%s\".", |
| 2229 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2230 | return LY_EVALID; |
| 2231 | } |
| 2232 | match = v; |
| 2233 | } |
| 2234 | |
| 2235 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2236 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2237 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2238 | e->value = (int32_t)enums_p[u].value; |
| 2239 | if (!u || e->value >= value) { |
| 2240 | value = e->value + 1; |
| 2241 | } |
| 2242 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2243 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2244 | if (e->value == (*enums)[v].value) { |
| 2245 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2246 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2247 | e->value, e->name, (*enums)[v].name); |
| 2248 | return LY_EVALID; |
| 2249 | } |
| 2250 | } |
| 2251 | } else if (base_enums) { |
| 2252 | /* inherit the assigned value */ |
| 2253 | e->value = base_enums[match].value; |
| 2254 | if (!u || e->value >= value) { |
| 2255 | value = e->value + 1; |
| 2256 | } |
| 2257 | } else { |
| 2258 | /* assign value automatically */ |
| 2259 | if (u && value == INT32_MIN) { |
| 2260 | /* counter overflow */ |
| 2261 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2262 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2263 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2264 | return LY_EVALID; |
| 2265 | } |
| 2266 | e->value = value++; |
| 2267 | } |
| 2268 | } else { /* LY_TYPE_BITS */ |
| 2269 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2270 | e->value = (int32_t)enums_p[u].value; |
| 2271 | if (!u || (uint32_t)e->value >= position) { |
| 2272 | position = (uint32_t)e->value + 1; |
| 2273 | } |
| 2274 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2275 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2276 | if (e->value == (*enums)[v].value) { |
| 2277 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2278 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2279 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2280 | return LY_EVALID; |
| 2281 | } |
| 2282 | } |
| 2283 | } else if (base_enums) { |
| 2284 | /* inherit the assigned value */ |
| 2285 | e->value = base_enums[match].value; |
| 2286 | if (!u || (uint32_t)e->value >= position) { |
| 2287 | position = (uint32_t)e->value + 1; |
| 2288 | } |
| 2289 | } else { |
| 2290 | /* assign value automatically */ |
| 2291 | if (u && position == 0) { |
| 2292 | /* counter overflow */ |
| 2293 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2294 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2295 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2296 | return LY_EVALID; |
| 2297 | } |
| 2298 | e->value = position++; |
| 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | if (base_enums) { |
| 2303 | /* the assigned values must not change from the derived type */ |
| 2304 | if (e->value != base_enums[match].value) { |
| 2305 | if (basetype == LY_TYPE_ENUM) { |
| 2306 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2307 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2308 | e->name, base_enums[match].value, e->value); |
| 2309 | } else { |
| 2310 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2311 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2312 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2313 | } |
| 2314 | return LY_EVALID; |
| 2315 | } |
| 2316 | } |
| 2317 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2318 | 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] | 2319 | 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] | 2320 | |
| 2321 | if (basetype == LY_TYPE_BITS) { |
| 2322 | /* keep bits ordered by position */ |
| 2323 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2324 | if (v != u) { |
| 2325 | memcpy(&storage, e, sizeof *e); |
| 2326 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2327 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2328 | } |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | done: |
| 2333 | return ret; |
| 2334 | } |
| 2335 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2336 | /** |
| 2337 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2338 | * |
| 2339 | * path-arg = absolute-path / relative-path |
| 2340 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2341 | * relative-path = 1*(".." "/") descendant-path |
| 2342 | * |
| 2343 | * @param[in,out] path Path to parse. |
| 2344 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2345 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2346 | * @param[out] name Name of the token. |
| 2347 | * @param[out] nam_len Length of the name. |
| 2348 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2349 | * must not be changed between consecutive calls. -1 if the |
| 2350 | * path is absolute. |
| 2351 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2352 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2353 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2354 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2355 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2356 | int *parent_times, int *has_predicate) |
| 2357 | { |
| 2358 | int par_times = 0; |
| 2359 | |
| 2360 | assert(path && *path); |
| 2361 | assert(parent_times); |
| 2362 | assert(prefix); |
| 2363 | assert(prefix_len); |
| 2364 | assert(name); |
| 2365 | assert(name_len); |
| 2366 | assert(has_predicate); |
| 2367 | |
| 2368 | *prefix = NULL; |
| 2369 | *prefix_len = 0; |
| 2370 | *name = NULL; |
| 2371 | *name_len = 0; |
| 2372 | *has_predicate = 0; |
| 2373 | |
| 2374 | if (!*parent_times) { |
| 2375 | if (!strncmp(*path, "..", 2)) { |
| 2376 | *path += 2; |
| 2377 | ++par_times; |
| 2378 | while (!strncmp(*path, "/..", 3)) { |
| 2379 | *path += 3; |
| 2380 | ++par_times; |
| 2381 | } |
| 2382 | } |
| 2383 | if (par_times) { |
| 2384 | *parent_times = par_times; |
| 2385 | } else { |
| 2386 | *parent_times = -1; |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | if (**path != '/') { |
| 2391 | return LY_EINVAL; |
| 2392 | } |
| 2393 | /* skip '/' */ |
| 2394 | ++(*path); |
| 2395 | |
| 2396 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2397 | 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] | 2398 | |
| 2399 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2400 | /* path continues by another token or this is the last token */ |
| 2401 | return LY_SUCCESS; |
| 2402 | } else if ((*path)[0] != '[') { |
| 2403 | /* unexpected character */ |
| 2404 | return LY_EINVAL; |
| 2405 | } else { |
| 2406 | /* predicate starting with [ */ |
| 2407 | *has_predicate = 1; |
| 2408 | return LY_SUCCESS; |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2413 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2414 | * |
| 2415 | * The set of features used for target must be a subset of features used for the leafref. |
| 2416 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2417 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2418 | * |
| 2419 | * @param[in] refnode The leafref node. |
| 2420 | * @param[in] target Tha target node of the leafref. |
| 2421 | * @return LY_SUCCESS or LY_EVALID; |
| 2422 | */ |
| 2423 | static LY_ERR |
| 2424 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2425 | { |
| 2426 | LY_ERR ret = LY_EVALID; |
| 2427 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2428 | LY_ARRAY_COUNT_TYPE u, v, count; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2429 | struct ly_set features = {0}; |
| 2430 | |
| 2431 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2432 | if (iter->iffeatures) { |
| 2433 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2434 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2435 | 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] | 2436 | } |
| 2437 | } |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2442 | * So when adding features applicable to the target into the features set, we should not be |
| 2443 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2444 | * to the leafref itself. */ |
| 2445 | count = features.count; |
| 2446 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2447 | if (iter->iffeatures) { |
| 2448 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2449 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2450 | 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] | 2451 | /* new feature was added (or LY_EMEM) */ |
| 2452 | goto cleanup; |
| 2453 | } |
| 2454 | } |
| 2455 | } |
| 2456 | } |
| 2457 | } |
| 2458 | ret = LY_SUCCESS; |
| 2459 | |
| 2460 | cleanup: |
| 2461 | ly_set_erase(&features, NULL); |
| 2462 | return ret; |
| 2463 | } |
| 2464 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2465 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2466 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2467 | struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2468 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2469 | /** |
| 2470 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2471 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2472 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2473 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2474 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2475 | * @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] | 2476 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2477 | * @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] | 2478 | * @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] | 2479 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2480 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2481 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2482 | * @return LY_ERR value. |
| 2483 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2484 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2485 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2486 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2487 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2488 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2489 | { |
| 2490 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2491 | struct lysc_type_bin *bin; |
| 2492 | struct lysc_type_num *num; |
| 2493 | struct lysc_type_str *str; |
| 2494 | struct lysc_type_bits *bits; |
| 2495 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2496 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2497 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2498 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2499 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2500 | |
| 2501 | switch (basetype) { |
| 2502 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2503 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2504 | |
| 2505 | /* 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] | 2506 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2507 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2508 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2509 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2510 | 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] | 2511 | } |
| 2512 | } |
| 2513 | |
| 2514 | if (tpdfname) { |
| 2515 | type_p->compiled = *type; |
| 2516 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2517 | } |
| 2518 | break; |
| 2519 | case LY_TYPE_BITS: |
| 2520 | /* RFC 7950 9.7 - bits */ |
| 2521 | bits = (struct lysc_type_bits*)(*type); |
| 2522 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2523 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2524 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2525 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2526 | } |
| 2527 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2528 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2529 | /* 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] | 2530 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2531 | 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] | 2532 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2533 | 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] | 2534 | free(*type); |
| 2535 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2536 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2537 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | if (tpdfname) { |
| 2541 | type_p->compiled = *type; |
| 2542 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2543 | } |
| 2544 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2545 | case LY_TYPE_DEC64: |
| 2546 | dec = (struct lysc_type_dec*)(*type); |
| 2547 | |
| 2548 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2549 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2550 | if (!type_p->fraction_digits) { |
| 2551 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2552 | 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] | 2553 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2554 | 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] | 2555 | free(*type); |
| 2556 | *type = NULL; |
| 2557 | } |
| 2558 | return LY_EVALID; |
| 2559 | } |
| 2560 | } else if (type_p->fraction_digits) { |
| 2561 | /* 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] | 2562 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2563 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2564 | "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] | 2565 | tpdfname); |
| 2566 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2567 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2568 | "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] | 2569 | free(*type); |
| 2570 | *type = NULL; |
| 2571 | } |
| 2572 | return LY_EVALID; |
| 2573 | } |
| 2574 | dec->fraction_digits = type_p->fraction_digits; |
| 2575 | |
| 2576 | /* RFC 7950 9.2.4 - range */ |
| 2577 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2578 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2579 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2580 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2581 | 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] | 2582 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | if (tpdfname) { |
| 2586 | type_p->compiled = *type; |
| 2587 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2588 | } |
| 2589 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2590 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2591 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2592 | |
| 2593 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2594 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2595 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2596 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2597 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2598 | 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] | 2599 | } |
| 2600 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2601 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2602 | } |
| 2603 | |
| 2604 | /* RFC 7950 9.4.5 - pattern */ |
| 2605 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2606 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2607 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2608 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2609 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2610 | } |
| 2611 | |
| 2612 | if (tpdfname) { |
| 2613 | type_p->compiled = *type; |
| 2614 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2615 | } |
| 2616 | break; |
| 2617 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2618 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2619 | |
| 2620 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2621 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2622 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2623 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2624 | } |
| 2625 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2626 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2627 | /* 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] | 2628 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2629 | 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] | 2630 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2631 | 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] | 2632 | free(*type); |
| 2633 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2634 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2635 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | if (tpdfname) { |
| 2639 | type_p->compiled = *type; |
| 2640 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2641 | } |
| 2642 | break; |
| 2643 | case LY_TYPE_INT8: |
| 2644 | case LY_TYPE_UINT8: |
| 2645 | case LY_TYPE_INT16: |
| 2646 | case LY_TYPE_UINT16: |
| 2647 | case LY_TYPE_INT32: |
| 2648 | case LY_TYPE_UINT32: |
| 2649 | case LY_TYPE_INT64: |
| 2650 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2651 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2652 | |
| 2653 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2654 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2655 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2656 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2657 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2658 | 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] | 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | if (tpdfname) { |
| 2663 | type_p->compiled = *type; |
| 2664 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2665 | } |
| 2666 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2667 | case LY_TYPE_IDENT: |
| 2668 | idref = (struct lysc_type_identityref*)(*type); |
| 2669 | |
| 2670 | /* RFC 7950 9.10.2 - base */ |
| 2671 | if (type_p->bases) { |
| 2672 | if (base) { |
| 2673 | /* only the directly derived identityrefs can contain base specification */ |
| 2674 | if (tpdfname) { |
| 2675 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2676 | "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] | 2677 | tpdfname); |
| 2678 | } else { |
| 2679 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2680 | "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] | 2681 | free(*type); |
| 2682 | *type = NULL; |
| 2683 | } |
| 2684 | return LY_EVALID; |
| 2685 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2686 | 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] | 2687 | } |
| 2688 | |
| 2689 | if (!base && !type_p->flags) { |
| 2690 | /* type derived from identityref built-in type must contain at least one base */ |
| 2691 | if (tpdfname) { |
| 2692 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2693 | } else { |
| 2694 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2695 | free(*type); |
| 2696 | *type = NULL; |
| 2697 | } |
| 2698 | return LY_EVALID; |
| 2699 | } |
| 2700 | |
| 2701 | if (tpdfname) { |
| 2702 | type_p->compiled = *type; |
| 2703 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2704 | } |
| 2705 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2706 | case LY_TYPE_LEAFREF: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2707 | lref = (struct lysc_type_leafref*)*type; |
| 2708 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2709 | /* RFC 7950 9.9.3 - require-instance */ |
| 2710 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2711 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2712 | if (tpdfname) { |
| 2713 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2714 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2715 | } else { |
| 2716 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2717 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2718 | free(*type); |
| 2719 | *type = NULL; |
| 2720 | } |
| 2721 | return LY_EVALID; |
| 2722 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2723 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2724 | } else if (base) { |
| 2725 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2726 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2727 | } else { |
| 2728 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2729 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2730 | } |
| 2731 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2732 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2733 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2734 | } else if (base) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2735 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path); |
| 2736 | lref->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2737 | } else if (tpdfname) { |
| 2738 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2739 | return LY_EVALID; |
| 2740 | } else { |
| 2741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2742 | free(*type); |
| 2743 | *type = NULL; |
| 2744 | return LY_EVALID; |
| 2745 | } |
| 2746 | if (tpdfname) { |
| 2747 | type_p->compiled = *type; |
| 2748 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2749 | } |
| 2750 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2751 | case LY_TYPE_INST: |
| 2752 | /* RFC 7950 9.9.3 - require-instance */ |
| 2753 | if (type_p->flags & LYS_SET_REQINST) { |
| 2754 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2755 | } else { |
| 2756 | /* default is true */ |
| 2757 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2758 | } |
| 2759 | |
| 2760 | if (tpdfname) { |
| 2761 | type_p->compiled = *type; |
| 2762 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2763 | } |
| 2764 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2765 | case LY_TYPE_UNION: |
| 2766 | un = (struct lysc_type_union*)(*type); |
| 2767 | |
| 2768 | /* RFC 7950 7.4 - type */ |
| 2769 | if (type_p->types) { |
| 2770 | if (base) { |
| 2771 | /* only the directly derived union can contain types specification */ |
| 2772 | if (tpdfname) { |
| 2773 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2774 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2775 | tpdfname); |
| 2776 | } else { |
| 2777 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2778 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2779 | free(*type); |
| 2780 | *type = NULL; |
| 2781 | } |
| 2782 | return LY_EVALID; |
| 2783 | } |
| 2784 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2785 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2786 | for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2787 | LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, |
| 2788 | &type_p->types[u], &un->types[u + additional], NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2789 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2790 | /* add space for additional types from the union subtype */ |
| 2791 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2792 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1, |
Radek Krejci | c4fa029 | 2020-05-14 10:54:49 +0200 | [diff] [blame] | 2793 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2794 | |
| 2795 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2796 | for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2797 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2798 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2799 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2800 | 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] | 2801 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2802 | |
| 2803 | lref->basetype = LY_TYPE_LEAFREF; |
| 2804 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path); |
| 2805 | lref->refcount = 1; |
| 2806 | lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2807 | 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] | 2808 | /* TODO extensions */ |
| 2809 | |
| 2810 | } else { |
| 2811 | un->types[u + additional] = un_aux->types[v]; |
| 2812 | ++un_aux->types[v]->refcount; |
| 2813 | } |
| 2814 | ++additional; |
| 2815 | LY_ARRAY_INCREMENT(un->types); |
| 2816 | } |
| 2817 | /* compensate u increment in main loop */ |
| 2818 | --additional; |
| 2819 | |
| 2820 | /* free the replaced union subtype */ |
| 2821 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2822 | } else { |
| 2823 | LY_ARRAY_INCREMENT(un->types); |
| 2824 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2825 | } |
| 2826 | } |
| 2827 | |
| 2828 | if (!base && !type_p->flags) { |
| 2829 | /* type derived from union built-in type must contain at least one type */ |
| 2830 | if (tpdfname) { |
| 2831 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2832 | } else { |
| 2833 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2834 | free(*type); |
| 2835 | *type = NULL; |
| 2836 | } |
| 2837 | return LY_EVALID; |
| 2838 | } |
| 2839 | |
| 2840 | if (tpdfname) { |
| 2841 | type_p->compiled = *type; |
| 2842 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2843 | } |
| 2844 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2845 | case LY_TYPE_BOOL: |
| 2846 | case LY_TYPE_EMPTY: |
| 2847 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2848 | break; |
| 2849 | } |
| 2850 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2851 | done: |
| 2852 | return ret; |
| 2853 | } |
| 2854 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2855 | /** |
| 2856 | * @brief Compile information about the leaf/leaf-list's type. |
| 2857 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2858 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2859 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2860 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2861 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2862 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2863 | * @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] | 2864 | * @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] | 2865 | * @return LY_ERR value. |
| 2866 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2867 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2868 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2869 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2870 | struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2871 | { |
| 2872 | LY_ERR ret = LY_SUCCESS; |
| 2873 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2874 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2875 | struct type_context { |
| 2876 | const struct lysp_tpdf *tpdf; |
| 2877 | struct lysp_node *node; |
| 2878 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2879 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2880 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2881 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2882 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2883 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2884 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2885 | |
| 2886 | (*type) = NULL; |
| 2887 | |
| 2888 | tctx = calloc(1, sizeof *tctx); |
| 2889 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2890 | 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] | 2891 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2892 | ret == LY_SUCCESS; |
| 2893 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2894 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2895 | if (basetype) { |
| 2896 | break; |
| 2897 | } |
| 2898 | |
| 2899 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2900 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2901 | 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] | 2902 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2903 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2904 | if (units && !*units) { |
| 2905 | /* inherit units */ |
| 2906 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2907 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2908 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2909 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2910 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2911 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2912 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2913 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2914 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2915 | break; |
| 2916 | } |
| 2917 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2918 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2919 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2920 | * 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] | 2921 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2922 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2923 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2924 | dummyloops = 1; |
| 2925 | goto preparenext; |
| 2926 | } else { |
| 2927 | tctx = NULL; |
| 2928 | break; |
| 2929 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2930 | } |
| 2931 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2932 | /* circular typedef reference detection */ |
| 2933 | for (u = 0; u < tpdf_chain.count; u++) { |
| 2934 | /* local part */ |
| 2935 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 2936 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2937 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2938 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2939 | free(tctx); |
| 2940 | ret = LY_EVALID; |
| 2941 | goto cleanup; |
| 2942 | } |
| 2943 | } |
| 2944 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 2945 | /* global part for unions corner case */ |
| 2946 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 2947 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2948 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2949 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2950 | free(tctx); |
| 2951 | ret = LY_EVALID; |
| 2952 | goto cleanup; |
| 2953 | } |
| 2954 | } |
| 2955 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2956 | /* store information for the following processing */ |
| 2957 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2958 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2959 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2960 | /* prepare next loop */ |
| 2961 | tctx_prev = tctx; |
| 2962 | tctx = calloc(1, sizeof *tctx); |
| 2963 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2964 | } |
| 2965 | free(tctx); |
| 2966 | |
| 2967 | /* allocate type according to the basetype */ |
| 2968 | switch (basetype) { |
| 2969 | case LY_TYPE_BINARY: |
| 2970 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2971 | break; |
| 2972 | case LY_TYPE_BITS: |
| 2973 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2974 | break; |
| 2975 | case LY_TYPE_BOOL: |
| 2976 | case LY_TYPE_EMPTY: |
| 2977 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2978 | break; |
| 2979 | case LY_TYPE_DEC64: |
| 2980 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2981 | break; |
| 2982 | case LY_TYPE_ENUM: |
| 2983 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2984 | break; |
| 2985 | case LY_TYPE_IDENT: |
| 2986 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2987 | break; |
| 2988 | case LY_TYPE_INST: |
| 2989 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2990 | break; |
| 2991 | case LY_TYPE_LEAFREF: |
| 2992 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2993 | break; |
| 2994 | case LY_TYPE_STRING: |
| 2995 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2996 | break; |
| 2997 | case LY_TYPE_UNION: |
| 2998 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2999 | break; |
| 3000 | case LY_TYPE_INT8: |
| 3001 | case LY_TYPE_UINT8: |
| 3002 | case LY_TYPE_INT16: |
| 3003 | case LY_TYPE_UINT16: |
| 3004 | case LY_TYPE_INT32: |
| 3005 | case LY_TYPE_UINT32: |
| 3006 | case LY_TYPE_INT64: |
| 3007 | case LY_TYPE_UINT64: |
| 3008 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3009 | break; |
| 3010 | case LY_TYPE_UNKNOWN: |
| 3011 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3012 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3013 | ret = LY_EVALID; |
| 3014 | goto cleanup; |
| 3015 | } |
| 3016 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3017 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3018 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3019 | ly_data_type2str[basetype]); |
| 3020 | free(*type); |
| 3021 | (*type) = NULL; |
| 3022 | ret = LY_EVALID; |
| 3023 | goto cleanup; |
| 3024 | } |
| 3025 | |
| 3026 | /* get restrictions from the referred typedefs */ |
| 3027 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3028 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3029 | |
| 3030 | /* remember the typedef context for circular check */ |
| 3031 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3032 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3033 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3034 | base = tctx->tpdf->type.compiled; |
| 3035 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3036 | } 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] | 3037 | /* no change, just use the type information from the base */ |
| 3038 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3039 | ++base->refcount; |
| 3040 | continue; |
| 3041 | } |
| 3042 | |
| 3043 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3044 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3045 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3046 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3047 | ret = LY_EVALID; |
| 3048 | goto cleanup; |
| 3049 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3050 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3051 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3052 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3053 | ret = LY_EVALID; |
| 3054 | goto cleanup; |
| 3055 | } |
| 3056 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3057 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3058 | /* TODO user type plugins */ |
| 3059 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3060 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3061 | 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] | 3062 | 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] | 3063 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3064 | LY_CHECK_GOTO(ret, cleanup); |
| 3065 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3066 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3067 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3068 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3069 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3070 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3071 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3072 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3073 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3074 | /* TODO user type plugins */ |
| 3075 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3076 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3077 | 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] | 3078 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3079 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3080 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3081 | free(*type); |
| 3082 | (*type) = base; |
| 3083 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3084 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3085 | if (dflt && !(*type)->dflt) { |
| 3086 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3087 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3088 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3089 | (*type)->dflt->realtype = (*type); |
| 3090 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3091 | 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] | 3092 | 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] | 3093 | if (err) { |
| 3094 | ly_err_print(err); |
| 3095 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3096 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3097 | ly_err_free(err); |
| 3098 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3099 | if (ret == LY_EINCOMPLETE) { |
| 3100 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3101 | 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] | 3102 | |
| 3103 | /* but in general result is so far ok */ |
| 3104 | ret = LY_SUCCESS; |
| 3105 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3106 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3107 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3108 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3109 | 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] | 3110 | |
| 3111 | cleanup: |
| 3112 | ly_set_erase(&tpdf_chain, free); |
| 3113 | return ret; |
| 3114 | } |
| 3115 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3116 | /** |
| 3117 | * @brief Compile status information of the given node. |
| 3118 | * |
| 3119 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3120 | * has the status correctly set during the compilation. |
| 3121 | * |
| 3122 | * @param[in] ctx Compile context |
| 3123 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3124 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3125 | * the compatibility with the parent's status value. |
| 3126 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3127 | * @return LY_ERR value. |
| 3128 | */ |
| 3129 | static LY_ERR |
| 3130 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3131 | { |
| 3132 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3133 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3134 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3135 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3136 | if ((parent_flags & 0x3) != 0x3) { |
| 3137 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3138 | * combination of bits (0x3) which marks the uses_status value */ |
| 3139 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3140 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3141 | } |
| 3142 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3143 | } else { |
| 3144 | (*node_flags) |= LYS_STATUS_CURR; |
| 3145 | } |
| 3146 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3147 | /* check status compatibility with the parent */ |
| 3148 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3149 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3150 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3151 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3152 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3153 | } else { /* LYS_STATUS_DEPRC */ |
| 3154 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3155 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3156 | } |
| 3157 | return LY_EVALID; |
| 3158 | } |
| 3159 | } |
| 3160 | return LY_SUCCESS; |
| 3161 | } |
| 3162 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3163 | /** |
| 3164 | * @brief Check uniqness of the node/action/notification name. |
| 3165 | * |
| 3166 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3167 | * structures, but they share the namespace so we need to check their name collisions. |
| 3168 | * |
| 3169 | * @param[in] ctx Compile context. |
| 3170 | * @param[in] children List (linked list) of data nodes to go through. |
| 3171 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3172 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3173 | * @param[in] name Name of the item to find in the given lists. |
| 3174 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3175 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3176 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3177 | */ |
| 3178 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3179 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions, |
| 3180 | const struct lysc_notif *notifs, const char *name, void *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3181 | { |
| 3182 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3183 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3184 | |
| 3185 | LY_LIST_FOR(children, iter) { |
| 3186 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3187 | goto error; |
| 3188 | } |
| 3189 | } |
| 3190 | LY_ARRAY_FOR(actions, u) { |
| 3191 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3192 | goto error; |
| 3193 | } |
| 3194 | } |
| 3195 | LY_ARRAY_FOR(notifs, u) { |
| 3196 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3197 | goto error; |
| 3198 | } |
| 3199 | } |
| 3200 | return LY_SUCCESS; |
| 3201 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3202 | 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] | 3203 | return LY_EEXIST; |
| 3204 | } |
| 3205 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3206 | 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] | 3207 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3208 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3209 | * @brief Compile parsed RPC/action schema node information. |
| 3210 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3211 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3212 | * @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] | 3213 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3214 | * @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). |
| 3215 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3216 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3217 | */ |
| 3218 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3219 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3220 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3221 | { |
| 3222 | LY_ERR ret = LY_SUCCESS; |
| 3223 | struct lysp_node *child_p; |
| 3224 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3225 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3226 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3227 | lysc_update_path(ctx, parent, action_p->name); |
| 3228 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3229 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3230 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3231 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3232 | action_p->name, action)) { |
| 3233 | return LY_EVALID; |
| 3234 | } |
| 3235 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3236 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3237 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3238 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3239 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3240 | return LY_EVALID; |
| 3241 | } |
| 3242 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3243 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3244 | action->module = ctx->mod; |
| 3245 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3246 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3247 | action->sp = action_p; |
| 3248 | } |
| 3249 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3250 | |
| 3251 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3252 | * 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] | 3253 | 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] | 3254 | |
| 3255 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3256 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3257 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3258 | 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] | 3259 | 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] | 3260 | |
| 3261 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3262 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3263 | 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] | 3264 | 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] | 3265 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3266 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3267 | 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] | 3268 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3269 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3270 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3271 | |
| 3272 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3273 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3274 | 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] | 3275 | 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] | 3276 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3277 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3278 | 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] | 3279 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3280 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3281 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3282 | |
| 3283 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3284 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3285 | ly_set_add(&ctx->xpath, action, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3286 | } |
| 3287 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3288 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3289 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3290 | return ret; |
| 3291 | } |
| 3292 | |
| 3293 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3294 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3295 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3296 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3297 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3298 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3299 | * @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] | 3300 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3301 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3302 | */ |
| 3303 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3304 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3305 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3306 | { |
| 3307 | LY_ERR ret = LY_SUCCESS; |
| 3308 | struct lysp_node *child_p; |
| 3309 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3310 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3311 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3312 | lysc_update_path(ctx, parent, notif_p->name); |
| 3313 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3314 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3315 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3316 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3317 | notif_p->name, notif)) { |
| 3318 | return LY_EVALID; |
| 3319 | } |
| 3320 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3321 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3322 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3323 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3324 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3325 | return LY_EVALID; |
| 3326 | } |
| 3327 | |
| 3328 | notif->nodetype = LYS_NOTIF; |
| 3329 | notif->module = ctx->mod; |
| 3330 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3331 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3332 | notif->sp = notif_p; |
| 3333 | } |
| 3334 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3335 | |
| 3336 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3337 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3338 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3339 | |
| 3340 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3341 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3342 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3343 | 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] | 3344 | 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] | 3345 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3346 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3347 | ly_set_add(&ctx->xpath, notif, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3348 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3349 | 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] | 3350 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3351 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3352 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3353 | 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] | 3354 | } |
| 3355 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3356 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3357 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3358 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3359 | return ret; |
| 3360 | } |
| 3361 | |
| 3362 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3363 | * @brief Compile parsed container node information. |
| 3364 | * @param[in] ctx Compile context |
| 3365 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3366 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3367 | * is enriched with the container-specific information. |
| 3368 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3369 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3370 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3371 | 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] | 3372 | { |
| 3373 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3374 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3375 | struct lysp_node *child_p; |
| 3376 | unsigned int u; |
| 3377 | LY_ERR ret = LY_SUCCESS; |
| 3378 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3379 | if (cont_p->presence) { |
| 3380 | cont->flags |= LYS_PRESENCE; |
| 3381 | } |
| 3382 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3383 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3384 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3385 | } |
| 3386 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3387 | 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] | 3388 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3389 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3390 | ly_set_add(&ctx->xpath, cont, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3391 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3392 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3393 | 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] | 3394 | |
| 3395 | done: |
| 3396 | return ret; |
| 3397 | } |
| 3398 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3399 | /* |
| 3400 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3401 | * @param[in] ctx Compile context. |
| 3402 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3403 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3404 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3405 | * @return LY_ERR value. |
| 3406 | */ |
| 3407 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3408 | 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] | 3409 | { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3410 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3411 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3412 | 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] | 3413 | leaf->units ? NULL : &leaf->units)); |
| 3414 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3415 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3416 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3417 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3418 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3419 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3420 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3421 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3422 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3423 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3424 | } |
| 3425 | } else { |
| 3426 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3427 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3428 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3429 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3430 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3431 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3432 | } |
| 3433 | } |
| 3434 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3435 | /* 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] | 3436 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3437 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3438 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3439 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3440 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3441 | /* 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] | 3442 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3443 | } |
| 3444 | } |
| 3445 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3446 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3447 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3448 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3449 | return LY_EVALID; |
| 3450 | } |
| 3451 | } |
| 3452 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3453 | return LY_SUCCESS; |
| 3454 | } |
| 3455 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3456 | /** |
| 3457 | * @brief Compile parsed leaf node information. |
| 3458 | * @param[in] ctx Compile context |
| 3459 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3460 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3461 | * is enriched with the leaf-specific information. |
| 3462 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3463 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3464 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3465 | 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] | 3466 | { |
| 3467 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3468 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3469 | unsigned int u; |
| 3470 | LY_ERR ret = LY_SUCCESS; |
| 3471 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3472 | 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] | 3473 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3474 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3475 | ly_set_add(&ctx->xpath, leaf, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3476 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3477 | if (leaf_p->units) { |
| 3478 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3479 | leaf->flags |= LYS_SET_UNITS; |
| 3480 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3481 | |
| 3482 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3483 | leaf->dflt = (void*)leaf_p->dflt; |
| 3484 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3485 | if (ret) { |
| 3486 | leaf->dflt = NULL; |
| 3487 | return ret; |
| 3488 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3489 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3490 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3491 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3492 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3493 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3494 | leaf->dflt->realtype = leaf->type; |
| 3495 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3496 | 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] | 3497 | 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] | 3498 | leaf->dflt->realtype->refcount++; |
| 3499 | if (err) { |
| 3500 | ly_err_print(err); |
| 3501 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3502 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3503 | ly_err_free(err); |
| 3504 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3505 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3506 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3507 | 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] | 3508 | |
| 3509 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3510 | ret = LY_SUCCESS; |
| 3511 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3512 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3513 | leaf->flags |= LYS_SET_DFLT; |
| 3514 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3515 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3516 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3517 | done: |
| 3518 | return ret; |
| 3519 | } |
| 3520 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3521 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3522 | * @brief Compile parsed leaf-list node information. |
| 3523 | * @param[in] ctx Compile context |
| 3524 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3525 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3526 | * is enriched with the leaf-list-specific information. |
| 3527 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3528 | */ |
| 3529 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3530 | 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] | 3531 | { |
| 3532 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3533 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3534 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3535 | LY_ERR ret = LY_SUCCESS; |
| 3536 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3537 | 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] | 3538 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3539 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3540 | ly_set_add(&ctx->xpath, llist, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3541 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3542 | if (llist_p->units) { |
| 3543 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3544 | llist->flags |= LYS_SET_UNITS; |
| 3545 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3546 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3547 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3548 | llist->dflts = (void*)llist_p->dflts; |
| 3549 | 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] | 3550 | if (ret != LY_SUCCESS) { |
| 3551 | /* make sure the defaults are freed correctly */ |
| 3552 | if (llist_p->dflts) { |
| 3553 | llist->dflts = NULL; |
| 3554 | } |
| 3555 | return ret; |
| 3556 | } |
| 3557 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3558 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3559 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3560 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done); |
| 3561 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(llist_p->dflts), ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3562 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3563 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3564 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3565 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3566 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3567 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3568 | llist->dflts[u]->realtype = llist->type; |
| 3569 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3570 | 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] | 3571 | 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] | 3572 | llist->dflts[u]->realtype->refcount++; |
| 3573 | if (err) { |
| 3574 | ly_err_print(err); |
| 3575 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3576 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3577 | ly_err_free(err); |
| 3578 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3579 | if (ret == LY_EINCOMPLETE) { |
| 3580 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3581 | 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] | 3582 | |
| 3583 | /* but in general result is so far ok */ |
| 3584 | ret = LY_SUCCESS; |
| 3585 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3586 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3587 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3588 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3589 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3590 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3591 | /* configuration data values must be unique - so check the default values */ |
| 3592 | LY_ARRAY_FOR(llist->dflts, u) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3593 | for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3594 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3595 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3596 | 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] | 3597 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3598 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3599 | if (dynamic) { |
| 3600 | free((char*)val); |
| 3601 | } |
| 3602 | return LY_EVALID; |
| 3603 | } |
| 3604 | } |
| 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | /* 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] | 3609 | |
| 3610 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3611 | if (llist->min) { |
| 3612 | llist->flags |= LYS_MAND_TRUE; |
| 3613 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3614 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3615 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3616 | done: |
| 3617 | return ret; |
| 3618 | } |
| 3619 | |
| 3620 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3621 | * @brief Compile information about list's uniques. |
| 3622 | * @param[in] ctx Compile context. |
| 3623 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3624 | * @param[in] uniques Sized array list of unique statements. |
| 3625 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3626 | * @return LY_ERR value. |
| 3627 | */ |
| 3628 | static LY_ERR |
| 3629 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3630 | { |
| 3631 | LY_ERR ret = LY_SUCCESS; |
| 3632 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3633 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3634 | const char *keystr, *delim; |
| 3635 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3636 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3637 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3638 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3639 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3640 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3641 | config = -1; |
| 3642 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3643 | keystr = uniques[v]; |
| 3644 | while (keystr) { |
| 3645 | delim = strpbrk(keystr, " \t\n"); |
| 3646 | if (delim) { |
| 3647 | len = delim - keystr; |
| 3648 | while (isspace(*delim)) { |
| 3649 | ++delim; |
| 3650 | } |
| 3651 | } else { |
| 3652 | len = strlen(keystr); |
| 3653 | } |
| 3654 | |
| 3655 | /* unique node must be present */ |
| 3656 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3657 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3658 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3659 | if (ret != LY_SUCCESS) { |
| 3660 | if (ret == LY_EDENIED) { |
| 3661 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3662 | "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] | 3663 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3664 | } |
| 3665 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3666 | } else if (flags) { |
| 3667 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3668 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3669 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3670 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3671 | } |
| 3672 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3673 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3674 | /* all referenced leafs must be of the same config type */ |
| 3675 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3676 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3677 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3678 | return LY_EVALID; |
| 3679 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3680 | config = 1; |
| 3681 | } else { /* LYS_CONFIG_R */ |
| 3682 | config = 0; |
| 3683 | } |
| 3684 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3685 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3686 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3687 | if (parent->nodetype == LYS_LIST) { |
| 3688 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3689 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3690 | return LY_EVALID; |
| 3691 | } |
| 3692 | } |
| 3693 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3694 | /* check status */ |
| 3695 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3696 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3697 | |
| 3698 | /* mark leaf as unique */ |
| 3699 | (*key)->flags |= LYS_UNIQUE; |
| 3700 | |
| 3701 | /* next unique value in line */ |
| 3702 | keystr = delim; |
| 3703 | } |
| 3704 | /* next unique definition */ |
| 3705 | } |
| 3706 | |
| 3707 | return LY_SUCCESS; |
| 3708 | } |
| 3709 | |
| 3710 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3711 | * @brief Compile parsed list node information. |
| 3712 | * @param[in] ctx Compile context |
| 3713 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3714 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3715 | * is enriched with the list-specific information. |
| 3716 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3717 | */ |
| 3718 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3719 | 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] | 3720 | { |
| 3721 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3722 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3723 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3724 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3725 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3726 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3727 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3728 | LY_ERR ret = LY_SUCCESS; |
| 3729 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3730 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3731 | if (list->min) { |
| 3732 | list->flags |= LYS_MAND_TRUE; |
| 3733 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3734 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3735 | |
| 3736 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3737 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3738 | } |
| 3739 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3740 | 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] | 3741 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3742 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3743 | ly_set_add(&ctx->xpath, list, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3744 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3745 | |
| 3746 | /* keys */ |
| 3747 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3748 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3749 | return LY_EVALID; |
| 3750 | } |
| 3751 | |
| 3752 | /* find all the keys (must be direct children) */ |
| 3753 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3754 | if (!keystr) { |
| 3755 | /* keyless list */ |
| 3756 | list->flags |= LYS_KEYLESS; |
| 3757 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3758 | while (keystr) { |
| 3759 | delim = strpbrk(keystr, " \t\n"); |
| 3760 | if (delim) { |
| 3761 | len = delim - keystr; |
| 3762 | while (isspace(*delim)) { |
| 3763 | ++delim; |
| 3764 | } |
| 3765 | } else { |
| 3766 | len = strlen(keystr); |
| 3767 | } |
| 3768 | |
| 3769 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3770 | 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] | 3771 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3772 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3773 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3774 | return LY_EVALID; |
| 3775 | } |
| 3776 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3777 | if (key->flags & LYS_KEY) { |
| 3778 | /* the node was already marked as a key */ |
| 3779 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3780 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3781 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3782 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3783 | |
| 3784 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3785 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3786 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3787 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3788 | return LY_EVALID; |
| 3789 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3790 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3791 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3792 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3793 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3794 | "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] | 3795 | return LY_EVALID; |
| 3796 | } |
| 3797 | } else { |
| 3798 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3799 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3800 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3801 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3802 | return LY_EVALID; |
| 3803 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3804 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3805 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3806 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3807 | return LY_EVALID; |
| 3808 | } |
| 3809 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3810 | |
| 3811 | /* check status */ |
| 3812 | 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] | 3813 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3814 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3815 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3816 | if (key->dflt) { |
| 3817 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3818 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3819 | free(key->dflt); |
| 3820 | key->dflt = NULL; |
| 3821 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3822 | } |
| 3823 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3824 | key->flags |= LYS_KEY; |
| 3825 | |
| 3826 | /* move it to the correct position */ |
| 3827 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3828 | /* fix links in closest previous siblings of the key */ |
| 3829 | if (key->next) { |
| 3830 | key->next->prev = key->prev; |
| 3831 | } else { |
| 3832 | /* last child */ |
| 3833 | list->child->prev = key->prev; |
| 3834 | } |
| 3835 | if (key->prev->next) { |
| 3836 | key->prev->next = key->next; |
| 3837 | } |
| 3838 | /* fix links in the key */ |
| 3839 | if (prev_key) { |
| 3840 | key->prev = (struct lysc_node*)prev_key; |
| 3841 | key->next = prev_key->next; |
| 3842 | } else { |
| 3843 | key->prev = list->child->prev; |
| 3844 | key->next = list->child; |
| 3845 | } |
| 3846 | /* fix links in closes future siblings of the key */ |
| 3847 | if (prev_key) { |
| 3848 | if (prev_key->next) { |
| 3849 | prev_key->next->prev = (struct lysc_node*)key; |
| 3850 | } else { |
| 3851 | list->child->prev = (struct lysc_node*)key; |
| 3852 | } |
| 3853 | prev_key->next = (struct lysc_node*)key; |
| 3854 | } else { |
| 3855 | list->child->prev = (struct lysc_node*)key; |
| 3856 | } |
| 3857 | /* fix links in parent */ |
| 3858 | if (!key->prev->next) { |
| 3859 | list->child = (struct lysc_node*)key; |
| 3860 | } |
| 3861 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3862 | |
| 3863 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3864 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3865 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3866 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | /* uniques */ |
| 3870 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3871 | 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] | 3872 | } |
| 3873 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3874 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3875 | 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] | 3876 | |
| 3877 | done: |
| 3878 | return ret; |
| 3879 | } |
| 3880 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3881 | /** |
| 3882 | * @brief Do some checks and set the default choice's case. |
| 3883 | * |
| 3884 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3885 | * |
| 3886 | * @param[in] ctx Compile context. |
| 3887 | * @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, |
| 3888 | * not the reference to the imported module. |
| 3889 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3890 | * @return LY_ERR value. |
| 3891 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3892 | static LY_ERR |
| 3893 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3894 | { |
| 3895 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3896 | const char *prefix = NULL, *name; |
| 3897 | size_t prefix_len = 0; |
| 3898 | |
| 3899 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3900 | name = strchr(dflt, ':'); |
| 3901 | if (name) { |
| 3902 | prefix = dflt; |
| 3903 | prefix_len = name - prefix; |
| 3904 | ++name; |
| 3905 | } else { |
| 3906 | name = dflt; |
| 3907 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3908 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3909 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3910 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3911 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3912 | prefix_len, prefix); |
| 3913 | return LY_EVALID; |
| 3914 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3915 | 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] | 3916 | if (!ch->dflt) { |
| 3917 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3918 | "Default case \"%s\" not found.", dflt); |
| 3919 | return LY_EVALID; |
| 3920 | } |
| 3921 | /* no mandatory nodes directly under the default case */ |
| 3922 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3923 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3924 | break; |
| 3925 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3926 | if (iter->flags & LYS_MAND_TRUE) { |
| 3927 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3928 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3929 | return LY_EVALID; |
| 3930 | } |
| 3931 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3932 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3933 | return LY_SUCCESS; |
| 3934 | } |
| 3935 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3936 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3937 | 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] | 3938 | { |
| 3939 | struct lys_module *mod; |
| 3940 | const char *prefix = NULL, *name; |
| 3941 | size_t prefix_len = 0; |
| 3942 | struct lysc_node_case *cs; |
| 3943 | struct lysc_node *node; |
| 3944 | |
| 3945 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3946 | name = strchr(dflt, ':'); |
| 3947 | if (name) { |
| 3948 | prefix = dflt; |
| 3949 | prefix_len = name - prefix; |
| 3950 | ++name; |
| 3951 | } else { |
| 3952 | name = dflt; |
| 3953 | } |
| 3954 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3955 | if (prefix) { |
| 3956 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3957 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3958 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3959 | "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] | 3960 | return LY_EVALID; |
| 3961 | } |
| 3962 | } else { |
| 3963 | mod = ctx->mod; |
| 3964 | } |
| 3965 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3966 | 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] | 3967 | if (!cs) { |
| 3968 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3969 | "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] | 3970 | return LY_EVALID; |
| 3971 | } |
| 3972 | |
| 3973 | /* check that there is no mandatory node */ |
| 3974 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3975 | if (node->parent != (struct lysc_node*)cs) { |
| 3976 | break; |
| 3977 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3978 | if (node->flags & LYS_MAND_TRUE) { |
| 3979 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3980 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3981 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3982 | return LY_EVALID; |
| 3983 | } |
| 3984 | } |
| 3985 | |
| 3986 | /* set the default case in choice */ |
| 3987 | ch->dflt = cs; |
| 3988 | cs->flags |= LYS_SET_DFLT; |
| 3989 | |
| 3990 | return LY_SUCCESS; |
| 3991 | } |
| 3992 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3993 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3994 | * @brief Compile parsed choice node information. |
| 3995 | * @param[in] ctx Compile context |
| 3996 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3997 | * @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] | 3998 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3999 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4000 | */ |
| 4001 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4002 | 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] | 4003 | { |
| 4004 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4005 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4006 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4007 | LY_ERR ret = LY_SUCCESS; |
| 4008 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4009 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4010 | if (child_p->nodetype == LYS_CASE) { |
| 4011 | 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] | 4012 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4013 | } |
| 4014 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4015 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4016 | } |
| 4017 | } |
| 4018 | |
| 4019 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4020 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4021 | 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] | 4022 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4023 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4024 | return ret; |
| 4025 | } |
| 4026 | |
| 4027 | /** |
| 4028 | * @brief Compile parsed anydata or anyxml node information. |
| 4029 | * @param[in] ctx Compile context |
| 4030 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4031 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4032 | * is enriched with the any-specific information. |
| 4033 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4034 | */ |
| 4035 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4036 | 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] | 4037 | { |
| 4038 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4039 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4040 | unsigned int u; |
| 4041 | LY_ERR ret = LY_SUCCESS; |
| 4042 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4043 | 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] | 4044 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4045 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4046 | ly_set_add(&ctx->xpath, any, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4047 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4048 | |
| 4049 | if (any->flags & LYS_CONFIG_W) { |
| 4050 | 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] | 4051 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4052 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4053 | done: |
| 4054 | return ret; |
| 4055 | } |
| 4056 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4057 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4058 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4059 | * |
| 4060 | * @param[in] ctx Compile context |
| 4061 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4062 | * the choice itself is expected instead of a specific case node. |
| 4063 | * @param[in] node Schema node to connect into the list. |
| 4064 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4065 | * 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] | 4066 | */ |
| 4067 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4068 | 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] | 4069 | { |
| 4070 | struct lysc_node **children; |
| 4071 | |
| 4072 | if (node->nodetype == LYS_CASE) { |
| 4073 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4074 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4075 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4076 | } |
| 4077 | if (children) { |
| 4078 | if (!(*children)) { |
| 4079 | /* first child */ |
| 4080 | *children = node; |
| 4081 | } else if (*children != node) { |
| 4082 | /* by the condition in previous branch we cover the choice/case children |
| 4083 | * - the children list is shared by the choice and the the first case, in addition |
| 4084 | * the first child of each case must be referenced from the case node. So the node is |
| 4085 | * actually always already inserted in case it is the first children - so here such |
| 4086 | * a situation actually corresponds to the first branch */ |
| 4087 | /* insert at the end of the parent's children list */ |
| 4088 | (*children)->prev->next = node; |
| 4089 | node->prev = (*children)->prev; |
| 4090 | (*children)->prev = node; |
| 4091 | |
| 4092 | /* check the name uniqueness */ |
| 4093 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4094 | lysc_node_notifs(parent), node->name, node)) { |
| 4095 | return LY_EEXIST; |
| 4096 | } |
| 4097 | } |
| 4098 | } |
| 4099 | return LY_SUCCESS; |
| 4100 | } |
| 4101 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4102 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4103 | * @brief Prepare the case structure in choice node for the new data node. |
| 4104 | * |
| 4105 | * 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 |
| 4106 | * created in the choice when the first child was processed. |
| 4107 | * |
| 4108 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4109 | * @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, |
| 4110 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4111 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4112 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4113 | * @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, |
| 4114 | * 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] | 4115 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4116 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4117 | 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] | 4118 | { |
| 4119 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4120 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4121 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4122 | unsigned int u; |
| 4123 | LY_ERR ret; |
| 4124 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4125 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4126 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4127 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4128 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4129 | return NULL; \ |
| 4130 | } \ |
| 4131 | } |
| 4132 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4133 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4134 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4135 | |
| 4136 | /* we have to add an implicit case node into the parent choice */ |
| 4137 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4138 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4139 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4140 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4141 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4142 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4143 | /* the case is already present since the child is not its first children */ |
| 4144 | return (struct lysc_node_case*)ch->cases->prev; |
| 4145 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4146 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4147 | |
| 4148 | /* explicit parent case is not present (this is its first child) */ |
| 4149 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4150 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4151 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4152 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4153 | cs->sp = node_p; |
| 4154 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4155 | /* 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] | 4156 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4157 | |
| 4158 | if (node_p->when) { |
| 4159 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4160 | 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] | 4161 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4162 | |
| 4163 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4164 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4165 | ly_set_add(&ctx->xpath, cs, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4166 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4167 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4168 | 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] | 4169 | } else { |
| 4170 | LOGINT(ctx->ctx); |
| 4171 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4172 | } |
| 4173 | cs->module = ctx->mod; |
| 4174 | cs->prev = (struct lysc_node*)cs; |
| 4175 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4176 | 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] | 4177 | cs->child = child; |
| 4178 | |
| 4179 | return cs; |
| 4180 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4181 | if (cs) { |
| 4182 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4183 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4184 | return NULL; |
| 4185 | |
| 4186 | #undef UNIQUE_CHECK |
| 4187 | } |
| 4188 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4189 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4190 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4191 | * |
| 4192 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4193 | * @param[in] node Target node where the config is supposed to be changed. |
| 4194 | * @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] | 4195 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4196 | * 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] | 4197 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4198 | * @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] | 4199 | * @return LY_ERR value. |
| 4200 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4201 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4202 | 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] | 4203 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4204 | { |
| 4205 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4206 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4207 | |
| 4208 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4209 | /* nothing to do */ |
| 4210 | return LY_SUCCESS; |
| 4211 | } |
| 4212 | |
| 4213 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4214 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4215 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4216 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4217 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4218 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4219 | return LY_EVALID; |
| 4220 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4221 | node->flags |= LYS_SET_CONFIG; |
| 4222 | } else { |
| 4223 | if (node->flags & LYS_SET_CONFIG) { |
| 4224 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4225 | /* setting config flags, but have node with explicit config true */ |
| 4226 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4227 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4228 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4229 | return LY_EVALID; |
| 4230 | } |
| 4231 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4232 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4233 | return LY_SUCCESS; |
| 4234 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4235 | } |
| 4236 | node->flags &= ~LYS_CONFIG_MASK; |
| 4237 | node->flags |= config; |
| 4238 | |
| 4239 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4240 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4241 | 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] | 4242 | } |
| 4243 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4244 | return LY_SUCCESS; |
| 4245 | } |
| 4246 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4247 | /** |
| 4248 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4249 | * |
| 4250 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4251 | * the flag to such parents from a mandatory children. |
| 4252 | * |
| 4253 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4254 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4255 | * (mandatory children was removed). |
| 4256 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4257 | void |
| 4258 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4259 | { |
| 4260 | struct lysc_node *iter; |
| 4261 | |
| 4262 | if (add) { /* set flag */ |
| 4263 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4264 | parent = parent->parent) { |
| 4265 | parent->flags |= LYS_MAND_TRUE; |
| 4266 | } |
| 4267 | } else { /* unset flag */ |
| 4268 | 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] | 4269 | 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] | 4270 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4271 | /* there is another mandatory node */ |
| 4272 | return; |
| 4273 | } |
| 4274 | } |
| 4275 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4276 | parent->flags &= ~LYS_MAND_TRUE; |
| 4277 | } |
| 4278 | } |
| 4279 | } |
| 4280 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4281 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4282 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4283 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4284 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4285 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4286 | */ |
| 4287 | static void |
| 4288 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4289 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4290 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4291 | size_t len; |
| 4292 | |
| 4293 | len = strlen(aug_p->nodeid); |
| 4294 | LY_ARRAY_FOR(result, v) { |
| 4295 | if (strlen(result[v]->nodeid) <= len) { |
| 4296 | continue; |
| 4297 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4298 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4299 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4300 | memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4301 | break; |
| 4302 | } |
| 4303 | } |
| 4304 | result[v] = aug_p; |
| 4305 | LY_ARRAY_INCREMENT(result); |
| 4306 | } |
| 4307 | |
| 4308 | /** |
| 4309 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4310 | * |
| 4311 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4312 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4313 | * |
| 4314 | * @param[in] ctx Compile context. |
| 4315 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4316 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4317 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4318 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4319 | * @return LY_ERR value. |
| 4320 | */ |
| 4321 | LY_ERR |
| 4322 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4323 | { |
| 4324 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4325 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4326 | |
| 4327 | assert(augments); |
| 4328 | |
| 4329 | /* get count of the augments in module and all its submodules */ |
| 4330 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4331 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4332 | } |
| 4333 | LY_ARRAY_FOR(inc_p, u) { |
| 4334 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4335 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4336 | } |
| 4337 | } |
| 4338 | |
| 4339 | if (!count) { |
| 4340 | *augments = NULL; |
| 4341 | return LY_SUCCESS; |
| 4342 | } |
| 4343 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4344 | |
| 4345 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4346 | * together, so there can be even /z/y betwwen them. */ |
| 4347 | LY_ARRAY_FOR(aug_p, u) { |
| 4348 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4349 | } |
| 4350 | LY_ARRAY_FOR(inc_p, u) { |
| 4351 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4352 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4353 | } |
| 4354 | } |
| 4355 | |
| 4356 | *augments = result; |
| 4357 | return LY_SUCCESS; |
| 4358 | } |
| 4359 | |
| 4360 | /** |
| 4361 | * @brief Compile the parsed augment connecting it into its target. |
| 4362 | * |
| 4363 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4364 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4365 | * are already implemented and compiled. |
| 4366 | * |
| 4367 | * @param[in] ctx Compile context. |
| 4368 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4369 | * @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 |
| 4370 | * children in case of the augmenting uses data. |
| 4371 | * @return LY_SUCCESS on success. |
| 4372 | * @return LY_EVALID on failure. |
| 4373 | */ |
| 4374 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4375 | 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] | 4376 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4377 | LY_ERR ret = LY_SUCCESS, rc; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4378 | struct lysp_node *node_p, *case_node_p; |
| 4379 | struct lysc_node *target; /* target target of the augment */ |
| 4380 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4381 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4382 | struct lys_module **aug_mod; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4383 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4384 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4385 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4386 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4387 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4388 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4389 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4390 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4391 | 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] | 4392 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4393 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4394 | if (ret != LY_SUCCESS) { |
| 4395 | if (ret == LY_EDENIED) { |
| 4396 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4397 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4398 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4399 | } |
| 4400 | return LY_EVALID; |
| 4401 | } |
| 4402 | |
| 4403 | /* check for mandatory nodes |
| 4404 | * - new cases augmenting some choice can have mandatory nodes |
| 4405 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4406 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4407 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4408 | allow_mandatory = 1; |
| 4409 | } |
| 4410 | |
| 4411 | when_shared = NULL; |
| 4412 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4413 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4414 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4415 | && !((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] | 4416 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4417 | && !(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] | 4418 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4419 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4420 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4421 | return LY_EVALID; |
| 4422 | } |
| 4423 | |
| 4424 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4425 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4426 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4427 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4428 | } else { |
| 4429 | 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] | 4430 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4431 | } |
| 4432 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4433 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4434 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4435 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4436 | * 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] | 4437 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4438 | /* 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] | 4439 | 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] | 4440 | } else if (target->nodetype == LYS_CHOICE) { |
| 4441 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4442 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4443 | } else { |
| 4444 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4445 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4446 | if (!node) { |
| 4447 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4448 | break; |
| 4449 | } |
| 4450 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4451 | } |
| 4452 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4453 | 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] | 4454 | node->flags &= ~LYS_MAND_TRUE; |
| 4455 | lys_compile_mandatory_parents(target, 0); |
| 4456 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4457 | "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] | 4458 | return LY_EVALID; |
| 4459 | } |
| 4460 | |
| 4461 | /* pass augment's when to all the children */ |
| 4462 | if (aug_p->when) { |
| 4463 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4464 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4465 | 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] | 4466 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4467 | |
| 4468 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4469 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4470 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4471 | } |
| 4472 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4473 | when_shared = *when; |
| 4474 | } else { |
| 4475 | ++when_shared->refcount; |
| 4476 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4477 | |
| 4478 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4479 | /* 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] | 4480 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4481 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4482 | } |
| 4483 | } |
| 4484 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4485 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4486 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4487 | switch (target->nodetype) { |
| 4488 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4489 | 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] | 4490 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4491 | 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] | 4492 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4493 | break; |
| 4494 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4495 | 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] | 4496 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4497 | 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] | 4498 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4499 | break; |
| 4500 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4501 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4502 | if (aug_p->actions) { |
| 4503 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4504 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4505 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4506 | return LY_EVALID; |
| 4507 | } |
| 4508 | if (aug_p->notifs) { |
| 4509 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4510 | "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] | 4511 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4512 | return LY_EVALID; |
| 4513 | } |
| 4514 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4515 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4516 | /* add this module into the target module augmented_by, if not there already */ |
| 4517 | rc = LY_SUCCESS; |
| 4518 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4519 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4520 | rc = LY_EEXIST; |
| 4521 | break; |
| 4522 | } |
| 4523 | } |
| 4524 | if (!rc) { |
| 4525 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4526 | *aug_mod = ctx->mod; |
| 4527 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4528 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4529 | lysc_update_path(ctx, NULL, NULL); |
| 4530 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4531 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4532 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4533 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4534 | return ret; |
| 4535 | } |
| 4536 | |
| 4537 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4538 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4539 | * |
| 4540 | * @param[in] ctx Compile context. |
| 4541 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4542 | * @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] | 4543 | * @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] | 4544 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4545 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4546 | * 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] | 4547 | * @return LY_ERR value. |
| 4548 | */ |
| 4549 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4550 | 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] | 4551 | { |
| 4552 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4553 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4554 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4555 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4556 | return LY_EVALID; |
| 4557 | } |
| 4558 | |
| 4559 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4560 | /* check if node has default value */ |
| 4561 | if (node->nodetype & LYS_LEAF) { |
| 4562 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4563 | if (refine_flag) { |
| 4564 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4565 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4566 | return LY_EVALID; |
| 4567 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4568 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4569 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4570 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4571 | |
| 4572 | /* update the list of incomplete default values if needed */ |
| 4573 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4574 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4575 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4576 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4577 | free(leaf->dflt); |
| 4578 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4579 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4580 | } |
| 4581 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4582 | if (refine_flag) { |
| 4583 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4584 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4585 | return LY_EVALID; |
| 4586 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4587 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4588 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4589 | 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] | 4590 | return LY_EVALID; |
| 4591 | } |
| 4592 | |
| 4593 | node->flags &= ~LYS_MAND_FALSE; |
| 4594 | node->flags |= LYS_MAND_TRUE; |
| 4595 | lys_compile_mandatory_parents(node->parent, 1); |
| 4596 | } else { |
| 4597 | /* make mandatory false */ |
| 4598 | node->flags &= ~LYS_MAND_TRUE; |
| 4599 | node->flags |= LYS_MAND_FALSE; |
| 4600 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4601 | 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] | 4602 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4603 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4604 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4605 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4606 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4607 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4608 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4609 | } |
| 4610 | } |
| 4611 | return LY_SUCCESS; |
| 4612 | } |
| 4613 | |
| 4614 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4615 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4616 | * If present, also apply uses's modificators. |
| 4617 | * |
| 4618 | * @param[in] ctx Compile context |
| 4619 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4620 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4621 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4622 | * the compile context. |
| 4623 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4624 | */ |
| 4625 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4626 | 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] | 4627 | { |
| 4628 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4629 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4630 | /* 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] | 4631 | struct lysc_node_container context_node_fake = |
| 4632 | {.nodetype = LYS_CONTAINER, |
| 4633 | .module = ctx->mod, |
| 4634 | .flags = parent ? parent->flags : 0, |
| 4635 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4636 | .prev = (struct lysc_node*)&context_node_fake, |
| 4637 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4638 | struct lysp_grp *grp = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4639 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4640 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4641 | int found; |
| 4642 | const char *id, *name, *prefix; |
| 4643 | size_t prefix_len, name_len; |
| 4644 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4645 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4646 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4647 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4648 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4649 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4650 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4651 | struct lysp_augment **augments = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4652 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4653 | struct lysc_notif **notifs = NULL; |
| 4654 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4655 | |
| 4656 | /* search for the grouping definition */ |
| 4657 | found = 0; |
| 4658 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4659 | 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] | 4660 | if (prefix) { |
| 4661 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4662 | if (!mod) { |
| 4663 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4664 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4665 | return LY_EVALID; |
| 4666 | } |
| 4667 | } else { |
| 4668 | mod = ctx->mod_def; |
| 4669 | } |
| 4670 | if (mod == ctx->mod_def) { |
| 4671 | 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] | 4672 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4673 | LY_ARRAY_FOR(grp, u) { |
| 4674 | if (!strcmp(grp[u].name, name)) { |
| 4675 | grp = &grp[u]; |
| 4676 | found = 1; |
| 4677 | break; |
| 4678 | } |
| 4679 | } |
| 4680 | } |
| 4681 | } |
| 4682 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4683 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4684 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4685 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4686 | for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4687 | if (!strcmp(grp[u].name, name)) { |
| 4688 | grp = &grp[u]; |
| 4689 | found = 1; |
| 4690 | } |
| 4691 | } |
| 4692 | } |
| 4693 | if (!found && mod->parsed->includes) { |
| 4694 | /* ... and all the submodules */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4695 | for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4696 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4697 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4698 | for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4699 | if (!strcmp(grp[v].name, name)) { |
| 4700 | grp = &grp[v]; |
| 4701 | found = 1; |
| 4702 | } |
| 4703 | } |
| 4704 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4705 | } |
| 4706 | } |
| 4707 | } |
| 4708 | if (!found) { |
| 4709 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4710 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4711 | return LY_EVALID; |
| 4712 | } |
| 4713 | |
| 4714 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4715 | grp_stack_count = ctx->groupings.count; |
| 4716 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4717 | if (grp_stack_count == ctx->groupings.count) { |
| 4718 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4719 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4720 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4721 | return LY_EVALID; |
| 4722 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4723 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4724 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4725 | grp->flags |= LYS_USED_GRP; |
| 4726 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4727 | |
| 4728 | /* switch context's mod_def */ |
| 4729 | mod_old = ctx->mod_def; |
| 4730 | ctx->mod_def = mod; |
| 4731 | |
| 4732 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4733 | 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] | 4734 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4735 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4736 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4737 | /* 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] | 4738 | 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] | 4739 | |
| 4740 | /* some preparation for applying refines */ |
| 4741 | if (grp->data == node_p) { |
| 4742 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4743 | if (parent) { |
| 4744 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4745 | } else if (ctx->mod->compiled->data) { |
| 4746 | child = ctx->mod->compiled->data; |
| 4747 | } else { |
| 4748 | child = NULL; |
| 4749 | } |
| 4750 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4751 | } |
| 4752 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4753 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4754 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4755 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4756 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4757 | /* 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] | 4758 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4759 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4760 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4761 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 4762 | |
| 4763 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4764 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4765 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4766 | } |
| 4767 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4768 | when_shared = *when; |
| 4769 | } else { |
| 4770 | ++when_shared->refcount; |
| 4771 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4772 | |
| 4773 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4774 | /* 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] | 4775 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4776 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4777 | } |
| 4778 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4779 | } |
| 4780 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4781 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4782 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4783 | /* 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] | 4784 | 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] | 4785 | } |
| 4786 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4787 | /* compile actions */ |
| 4788 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4789 | if (actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4790 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4791 | 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] | 4792 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4793 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4794 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 4795 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 4796 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4797 | } |
| 4798 | } |
| 4799 | |
| 4800 | /* compile notifications */ |
| 4801 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4802 | if (notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4803 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4804 | 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] | 4805 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4806 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4807 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 4808 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 4809 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4810 | } |
| 4811 | } |
| 4812 | |
| 4813 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4814 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4815 | 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] | 4816 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4817 | 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] | 4818 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4819 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4820 | /* reload previous context's mod_def */ |
| 4821 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4822 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4823 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4824 | /* apply refine */ |
| 4825 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4826 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4827 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4828 | 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] | 4829 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4830 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4831 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4832 | |
| 4833 | /* default value */ |
| 4834 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4835 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4836 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4837 | "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.", |
| 4838 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4839 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4840 | } |
| 4841 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4842 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4843 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4844 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4845 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4846 | } |
| 4847 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4848 | struct ly_err_item *err = NULL; |
| 4849 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4850 | if (leaf->dflt) { |
| 4851 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4852 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4853 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4854 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4855 | } else { |
| 4856 | /* prepare a new one */ |
| 4857 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4858 | leaf->dflt->realtype = leaf->type; |
| 4859 | } |
| 4860 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4861 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4862 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4863 | 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] | 4864 | 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] | 4865 | leaf->dflt->realtype->refcount++; |
| 4866 | if (err) { |
| 4867 | ly_err_print(err); |
| 4868 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4869 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4870 | ly_err_free(err); |
| 4871 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4872 | if (rc == LY_EINCOMPLETE) { |
| 4873 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4874 | 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] | 4875 | |
| 4876 | /* but in general result is so far ok */ |
| 4877 | rc = LY_SUCCESS; |
| 4878 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4879 | LY_CHECK_GOTO(rc, cleanup); |
| 4880 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4881 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4882 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4883 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4884 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4885 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4886 | "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] | 4887 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4888 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4889 | |
| 4890 | /* remove previous set of default values */ |
| 4891 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4892 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4893 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 4894 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 4895 | free(llist->dflts[u]); |
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 | LY_ARRAY_FREE(llist->dflts); |
| 4898 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4899 | LY_ARRAY_FREE(llist->dflts_mods); |
| 4900 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4901 | |
| 4902 | /* create the new set of the default values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4903 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup); |
| 4904 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4905 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4906 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4907 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 4908 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4909 | LY_ARRAY_INCREMENT(llist->dflts); |
| 4910 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 4911 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4912 | 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] | 4913 | 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] | 4914 | 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] | 4915 | llist->dflts[u]->realtype->refcount++; |
| 4916 | if (err) { |
| 4917 | ly_err_print(err); |
| 4918 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4919 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 4920 | ly_err_free(err); |
| 4921 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4922 | if (rc == LY_EINCOMPLETE) { |
| 4923 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4924 | 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] | 4925 | |
| 4926 | /* but in general result is so far ok */ |
| 4927 | rc = LY_SUCCESS; |
| 4928 | } |
| 4929 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4930 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4931 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4932 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4933 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4934 | /* unset LYS_SET_DFLT from the current default case */ |
| 4935 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4936 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4937 | 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] | 4938 | } |
| 4939 | } |
| 4940 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4941 | /* description */ |
| 4942 | if (rfn->dsc) { |
| 4943 | FREE_STRING(ctx->ctx, node->dsc); |
| 4944 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4945 | } |
| 4946 | |
| 4947 | /* reference */ |
| 4948 | if (rfn->ref) { |
| 4949 | FREE_STRING(ctx->ctx, node->ref); |
| 4950 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4951 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4952 | |
| 4953 | /* config */ |
| 4954 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4955 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4956 | 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] | 4957 | } else { |
| 4958 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4959 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4960 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4961 | } |
| 4962 | |
| 4963 | /* mandatory */ |
| 4964 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4965 | 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] | 4966 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4967 | |
| 4968 | /* presence */ |
| 4969 | if (rfn->presence) { |
| 4970 | if (node->nodetype != LYS_CONTAINER) { |
| 4971 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4972 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4973 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4974 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4975 | } |
| 4976 | node->flags |= LYS_PRESENCE; |
| 4977 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4978 | |
| 4979 | /* must */ |
| 4980 | if (rfn->musts) { |
| 4981 | switch (node->nodetype) { |
| 4982 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4983 | 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] | 4984 | break; |
| 4985 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4986 | 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] | 4987 | break; |
| 4988 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4989 | 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] | 4990 | break; |
| 4991 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4992 | 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] | 4993 | break; |
| 4994 | case LYS_ANYXML: |
| 4995 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4996 | 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] | 4997 | break; |
| 4998 | default: |
| 4999 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5000 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5001 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5002 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5003 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5004 | ly_set_add(&ctx->xpath, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5005 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5006 | |
| 5007 | /* min/max-elements */ |
| 5008 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5009 | switch (node->nodetype) { |
| 5010 | case LYS_LEAFLIST: |
| 5011 | if (rfn->flags & LYS_SET_MAX) { |
| 5012 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5013 | } |
| 5014 | if (rfn->flags & LYS_SET_MIN) { |
| 5015 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5016 | if (rfn->min) { |
| 5017 | node->flags |= LYS_MAND_TRUE; |
| 5018 | lys_compile_mandatory_parents(node->parent, 1); |
| 5019 | } else { |
| 5020 | node->flags &= ~LYS_MAND_TRUE; |
| 5021 | lys_compile_mandatory_parents(node->parent, 0); |
| 5022 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5023 | } |
| 5024 | break; |
| 5025 | case LYS_LIST: |
| 5026 | if (rfn->flags & LYS_SET_MAX) { |
| 5027 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5028 | } |
| 5029 | if (rfn->flags & LYS_SET_MIN) { |
| 5030 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5031 | if (rfn->min) { |
| 5032 | node->flags |= LYS_MAND_TRUE; |
| 5033 | lys_compile_mandatory_parents(node->parent, 1); |
| 5034 | } else { |
| 5035 | node->flags &= ~LYS_MAND_TRUE; |
| 5036 | lys_compile_mandatory_parents(node->parent, 0); |
| 5037 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5038 | } |
| 5039 | break; |
| 5040 | default: |
| 5041 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5042 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5043 | (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] | 5044 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5045 | } |
| 5046 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5047 | |
| 5048 | /* if-feature */ |
| 5049 | if (rfn->iffeatures) { |
| 5050 | /* 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] | 5051 | 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] | 5052 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5053 | |
| 5054 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5055 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5056 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5057 | /* 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] | 5058 | for (uint32_t i = 0; i < refined.count; ++i) { |
| 5059 | node = (struct lysc_node*)refined.objs[i]; |
| 5060 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5061 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5062 | |
| 5063 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5064 | if ((node->flags & LYS_MAND_TRUE) && |
| 5065 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5066 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5067 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5068 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5069 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5073 | if (node->nodetype == LYS_LIST) { |
| 5074 | min = ((struct lysc_node_list*)node)->min; |
| 5075 | max = ((struct lysc_node_list*)node)->max; |
| 5076 | } else { |
| 5077 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5078 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5079 | } |
| 5080 | if (min > max) { |
| 5081 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5082 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5083 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5084 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5085 | } |
| 5086 | } |
| 5087 | } |
| 5088 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5089 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5090 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5091 | |
| 5092 | cleanup: |
| 5093 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5094 | if (context_node_fake.child) { |
| 5095 | context_node_fake.child->prev = child; |
| 5096 | } |
| 5097 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5098 | child->parent = parent; |
| 5099 | } |
| 5100 | |
| 5101 | if (uses_p->augments || uses_p->refines) { |
| 5102 | /* 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] | 5103 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5104 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5105 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5106 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5107 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5108 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5109 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5110 | } |
| 5111 | } |
| 5112 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5113 | /* reload previous context's mod_def */ |
| 5114 | ctx->mod_def = mod_old; |
| 5115 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5116 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5117 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5118 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5119 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5120 | |
| 5121 | return ret; |
| 5122 | } |
| 5123 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5124 | static int |
| 5125 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5126 | { |
| 5127 | struct lysp_node *iter; |
| 5128 | int len = 0; |
| 5129 | |
| 5130 | *path = NULL; |
| 5131 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5132 | char *s = *path; |
| 5133 | char *id; |
| 5134 | |
| 5135 | switch (iter->nodetype) { |
| 5136 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5137 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5138 | break; |
| 5139 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5140 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5141 | break; |
| 5142 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5143 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5144 | break; |
| 5145 | default: |
| 5146 | id = strdup(iter->name); |
| 5147 | break; |
| 5148 | } |
| 5149 | |
| 5150 | if (!iter->parent) { |
| 5151 | /* print prefix */ |
| 5152 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5153 | } else { |
| 5154 | /* prefix is the same as in parent */ |
| 5155 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5156 | } |
| 5157 | free(s); |
| 5158 | free(id); |
| 5159 | } |
| 5160 | |
| 5161 | if (len < 0) { |
| 5162 | free(*path); |
| 5163 | *path = NULL; |
| 5164 | } else if (len == 0) { |
| 5165 | *path = strdup("/"); |
| 5166 | len = 1; |
| 5167 | } |
| 5168 | return len; |
| 5169 | } |
| 5170 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5171 | /** |
| 5172 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5173 | * |
| 5174 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5175 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5176 | */ |
| 5177 | static LY_ERR |
| 5178 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5179 | { |
| 5180 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5181 | char *path; |
| 5182 | int len; |
| 5183 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5184 | struct lysp_node_uses fake_uses = { |
| 5185 | .parent = node_p, |
| 5186 | .nodetype = LYS_USES, |
| 5187 | .flags = 0, .next = NULL, |
| 5188 | .name = grp->name, |
| 5189 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5190 | .refines = NULL, .augments = NULL |
| 5191 | }; |
| 5192 | struct lysc_node_container fake_container = { |
| 5193 | .nodetype = LYS_CONTAINER, |
| 5194 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5195 | .module = ctx->mod, |
| 5196 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5197 | .prev = (struct lysc_node*)&fake_container, |
| 5198 | .name = "fake", |
| 5199 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5200 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5201 | }; |
| 5202 | |
| 5203 | if (grp->parent) { |
| 5204 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5205 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5206 | |
| 5207 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5208 | if (len < 0) { |
| 5209 | LOGMEM(ctx->ctx); |
| 5210 | return LY_EMEM; |
| 5211 | } |
| 5212 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5213 | ctx->path_len = (uint16_t)len; |
| 5214 | free(path); |
| 5215 | |
| 5216 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5217 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5218 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5219 | lysc_update_path(ctx, NULL, NULL); |
| 5220 | lysc_update_path(ctx, NULL, NULL); |
| 5221 | |
| 5222 | ctx->path_len = 1; |
| 5223 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5224 | |
| 5225 | /* cleanup */ |
| 5226 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5227 | |
| 5228 | return ret; |
| 5229 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5230 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5231 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5232 | * @brief Compile parsed schema node information. |
| 5233 | * @param[in] ctx Compile context |
| 5234 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5235 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5236 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5237 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5238 | * @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). |
| 5239 | * 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] | 5240 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5241 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5242 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5243 | 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] | 5244 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5245 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5246 | struct lysc_node *node; |
| 5247 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5248 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5249 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5250 | 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] | 5251 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5252 | if (node_p->nodetype != LYS_USES) { |
| 5253 | lysc_update_path(ctx, parent, node_p->name); |
| 5254 | } else { |
| 5255 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5256 | lysc_update_path(ctx, NULL, node_p->name); |
| 5257 | } |
| 5258 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5259 | switch (node_p->nodetype) { |
| 5260 | case LYS_CONTAINER: |
| 5261 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5262 | node_compile_spec = lys_compile_node_container; |
| 5263 | break; |
| 5264 | case LYS_LEAF: |
| 5265 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5266 | node_compile_spec = lys_compile_node_leaf; |
| 5267 | break; |
| 5268 | case LYS_LIST: |
| 5269 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5270 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5271 | break; |
| 5272 | case LYS_LEAFLIST: |
| 5273 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5274 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5275 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5276 | case LYS_CHOICE: |
| 5277 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5278 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5279 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5280 | case LYS_ANYXML: |
| 5281 | case LYS_ANYDATA: |
| 5282 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5283 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5284 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5285 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5286 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5287 | lysc_update_path(ctx, NULL, NULL); |
| 5288 | lysc_update_path(ctx, NULL, NULL); |
| 5289 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5290 | default: |
| 5291 | LOGINT(ctx->ctx); |
| 5292 | return LY_EINT; |
| 5293 | } |
| 5294 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5295 | node->nodetype = node_p->nodetype; |
| 5296 | node->module = ctx->mod; |
| 5297 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5298 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5299 | |
| 5300 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5301 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5302 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5303 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5304 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5305 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5306 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5307 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5308 | node->flags |= LYS_CONFIG_R; |
| 5309 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5310 | /* config not explicitely set, inherit it from parent */ |
| 5311 | if (parent) { |
| 5312 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5313 | } else { |
| 5314 | /* default is config true */ |
| 5315 | node->flags |= LYS_CONFIG_W; |
| 5316 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5317 | } else { |
| 5318 | /* config set explicitely */ |
| 5319 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5320 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5321 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5322 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5323 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5324 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5325 | goto error; |
| 5326 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5327 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5328 | /* *list ordering */ |
| 5329 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5330 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5331 | 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] | 5332 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5333 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5334 | node->flags &= ~LYS_ORDBY_MASK; |
| 5335 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5336 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5337 | /* default ordering is system */ |
| 5338 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5339 | } |
| 5340 | } |
| 5341 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5342 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5343 | * 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] | 5344 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5345 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5346 | * 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] | 5347 | 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] | 5348 | } |
| 5349 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5350 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5351 | node->sp = node_p; |
| 5352 | } |
| 5353 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5354 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5355 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5356 | if (node_p->when) { |
| 5357 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5358 | 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] | 5359 | |
| 5360 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5361 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5362 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5363 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5364 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5365 | 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] | 5366 | |
| 5367 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5368 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5369 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5370 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5371 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5372 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5373 | if (node->flags & LYS_MAND_TRUE) { |
| 5374 | lys_compile_mandatory_parents(parent, 1); |
| 5375 | } |
| 5376 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5377 | lysc_update_path(ctx, NULL, NULL); |
| 5378 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5379 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5380 | if (parent) { |
| 5381 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5382 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5383 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5384 | } else { |
| 5385 | lysc_update_path(ctx, parent, node->name); |
| 5386 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5387 | 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] | 5388 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5389 | if (uses_status) { |
| 5390 | |
| 5391 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5392 | /* 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] | 5393 | * it directly gets the same status flags as the choice; |
| 5394 | * 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] | 5395 | 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] | 5396 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5397 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5398 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5399 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5400 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5401 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5402 | 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] | 5403 | |
| 5404 | if (parent->nodetype == LYS_CHOICE) { |
| 5405 | lysc_update_path(ctx, NULL, NULL); |
| 5406 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5407 | } else { |
| 5408 | /* top-level element */ |
| 5409 | if (!ctx->mod->compiled->data) { |
| 5410 | ctx->mod->compiled->data = node; |
| 5411 | } else { |
| 5412 | /* insert at the end of the module's top-level nodes list */ |
| 5413 | ctx->mod->compiled->data->prev->next = node; |
| 5414 | node->prev = ctx->mod->compiled->data->prev; |
| 5415 | ctx->mod->compiled->data->prev = node; |
| 5416 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5417 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5418 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5419 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5420 | return LY_EVALID; |
| 5421 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5422 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5423 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5424 | |
| 5425 | return LY_SUCCESS; |
| 5426 | |
| 5427 | error: |
| 5428 | lysc_node_free(ctx->ctx, node); |
| 5429 | return ret; |
| 5430 | } |
| 5431 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5432 | static void |
| 5433 | lysc_disconnect(struct lysc_node *node) |
| 5434 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5435 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5436 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5437 | int remove_cs = 0; |
| 5438 | |
| 5439 | parent = node->parent; |
| 5440 | |
| 5441 | /* parent's first child */ |
| 5442 | if (!parent) { |
| 5443 | return; |
| 5444 | } |
| 5445 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5446 | cs = (struct lysc_node_case*)node; |
| 5447 | } else if (parent->nodetype == LYS_CASE) { |
| 5448 | /* disconnecting some node in a case */ |
| 5449 | cs = (struct lysc_node_case*)parent; |
| 5450 | parent = cs->parent; |
| 5451 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5452 | if (child == node) { |
| 5453 | if (cs->child == child) { |
| 5454 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5455 | /* case with a single child -> remove also the case */ |
| 5456 | child->parent = NULL; |
| 5457 | remove_cs = 1; |
| 5458 | } else { |
| 5459 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5460 | } |
| 5461 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5462 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5463 | } |
| 5464 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5465 | if (!remove_cs) { |
| 5466 | cs = NULL; |
| 5467 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5468 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5469 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5470 | } |
| 5471 | |
| 5472 | if (cs) { |
| 5473 | if (remove_cs) { |
| 5474 | /* cs has only one child which is being also removed */ |
| 5475 | lysc_disconnect((struct lysc_node*)cs); |
| 5476 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5477 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5478 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5479 | /* default case removed */ |
| 5480 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5481 | } |
| 5482 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5483 | /* first case removed */ |
| 5484 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5485 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5486 | if (cs->child) { |
| 5487 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5488 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5489 | prev = cs->child->prev; |
| 5490 | } /* else all the children are under a single case */ |
| 5491 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5492 | if (child->parent != (struct lysc_node*)cs) { |
| 5493 | break; |
| 5494 | } |
| 5495 | lysc_node_free(node->module->ctx, child); |
| 5496 | } |
| 5497 | if (prev) { |
| 5498 | if (prev->next) { |
| 5499 | prev->next = child; |
| 5500 | } |
| 5501 | if (child) { |
| 5502 | child->prev = prev; |
| 5503 | } else { |
| 5504 | /* link from the first child under the cases */ |
| 5505 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5506 | } |
| 5507 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5508 | } |
| 5509 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5510 | } |
| 5511 | |
| 5512 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5513 | if (node->prev->next) { |
| 5514 | node->prev->next = node->next; |
| 5515 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5516 | if (node->next) { |
| 5517 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5518 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5519 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5520 | if (child) { |
| 5521 | child->prev = node->prev; |
| 5522 | } |
| 5523 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5524 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5525 | } |
| 5526 | } |
| 5527 | |
| 5528 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5529 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5530 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5531 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5532 | struct ly_set devs_p = {0}; |
| 5533 | struct ly_set targets = {0}; |
| 5534 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5535 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5536 | struct lysc_action *rpcs; |
| 5537 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5538 | struct lysp_deviation *dev; |
| 5539 | struct lysp_deviate *d, **dp_new; |
| 5540 | struct lysp_deviate_add *d_add; |
| 5541 | struct lysp_deviate_del *d_del; |
| 5542 | struct lysp_deviate_rpl *d_rpl; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5543 | LY_ARRAY_COUNT_TYPE u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5544 | struct lysc_deviation { |
| 5545 | const char *nodeid; |
| 5546 | struct lysc_node *target; /* target node of the deviation */ |
| 5547 | 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] | 5548 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5549 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5550 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5551 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5552 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5553 | const char *prefix, *name, *nodeid, *dflt; |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 5554 | struct lys_module *mod, **dev_mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5555 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5556 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5557 | |
| 5558 | /* get all deviations from the module and all its submodules ... */ |
| 5559 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5560 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5561 | } |
| 5562 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5563 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5564 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5565 | } |
| 5566 | } |
| 5567 | if (!devs_p.count) { |
| 5568 | /* nothing to do */ |
| 5569 | return LY_SUCCESS; |
| 5570 | } |
| 5571 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5572 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5573 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5574 | /* ... and group them by the target node */ |
| 5575 | devs = calloc(devs_p.count, sizeof *devs); |
| 5576 | for (u = 0; u < devs_p.count; ++u) { |
| 5577 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5578 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5579 | |
| 5580 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5581 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5582 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5583 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5584 | /* move the target pointer to input/output to make them different from the action and |
| 5585 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5586 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5587 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5588 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5589 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5590 | flags |= LYSC_OPT_INTERNAL; |
| 5591 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5592 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5593 | flags |= LYSC_OPT_INTERNAL; |
| 5594 | } |
| 5595 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5596 | /* insert into the set of targets with duplicity detection */ |
| 5597 | i = ly_set_add(&targets, target, 0); |
| 5598 | if (!devs[i]) { |
| 5599 | /* new record */ |
| 5600 | devs[i] = calloc(1, sizeof **devs); |
| 5601 | devs[i]->target = target; |
| 5602 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5603 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5604 | } |
| 5605 | /* add deviates into the deviation's list of deviates */ |
| 5606 | for (d = dev->deviates; d; d = d->next) { |
| 5607 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5608 | *dp_new = d; |
| 5609 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5610 | devs[i]->not_supported = 1; |
| 5611 | } |
| 5612 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5613 | |
| 5614 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5615 | } |
| 5616 | |
| 5617 | /* MACROS for deviates checking */ |
| 5618 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5619 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5620 | 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] | 5621 | goto cleanup; \ |
| 5622 | } |
| 5623 | |
| 5624 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5625 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5626 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5627 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5628 | goto cleanup; \ |
| 5629 | } |
| 5630 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5631 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5632 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5633 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5634 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5635 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5636 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5637 | goto cleanup; \ |
| 5638 | } |
| 5639 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5640 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5641 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5642 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5643 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5644 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
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 | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5646 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5647 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5648 | goto cleanup; \ |
| 5649 | } |
| 5650 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5651 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5652 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5653 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5654 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5655 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5656 | goto cleanup; \ |
| 5657 | } |
| 5658 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5659 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5660 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5661 | 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] | 5662 | goto cleanup; \ |
| 5663 | } |
| 5664 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5665 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5666 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5667 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5668 | "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] | 5669 | goto cleanup; \ |
| 5670 | } |
| 5671 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5672 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5673 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5674 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5675 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5676 | 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] | 5677 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5678 | if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5679 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5680 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5681 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5682 | goto cleanup; \ |
| 5683 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5684 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5685 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5686 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5687 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5688 | (LY_ARRAY_COUNT(((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] | 5689 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5690 | if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5691 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5692 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | /* apply deviations */ |
| 5696 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5697 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5698 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5699 | struct ly_err_item *err = NULL; |
| 5700 | |
| 5701 | dflt = NULL; |
| 5702 | changed_type = 0; |
| 5703 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5704 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5705 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5706 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5707 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5708 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5709 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5710 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5711 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5712 | } |
| 5713 | } |
| 5714 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5715 | /* not-supported */ |
| 5716 | if (devs[u]->not_supported) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5717 | if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) { |
| 5718 | LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.", |
| 5719 | LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5720 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5721 | |
| 5722 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5723 | if (devs[u]->target->parent) { \ |
| 5724 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5725 | } else { \ |
| 5726 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5727 | } \ |
| 5728 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5729 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5730 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5731 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5732 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5733 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5734 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5735 | } |
| 5736 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5737 | if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5738 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5739 | /* remove RPC's/action's input */ |
| 5740 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5741 | 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] | 5742 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5743 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5744 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5745 | /* remove RPC's/action's output */ |
| 5746 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5747 | 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] | 5748 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5749 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5750 | } else { |
| 5751 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5752 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5753 | } |
| 5754 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5755 | /* remove Notification */ |
| 5756 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5757 | } else { |
| 5758 | /* remove the target node */ |
| 5759 | lysc_disconnect(devs[u]->target); |
| 5760 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5761 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5762 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5763 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5764 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5765 | continue; |
| 5766 | } |
| 5767 | |
| 5768 | /* list of deviates (not-supported is not present in the list) */ |
| 5769 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5770 | d = devs[u]->deviates[v]; |
| 5771 | |
| 5772 | switch (d->mod) { |
| 5773 | case LYS_DEV_ADD: |
| 5774 | d_add = (struct lysp_deviate_add*)d; |
| 5775 | /* [units-stmt] */ |
| 5776 | if (d_add->units) { |
| 5777 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5778 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5779 | |
| 5780 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5781 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5782 | } |
| 5783 | |
| 5784 | /* *must-stmt */ |
| 5785 | if (d_add->musts) { |
| 5786 | switch (devs[u]->target->nodetype) { |
| 5787 | case LYS_CONTAINER: |
| 5788 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5789 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 5790 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5791 | break; |
| 5792 | case LYS_LEAF: |
| 5793 | case LYS_LEAFLIST: |
| 5794 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5795 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 5796 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5797 | break; |
| 5798 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5799 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 5800 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5801 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5802 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5803 | case LYS_ACTION: |
| 5804 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5805 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 5806 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5807 | break; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 5808 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5809 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 5810 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5811 | break; |
| 5812 | } |
| 5813 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5814 | default: |
| 5815 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5816 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5817 | goto cleanup; |
| 5818 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5819 | ly_set_add(&ctx->xpath, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5820 | } |
| 5821 | |
| 5822 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5823 | if (d_add->uniques) { |
| 5824 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5825 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5826 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5827 | |
| 5828 | /* *default-stmt */ |
| 5829 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5830 | switch (devs[u]->target->nodetype) { |
| 5831 | case LYS_LEAF: |
| 5832 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5833 | 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] | 5834 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5835 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5836 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5837 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5838 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5839 | } else { |
| 5840 | /* prepare new default value storage */ |
| 5841 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5842 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5843 | dflt = d_add->dflts[0]; |
| 5844 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5845 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5846 | /* mark the new default values as leaf's own */ |
| 5847 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5848 | break; |
| 5849 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5850 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5851 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5852 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5853 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5854 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5855 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5856 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5857 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5858 | LY_ARRAY_FREE(llist->dflts); |
| 5859 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5860 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5861 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5862 | } |
| 5863 | /* add new default value(s) */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5864 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup); |
| 5865 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup); |
| 5866 | for (x = y = LY_ARRAY_COUNT(llist->dflts); |
| 5867 | x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5868 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5869 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5870 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5871 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5872 | llist->dflts[x]->realtype = llist->type; |
| 5873 | 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] | 5874 | 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] | 5875 | (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] | 5876 | llist->dflts[x]->realtype->refcount++; |
| 5877 | if (err) { |
| 5878 | ly_err_print(err); |
| 5879 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5880 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5881 | d_add->dflts[x - y], err->msg); |
| 5882 | ly_err_free(err); |
| 5883 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5884 | if (rc == LY_EINCOMPLETE) { |
| 5885 | /* postpone default compilation when the tree is complete */ |
| 5886 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5887 | |
| 5888 | /* but in general result is so far ok */ |
| 5889 | rc = LY_SUCCESS; |
| 5890 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5891 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5892 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5893 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5894 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5895 | break; |
| 5896 | case LYS_CHOICE: |
| 5897 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5898 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5899 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5900 | * 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] | 5901 | 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] | 5902 | goto cleanup; |
| 5903 | } |
| 5904 | break; |
| 5905 | default: |
| 5906 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5907 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5908 | goto cleanup; |
| 5909 | } |
| 5910 | } |
| 5911 | |
| 5912 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5913 | if (d_add->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5914 | 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] | 5915 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5916 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5917 | goto cleanup; |
| 5918 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5919 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5920 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5921 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5922 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5923 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5924 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5925 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5926 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5927 | goto cleanup; |
| 5928 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5929 | 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] | 5930 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5931 | |
| 5932 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5933 | if (d_add->flags & LYS_MAND_MASK) { |
| 5934 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5935 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5936 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5937 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5938 | goto cleanup; |
| 5939 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5940 | 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] | 5941 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5942 | |
| 5943 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5944 | if (d_add->flags & LYS_SET_MIN) { |
| 5945 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5946 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5947 | /* change value */ |
| 5948 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5949 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5950 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5951 | /* change value */ |
| 5952 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5953 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5954 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5955 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5956 | goto cleanup; |
| 5957 | } |
| 5958 | if (d_add->min) { |
| 5959 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5960 | } |
| 5961 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5962 | |
| 5963 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5964 | if (d_add->flags & LYS_SET_MAX) { |
| 5965 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5966 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5967 | /* change value */ |
| 5968 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5969 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5970 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5971 | /* change value */ |
| 5972 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5973 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5974 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5975 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 5976 | goto cleanup; |
| 5977 | } |
| 5978 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5979 | |
| 5980 | break; |
| 5981 | case LYS_DEV_DELETE: |
| 5982 | d_del = (struct lysp_deviate_del*)d; |
| 5983 | |
| 5984 | /* [units-stmt] */ |
| 5985 | if (d_del->units) { |
| 5986 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5987 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 5988 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 5989 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5990 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5991 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5992 | goto cleanup; |
| 5993 | } |
| 5994 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5995 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5996 | } |
| 5997 | |
| 5998 | /* *must-stmt */ |
| 5999 | if (d_del->musts) { |
| 6000 | switch (devs[u]->target->nodetype) { |
| 6001 | case LYS_CONTAINER: |
| 6002 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6003 | 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] | 6004 | break; |
| 6005 | case LYS_LEAF: |
| 6006 | case LYS_LEAFLIST: |
| 6007 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6008 | 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] | 6009 | break; |
| 6010 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6011 | 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] | 6012 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6013 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6014 | case LYS_ACTION: |
| 6015 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6016 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6017 | break; |
| 6018 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6019 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6020 | break; |
| 6021 | } |
| 6022 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6023 | default: |
| 6024 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6025 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6026 | goto cleanup; |
| 6027 | } |
| 6028 | } |
| 6029 | |
| 6030 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6031 | if (d_del->uniques) { |
| 6032 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6033 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6034 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6035 | LY_ARRAY_FOR(list->uniques, z) { |
| 6036 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6037 | nodeid = strpbrk(name, " \t\n"); |
| 6038 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 6039 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6040 | break; |
| 6041 | } |
| 6042 | while (isspace(*nodeid)) { |
| 6043 | ++nodeid; |
| 6044 | } |
| 6045 | } else { |
| 6046 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6047 | break; |
| 6048 | } |
| 6049 | } |
| 6050 | } |
| 6051 | if (!name) { |
| 6052 | /* complete match - remove the unique */ |
| 6053 | LY_ARRAY_DECREMENT(list->uniques); |
| 6054 | LY_ARRAY_FREE(list->uniques[z]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6055 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques)); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6056 | --z; |
| 6057 | break; |
| 6058 | } |
| 6059 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6060 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6061 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6062 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6063 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6064 | goto cleanup; |
| 6065 | } |
| 6066 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6067 | if (!LY_ARRAY_COUNT(list->uniques)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6068 | LY_ARRAY_FREE(list->uniques); |
| 6069 | list->uniques = NULL; |
| 6070 | } |
| 6071 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6072 | |
| 6073 | /* *default-stmt */ |
| 6074 | if (d_del->dflts) { |
| 6075 | switch (devs[u]->target->nodetype) { |
| 6076 | case LYS_LEAF: |
| 6077 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6078 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6079 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6080 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6081 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6082 | 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] | 6083 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6084 | if (i) { |
| 6085 | free((char*)dflt); |
| 6086 | } |
| 6087 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6088 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6089 | d_del->dflts[0], dflt); |
| 6090 | goto cleanup; |
| 6091 | } |
| 6092 | if (i) { |
| 6093 | free((char*)dflt); |
| 6094 | } |
| 6095 | dflt = NULL; |
| 6096 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6097 | /* update the list of incomplete default values if needed */ |
| 6098 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6099 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6100 | /* remove the default specification */ |
| 6101 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6102 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6103 | free(leaf->dflt); |
| 6104 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6105 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6106 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6107 | break; |
| 6108 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6109 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6110 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6111 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6112 | 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] | 6113 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6114 | if (i) { |
| 6115 | free((char*)dflt); |
| 6116 | } |
| 6117 | dflt = NULL; |
| 6118 | break; |
| 6119 | } |
| 6120 | if (i) { |
| 6121 | free((char*)dflt); |
| 6122 | } |
| 6123 | dflt = NULL; |
| 6124 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6125 | if (y == LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6127 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6128 | goto cleanup; |
| 6129 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6130 | |
| 6131 | /* update the list of incomplete default values if needed */ |
| 6132 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6133 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6134 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6135 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6136 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6137 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6138 | free(llist->dflts[y]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6139 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 6140 | memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_COUNT(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods)); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6141 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6142 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6143 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6144 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6145 | LY_ARRAY_FREE(llist->dflts); |
| 6146 | llist->dflts = NULL; |
| 6147 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6148 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6149 | break; |
| 6150 | case LYS_CHOICE: |
| 6151 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6152 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6153 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6154 | 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] | 6155 | if (prefix) { |
| 6156 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6157 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6158 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6159 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6160 | "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] | 6161 | goto cleanup; |
| 6162 | } |
| 6163 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6164 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6165 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6166 | "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] | 6167 | goto cleanup; |
| 6168 | } |
| 6169 | } |
| 6170 | /* else { |
| 6171 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6172 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6173 | * the module of the default case, just matching its name */ |
| 6174 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6175 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6176 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6177 | 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] | 6178 | goto cleanup; |
| 6179 | } |
| 6180 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6181 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6182 | break; |
| 6183 | default: |
| 6184 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6185 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6186 | goto cleanup; |
| 6187 | } |
| 6188 | } |
| 6189 | |
| 6190 | break; |
| 6191 | case LYS_DEV_REPLACE: |
| 6192 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6193 | |
| 6194 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6195 | if (d_rpl->type) { |
| 6196 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6197 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6198 | 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] | 6199 | |
| 6200 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6201 | /* the target has default from the previous type - remove it */ |
| 6202 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6203 | /* update the list of incomplete default values if needed */ |
| 6204 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6205 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6206 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6207 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6208 | free(leaf->dflt); |
| 6209 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6210 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6211 | } else { /* LYS_LEAFLIST */ |
| 6212 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6213 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6214 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6215 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6216 | free(llist->dflts[x]); |
| 6217 | } |
| 6218 | LY_ARRAY_FREE(llist->dflts); |
| 6219 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6220 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6221 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6222 | } |
| 6223 | } |
| 6224 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6225 | /* there is no default value, do not set changed_type after type compilation |
| 6226 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6227 | changed_type = -1; |
| 6228 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6229 | 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] | 6230 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6231 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6232 | |
| 6233 | /* [units-stmt] */ |
| 6234 | if (d_rpl->units) { |
| 6235 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6236 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6237 | units, "replacing", "units", d_rpl->units); |
| 6238 | |
| 6239 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6240 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6241 | } |
| 6242 | |
| 6243 | /* [default-stmt] */ |
| 6244 | if (d_rpl->dflt) { |
| 6245 | switch (devs[u]->target->nodetype) { |
| 6246 | case LYS_LEAF: |
| 6247 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6248 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6249 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6250 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6251 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6252 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6253 | dflt = d_rpl->dflt; |
| 6254 | /* 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] | 6255 | break; |
| 6256 | case LYS_CHOICE: |
| 6257 | 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] | 6258 | 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] | 6259 | goto cleanup; |
| 6260 | } |
| 6261 | break; |
| 6262 | default: |
| 6263 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6264 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6265 | goto cleanup; |
| 6266 | } |
| 6267 | } |
| 6268 | |
| 6269 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6270 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6271 | 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] | 6272 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6273 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6274 | goto cleanup; |
| 6275 | } |
| 6276 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6277 | 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] | 6278 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6279 | goto cleanup; |
| 6280 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6281 | 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] | 6282 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6283 | |
| 6284 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6285 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6286 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6287 | 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] | 6288 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6289 | goto cleanup; |
| 6290 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6291 | 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] | 6292 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6293 | |
| 6294 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6295 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6296 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6297 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6298 | /* change value */ |
| 6299 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6300 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6301 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6302 | /* change value */ |
| 6303 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6304 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6305 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6306 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6307 | goto cleanup; |
| 6308 | } |
| 6309 | if (d_rpl->min) { |
| 6310 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6311 | } |
| 6312 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6313 | |
| 6314 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6315 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6316 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6317 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6318 | /* change value */ |
| 6319 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6320 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6321 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6322 | /* change value */ |
| 6323 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6324 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6325 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6326 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6327 | goto cleanup; |
| 6328 | } |
| 6329 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6330 | |
| 6331 | break; |
| 6332 | default: |
| 6333 | LOGINT(ctx->ctx); |
| 6334 | goto cleanup; |
| 6335 | } |
| 6336 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6337 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6338 | /* final check when all deviations of a single target node are applied */ |
| 6339 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6340 | /* check min-max compatibility */ |
| 6341 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6342 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6343 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6344 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6345 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6346 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6347 | } else { |
| 6348 | min = max = 0; |
| 6349 | } |
| 6350 | if (min > max) { |
| 6351 | 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] | 6352 | "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] | 6353 | goto cleanup; |
| 6354 | } |
| 6355 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6356 | if (dflt) { |
| 6357 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6358 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6359 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6360 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6361 | 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] | 6362 | 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] | 6363 | leaf->dflt->realtype->refcount++; |
| 6364 | if (err) { |
| 6365 | ly_err_print(err); |
| 6366 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6367 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6368 | ly_err_free(err); |
| 6369 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6370 | if (rc == LY_EINCOMPLETE) { |
| 6371 | /* postpone default compilation when the tree is complete */ |
| 6372 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6373 | |
| 6374 | /* but in general result is so far ok */ |
| 6375 | rc = LY_SUCCESS; |
| 6376 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6377 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6378 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6379 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6380 | int dynamic; |
| 6381 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6382 | 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] | 6383 | |
| 6384 | /* update the list of incomplete default values if needed */ |
| 6385 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6386 | |
| 6387 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6388 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6389 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6390 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6391 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6392 | 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] | 6393 | 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] | 6394 | leaf->dflt->realtype->refcount++; |
| 6395 | if (err) { |
| 6396 | ly_err_print(err); |
| 6397 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6398 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6399 | ly_err_free(err); |
| 6400 | } |
| 6401 | if (dynamic) { |
| 6402 | free((void*)dflt); |
| 6403 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6404 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6405 | if (rc == LY_EINCOMPLETE) { |
| 6406 | /* postpone default compilation when the tree is complete */ |
| 6407 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6408 | |
| 6409 | /* but in general result is so far ok */ |
| 6410 | rc = LY_SUCCESS; |
| 6411 | } |
| 6412 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6413 | } else { /* LYS_LEAFLIST */ |
| 6414 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6415 | 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] | 6416 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6417 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6418 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6419 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6420 | 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] | 6421 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6422 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6423 | llist->dflts[x]->realtype->refcount++; |
| 6424 | if (err) { |
| 6425 | ly_err_print(err); |
| 6426 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6427 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6428 | dflt, err->msg); |
| 6429 | ly_err_free(err); |
| 6430 | } |
| 6431 | if (dynamic) { |
| 6432 | free((void*)dflt); |
| 6433 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6434 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6435 | if (rc == LY_EINCOMPLETE) { |
| 6436 | /* postpone default compilation when the tree is complete */ |
| 6437 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6438 | |
| 6439 | /* but in general result is so far ok */ |
| 6440 | rc = LY_SUCCESS; |
| 6441 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6442 | LY_CHECK_GOTO(rc, cleanup); |
| 6443 | } |
| 6444 | } |
| 6445 | } |
| 6446 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6447 | /* check mandatory - default compatibility */ |
| 6448 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6449 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6450 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6451 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6452 | "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] | 6453 | goto cleanup; |
| 6454 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6455 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6456 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6457 | 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] | 6458 | goto cleanup; |
| 6459 | } |
| 6460 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6461 | /* mandatory node under a default case */ |
| 6462 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6463 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6464 | 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] | 6465 | goto cleanup; |
| 6466 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6467 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6468 | /* add this module into the target module deviated_by, if not there already */ |
| 6469 | rc = LY_SUCCESS; |
| 6470 | LY_ARRAY_FOR(devs[u]->target->module->compiled->deviated_by, v) { |
| 6471 | if (devs[u]->target->module->compiled->deviated_by[v] == mod_p->mod) { |
| 6472 | rc = LY_EEXIST; |
| 6473 | break; |
| 6474 | } |
| 6475 | } |
| 6476 | if (!rc) { |
| 6477 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup); |
| 6478 | *dev_mod = mod_p->mod; |
| 6479 | } |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 6480 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6481 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6482 | } |
| 6483 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6484 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6485 | ret = LY_SUCCESS; |
| 6486 | |
| 6487 | cleanup: |
| 6488 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6489 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6490 | free(devs[u]); |
| 6491 | } |
| 6492 | free(devs); |
| 6493 | ly_set_erase(&targets, NULL); |
| 6494 | ly_set_erase(&devs_p, NULL); |
| 6495 | |
| 6496 | return ret; |
| 6497 | } |
| 6498 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6499 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6500 | * @brief Compile the given YANG submodule into the main module. |
| 6501 | * @param[in] ctx Compile context |
| 6502 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6503 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6504 | */ |
| 6505 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6506 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6507 | { |
| 6508 | unsigned int u; |
| 6509 | LY_ERR ret = LY_SUCCESS; |
| 6510 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6511 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6512 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6513 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6514 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6515 | if (!mainmod->mod->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6516 | /* features are compiled directly into the compiled module structure, |
| 6517 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6518 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6519 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6520 | LY_CHECK_GOTO(ret, error); |
| 6521 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6522 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6523 | if (!mainmod->mod->dis_identities) { |
| 6524 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities); |
| 6525 | LY_CHECK_GOTO(ret, error); |
| 6526 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6527 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6528 | /* data nodes */ |
| 6529 | LY_LIST_FOR(submod->data, node_p) { |
| 6530 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6531 | LY_CHECK_GOTO(ret, error); |
| 6532 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6533 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6534 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6535 | 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] | 6536 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6537 | error: |
| 6538 | return ret; |
| 6539 | } |
| 6540 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6541 | static void * |
| 6542 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6543 | { |
| 6544 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6545 | if (substmts[u].stmt == stmt) { |
| 6546 | return substmts[u].storage; |
| 6547 | } |
| 6548 | } |
| 6549 | return NULL; |
| 6550 | } |
| 6551 | |
| 6552 | LY_ERR |
| 6553 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6554 | { |
| 6555 | LY_ERR ret = LY_EVALID, r; |
| 6556 | unsigned int u; |
| 6557 | struct lysp_stmt *stmt; |
| 6558 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6559 | |
| 6560 | /* check for invalid substatements */ |
| 6561 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6562 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6563 | continue; |
| 6564 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6565 | for (u = 0; substmts[u].stmt; ++u) { |
| 6566 | if (substmts[u].stmt == stmt->kw) { |
| 6567 | break; |
| 6568 | } |
| 6569 | } |
| 6570 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6571 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6572 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6573 | goto cleanup; |
| 6574 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6575 | } |
| 6576 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6577 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6578 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6579 | /* keep order of the processing the same as the order in the defined substmts, |
| 6580 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6581 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6582 | int stmt_present = 0; |
| 6583 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6584 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6585 | if (substmts[u].stmt != stmt->kw) { |
| 6586 | continue; |
| 6587 | } |
| 6588 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6589 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6590 | if (substmts[u].storage) { |
| 6591 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6592 | case LY_STMT_STATUS: |
| 6593 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6594 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6595 | break; |
| 6596 | case LY_STMT_UNITS: { |
| 6597 | const char **units; |
| 6598 | |
| 6599 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6600 | /* single item */ |
| 6601 | if (*((const char **)substmts[u].storage)) { |
| 6602 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6603 | goto cleanup; |
| 6604 | } |
| 6605 | units = (const char **)substmts[u].storage; |
| 6606 | } else { |
| 6607 | /* sized array */ |
| 6608 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6609 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6610 | } |
| 6611 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6612 | break; |
| 6613 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6614 | case LY_STMT_TYPE: { |
| 6615 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6616 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6617 | |
| 6618 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6619 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6620 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6621 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6622 | goto cleanup; |
| 6623 | } |
| 6624 | compiled = substmts[u].storage; |
| 6625 | } else { |
| 6626 | /* sized array */ |
| 6627 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6628 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6629 | compiled = (void*)type; |
| 6630 | } |
| 6631 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6632 | 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] | 6633 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6634 | 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] | 6635 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6636 | lysp_type_free(ctx->ctx, parsed); |
| 6637 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6638 | break; |
| 6639 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6640 | case LY_STMT_IF_FEATURE: { |
| 6641 | struct lysc_iffeature *iff = NULL; |
| 6642 | |
| 6643 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6644 | /* single item */ |
| 6645 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6646 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6647 | goto cleanup; |
| 6648 | } |
| 6649 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6650 | } else { |
| 6651 | /* sized array */ |
| 6652 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6653 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6654 | } |
| 6655 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6656 | break; |
| 6657 | } |
| 6658 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6659 | * 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] | 6660 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6661 | 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.", |
| 6662 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6663 | goto cleanup; |
| 6664 | } |
| 6665 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6666 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6667 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6668 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6669 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6670 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6671 | goto cleanup; |
| 6672 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6673 | } |
| 6674 | |
| 6675 | ret = LY_SUCCESS; |
| 6676 | |
| 6677 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6678 | return ret; |
| 6679 | } |
| 6680 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6681 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6682 | * @brief Check when for cyclic dependencies. |
| 6683 | * @param[in] set Set with all the referenced nodes. |
| 6684 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6685 | * @return LY_ERR value |
| 6686 | */ |
| 6687 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6688 | 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] | 6689 | { |
| 6690 | struct lyxp_set tmp_set; |
| 6691 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6692 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6693 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6694 | int idx; |
| 6695 | struct lysc_when *when; |
| 6696 | LY_ERR ret = LY_SUCCESS; |
| 6697 | |
| 6698 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6699 | |
| 6700 | /* prepare in_ctx of the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6701 | for ( i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6702 | xp_scnode = &set->val.scnodes[i]; |
| 6703 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6704 | if (xp_scnode->in_ctx != -1) { |
| 6705 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6706 | xp_scnode->in_ctx = 1; |
| 6707 | } |
| 6708 | } |
| 6709 | |
| 6710 | for (i = 0; i < set->used; ++i) { |
| 6711 | xp_scnode = &set->val.scnodes[i]; |
| 6712 | if (xp_scnode->in_ctx != 1) { |
| 6713 | /* already checked */ |
| 6714 | continue; |
| 6715 | } |
| 6716 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6717 | 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] | 6718 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6719 | /* no when to check */ |
| 6720 | xp_scnode->in_ctx = 0; |
| 6721 | continue; |
| 6722 | } |
| 6723 | |
| 6724 | node = xp_scnode->scnode; |
| 6725 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6726 | LY_ARRAY_FOR(node->when, u) { |
| 6727 | when = node->when[u]; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 6728 | ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6729 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6730 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6731 | 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] | 6732 | goto cleanup; |
| 6733 | } |
| 6734 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6735 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6736 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6737 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6738 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6739 | 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] | 6740 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6741 | 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] | 6742 | ret = LY_EVALID; |
| 6743 | goto cleanup; |
| 6744 | } |
| 6745 | |
| 6746 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6747 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6748 | } else { |
| 6749 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6750 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6751 | } |
| 6752 | } |
| 6753 | |
| 6754 | /* merge this set into the global when set */ |
| 6755 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6756 | } |
| 6757 | |
| 6758 | /* check when of non-data parents as well */ |
| 6759 | node = node->parent; |
| 6760 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6761 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6762 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6763 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6764 | } |
| 6765 | |
| 6766 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6767 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6768 | return ret; |
| 6769 | } |
| 6770 | |
| 6771 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6772 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6773 | * @param[in] ctx Compile context. |
| 6774 | * @param[in] node Node to check. |
| 6775 | * @return LY_ERR value |
| 6776 | */ |
| 6777 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6778 | 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] | 6779 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6780 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6781 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6782 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6783 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6784 | struct lysc_when **when = NULL; |
| 6785 | struct lysc_must *musts = NULL; |
| 6786 | LY_ERR ret = LY_SUCCESS; |
| 6787 | |
| 6788 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6789 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6790 | |
| 6791 | switch (node->nodetype) { |
| 6792 | case LYS_CONTAINER: |
| 6793 | when = ((struct lysc_node_container *)node)->when; |
| 6794 | musts = ((struct lysc_node_container *)node)->musts; |
| 6795 | break; |
| 6796 | case LYS_CHOICE: |
| 6797 | when = ((struct lysc_node_choice *)node)->when; |
| 6798 | break; |
| 6799 | case LYS_LEAF: |
| 6800 | when = ((struct lysc_node_leaf *)node)->when; |
| 6801 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6802 | break; |
| 6803 | case LYS_LEAFLIST: |
| 6804 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6805 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6806 | break; |
| 6807 | case LYS_LIST: |
| 6808 | when = ((struct lysc_node_list *)node)->when; |
| 6809 | musts = ((struct lysc_node_list *)node)->musts; |
| 6810 | break; |
| 6811 | case LYS_ANYXML: |
| 6812 | case LYS_ANYDATA: |
| 6813 | when = ((struct lysc_node_anydata *)node)->when; |
| 6814 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6815 | break; |
| 6816 | case LYS_CASE: |
| 6817 | when = ((struct lysc_node_case *)node)->when; |
| 6818 | break; |
| 6819 | case LYS_NOTIF: |
| 6820 | musts = ((struct lysc_notif *)node)->musts; |
| 6821 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6822 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6823 | case LYS_ACTION: |
| 6824 | /* first process input musts */ |
| 6825 | musts = ((struct lysc_action *)node)->input.musts; |
| 6826 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6827 | default: |
| 6828 | /* nothing to check */ |
| 6829 | break; |
| 6830 | } |
| 6831 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6832 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6833 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6834 | 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] | 6835 | 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] | 6836 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6837 | 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] | 6838 | goto cleanup; |
| 6839 | } |
| 6840 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6841 | ctx->path[0] = '\0'; |
| 6842 | 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] | 6843 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6844 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6845 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6846 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6847 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6848 | /* 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] | 6849 | 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] | 6850 | schema->name); |
| 6851 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6852 | |
| 6853 | /* check dummy node accessing */ |
| 6854 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6855 | 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] | 6856 | ret = LY_EVALID; |
| 6857 | goto cleanup; |
| 6858 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6859 | } |
| 6860 | } |
| 6861 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6862 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6863 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6864 | LY_CHECK_GOTO(ret, cleanup); |
| 6865 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6866 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6867 | } |
| 6868 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6869 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6870 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6871 | LY_ARRAY_FOR(musts, u) { |
| 6872 | 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] | 6873 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6874 | 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] | 6875 | goto cleanup; |
| 6876 | } |
| 6877 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6878 | ctx->path[0] = '\0'; |
| 6879 | 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] | 6880 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6881 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6882 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6883 | /* 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] | 6884 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6885 | 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] | 6886 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6887 | } |
| 6888 | } |
| 6889 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6890 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6891 | } |
| 6892 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6893 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6894 | /* now check output musts */ |
| 6895 | input_done = 1; |
| 6896 | musts = ((struct lysc_action *)node)->output.musts; |
| 6897 | opts = LYXP_SCNODE_OUTPUT; |
| 6898 | goto check_musts; |
| 6899 | } |
| 6900 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6901 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6902 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6903 | return ret; |
| 6904 | } |
| 6905 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6906 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6907 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 6908 | { |
| 6909 | const struct lysc_node *target = NULL; |
| 6910 | struct ly_path *p; |
| 6911 | struct lysc_type *type; |
| 6912 | |
| 6913 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6914 | |
| 6915 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6916 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 6917 | lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, |
| 6918 | lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6919 | |
| 6920 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6921 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6922 | ly_path_free(node->module->ctx, p); |
| 6923 | |
| 6924 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6925 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6926 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 6927 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 6928 | return LY_EVALID; |
| 6929 | } |
| 6930 | |
| 6931 | /* check status */ |
| 6932 | ctx->path[0] = '\0'; |
| 6933 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6934 | ctx->path_len = strlen(ctx->path); |
| 6935 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 6936 | return LY_EVALID; |
| 6937 | } |
| 6938 | ctx->path_len = 1; |
| 6939 | ctx->path[1] = '\0'; |
| 6940 | |
| 6941 | /* check config */ |
| 6942 | if (lref->require_instance && (node->flags & LYS_CONFIG_W)) { |
| 6943 | if (target->flags & LYS_CONFIG_R) { |
| 6944 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 6945 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 6946 | return LY_EVALID; |
| 6947 | } |
| 6948 | } |
| 6949 | |
| 6950 | /* store the target's type and check for circular chain of leafrefs */ |
| 6951 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 6952 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 6953 | if (type == (struct lysc_type *)lref) { |
| 6954 | /* circular chain detected */ |
| 6955 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6956 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 6957 | return LY_EVALID; |
| 6958 | } |
| 6959 | } |
| 6960 | |
| 6961 | /* check if leafref and its target are under common if-features */ |
| 6962 | if (lys_compile_leafref_features_validate(node, target)) { |
| 6963 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6964 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 6965 | " features applicable to the leafref itself.", lref->path->expr); |
| 6966 | return LY_EVALID; |
| 6967 | } |
| 6968 | |
| 6969 | return LY_SUCCESS; |
| 6970 | } |
| 6971 | |
| 6972 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6973 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 6974 | { |
| 6975 | struct lysc_ext_instance *ext; |
| 6976 | struct lysp_ext_instance *ext_p = NULL; |
| 6977 | struct lysp_stmt *stmt; |
| 6978 | const struct lys_module *ext_mod; |
| 6979 | LY_ERR ret = LY_SUCCESS; |
| 6980 | |
| 6981 | /* create the parsed extension instance manually */ |
| 6982 | ext_p = calloc(1, sizeof *ext_p); |
| 6983 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 6984 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 6985 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 6986 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 6987 | ext_p->insubstmt_index = 0; |
| 6988 | |
| 6989 | stmt = calloc(1, sizeof *ext_p->child); |
| 6990 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 6991 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 6992 | stmt->kw = LY_STMT_TYPE; |
| 6993 | ext_p->child = stmt; |
| 6994 | |
| 6995 | /* allocate new extension instance */ |
| 6996 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 6997 | |
| 6998 | /* manually get extension definition module */ |
| 6999 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7000 | |
| 7001 | /* compile the extension instance */ |
| 7002 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7003 | |
| 7004 | cleanup: |
| 7005 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7006 | free(ext_p); |
| 7007 | return ret; |
| 7008 | } |
| 7009 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7010 | static LY_ERR |
| 7011 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7012 | { |
| 7013 | struct lysc_node *node; |
| 7014 | struct lysc_type *type, *typeiter; |
| 7015 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7016 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7017 | uint32_t i; |
| 7018 | |
| 7019 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7020 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7021 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7022 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7023 | node = ctx->leafrefs.objs[i]; |
| 7024 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7025 | type = ((struct lysc_node_leaf *)node)->type; |
| 7026 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7027 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7028 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7029 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7030 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7031 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7032 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7033 | } |
| 7034 | } |
| 7035 | } |
| 7036 | } |
| 7037 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7038 | /* store pointer to the real type */ |
| 7039 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7040 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7041 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7042 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7043 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7044 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
| 7045 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7046 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7047 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7048 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7049 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7050 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7051 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7052 | } |
| 7053 | } |
| 7054 | } |
| 7055 | } |
| 7056 | |
| 7057 | /* check xpath */ |
| 7058 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7059 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7060 | } |
| 7061 | |
| 7062 | /* finish incomplete default values compilation */ |
| 7063 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 7064 | struct ly_err_item *err = NULL; |
| 7065 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
| 7066 | LY_ERR ret; |
| 7067 | ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original), |
| 7068 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7069 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7070 | if (err) { |
| 7071 | ly_err_print(err); |
| 7072 | ctx->path[0] = '\0'; |
| 7073 | lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7074 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7075 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7076 | ly_err_free(err); |
| 7077 | } |
| 7078 | LY_CHECK_RET(ret); |
| 7079 | } |
| 7080 | |
| 7081 | return LY_SUCCESS; |
| 7082 | } |
| 7083 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7084 | LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7085 | lys_compile(struct lys_module **mod, int options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7086 | { |
| 7087 | struct lysc_ctx ctx = {0}; |
| 7088 | struct lysc_module *mod_c; |
| 7089 | struct lysp_module *sp; |
| 7090 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7091 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7092 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7093 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7094 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7095 | uint32_t i; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7096 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7097 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7098 | 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] | 7099 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7100 | if (!(*mod)->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7101 | /* just imported modules are not compiled */ |
| 7102 | return LY_SUCCESS; |
| 7103 | } |
| 7104 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7105 | sp = (*mod)->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7106 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7107 | ctx.ctx = (*mod)->ctx; |
| 7108 | ctx.mod = *mod; |
| 7109 | ctx.mod_def = *mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7110 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7111 | ctx.path_len = 1; |
| 7112 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7113 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7114 | (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7115 | LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM); |
| 7116 | mod_c->mod = *mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7117 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7118 | 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] | 7119 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7120 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7121 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7122 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7123 | |
| 7124 | /* features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7125 | if ((*mod)->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7126 | /* there is already precompiled array of features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7127 | mod_c->features = (*mod)->dis_features; |
| 7128 | (*mod)->dis_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7129 | } else { |
| 7130 | /* features are compiled directly into the compiled module structure, |
| 7131 | * 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] | 7132 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7133 | LY_CHECK_GOTO(ret, error); |
| 7134 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7135 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7136 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7137 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7138 | * are present. */ |
| 7139 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7140 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7141 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7142 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7143 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7144 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7145 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7146 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7147 | 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] | 7148 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7149 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7150 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7151 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7152 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7153 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7154 | /* identities, work similarly to features with the precompilation */ |
| 7155 | if ((*mod)->dis_identities) { |
| 7156 | mod_c->identities = (*mod)->dis_identities; |
| 7157 | (*mod)->dis_identities = NULL; |
| 7158 | } else { |
| 7159 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities); |
| 7160 | LY_CHECK_GOTO(ret, error); |
| 7161 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7162 | if (sp->identities) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7163 | 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] | 7164 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7165 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7166 | LY_ARRAY_FOR(sp->includes, v) { |
| 7167 | if (sp->includes[v].submodule->identities) { |
| 7168 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7169 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities); |
| 7170 | LY_CHECK_GOTO(ret, error); |
| 7171 | lysc_update_path(&ctx, NULL, NULL); |
| 7172 | } |
| 7173 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7174 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7175 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7176 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7177 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7178 | 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] | 7179 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7180 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7181 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7182 | 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] | 7183 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7184 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7185 | 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] | 7186 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7187 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7188 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7189 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7190 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7191 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7192 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7193 | /* extension instances TODO cover extension instances from submodules */ |
| 7194 | 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] | 7195 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7196 | /* finish compilation for all unresolved items in the context */ |
| 7197 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7198 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7199 | /* validate non-instantiated groupings from the parsed schema, |
| 7200 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7201 | ctx.options |= LYSC_OPT_GROUPING; |
| 7202 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7203 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7204 | 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] | 7205 | } |
| 7206 | } |
| 7207 | LY_LIST_FOR(sp->data, node_p) { |
| 7208 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7209 | LY_ARRAY_FOR(grps, u) { |
| 7210 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7211 | 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] | 7212 | } |
| 7213 | } |
| 7214 | } |
| 7215 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7216 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7217 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7218 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7219 | } |
| 7220 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7221 | #if 0 |
| 7222 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7223 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7224 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7225 | * the anotation definitions available in the internal schema structure. */ |
| 7226 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7227 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7228 | lys_free(mod, NULL, 1, 1); |
| 7229 | return NULL; |
| 7230 | } |
| 7231 | } |
| 7232 | #endif |
| 7233 | |
| 7234 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7235 | if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) { |
| 7236 | 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] | 7237 | } |
| 7238 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7239 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7240 | ly_set_erase(&ctx.xpath, NULL); |
| 7241 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7242 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7243 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7244 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7245 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7246 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7247 | lysp_module_free((*mod)->parsed); |
| 7248 | (*mod)->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7249 | } |
| 7250 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7251 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7252 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7253 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7254 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7255 | if (m->implemented == 2) { |
| 7256 | m->implemented = 1; |
| 7257 | } |
| 7258 | } |
| 7259 | } |
| 7260 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7261 | (*mod)->compiled = mod_c; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7262 | return LY_SUCCESS; |
| 7263 | |
| 7264 | error: |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7265 | lys_feature_precompile_revert(&ctx, *mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7266 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7267 | ly_set_erase(&ctx.xpath, NULL); |
| 7268 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7269 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7270 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7271 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7272 | lysc_module_free(mod_c, NULL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7273 | (*mod)->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7274 | |
| 7275 | /* revert compilation of modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7276 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7277 | m = ctx.ctx->list.objs[i]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7278 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7279 | /* revert features list to the precompiled state */ |
| 7280 | lys_feature_precompile_revert(&ctx, m); |
| 7281 | /* mark module as imported-only / not-implemented */ |
| 7282 | m->implemented = 0; |
| 7283 | /* free the compiled version of the module */ |
| 7284 | lysc_module_free(m->compiled, NULL); |
| 7285 | m->compiled = NULL; |
| 7286 | } |
| 7287 | } |
| 7288 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7289 | /* remove the module itself from the context and free it */ |
| 7290 | ly_set_rm(&ctx.ctx->list, *mod, NULL); |
| 7291 | lys_module_free(*mod, NULL); |
| 7292 | *mod = NULL; |
| 7293 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7294 | return ret; |
| 7295 | } |