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" |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 39 | #include "tree_data_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 40 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 41 | #include "tree_schema_internal.h" |
| 42 | #include "xpath.h" |
| 43 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 44 | static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, |
| 45 | void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod); |
| 46 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 47 | /** |
| 48 | * @brief Duplicate string into dictionary |
| 49 | * @param[in] CTX libyang context of the dictionary. |
| 50 | * @param[in] ORIG String to duplicate. |
| 51 | * @param[out] DUP Where to store the result. |
| 52 | */ |
| 53 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 54 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 55 | #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] | 56 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 57 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 58 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 59 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 60 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 61 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 62 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 63 | } \ |
| 64 | } |
| 65 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 66 | #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] | 67 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 68 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 69 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 70 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 71 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 72 | 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] | 73 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 74 | } \ |
| 75 | } |
| 76 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 77 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 78 | if (EXTS_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 79 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \ |
| 80 | 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] | 81 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 82 | 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] | 83 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 84 | } \ |
| 85 | } |
| 86 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 87 | #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] | 88 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 89 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 90 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 91 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 92 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 93 | 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] | 94 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 95 | } \ |
| 96 | } |
| 97 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 98 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 99 | if (MEMBER_P) { \ |
| 100 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 101 | 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] | 102 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 103 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 104 | } |
| 105 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 106 | #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] | 107 | if (MEMBER_P) { \ |
| 108 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 109 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 110 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 111 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 112 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 113 | } |
| 114 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 115 | #define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 116 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 117 | 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] | 118 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 119 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 120 | return LY_EVALID; \ |
| 121 | } \ |
| 122 | } \ |
| 123 | } |
| 124 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 125 | #define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 126 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 127 | 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] | 128 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \ |
| 129 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 130 | return LY_EVALID; \ |
| 131 | } \ |
| 132 | } \ |
| 133 | } |
| 134 | |
| 135 | struct lysc_ext * |
| 136 | lysc_ext_dup(struct lysc_ext *orig) |
| 137 | { |
| 138 | ++orig->refcount; |
| 139 | return orig; |
| 140 | } |
| 141 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 142 | static struct lysc_ext_instance * |
| 143 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 144 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 145 | /* TODO - extensions, increase refcount */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 146 | (void) ctx; |
| 147 | (void) orig; |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 151 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 152 | * @brief Add record into the compile context's list of incomplete default values. |
| 153 | * @param[in] ctx Compile context with the incomplete default values list. |
| 154 | * @param[in] context_node Context schema node to store in the record. |
| 155 | * @param[in] dflt Incomplete default value to store in the record. |
| 156 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 157 | * @return LY_EMEM in case of memory allocation failure. |
| 158 | * @return LY_SUCCESS |
| 159 | */ |
| 160 | static LY_ERR |
| 161 | lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod) |
| 162 | { |
| 163 | struct lysc_incomplete_dflt *r; |
| 164 | r = malloc(sizeof *r); |
| 165 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 166 | r->context_node = context_node; |
| 167 | r->dflt = dflt; |
| 168 | r->dflt_mod = dflt_mod; |
| 169 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 170 | |
| 171 | return LY_SUCCESS; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 176 | * @param[in] ctx Compile context with the incomplete default values list. |
| 177 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 178 | */ |
| 179 | static void |
| 180 | lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt) |
| 181 | { |
| 182 | unsigned int u; |
| 183 | struct lysc_incomplete_dflt *r; |
| 184 | |
| 185 | for (u = 0; u < ctx->dflts.count; ++u) { |
| 186 | r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u]; |
| 187 | if (r->dflt == dflt) { |
| 188 | free(ctx->dflts.objs[u]); |
| 189 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 190 | --ctx->dflts.count; |
| 191 | return; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 196 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 197 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 198 | { |
| 199 | int len; |
| 200 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 201 | |
| 202 | if (!name) { |
| 203 | /* removing last path segment */ |
| 204 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 205 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 206 | if (ctx->path[ctx->path_len] == '=') { |
| 207 | ctx->path[ctx->path_len++] = '}'; |
| 208 | } else { |
| 209 | /* not a top-level special tag, remove also preceiding '/' */ |
| 210 | goto remove_nodelevel; |
| 211 | } |
| 212 | } else { |
| 213 | remove_nodelevel: |
| 214 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 215 | if (ctx->path_len == 0) { |
| 216 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 217 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | /* set new terminating NULL-byte */ |
| 221 | ctx->path[ctx->path_len] = '\0'; |
| 222 | } else { |
| 223 | if (ctx->path_len > 1) { |
| 224 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 225 | /* extension of the special tag */ |
| 226 | nextlevel = 2; |
| 227 | --ctx->path_len; |
| 228 | } else { |
| 229 | /* there is already some path, so add next level */ |
| 230 | nextlevel = 1; |
| 231 | } |
| 232 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 233 | |
| 234 | if (nextlevel != 2) { |
| 235 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 236 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 237 | 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] | 238 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 239 | 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] | 240 | } |
| 241 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 242 | 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] | 243 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 244 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 245 | /* output truncated */ |
| 246 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 247 | } else { |
| 248 | ctx->path_len += len; |
| 249 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 254 | * @brief Duplicate the compiled pattern structure. |
| 255 | * |
| 256 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 257 | * |
| 258 | * @param[in] orig The pattern structure to duplicate. |
| 259 | * @return The duplicated structure to use. |
| 260 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 261 | static struct lysc_pattern* |
| 262 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 263 | { |
| 264 | ++orig->refcount; |
| 265 | return orig; |
| 266 | } |
| 267 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 268 | /** |
| 269 | * @brief Duplicate the array of compiled patterns. |
| 270 | * |
| 271 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 272 | * |
| 273 | * @param[in] ctx Libyang context for logging. |
| 274 | * @param[in] orig The patterns sized array to duplicate. |
| 275 | * @return New sized array as a copy of @p orig. |
| 276 | * @return NULL in case of memory allocation error. |
| 277 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 278 | static struct lysc_pattern** |
| 279 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 280 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 281 | struct lysc_pattern **dup = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 282 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 283 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 284 | assert(orig); |
| 285 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 286 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 287 | LY_ARRAY_FOR(orig, u) { |
| 288 | dup[u] = lysc_pattern_dup(orig[u]); |
| 289 | LY_ARRAY_INCREMENT(dup); |
| 290 | } |
| 291 | return dup; |
| 292 | } |
| 293 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 294 | /** |
| 295 | * @brief Duplicate compiled range structure. |
| 296 | * |
| 297 | * @param[in] ctx Libyang context for logging. |
| 298 | * @param[in] orig The range structure to be duplicated. |
| 299 | * @return New compiled range structure as a copy of @p orig. |
| 300 | * @return NULL in case of memory allocation error. |
| 301 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 302 | struct lysc_range* |
| 303 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 304 | { |
| 305 | struct lysc_range *dup; |
| 306 | LY_ERR ret; |
| 307 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 308 | assert(orig); |
| 309 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 310 | dup = calloc(1, sizeof *dup); |
| 311 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 312 | if (orig->parts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 313 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup); |
| 314 | LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts); |
| 315 | 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] | 316 | } |
| 317 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 318 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 319 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 320 | |
| 321 | return dup; |
| 322 | cleanup: |
| 323 | free(dup); |
| 324 | (void) ret; /* set but not used due to the return type */ |
| 325 | return NULL; |
| 326 | } |
| 327 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 328 | /** |
| 329 | * @brief Stack for processing if-feature expressions. |
| 330 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 331 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 332 | int size; /**< number of items in the stack */ |
| 333 | int index; /**< first empty item */ |
| 334 | 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] | 335 | }; |
| 336 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 337 | /** |
| 338 | * @brief Add @ref ifftokens into the stack. |
| 339 | * @param[in] stack The if-feature stack to use. |
| 340 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 341 | * @return LY_EMEM in case of memory allocation error |
| 342 | * @return LY_ESUCCESS if the value successfully stored. |
| 343 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 344 | static LY_ERR |
| 345 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 346 | { |
| 347 | if (stack->index == stack->size) { |
| 348 | stack->size += 4; |
| 349 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 350 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 351 | } |
| 352 | stack->stack[stack->index++] = value; |
| 353 | return LY_SUCCESS; |
| 354 | } |
| 355 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 356 | /** |
| 357 | * @brief Get (and remove) the last item form the stack. |
| 358 | * @param[in] stack The if-feature stack to use. |
| 359 | * @return The value from the top of the stack. |
| 360 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 361 | static uint8_t |
| 362 | iff_stack_pop(struct iff_stack *stack) |
| 363 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 364 | assert(stack && stack->index); |
| 365 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 366 | stack->index--; |
| 367 | return stack->stack[stack->index]; |
| 368 | } |
| 369 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 370 | /** |
| 371 | * @brief Clean up the stack. |
| 372 | * @param[in] stack The if-feature stack to use. |
| 373 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 374 | static void |
| 375 | iff_stack_clean(struct iff_stack *stack) |
| 376 | { |
| 377 | stack->size = 0; |
| 378 | free(stack->stack); |
| 379 | } |
| 380 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 381 | /** |
| 382 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 383 | * (libyang format of the if-feature expression). |
| 384 | * @param[in,out] list The 2bits array to modify. |
| 385 | * @param[in] op The operand (@ref ifftokens) to store. |
| 386 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 387 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 388 | static void |
| 389 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 390 | { |
| 391 | uint8_t *item; |
| 392 | uint8_t mask = 3; |
| 393 | |
| 394 | assert(pos >= 0); |
| 395 | assert(op <= 3); /* max 2 bits */ |
| 396 | |
| 397 | item = &list[pos / 4]; |
| 398 | mask = mask << 2 * (pos % 4); |
| 399 | *item = (*item) & ~mask; |
| 400 | *item = (*item) | (op << 2 * (pos % 4)); |
| 401 | } |
| 402 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 403 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 404 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 405 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 406 | /** |
| 407 | * @brief Find a feature of the given name and referenced in the given module. |
| 408 | * |
| 409 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 410 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 411 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 412 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 413 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 414 | * assigned till that time will be still valid. |
| 415 | * |
| 416 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 417 | * @param[in] name Name of the feature including possible prefix. |
| 418 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 419 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 420 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 421 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 422 | 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] | 423 | { |
| 424 | size_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 425 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 426 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 427 | |
| 428 | for (i = 0; i < len; ++i) { |
| 429 | if (name[i] == ':') { |
| 430 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 431 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 432 | LY_CHECK_RET(!mod, NULL); |
| 433 | |
| 434 | name = &name[i + 1]; |
| 435 | len = len - i - 1; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 440 | if (mod->implemented) { |
| 441 | /* module is implemented so there is already the compiled schema */ |
| 442 | flist = mod->compiled->features; |
| 443 | } else { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 444 | flist = mod->dis_features; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 445 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 446 | LY_ARRAY_FOR(flist, u) { |
| 447 | f = &flist[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 448 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 449 | return f; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | return NULL; |
| 454 | } |
| 455 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 456 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 457 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 458 | */ |
| 459 | static LY_ERR |
| 460 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 461 | { |
| 462 | LY_ERR ret = LY_SUCCESS; |
| 463 | |
| 464 | if (!ext_p->compiled) { |
| 465 | lysc_update_path(ctx, NULL, "{extension}"); |
| 466 | lysc_update_path(ctx, NULL, ext_p->name); |
| 467 | |
| 468 | /* compile the extension definition */ |
| 469 | ext_p->compiled = calloc(1, sizeof **ext); |
| 470 | ext_p->compiled->refcount = 1; |
| 471 | DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name); |
| 472 | DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument); |
| 473 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 474 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 475 | |
| 476 | lysc_update_path(ctx, NULL, NULL); |
| 477 | lysc_update_path(ctx, NULL, NULL); |
| 478 | |
| 479 | /* find extension definition plugin */ |
| 480 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 481 | } |
| 482 | |
| 483 | *ext = lysc_ext_dup(ext_p->compiled); |
| 484 | |
| 485 | done: |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 490 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 491 | * |
| 492 | * @param[in] ctx Compilation context. |
| 493 | * @param[in] ext_p Parsed extension instance. |
| 494 | * @param[in,out] ext Prepared compiled extension instance. |
| 495 | * @param[in] parent Extension instance parent. |
| 496 | * @param[in] parent_type Extension instance parent type. |
| 497 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 498 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 499 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 500 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
| 501 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 502 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 503 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 504 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 505 | size_t u; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 506 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 507 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 508 | |
| 509 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 510 | ext->insubstmt = ext_p->insubstmt; |
| 511 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 512 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 513 | ext->parent = parent; |
| 514 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 515 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 516 | 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] | 517 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 518 | /* get module where the extension definition should be placed */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 519 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u); |
| 520 | if (ext_p->yin) { |
| 521 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 522 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 523 | * YANG (import) prefix must be done here. */ |
| 524 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 525 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 526 | u = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 527 | } else { |
| 528 | assert(ctx->mod_def->parsed); |
| 529 | LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) { |
| 530 | if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 531 | char *s; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 532 | LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1, |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 533 | ret = LY_EMEM, cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 534 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 535 | u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 536 | break; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | if (!prefixed_name) { |
| 541 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 542 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 543 | goto cleanup; |
| 544 | } |
| 545 | } else { |
| 546 | prefixed_name = ext_p->name; |
| 547 | } |
| 548 | lysc_update_path(ctx, NULL, prefixed_name); |
| 549 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 550 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 551 | 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] | 552 | if (!ext_mod) { |
| 553 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 554 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 555 | goto cleanup; |
| 556 | } else if (!ext_mod->parsed->extensions) { |
| 557 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 558 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 559 | prefixed_name, ext_mod->name); |
| 560 | goto cleanup; |
| 561 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 562 | } |
| 563 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 564 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 565 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 566 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 567 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 568 | /* compile extension definition and assign it */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 569 | 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] | 570 | break; |
| 571 | } |
| 572 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 573 | if (!ext->def) { |
| 574 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 575 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 576 | goto cleanup; |
| 577 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 578 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 579 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 580 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 581 | * 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] | 582 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 583 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 584 | struct lysp_stmt *stmt = NULL; |
| 585 | |
| 586 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 587 | /* ... argument was the first XML child element */ |
| 588 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 589 | /* TODO check namespace of the statement */ |
| 590 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 591 | stmt = ext_p->child; |
| 592 | } |
| 593 | } |
| 594 | } else { |
| 595 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 596 | * with LYS_YIN_ATTR flag */ |
| 597 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 598 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 599 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 600 | break; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | if (!stmt) { |
| 605 | /* missing extension's argument */ |
| 606 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 607 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 608 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 609 | |
| 610 | } |
| 611 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 612 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 613 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 614 | if (prefixed_name != ext_p->name) { |
| 615 | lydict_remove(ctx->ctx, ext_p->name); |
| 616 | ext_p->name = prefixed_name; |
| 617 | if (!ext_p->argument && ext->argument) { |
| 618 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 619 | } |
| 620 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 621 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 622 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 623 | if (ext->argument) { |
| 624 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 625 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 626 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 627 | if (ext->argument) { |
| 628 | lysc_update_path(ctx, NULL, NULL); |
| 629 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 630 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 631 | ext_p->compiled = ext; |
| 632 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 633 | ret = LY_SUCCESS; |
| 634 | cleanup: |
| 635 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 636 | lydict_remove(ctx->ctx, prefixed_name); |
| 637 | } |
| 638 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 639 | lysc_update_path(ctx, NULL, NULL); |
| 640 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 641 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 642 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 646 | * @brief Compile information from the if-feature statement |
| 647 | * @param[in] ctx Compile context. |
| 648 | * @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] | 649 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 650 | * @return LY_ERR value. |
| 651 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 652 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 653 | 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] | 654 | { |
| 655 | const char *c = *value; |
| 656 | int r, rc = EXIT_FAILURE; |
| 657 | int i, j, last_not, checkversion = 0; |
| 658 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 659 | uint8_t op; |
| 660 | struct iff_stack stack = {0, 0, NULL}; |
| 661 | struct lysc_feature *f; |
| 662 | |
| 663 | assert(c); |
| 664 | |
| 665 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 666 | for (i = j = last_not = 0; c[i]; i++) { |
| 667 | if (c[i] == '(') { |
| 668 | j++; |
| 669 | checkversion = 1; |
| 670 | continue; |
| 671 | } else if (c[i] == ')') { |
| 672 | j--; |
| 673 | continue; |
| 674 | } else if (isspace(c[i])) { |
| 675 | checkversion = 1; |
| 676 | continue; |
| 677 | } |
| 678 | |
| 679 | 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] | 680 | int sp; |
| 681 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 682 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 683 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 684 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 685 | return LY_EVALID; |
| 686 | } else if (!isspace(c[i + r])) { |
| 687 | /* feature name starting with the not/and/or */ |
| 688 | last_not = 0; |
| 689 | f_size++; |
| 690 | } else if (c[i] == 'n') { /* not operation */ |
| 691 | if (last_not) { |
| 692 | /* double not */ |
| 693 | expr_size = expr_size - 2; |
| 694 | last_not = 0; |
| 695 | } else { |
| 696 | last_not = 1; |
| 697 | } |
| 698 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 699 | if (f_exp != f_size) { |
| 700 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 701 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 702 | return LY_EVALID; |
| 703 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 704 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 705 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 706 | /* not a not operation */ |
| 707 | last_not = 0; |
| 708 | } |
| 709 | i += r; |
| 710 | } else { |
| 711 | f_size++; |
| 712 | last_not = 0; |
| 713 | } |
| 714 | expr_size++; |
| 715 | |
| 716 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 717 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 718 | i--; |
| 719 | break; |
| 720 | } |
| 721 | i++; |
| 722 | } |
| 723 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 724 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 725 | /* not matching count of ( and ) */ |
| 726 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 727 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 728 | return LY_EVALID; |
| 729 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 730 | if (f_exp != f_size) { |
| 731 | /* features do not match the needed arguments for the logical operations */ |
| 732 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 733 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 734 | "the required number of operands for the operations.", *value); |
| 735 | return LY_EVALID; |
| 736 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 737 | |
| 738 | if (checkversion || expr_size > 1) { |
| 739 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 740 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 742 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 743 | return LY_EVALID; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /* allocate the memory */ |
| 748 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 749 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 750 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 751 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 752 | |
| 753 | stack.size = expr_size; |
| 754 | f_size--; expr_size--; /* used as indexes from now */ |
| 755 | |
| 756 | for (i--; i >= 0; i--) { |
| 757 | if (c[i] == ')') { |
| 758 | /* push it on stack */ |
| 759 | iff_stack_push(&stack, LYS_IFF_RP); |
| 760 | continue; |
| 761 | } else if (c[i] == '(') { |
| 762 | /* pop from the stack into result all operators until ) */ |
| 763 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 764 | iff_setop(iff->expr, op, expr_size--); |
| 765 | } |
| 766 | continue; |
| 767 | } else if (isspace(c[i])) { |
| 768 | continue; |
| 769 | } |
| 770 | |
| 771 | /* end of operator or operand -> find beginning and get what is it */ |
| 772 | j = i + 1; |
| 773 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 774 | i--; |
| 775 | } |
| 776 | i++; /* go back by one step */ |
| 777 | |
| 778 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 779 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 780 | /* double not */ |
| 781 | iff_stack_pop(&stack); |
| 782 | } else { |
| 783 | /* not has the highest priority, so do not pop from the stack |
| 784 | * as in case of AND and OR */ |
| 785 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 786 | } |
| 787 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 788 | /* as for OR - pop from the stack all operators with the same or higher |
| 789 | * priority and store them to the result, then push the AND to the stack */ |
| 790 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 791 | op = iff_stack_pop(&stack); |
| 792 | iff_setop(iff->expr, op, expr_size--); |
| 793 | } |
| 794 | iff_stack_push(&stack, LYS_IFF_AND); |
| 795 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 796 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 797 | op = iff_stack_pop(&stack); |
| 798 | iff_setop(iff->expr, op, expr_size--); |
| 799 | } |
| 800 | iff_stack_push(&stack, LYS_IFF_OR); |
| 801 | } else { |
| 802 | /* feature name, length is j - i */ |
| 803 | |
| 804 | /* add it to the expression */ |
| 805 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 806 | |
| 807 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 808 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 809 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 810 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 811 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 812 | iff->features[f_size] = f; |
| 813 | LY_ARRAY_INCREMENT(iff->features); |
| 814 | f_size--; |
| 815 | } |
| 816 | } |
| 817 | while (stack.index) { |
| 818 | op = iff_stack_pop(&stack); |
| 819 | iff_setop(iff->expr, op, expr_size--); |
| 820 | } |
| 821 | |
| 822 | if (++expr_size || ++f_size) { |
| 823 | /* not all expected operators and operands found */ |
| 824 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 825 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 826 | rc = LY_EINT; |
| 827 | } else { |
| 828 | rc = LY_SUCCESS; |
| 829 | } |
| 830 | |
| 831 | error: |
| 832 | /* cleanup */ |
| 833 | iff_stack_clean(&stack); |
| 834 | |
| 835 | return rc; |
| 836 | } |
| 837 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 838 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 839 | * @brief Get the XPath context node for the given schema node. |
| 840 | * @param[in] start The schema node where the XPath expression appears. |
| 841 | * @return The context node to evaluate XPath expression in given schema node. |
| 842 | * @return NULL in case the context node is the root node. |
| 843 | */ |
| 844 | static struct lysc_node * |
| 845 | lysc_xpath_context(struct lysc_node *start) |
| 846 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 847 | 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] | 848 | start = start->parent); |
| 849 | return start; |
| 850 | } |
| 851 | |
| 852 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 853 | * @brief Compile information from the when statement |
| 854 | * @param[in] ctx Compile context. |
| 855 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 856 | * @param[in] flags Flags of the node with the "when" defiition. |
| 857 | * @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] | 858 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 859 | * @return LY_ERR value. |
| 860 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 861 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 862 | 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] | 863 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 864 | LY_ERR ret = LY_SUCCESS; |
| 865 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 866 | *when = calloc(1, sizeof **when); |
| 867 | (*when)->refcount = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 868 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 869 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 870 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 871 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 872 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 873 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 874 | 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] | 875 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 876 | |
| 877 | done: |
| 878 | return ret; |
| 879 | } |
| 880 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 881 | /** |
| 882 | * @brief Compile information from the must statement |
| 883 | * @param[in] ctx Compile context. |
| 884 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 885 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 886 | * @return LY_ERR value. |
| 887 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 888 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 889 | 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] | 890 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 891 | LY_ERR ret = LY_SUCCESS; |
| 892 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 893 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 894 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 895 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 896 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 897 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 898 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 899 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 900 | 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] | 901 | |
| 902 | done: |
| 903 | return ret; |
| 904 | } |
| 905 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 906 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 907 | * @brief Compile information in the import statement - make sure there is the target module |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 908 | * @param[in] ctx Compile context. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 909 | * @param[in] imp_p The parsed import statement structure to fill the module to. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 910 | * @return LY_ERR value. |
| 911 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 912 | static LY_ERR |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 913 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 914 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 915 | const struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 916 | LY_ERR ret = LY_SUCCESS; |
| 917 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 918 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 919 | * 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] | 920 | * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 921 | if (!imp_p->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 922 | /* try to use filepath if present */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 923 | if (imp_p->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 924 | struct ly_in *in; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 925 | if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 926 | LOGINT(ctx->ctx); |
| 927 | } else { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 928 | LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)], |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 929 | ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 930 | if (mod != imp_p->module) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 931 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 932 | imp_p->module->filepath, imp_p->module->name); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 933 | mod = NULL; |
| 934 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 935 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 936 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 937 | } |
| 938 | if (!mod) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 939 | if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 940 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 941 | imp_p->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 942 | return LY_ENOTFOUND; |
| 943 | } |
| 944 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 945 | } |
| 946 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 947 | return ret; |
| 948 | } |
| 949 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 950 | LY_ERR |
| 951 | lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 952 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 953 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 954 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 955 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 956 | LY_ERR ret = LY_SUCCESS; |
| 957 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 958 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 959 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 960 | if (!ctx_sc) { |
| 961 | context.ctx = ctx; |
| 962 | context.mod = module; |
| 963 | context.path_len = 1; |
| 964 | context.path[0] = '/'; |
| 965 | ctx_sc = &context; |
| 966 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 967 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 968 | if (!identities_p) { |
| 969 | return LY_SUCCESS; |
| 970 | } |
| 971 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 972 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 976 | 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] | 977 | LY_ARRAY_FOR(identities_p, u) { |
| 978 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 979 | |
| 980 | LY_ARRAY_INCREMENT(*identities); |
| 981 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name); |
| 982 | DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name); |
| 983 | DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc); |
| 984 | DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref); |
| 985 | (*identities)[offset + u].module = ctx_sc->mod; |
| 986 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
| 987 | lys_compile_iffeature, ret, done); |
| 988 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 989 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
| 990 | LYEXT_PAR_IDENT, ret, done); |
| 991 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 992 | |
| 993 | lysc_update_path(ctx_sc, NULL, NULL); |
| 994 | } |
| 995 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 996 | done: |
| 997 | return ret; |
| 998 | } |
| 999 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1000 | /** |
| 1001 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1002 | * |
| 1003 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1004 | * |
| 1005 | * @param[in] ctx Compile context for logging. |
| 1006 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1007 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1008 | * @return LY_SUCCESS if everything is ok. |
| 1009 | * @return LY_EVALID if the identity is derived from itself. |
| 1010 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1011 | static LY_ERR |
| 1012 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1013 | { |
| 1014 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1015 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1016 | struct ly_set recursion = {0}; |
| 1017 | struct lysc_ident *drv; |
| 1018 | |
| 1019 | if (!derived) { |
| 1020 | return LY_SUCCESS; |
| 1021 | } |
| 1022 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1023 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1024 | if (ident == derived[u]) { |
| 1025 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1026 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1027 | goto cleanup; |
| 1028 | } |
| 1029 | ly_set_add(&recursion, derived[u], 0); |
| 1030 | } |
| 1031 | |
| 1032 | for (v = 0; v < recursion.count; ++v) { |
| 1033 | drv = recursion.objs[v]; |
| 1034 | if (!drv->derived) { |
| 1035 | continue; |
| 1036 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1037 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1038 | if (ident == drv->derived[u]) { |
| 1039 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1040 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1041 | goto cleanup; |
| 1042 | } |
| 1043 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1044 | } |
| 1045 | } |
| 1046 | ret = LY_SUCCESS; |
| 1047 | |
| 1048 | cleanup: |
| 1049 | ly_set_erase(&recursion, NULL); |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1053 | /** |
| 1054 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1055 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1056 | * 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] | 1057 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1058 | * to distinguish these two use cases. |
| 1059 | * |
| 1060 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1061 | * @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] | 1062 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1063 | * @param[in] bases Array of bases of identityref to fill in. |
| 1064 | * @return LY_ERR value. |
| 1065 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1066 | static LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1067 | lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, |
| 1068 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1069 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1070 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1071 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1072 | struct lys_module *mod; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1073 | struct lysc_ident **idref, *identities; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1074 | |
| 1075 | assert(ident || bases); |
| 1076 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1077 | if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1078 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1079 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1080 | return LY_EVALID; |
| 1081 | } |
| 1082 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1083 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1084 | s = strchr(bases_p[u], ':'); |
| 1085 | if (s) { |
| 1086 | /* prefixed identity */ |
| 1087 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1088 | 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] | 1089 | } else { |
| 1090 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1091 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1092 | } |
| 1093 | if (!mod) { |
| 1094 | if (ident) { |
| 1095 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1096 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1097 | } else { |
| 1098 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1099 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1100 | } |
| 1101 | return LY_EVALID; |
| 1102 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1103 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1104 | idref = NULL; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1105 | if (mod->compiled) { |
| 1106 | identities = mod->compiled->identities; |
| 1107 | } else { |
| 1108 | identities = mod->dis_identities; |
| 1109 | } |
| 1110 | LY_ARRAY_FOR(identities, v) { |
| 1111 | if (!strcmp(name, identities[v].name)) { |
| 1112 | if (ident) { |
| 1113 | if (ident == &identities[v]) { |
| 1114 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1115 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1116 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1117 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1118 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived)); |
| 1119 | /* we have match! store the backlink */ |
| 1120 | LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM); |
| 1121 | *idref = ident; |
| 1122 | } else { |
| 1123 | /* we have match! store the found identity */ |
| 1124 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 1125 | *idref = &identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1126 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1127 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | if (!idref || !(*idref)) { |
| 1131 | if (ident) { |
| 1132 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1133 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1134 | } else { |
| 1135 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1136 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1137 | } |
| 1138 | return LY_EVALID; |
| 1139 | } |
| 1140 | } |
| 1141 | return LY_SUCCESS; |
| 1142 | } |
| 1143 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1144 | /** |
| 1145 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1146 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1147 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1148 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1149 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1150 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1151 | static LY_ERR |
| 1152 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1153 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1154 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1155 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1156 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1157 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1158 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1159 | continue; |
| 1160 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1161 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1162 | 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] | 1163 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1164 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1165 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1166 | return LY_SUCCESS; |
| 1167 | } |
| 1168 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1169 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1170 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 1171 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1172 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1173 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1174 | struct lysc_ctx context = {0}; |
| 1175 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1176 | assert(ctx_sc || ctx); |
| 1177 | |
| 1178 | if (!ctx_sc) { |
| 1179 | context.ctx = ctx; |
| 1180 | context.mod = module; |
| 1181 | context.path_len = 1; |
| 1182 | context.path[0] = '/'; |
| 1183 | ctx_sc = &context; |
| 1184 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1185 | |
| 1186 | if (!features_p) { |
| 1187 | return LY_SUCCESS; |
| 1188 | } |
| 1189 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1190 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1193 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1194 | 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] | 1195 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1196 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1197 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1198 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1199 | 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] | 1200 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1201 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1202 | 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] | 1203 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1204 | (*features)[offset + u].module = ctx_sc->mod; |
| 1205 | |
| 1206 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1207 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1208 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1209 | |
| 1210 | return LY_SUCCESS; |
| 1211 | } |
| 1212 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1213 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1214 | * @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] | 1215 | * |
| 1216 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1217 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1218 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1219 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1220 | * being currently processed). |
| 1221 | * @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] | 1222 | * @return LY_SUCCESS if everything is ok. |
| 1223 | * @return LY_EVALID if the feature references indirectly itself. |
| 1224 | */ |
| 1225 | static LY_ERR |
| 1226 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1227 | { |
| 1228 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1229 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1230 | struct ly_set recursion = {0}; |
| 1231 | struct lysc_feature *drv; |
| 1232 | |
| 1233 | if (!depfeatures) { |
| 1234 | return LY_SUCCESS; |
| 1235 | } |
| 1236 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1237 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1238 | if (feature == depfeatures[u]) { |
| 1239 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1240 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1241 | goto cleanup; |
| 1242 | } |
| 1243 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1244 | } |
| 1245 | |
| 1246 | for (v = 0; v < recursion.count; ++v) { |
| 1247 | drv = recursion.objs[v]; |
| 1248 | if (!drv->depfeatures) { |
| 1249 | continue; |
| 1250 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1251 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1252 | if (feature == drv->depfeatures[u]) { |
| 1253 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1254 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1255 | goto cleanup; |
| 1256 | } |
| 1257 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1258 | } |
| 1259 | } |
| 1260 | ret = LY_SUCCESS; |
| 1261 | |
| 1262 | cleanup: |
| 1263 | ly_set_erase(&recursion, NULL); |
| 1264 | return ret; |
| 1265 | } |
| 1266 | |
| 1267 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1268 | * @brief Create pre-compiled features array. |
| 1269 | * |
| 1270 | * See lys_feature_precompile() for more details. |
| 1271 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1272 | * @param[in] ctx Compile context. |
| 1273 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1274 | * @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] | 1275 | * @return LY_ERR value. |
| 1276 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1277 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1278 | 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] | 1279 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1280 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1281 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1282 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1283 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1284 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1285 | /* find the preprecompiled feature */ |
| 1286 | LY_ARRAY_FOR(features, x) { |
| 1287 | if (strcmp(features[x].name, feature_p->name)) { |
| 1288 | continue; |
| 1289 | } |
| 1290 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1291 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1292 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1293 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1294 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1295 | 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] | 1296 | 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] | 1297 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1298 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1299 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1300 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1301 | /* check for circular dependency - direct reference first,... */ |
| 1302 | if (feature == feature->iffeatures[u].features[v]) { |
| 1303 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1304 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1305 | return LY_EVALID; |
| 1306 | } |
| 1307 | /* ... and indirect circular reference */ |
| 1308 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1309 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1310 | /* add itself into the dependants list */ |
| 1311 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1312 | *df = feature; |
| 1313 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1314 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1315 | } |
| 1316 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1317 | lysc_update_path(ctx, NULL, NULL); |
| 1318 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1319 | done: |
| 1320 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1321 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1322 | |
| 1323 | LOGINT(ctx->ctx); |
| 1324 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1325 | } |
| 1326 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1327 | /** |
| 1328 | * @brief Revert compiled list of features back to the precompiled state. |
| 1329 | * |
| 1330 | * 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] | 1331 | * 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] | 1332 | * |
| 1333 | * @param[in] ctx Compilation context. |
| 1334 | * @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] | 1335 | * and supposed to hold the dis_features list. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1336 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1337 | static void |
| 1338 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1339 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1340 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1341 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1342 | /* keep the dis_features list until the complete lys_module is freed */ |
| 1343 | mod->dis_features = mod->compiled->features; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1344 | mod->compiled->features = NULL; |
| 1345 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1346 | /* 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] | 1347 | * which may points into the data being freed here */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1348 | LY_ARRAY_FOR(mod->dis_features, u) { |
| 1349 | LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) { |
| 1350 | lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1351 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1352 | LY_ARRAY_FREE(mod->dis_features[u].iffeatures); |
| 1353 | mod->dis_features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1354 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1355 | LY_ARRAY_FOR(mod->dis_features[u].exts, v) { |
| 1356 | lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1357 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1358 | LY_ARRAY_FREE(mod->dis_features[u].exts); |
| 1359 | mod->dis_features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1363 | /** |
| 1364 | * @brief Validate and normalize numeric value from a range definition. |
| 1365 | * @param[in] ctx Compile context. |
| 1366 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1367 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1368 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1369 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1370 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1371 | * @param[in] value String value of the range boundary. |
| 1372 | * @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. |
| 1373 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1374 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1375 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1376 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1377 | 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] | 1378 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1379 | size_t fraction = 0, size; |
| 1380 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1381 | *len = 0; |
| 1382 | |
| 1383 | assert(value); |
| 1384 | /* parse value */ |
| 1385 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1386 | return LY_EVALID; |
| 1387 | } |
| 1388 | |
| 1389 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1390 | ++(*len); |
| 1391 | } |
| 1392 | |
| 1393 | while (isdigit(value[*len])) { |
| 1394 | ++(*len); |
| 1395 | } |
| 1396 | |
| 1397 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1398 | if (basetype == LY_TYPE_DEC64) { |
| 1399 | goto decimal; |
| 1400 | } else { |
| 1401 | *valcopy = strndup(value, *len); |
| 1402 | return LY_SUCCESS; |
| 1403 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1404 | } |
| 1405 | fraction = *len; |
| 1406 | |
| 1407 | ++(*len); |
| 1408 | while (isdigit(value[*len])) { |
| 1409 | ++(*len); |
| 1410 | } |
| 1411 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1412 | if (basetype == LY_TYPE_DEC64) { |
| 1413 | decimal: |
| 1414 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1415 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1416 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1417 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1418 | *len, value, frdigits); |
| 1419 | return LY_EINVAL; |
| 1420 | } |
| 1421 | if (fraction) { |
| 1422 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1423 | } else { |
| 1424 | size = (*len) + frdigits + 1; |
| 1425 | } |
| 1426 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1427 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1428 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1429 | (*valcopy)[size - 1] = '\0'; |
| 1430 | if (fraction) { |
| 1431 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1432 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1433 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1434 | } else { |
| 1435 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1436 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1437 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1438 | } |
| 1439 | return LY_SUCCESS; |
| 1440 | } |
| 1441 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1442 | /** |
| 1443 | * @brief Check that values in range are in ascendant order. |
| 1444 | * @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] | 1445 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1446 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1447 | * @param[in] value Current value to check. |
| 1448 | * @param[in] prev_value The last seen value. |
| 1449 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1450 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1451 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1452 | 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] | 1453 | { |
| 1454 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1455 | 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] | 1456 | return LY_EEXIST; |
| 1457 | } |
| 1458 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1459 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1460 | return LY_EEXIST; |
| 1461 | } |
| 1462 | } |
| 1463 | return LY_SUCCESS; |
| 1464 | } |
| 1465 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1466 | /** |
| 1467 | * @brief Set min/max value of the range part. |
| 1468 | * @param[in] ctx Compile context. |
| 1469 | * @param[in] part Range part structure to fill. |
| 1470 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1471 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1472 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1473 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1474 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1475 | * @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] | 1476 | * @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] | 1477 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1478 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1479 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1480 | * frdigits value), LY_EMEM. |
| 1481 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1482 | static LY_ERR |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 1483 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, |
| 1484 | int first, int length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1485 | { |
| 1486 | LY_ERR ret = LY_SUCCESS; |
| 1487 | char *valcopy = NULL; |
| 1488 | size_t len; |
| 1489 | |
| 1490 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1491 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1492 | LY_CHECK_GOTO(ret, finalize); |
| 1493 | } |
| 1494 | if (!valcopy && base_range) { |
| 1495 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1496 | 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] | 1497 | } else { |
| 1498 | part->min_64 = base_range->parts[0].min_64; |
| 1499 | } |
| 1500 | if (!first) { |
| 1501 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1502 | } |
| 1503 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1507 | case LY_TYPE_INT8: /* range */ |
| 1508 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1509 | 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] | 1510 | } else if (max) { |
| 1511 | part->max_64 = INT64_C(127); |
| 1512 | } else { |
| 1513 | part->min_64 = INT64_C(-128); |
| 1514 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1515 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1516 | 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] | 1517 | } |
| 1518 | break; |
| 1519 | case LY_TYPE_INT16: /* range */ |
| 1520 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1521 | 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] | 1522 | } else if (max) { |
| 1523 | part->max_64 = INT64_C(32767); |
| 1524 | } else { |
| 1525 | part->min_64 = INT64_C(-32768); |
| 1526 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1527 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1528 | 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] | 1529 | } |
| 1530 | break; |
| 1531 | case LY_TYPE_INT32: /* range */ |
| 1532 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1533 | 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] | 1534 | } else if (max) { |
| 1535 | part->max_64 = INT64_C(2147483647); |
| 1536 | } else { |
| 1537 | part->min_64 = INT64_C(-2147483648); |
| 1538 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1539 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1540 | 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] | 1541 | } |
| 1542 | break; |
| 1543 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1544 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1545 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1546 | 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] | 1547 | max ? &part->max_64 : &part->min_64); |
| 1548 | } else if (max) { |
| 1549 | part->max_64 = INT64_C(9223372036854775807); |
| 1550 | } else { |
| 1551 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1552 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1553 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1554 | 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] | 1555 | } |
| 1556 | break; |
| 1557 | case LY_TYPE_UINT8: /* range */ |
| 1558 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1559 | 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] | 1560 | } else if (max) { |
| 1561 | part->max_u64 = UINT64_C(255); |
| 1562 | } else { |
| 1563 | part->min_u64 = UINT64_C(0); |
| 1564 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1565 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1566 | 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] | 1567 | } |
| 1568 | break; |
| 1569 | case LY_TYPE_UINT16: /* range */ |
| 1570 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1571 | 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] | 1572 | } else if (max) { |
| 1573 | part->max_u64 = UINT64_C(65535); |
| 1574 | } else { |
| 1575 | part->min_u64 = UINT64_C(0); |
| 1576 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1577 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1578 | 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] | 1579 | } |
| 1580 | break; |
| 1581 | case LY_TYPE_UINT32: /* range */ |
| 1582 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1583 | 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] | 1584 | } else if (max) { |
| 1585 | part->max_u64 = UINT64_C(4294967295); |
| 1586 | } else { |
| 1587 | part->min_u64 = UINT64_C(0); |
| 1588 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1589 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1590 | 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] | 1591 | } |
| 1592 | break; |
| 1593 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1594 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1595 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1596 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1597 | 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] | 1598 | } else if (max) { |
| 1599 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1600 | } else { |
| 1601 | part->min_u64 = UINT64_C(0); |
| 1602 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1603 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1604 | 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] | 1605 | } |
| 1606 | break; |
| 1607 | default: |
| 1608 | LOGINT(ctx->ctx); |
| 1609 | ret = LY_EINT; |
| 1610 | } |
| 1611 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1612 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1613 | if (ret == LY_EDENIED) { |
| 1614 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1615 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1616 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1617 | } else if (ret == LY_EVALID) { |
| 1618 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1619 | "Invalid %s restriction - invalid value \"%s\".", |
| 1620 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1621 | } else if (ret == LY_EEXIST) { |
| 1622 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1623 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1624 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1625 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1626 | } else if (!ret && value) { |
| 1627 | *value = *value + len; |
| 1628 | } |
| 1629 | free(valcopy); |
| 1630 | return ret; |
| 1631 | } |
| 1632 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1633 | /** |
| 1634 | * @brief Compile the parsed range restriction. |
| 1635 | * @param[in] ctx Compile context. |
| 1636 | * @param[in] range_p Parsed range structure to compile. |
| 1637 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1638 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1639 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1640 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1641 | * range restriction must be more restrictive than the base_range. |
| 1642 | * @param[in,out] range Pointer to the created current range structure. |
| 1643 | * @return LY_ERR value. |
| 1644 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1645 | static LY_ERR |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 1646 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, |
| 1647 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1648 | { |
| 1649 | LY_ERR ret = LY_EVALID; |
| 1650 | const char *expr; |
| 1651 | struct lysc_range_part *parts = NULL, *part; |
| 1652 | int range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1653 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1654 | |
| 1655 | assert(range); |
| 1656 | assert(range_p); |
| 1657 | |
| 1658 | expr = range_p->arg; |
| 1659 | while(1) { |
| 1660 | if (isspace(*expr)) { |
| 1661 | ++expr; |
| 1662 | } else if (*expr == '\0') { |
| 1663 | if (range_expected) { |
| 1664 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1665 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1666 | length_restr ? "length" : "range", range_p->arg); |
| 1667 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1668 | } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1669 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1670 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1671 | length_restr ? "length" : "range", range_p->arg); |
| 1672 | goto cleanup; |
| 1673 | } |
| 1674 | parts_done++; |
| 1675 | break; |
| 1676 | } else if (!strncmp(expr, "min", 3)) { |
| 1677 | if (parts) { |
| 1678 | /* min cannot be used elsewhere than in the first part */ |
| 1679 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1680 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1681 | expr - range_p->arg, range_p->arg); |
| 1682 | goto cleanup; |
| 1683 | } |
| 1684 | expr += 3; |
| 1685 | |
| 1686 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1687 | 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] | 1688 | part->max_64 = part->min_64; |
| 1689 | } else if (*expr == '|') { |
| 1690 | if (!parts || range_expected) { |
| 1691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1692 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1693 | goto cleanup; |
| 1694 | } |
| 1695 | expr++; |
| 1696 | parts_done++; |
| 1697 | /* process next part of the expression */ |
| 1698 | } else if (!strncmp(expr, "..", 2)) { |
| 1699 | expr += 2; |
| 1700 | while (isspace(*expr)) { |
| 1701 | expr++; |
| 1702 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1703 | if (!parts || LY_ARRAY_COUNT(parts) == parts_done) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1704 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1705 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1706 | goto cleanup; |
| 1707 | } |
| 1708 | /* continue expecting the upper boundary */ |
| 1709 | range_expected = 1; |
| 1710 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1711 | /* number */ |
| 1712 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1713 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1714 | 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] | 1715 | range_expected = 0; |
| 1716 | } else { |
| 1717 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1718 | 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] | 1719 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1720 | part->max_64 = part->min_64; |
| 1721 | } |
| 1722 | |
| 1723 | /* continue with possible another expression part */ |
| 1724 | } else if (!strncmp(expr, "max", 3)) { |
| 1725 | expr += 3; |
| 1726 | while (isspace(*expr)) { |
| 1727 | expr++; |
| 1728 | } |
| 1729 | if (*expr != '\0') { |
| 1730 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1731 | length_restr ? "length" : "range", expr); |
| 1732 | goto cleanup; |
| 1733 | } |
| 1734 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1735 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1736 | 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] | 1737 | range_expected = 0; |
| 1738 | } else { |
| 1739 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1740 | 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] | 1741 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1742 | part->min_64 = part->max_64; |
| 1743 | } |
| 1744 | } else { |
| 1745 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1746 | length_restr ? "length" : "range", expr); |
| 1747 | goto cleanup; |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | /* check with the previous range/length restriction */ |
| 1752 | if (base_range) { |
| 1753 | switch (basetype) { |
| 1754 | case LY_TYPE_BINARY: |
| 1755 | case LY_TYPE_UINT8: |
| 1756 | case LY_TYPE_UINT16: |
| 1757 | case LY_TYPE_UINT32: |
| 1758 | case LY_TYPE_UINT64: |
| 1759 | case LY_TYPE_STRING: |
| 1760 | uns = 1; |
| 1761 | break; |
| 1762 | case LY_TYPE_DEC64: |
| 1763 | case LY_TYPE_INT8: |
| 1764 | case LY_TYPE_INT16: |
| 1765 | case LY_TYPE_INT32: |
| 1766 | case LY_TYPE_INT64: |
| 1767 | uns = 0; |
| 1768 | break; |
| 1769 | default: |
| 1770 | LOGINT(ctx->ctx); |
| 1771 | ret = LY_EINT; |
| 1772 | goto cleanup; |
| 1773 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1774 | 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] | 1775 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1776 | goto baseerror; |
| 1777 | } |
| 1778 | /* current lower bound is not lower than the base */ |
| 1779 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1780 | /* base has single value */ |
| 1781 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1782 | /* both lower bounds are the same */ |
| 1783 | if (parts[u].min_64 != parts[u].max_64) { |
| 1784 | /* current continues with a range */ |
| 1785 | goto baseerror; |
| 1786 | } else { |
| 1787 | /* equal single values, move both forward */ |
| 1788 | ++v; |
| 1789 | continue; |
| 1790 | } |
| 1791 | } else { |
| 1792 | /* base is single value lower than current range, so the |
| 1793 | * value from base range is removed in the current, |
| 1794 | * move only base and repeat checking */ |
| 1795 | ++v; |
| 1796 | --u; |
| 1797 | continue; |
| 1798 | } |
| 1799 | } else { |
| 1800 | /* base is the range */ |
| 1801 | if (parts[u].min_64 == parts[u].max_64) { |
| 1802 | /* current is a single value */ |
| 1803 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1804 | /* current is behind the base range, so base range is omitted, |
| 1805 | * move the base and keep the current for further check */ |
| 1806 | ++v; |
| 1807 | --u; |
| 1808 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1809 | continue; |
| 1810 | } else { |
| 1811 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1812 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1813 | /* higher bound is higher than the current higher bound */ |
| 1814 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1815 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1816 | * continue with the same current, but move the base */ |
| 1817 | --u; |
| 1818 | ++v; |
| 1819 | continue; |
| 1820 | } |
| 1821 | /* current range starts within the base range but end behind it */ |
| 1822 | goto baseerror; |
| 1823 | } else { |
| 1824 | /* current range is smaller than the base, |
| 1825 | * move current, but stay with the base */ |
| 1826 | continue; |
| 1827 | } |
| 1828 | } |
| 1829 | } |
| 1830 | } |
| 1831 | if (u != parts_done) { |
| 1832 | baseerror: |
| 1833 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1834 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1835 | length_restr ? "length" : "range", range_p->arg); |
| 1836 | goto cleanup; |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | if (!(*range)) { |
| 1841 | *range = calloc(1, sizeof **range); |
| 1842 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1843 | } |
| 1844 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1845 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1846 | if (range_p->eapptag) { |
| 1847 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1848 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1849 | } |
| 1850 | if (range_p->emsg) { |
| 1851 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1852 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1853 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1854 | if (range_p->dsc) { |
| 1855 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1856 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1857 | } |
| 1858 | if (range_p->ref) { |
| 1859 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1860 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1861 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1862 | /* extensions are taken only from the last range by the caller */ |
| 1863 | |
| 1864 | (*range)->parts = parts; |
| 1865 | parts = NULL; |
| 1866 | ret = LY_SUCCESS; |
| 1867 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1868 | LY_ARRAY_FREE(parts); |
| 1869 | |
| 1870 | return ret; |
| 1871 | } |
| 1872 | |
| 1873 | /** |
| 1874 | * @brief Checks pattern syntax. |
| 1875 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1876 | * @param[in] ctx Context. |
| 1877 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1878 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1879 | * @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] | 1880 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1881 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1882 | LY_ERR |
| 1883 | 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] | 1884 | { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1885 | int idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1886 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1887 | int err_code; |
| 1888 | const char *orig_ptr; |
| 1889 | PCRE2_SIZE err_offset; |
| 1890 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1891 | #define URANGE_LEN 19 |
| 1892 | char *ublock2urange[][2] = { |
| 1893 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1894 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1895 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1896 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1897 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1898 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1899 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1900 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1901 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1902 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1903 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1904 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1905 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1906 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1907 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1908 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1909 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1910 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1911 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1912 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1913 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1914 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1915 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1916 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1917 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1918 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1919 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1920 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1921 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1922 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1923 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1924 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1925 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1926 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1927 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1928 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1929 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1930 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1931 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1932 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1933 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1934 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1935 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1936 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1937 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1938 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1939 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1940 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1941 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1942 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1943 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1944 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1945 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1946 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1947 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1948 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1949 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1950 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1951 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1952 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1953 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1954 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1955 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1956 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1957 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1958 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1959 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1960 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1961 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1962 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1963 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1964 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1965 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1966 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1967 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1968 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1969 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1970 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1971 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1972 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1973 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1974 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1975 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1976 | {NULL, NULL} |
| 1977 | }; |
| 1978 | |
| 1979 | /* adjust the expression to a Perl equivalent |
| 1980 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1981 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1982 | /* allocate space for the transformed pattern */ |
| 1983 | size = strlen(pattern) + 1; |
| 1984 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1985 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1986 | perl_regex[0] = '\0'; |
| 1987 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1988 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 1989 | brack = 0; |
| 1990 | idx = 0; |
| 1991 | orig_ptr = pattern; |
| 1992 | while (orig_ptr[0]) { |
| 1993 | switch (orig_ptr[0]) { |
| 1994 | case '$': |
| 1995 | case '^': |
| 1996 | if (!brack) { |
| 1997 | /* make space for the extra character */ |
| 1998 | ++size; |
| 1999 | perl_regex = ly_realloc(perl_regex, size); |
| 2000 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2001 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2002 | /* print escape slash */ |
| 2003 | perl_regex[idx] = '\\'; |
| 2004 | ++idx; |
| 2005 | } |
| 2006 | break; |
| 2007 | case '[': |
| 2008 | /* must not be escaped */ |
| 2009 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2010 | ++brack; |
| 2011 | } |
| 2012 | break; |
| 2013 | case ']': |
| 2014 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2015 | /* pattern was checked and compiled already */ |
| 2016 | assert(brack); |
| 2017 | --brack; |
| 2018 | } |
| 2019 | break; |
| 2020 | default: |
| 2021 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2022 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2023 | |
| 2024 | /* copy char */ |
| 2025 | perl_regex[idx] = orig_ptr[0]; |
| 2026 | |
| 2027 | ++idx; |
| 2028 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2029 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2030 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2031 | |
| 2032 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2033 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2034 | start = ptr - perl_regex; |
| 2035 | |
| 2036 | ptr = strchr(ptr, '}'); |
| 2037 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2038 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2039 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2040 | free(perl_regex); |
| 2041 | return LY_EVALID; |
| 2042 | } |
| 2043 | end = (ptr - perl_regex) + 1; |
| 2044 | |
| 2045 | /* need more space */ |
| 2046 | if (end - start < URANGE_LEN) { |
| 2047 | 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] | 2048 | 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] | 2049 | } |
| 2050 | |
| 2051 | /* find our range */ |
| 2052 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2053 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2054 | break; |
| 2055 | } |
| 2056 | } |
| 2057 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2058 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2059 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2060 | free(perl_regex); |
| 2061 | return LY_EVALID; |
| 2062 | } |
| 2063 | |
| 2064 | /* 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] | 2065 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2066 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2067 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2068 | } |
| 2069 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2070 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2071 | } |
| 2072 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2073 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2074 | /* skip brackets */ |
| 2075 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2076 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2077 | } else { |
| 2078 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2079 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2084 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2085 | 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] | 2086 | &err_code, &err_offset, NULL); |
| 2087 | if (!code_local) { |
| 2088 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2089 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2090 | 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] | 2091 | free(perl_regex); |
| 2092 | return LY_EVALID; |
| 2093 | } |
| 2094 | free(perl_regex); |
| 2095 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2096 | if (code) { |
| 2097 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2098 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2099 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | return LY_SUCCESS; |
| 2103 | |
| 2104 | #undef URANGE_LEN |
| 2105 | } |
| 2106 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2107 | /** |
| 2108 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2109 | * @param[in] ctx Compile context. |
| 2110 | * @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] | 2111 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2112 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2113 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2114 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2115 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2116 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2117 | 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] | 2118 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2119 | { |
| 2120 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2121 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2122 | LY_ERR ret = LY_SUCCESS; |
| 2123 | |
| 2124 | /* first, copy the patterns from the base type */ |
| 2125 | if (base_patterns) { |
| 2126 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2127 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2128 | } |
| 2129 | |
| 2130 | LY_ARRAY_FOR(patterns_p, u) { |
| 2131 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2132 | *pattern = calloc(1, sizeof **pattern); |
| 2133 | ++(*pattern)->refcount; |
| 2134 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2135 | 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] | 2136 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2137 | |
| 2138 | if (patterns_p[u].arg[0] == 0x15) { |
| 2139 | (*pattern)->inverted = 1; |
| 2140 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2141 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2142 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2143 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2144 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2145 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2146 | 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] | 2147 | } |
| 2148 | done: |
| 2149 | return ret; |
| 2150 | } |
| 2151 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2152 | /** |
| 2153 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2154 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2155 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2156 | 0 /* LY_TYPE_UNKNOWN */, |
| 2157 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2158 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2159 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2160 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2161 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2162 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2163 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2164 | 0 /* LY_TYPE_BOOL */, |
| 2165 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2166 | 0 /* LY_TYPE_EMPTY */, |
| 2167 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2168 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2169 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2170 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2171 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2172 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2173 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2174 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2175 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2176 | }; |
| 2177 | |
| 2178 | /** |
| 2179 | * @brief stringification of the YANG built-in data types |
| 2180 | */ |
| 2181 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2182 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2183 | "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] | 2184 | }; |
| 2185 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2186 | /** |
| 2187 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2188 | * @param[in] ctx Compile context. |
| 2189 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2190 | * @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] | 2191 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2192 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2193 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2194 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2195 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2196 | 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] | 2197 | 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] | 2198 | { |
| 2199 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2200 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2201 | int32_t value = 0; |
| 2202 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2203 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2204 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2205 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2206 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2207 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2208 | return LY_EVALID; |
| 2209 | } |
| 2210 | |
| 2211 | LY_ARRAY_FOR(enums_p, u) { |
| 2212 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2213 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2214 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2215 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2216 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2217 | if (base_enums) { |
| 2218 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2219 | LY_ARRAY_FOR(base_enums, v) { |
| 2220 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2221 | break; |
| 2222 | } |
| 2223 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2224 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2225 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2226 | "Invalid %s - derived type adds new item \"%s\".", |
| 2227 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2228 | return LY_EVALID; |
| 2229 | } |
| 2230 | match = v; |
| 2231 | } |
| 2232 | |
| 2233 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2234 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2235 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2236 | e->value = (int32_t)enums_p[u].value; |
| 2237 | if (!u || e->value >= value) { |
| 2238 | value = e->value + 1; |
| 2239 | } |
| 2240 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2241 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2242 | if (e->value == (*enums)[v].value) { |
| 2243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2244 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2245 | e->value, e->name, (*enums)[v].name); |
| 2246 | return LY_EVALID; |
| 2247 | } |
| 2248 | } |
| 2249 | } else if (base_enums) { |
| 2250 | /* inherit the assigned value */ |
| 2251 | e->value = base_enums[match].value; |
| 2252 | if (!u || e->value >= value) { |
| 2253 | value = e->value + 1; |
| 2254 | } |
| 2255 | } else { |
| 2256 | /* assign value automatically */ |
| 2257 | if (u && value == INT32_MIN) { |
| 2258 | /* counter overflow */ |
| 2259 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2260 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2261 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2262 | return LY_EVALID; |
| 2263 | } |
| 2264 | e->value = value++; |
| 2265 | } |
| 2266 | } else { /* LY_TYPE_BITS */ |
| 2267 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2268 | e->value = (int32_t)enums_p[u].value; |
| 2269 | if (!u || (uint32_t)e->value >= position) { |
| 2270 | position = (uint32_t)e->value + 1; |
| 2271 | } |
| 2272 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2273 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2274 | if (e->value == (*enums)[v].value) { |
| 2275 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2276 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2277 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2278 | return LY_EVALID; |
| 2279 | } |
| 2280 | } |
| 2281 | } else if (base_enums) { |
| 2282 | /* inherit the assigned value */ |
| 2283 | e->value = base_enums[match].value; |
| 2284 | if (!u || (uint32_t)e->value >= position) { |
| 2285 | position = (uint32_t)e->value + 1; |
| 2286 | } |
| 2287 | } else { |
| 2288 | /* assign value automatically */ |
| 2289 | if (u && position == 0) { |
| 2290 | /* counter overflow */ |
| 2291 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2292 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2293 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2294 | return LY_EVALID; |
| 2295 | } |
| 2296 | e->value = position++; |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | if (base_enums) { |
| 2301 | /* the assigned values must not change from the derived type */ |
| 2302 | if (e->value != base_enums[match].value) { |
| 2303 | if (basetype == LY_TYPE_ENUM) { |
| 2304 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2305 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2306 | e->name, base_enums[match].value, e->value); |
| 2307 | } else { |
| 2308 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2309 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2310 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2311 | } |
| 2312 | return LY_EVALID; |
| 2313 | } |
| 2314 | } |
| 2315 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2316 | 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] | 2317 | 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] | 2318 | |
| 2319 | if (basetype == LY_TYPE_BITS) { |
| 2320 | /* keep bits ordered by position */ |
| 2321 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2322 | if (v != u) { |
| 2323 | memcpy(&storage, e, sizeof *e); |
| 2324 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2325 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2326 | } |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | done: |
| 2331 | return ret; |
| 2332 | } |
| 2333 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2334 | /** |
| 2335 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2336 | * |
| 2337 | * path-arg = absolute-path / relative-path |
| 2338 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2339 | * relative-path = 1*(".." "/") descendant-path |
| 2340 | * |
| 2341 | * @param[in,out] path Path to parse. |
| 2342 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2343 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2344 | * @param[out] name Name of the token. |
| 2345 | * @param[out] nam_len Length of the name. |
| 2346 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2347 | * must not be changed between consecutive calls. -1 if the |
| 2348 | * path is absolute. |
| 2349 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2350 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2351 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2352 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2353 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2354 | int *parent_times, int *has_predicate) |
| 2355 | { |
| 2356 | int par_times = 0; |
| 2357 | |
| 2358 | assert(path && *path); |
| 2359 | assert(parent_times); |
| 2360 | assert(prefix); |
| 2361 | assert(prefix_len); |
| 2362 | assert(name); |
| 2363 | assert(name_len); |
| 2364 | assert(has_predicate); |
| 2365 | |
| 2366 | *prefix = NULL; |
| 2367 | *prefix_len = 0; |
| 2368 | *name = NULL; |
| 2369 | *name_len = 0; |
| 2370 | *has_predicate = 0; |
| 2371 | |
| 2372 | if (!*parent_times) { |
| 2373 | if (!strncmp(*path, "..", 2)) { |
| 2374 | *path += 2; |
| 2375 | ++par_times; |
| 2376 | while (!strncmp(*path, "/..", 3)) { |
| 2377 | *path += 3; |
| 2378 | ++par_times; |
| 2379 | } |
| 2380 | } |
| 2381 | if (par_times) { |
| 2382 | *parent_times = par_times; |
| 2383 | } else { |
| 2384 | *parent_times = -1; |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | if (**path != '/') { |
| 2389 | return LY_EINVAL; |
| 2390 | } |
| 2391 | /* skip '/' */ |
| 2392 | ++(*path); |
| 2393 | |
| 2394 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2395 | 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] | 2396 | |
| 2397 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2398 | /* path continues by another token or this is the last token */ |
| 2399 | return LY_SUCCESS; |
| 2400 | } else if ((*path)[0] != '[') { |
| 2401 | /* unexpected character */ |
| 2402 | return LY_EINVAL; |
| 2403 | } else { |
| 2404 | /* predicate starting with [ */ |
| 2405 | *has_predicate = 1; |
| 2406 | return LY_SUCCESS; |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2411 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2412 | * |
| 2413 | * The set of features used for target must be a subset of features used for the leafref. |
| 2414 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2415 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2416 | * |
| 2417 | * @param[in] refnode The leafref node. |
| 2418 | * @param[in] target Tha target node of the leafref. |
| 2419 | * @return LY_SUCCESS or LY_EVALID; |
| 2420 | */ |
| 2421 | static LY_ERR |
| 2422 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2423 | { |
| 2424 | LY_ERR ret = LY_EVALID; |
| 2425 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2426 | LY_ARRAY_COUNT_TYPE u, v, count; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2427 | struct ly_set features = {0}; |
| 2428 | |
| 2429 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2430 | if (iter->iffeatures) { |
| 2431 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2432 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2433 | 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] | 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2440 | * So when adding features applicable to the target into the features set, we should not be |
| 2441 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2442 | * to the leafref itself. */ |
| 2443 | count = features.count; |
| 2444 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2445 | if (iter->iffeatures) { |
| 2446 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2447 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2448 | 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] | 2449 | /* new feature was added (or LY_EMEM) */ |
| 2450 | goto cleanup; |
| 2451 | } |
| 2452 | } |
| 2453 | } |
| 2454 | } |
| 2455 | } |
| 2456 | ret = LY_SUCCESS; |
| 2457 | |
| 2458 | cleanup: |
| 2459 | ly_set_erase(&features, NULL); |
| 2460 | return ret; |
| 2461 | } |
| 2462 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2463 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2464 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2465 | struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2466 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2467 | /** |
| 2468 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2469 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2470 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2471 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2472 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2473 | * @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] | 2474 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2475 | * @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] | 2476 | * @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] | 2477 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2478 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2479 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2480 | * @return LY_ERR value. |
| 2481 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2482 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2483 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2484 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2485 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2486 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2487 | { |
| 2488 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2489 | struct lysc_type_bin *bin; |
| 2490 | struct lysc_type_num *num; |
| 2491 | struct lysc_type_str *str; |
| 2492 | struct lysc_type_bits *bits; |
| 2493 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2494 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2495 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2496 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2497 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2498 | |
| 2499 | switch (basetype) { |
| 2500 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2501 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2502 | |
| 2503 | /* 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] | 2504 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2505 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2506 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2507 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2508 | 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] | 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | if (tpdfname) { |
| 2513 | type_p->compiled = *type; |
| 2514 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2515 | } |
| 2516 | break; |
| 2517 | case LY_TYPE_BITS: |
| 2518 | /* RFC 7950 9.7 - bits */ |
| 2519 | bits = (struct lysc_type_bits*)(*type); |
| 2520 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2521 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2522 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2523 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2524 | } |
| 2525 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2526 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2527 | /* 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] | 2528 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2529 | 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] | 2530 | } else { |
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", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2532 | free(*type); |
| 2533 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2534 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2535 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | if (tpdfname) { |
| 2539 | type_p->compiled = *type; |
| 2540 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2541 | } |
| 2542 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2543 | case LY_TYPE_DEC64: |
| 2544 | dec = (struct lysc_type_dec*)(*type); |
| 2545 | |
| 2546 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2547 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2548 | if (!type_p->fraction_digits) { |
| 2549 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2550 | 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] | 2551 | } else { |
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", ""); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2553 | free(*type); |
| 2554 | *type = NULL; |
| 2555 | } |
| 2556 | return LY_EVALID; |
| 2557 | } |
| 2558 | } else if (type_p->fraction_digits) { |
| 2559 | /* 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] | 2560 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2561 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2562 | "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] | 2563 | tpdfname); |
| 2564 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2565 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2566 | "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] | 2567 | free(*type); |
| 2568 | *type = NULL; |
| 2569 | } |
| 2570 | return LY_EVALID; |
| 2571 | } |
| 2572 | dec->fraction_digits = type_p->fraction_digits; |
| 2573 | |
| 2574 | /* RFC 7950 9.2.4 - range */ |
| 2575 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2576 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2577 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2578 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2579 | 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] | 2580 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | if (tpdfname) { |
| 2584 | type_p->compiled = *type; |
| 2585 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2586 | } |
| 2587 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2588 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2589 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2590 | |
| 2591 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2592 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2593 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2594 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2595 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2596 | 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] | 2597 | } |
| 2598 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2599 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2600 | } |
| 2601 | |
| 2602 | /* RFC 7950 9.4.5 - pattern */ |
| 2603 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2604 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2605 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2606 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2607 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2608 | } |
| 2609 | |
| 2610 | if (tpdfname) { |
| 2611 | type_p->compiled = *type; |
| 2612 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2613 | } |
| 2614 | break; |
| 2615 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2616 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2617 | |
| 2618 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2619 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2620 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2621 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2622 | } |
| 2623 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2624 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2625 | /* 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] | 2626 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2627 | 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] | 2628 | } else { |
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", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2630 | free(*type); |
| 2631 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2632 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2633 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | if (tpdfname) { |
| 2637 | type_p->compiled = *type; |
| 2638 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2639 | } |
| 2640 | break; |
| 2641 | case LY_TYPE_INT8: |
| 2642 | case LY_TYPE_UINT8: |
| 2643 | case LY_TYPE_INT16: |
| 2644 | case LY_TYPE_UINT16: |
| 2645 | case LY_TYPE_INT32: |
| 2646 | case LY_TYPE_UINT32: |
| 2647 | case LY_TYPE_INT64: |
| 2648 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2649 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2650 | |
| 2651 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2652 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2653 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2654 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2655 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2656 | 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] | 2657 | } |
| 2658 | } |
| 2659 | |
| 2660 | if (tpdfname) { |
| 2661 | type_p->compiled = *type; |
| 2662 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2663 | } |
| 2664 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2665 | case LY_TYPE_IDENT: |
| 2666 | idref = (struct lysc_type_identityref*)(*type); |
| 2667 | |
| 2668 | /* RFC 7950 9.10.2 - base */ |
| 2669 | if (type_p->bases) { |
| 2670 | if (base) { |
| 2671 | /* only the directly derived identityrefs can contain base specification */ |
| 2672 | if (tpdfname) { |
| 2673 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2674 | "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] | 2675 | tpdfname); |
| 2676 | } else { |
| 2677 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2678 | "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] | 2679 | free(*type); |
| 2680 | *type = NULL; |
| 2681 | } |
| 2682 | return LY_EVALID; |
| 2683 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2684 | 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] | 2685 | } |
| 2686 | |
| 2687 | if (!base && !type_p->flags) { |
| 2688 | /* type derived from identityref built-in type must contain at least one base */ |
| 2689 | if (tpdfname) { |
| 2690 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2691 | } else { |
| 2692 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2693 | free(*type); |
| 2694 | *type = NULL; |
| 2695 | } |
| 2696 | return LY_EVALID; |
| 2697 | } |
| 2698 | |
| 2699 | if (tpdfname) { |
| 2700 | type_p->compiled = *type; |
| 2701 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2702 | } |
| 2703 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2704 | case LY_TYPE_LEAFREF: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2705 | lref = (struct lysc_type_leafref*)*type; |
| 2706 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2707 | /* RFC 7950 9.9.3 - require-instance */ |
| 2708 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2709 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2710 | if (tpdfname) { |
| 2711 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2712 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2713 | } else { |
| 2714 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2715 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2716 | free(*type); |
| 2717 | *type = NULL; |
| 2718 | } |
| 2719 | return LY_EVALID; |
| 2720 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2721 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2722 | } else if (base) { |
| 2723 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2724 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2725 | } else { |
| 2726 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2727 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2728 | } |
| 2729 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2730 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2731 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2732 | } else if (base) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2733 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path); |
| 2734 | lref->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2735 | } else if (tpdfname) { |
| 2736 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2737 | return LY_EVALID; |
| 2738 | } else { |
| 2739 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2740 | free(*type); |
| 2741 | *type = NULL; |
| 2742 | return LY_EVALID; |
| 2743 | } |
| 2744 | if (tpdfname) { |
| 2745 | type_p->compiled = *type; |
| 2746 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2747 | } |
| 2748 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2749 | case LY_TYPE_INST: |
| 2750 | /* RFC 7950 9.9.3 - require-instance */ |
| 2751 | if (type_p->flags & LYS_SET_REQINST) { |
| 2752 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2753 | } else { |
| 2754 | /* default is true */ |
| 2755 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2756 | } |
| 2757 | |
| 2758 | if (tpdfname) { |
| 2759 | type_p->compiled = *type; |
| 2760 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2761 | } |
| 2762 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2763 | case LY_TYPE_UNION: |
| 2764 | un = (struct lysc_type_union*)(*type); |
| 2765 | |
| 2766 | /* RFC 7950 7.4 - type */ |
| 2767 | if (type_p->types) { |
| 2768 | if (base) { |
| 2769 | /* only the directly derived union can contain types specification */ |
| 2770 | if (tpdfname) { |
| 2771 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2772 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2773 | tpdfname); |
| 2774 | } else { |
| 2775 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2776 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2777 | free(*type); |
| 2778 | *type = NULL; |
| 2779 | } |
| 2780 | return LY_EVALID; |
| 2781 | } |
| 2782 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2783 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2784 | 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] | 2785 | LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, |
| 2786 | &type_p->types[u], &un->types[u + additional], NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2787 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2788 | /* add space for additional types from the union subtype */ |
| 2789 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2790 | 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] | 2791 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2792 | |
| 2793 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2794 | 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] | 2795 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2796 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2797 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2798 | 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] | 2799 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2800 | |
| 2801 | lref->basetype = LY_TYPE_LEAFREF; |
| 2802 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path); |
| 2803 | lref->refcount = 1; |
| 2804 | lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2805 | 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] | 2806 | /* TODO extensions */ |
| 2807 | |
| 2808 | } else { |
| 2809 | un->types[u + additional] = un_aux->types[v]; |
| 2810 | ++un_aux->types[v]->refcount; |
| 2811 | } |
| 2812 | ++additional; |
| 2813 | LY_ARRAY_INCREMENT(un->types); |
| 2814 | } |
| 2815 | /* compensate u increment in main loop */ |
| 2816 | --additional; |
| 2817 | |
| 2818 | /* free the replaced union subtype */ |
| 2819 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2820 | } else { |
| 2821 | LY_ARRAY_INCREMENT(un->types); |
| 2822 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | if (!base && !type_p->flags) { |
| 2827 | /* type derived from union built-in type must contain at least one type */ |
| 2828 | if (tpdfname) { |
| 2829 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2830 | } else { |
| 2831 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2832 | free(*type); |
| 2833 | *type = NULL; |
| 2834 | } |
| 2835 | return LY_EVALID; |
| 2836 | } |
| 2837 | |
| 2838 | if (tpdfname) { |
| 2839 | type_p->compiled = *type; |
| 2840 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2841 | } |
| 2842 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2843 | case LY_TYPE_BOOL: |
| 2844 | case LY_TYPE_EMPTY: |
| 2845 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2846 | break; |
| 2847 | } |
| 2848 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2849 | done: |
| 2850 | return ret; |
| 2851 | } |
| 2852 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2853 | /** |
| 2854 | * @brief Compile information about the leaf/leaf-list's type. |
| 2855 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2856 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2857 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2858 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2859 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2860 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2861 | * @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] | 2862 | * @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] | 2863 | * @return LY_ERR value. |
| 2864 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2865 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2866 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2867 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2868 | struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2869 | { |
| 2870 | LY_ERR ret = LY_SUCCESS; |
| 2871 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2872 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2873 | struct type_context { |
| 2874 | const struct lysp_tpdf *tpdf; |
| 2875 | struct lysp_node *node; |
| 2876 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2877 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2878 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2879 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2880 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2881 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2882 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2883 | |
| 2884 | (*type) = NULL; |
| 2885 | |
| 2886 | tctx = calloc(1, sizeof *tctx); |
| 2887 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2888 | 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] | 2889 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2890 | ret == LY_SUCCESS; |
| 2891 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2892 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2893 | if (basetype) { |
| 2894 | break; |
| 2895 | } |
| 2896 | |
| 2897 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2898 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2899 | 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] | 2900 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2901 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2902 | if (units && !*units) { |
| 2903 | /* inherit units */ |
| 2904 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2905 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2906 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2907 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2908 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2909 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2910 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2911 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2912 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2913 | break; |
| 2914 | } |
| 2915 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2916 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2917 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2918 | * 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] | 2919 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2920 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2921 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2922 | dummyloops = 1; |
| 2923 | goto preparenext; |
| 2924 | } else { |
| 2925 | tctx = NULL; |
| 2926 | break; |
| 2927 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2928 | } |
| 2929 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2930 | /* circular typedef reference detection */ |
| 2931 | for (u = 0; u < tpdf_chain.count; u++) { |
| 2932 | /* local part */ |
| 2933 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 2934 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2935 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2936 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2937 | free(tctx); |
| 2938 | ret = LY_EVALID; |
| 2939 | goto cleanup; |
| 2940 | } |
| 2941 | } |
| 2942 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 2943 | /* global part for unions corner case */ |
| 2944 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 2945 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2946 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2947 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2948 | free(tctx); |
| 2949 | ret = LY_EVALID; |
| 2950 | goto cleanup; |
| 2951 | } |
| 2952 | } |
| 2953 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2954 | /* store information for the following processing */ |
| 2955 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2956 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2957 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2958 | /* prepare next loop */ |
| 2959 | tctx_prev = tctx; |
| 2960 | tctx = calloc(1, sizeof *tctx); |
| 2961 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2962 | } |
| 2963 | free(tctx); |
| 2964 | |
| 2965 | /* allocate type according to the basetype */ |
| 2966 | switch (basetype) { |
| 2967 | case LY_TYPE_BINARY: |
| 2968 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2969 | break; |
| 2970 | case LY_TYPE_BITS: |
| 2971 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2972 | break; |
| 2973 | case LY_TYPE_BOOL: |
| 2974 | case LY_TYPE_EMPTY: |
| 2975 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2976 | break; |
| 2977 | case LY_TYPE_DEC64: |
| 2978 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2979 | break; |
| 2980 | case LY_TYPE_ENUM: |
| 2981 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2982 | break; |
| 2983 | case LY_TYPE_IDENT: |
| 2984 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2985 | break; |
| 2986 | case LY_TYPE_INST: |
| 2987 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2988 | break; |
| 2989 | case LY_TYPE_LEAFREF: |
| 2990 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2991 | break; |
| 2992 | case LY_TYPE_STRING: |
| 2993 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2994 | break; |
| 2995 | case LY_TYPE_UNION: |
| 2996 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2997 | break; |
| 2998 | case LY_TYPE_INT8: |
| 2999 | case LY_TYPE_UINT8: |
| 3000 | case LY_TYPE_INT16: |
| 3001 | case LY_TYPE_UINT16: |
| 3002 | case LY_TYPE_INT32: |
| 3003 | case LY_TYPE_UINT32: |
| 3004 | case LY_TYPE_INT64: |
| 3005 | case LY_TYPE_UINT64: |
| 3006 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3007 | break; |
| 3008 | case LY_TYPE_UNKNOWN: |
| 3009 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3010 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3011 | ret = LY_EVALID; |
| 3012 | goto cleanup; |
| 3013 | } |
| 3014 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3015 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3016 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3017 | ly_data_type2str[basetype]); |
| 3018 | free(*type); |
| 3019 | (*type) = NULL; |
| 3020 | ret = LY_EVALID; |
| 3021 | goto cleanup; |
| 3022 | } |
| 3023 | |
| 3024 | /* get restrictions from the referred typedefs */ |
| 3025 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3026 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3027 | |
| 3028 | /* remember the typedef context for circular check */ |
| 3029 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3030 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3031 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3032 | base = tctx->tpdf->type.compiled; |
| 3033 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3034 | } 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] | 3035 | /* no change, just use the type information from the base */ |
| 3036 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3037 | ++base->refcount; |
| 3038 | continue; |
| 3039 | } |
| 3040 | |
| 3041 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3042 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3043 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3044 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3045 | ret = LY_EVALID; |
| 3046 | goto cleanup; |
| 3047 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3048 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3049 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3050 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3051 | ret = LY_EVALID; |
| 3052 | goto cleanup; |
| 3053 | } |
| 3054 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3055 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3056 | /* TODO user type plugins */ |
| 3057 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3058 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3059 | 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] | 3060 | 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] | 3061 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3062 | LY_CHECK_GOTO(ret, cleanup); |
| 3063 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3064 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3065 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3066 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3067 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3068 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3069 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3070 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3071 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3072 | /* TODO user type plugins */ |
| 3073 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3074 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3075 | 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] | 3076 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3077 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3078 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3079 | free(*type); |
| 3080 | (*type) = base; |
| 3081 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3082 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3083 | if (dflt && !(*type)->dflt) { |
| 3084 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3085 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3086 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3087 | (*type)->dflt->realtype = (*type); |
| 3088 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3089 | 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] | 3090 | 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] | 3091 | if (err) { |
| 3092 | ly_err_print(err); |
| 3093 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3094 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3095 | ly_err_free(err); |
| 3096 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3097 | if (ret == LY_EINCOMPLETE) { |
| 3098 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3099 | 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] | 3100 | |
| 3101 | /* but in general result is so far ok */ |
| 3102 | ret = LY_SUCCESS; |
| 3103 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3104 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3105 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3106 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3107 | 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] | 3108 | |
| 3109 | cleanup: |
| 3110 | ly_set_erase(&tpdf_chain, free); |
| 3111 | return ret; |
| 3112 | } |
| 3113 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3114 | /** |
| 3115 | * @brief Compile status information of the given node. |
| 3116 | * |
| 3117 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3118 | * has the status correctly set during the compilation. |
| 3119 | * |
| 3120 | * @param[in] ctx Compile context |
| 3121 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3122 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3123 | * the compatibility with the parent's status value. |
| 3124 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3125 | * @return LY_ERR value. |
| 3126 | */ |
| 3127 | static LY_ERR |
| 3128 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3129 | { |
| 3130 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3131 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3132 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3133 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3134 | if ((parent_flags & 0x3) != 0x3) { |
| 3135 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3136 | * combination of bits (0x3) which marks the uses_status value */ |
| 3137 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3138 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3139 | } |
| 3140 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3141 | } else { |
| 3142 | (*node_flags) |= LYS_STATUS_CURR; |
| 3143 | } |
| 3144 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3145 | /* check status compatibility with the parent */ |
| 3146 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3147 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3148 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3149 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3150 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3151 | } else { /* LYS_STATUS_DEPRC */ |
| 3152 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3153 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3154 | } |
| 3155 | return LY_EVALID; |
| 3156 | } |
| 3157 | } |
| 3158 | return LY_SUCCESS; |
| 3159 | } |
| 3160 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3161 | /** |
| 3162 | * @brief Check uniqness of the node/action/notification name. |
| 3163 | * |
| 3164 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3165 | * structures, but they share the namespace so we need to check their name collisions. |
| 3166 | * |
| 3167 | * @param[in] ctx Compile context. |
| 3168 | * @param[in] children List (linked list) of data nodes to go through. |
| 3169 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3170 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3171 | * @param[in] name Name of the item to find in the given lists. |
| 3172 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3173 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3174 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3175 | */ |
| 3176 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3177 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions, |
| 3178 | const struct lysc_notif *notifs, const char *name, void *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3179 | { |
| 3180 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3181 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3182 | |
| 3183 | LY_LIST_FOR(children, iter) { |
| 3184 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3185 | goto error; |
| 3186 | } |
| 3187 | } |
| 3188 | LY_ARRAY_FOR(actions, u) { |
| 3189 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3190 | goto error; |
| 3191 | } |
| 3192 | } |
| 3193 | LY_ARRAY_FOR(notifs, u) { |
| 3194 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3195 | goto error; |
| 3196 | } |
| 3197 | } |
| 3198 | return LY_SUCCESS; |
| 3199 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3200 | 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] | 3201 | return LY_EEXIST; |
| 3202 | } |
| 3203 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3204 | 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] | 3205 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3206 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3207 | * @brief Compile parsed RPC/action schema node information. |
| 3208 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3209 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3210 | * @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] | 3211 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3212 | * @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). |
| 3213 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3214 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3215 | */ |
| 3216 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3217 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3218 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3219 | { |
| 3220 | LY_ERR ret = LY_SUCCESS; |
| 3221 | struct lysp_node *child_p; |
| 3222 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3223 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3224 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3225 | lysc_update_path(ctx, parent, action_p->name); |
| 3226 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3227 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3228 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3229 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3230 | action_p->name, action)) { |
| 3231 | return LY_EVALID; |
| 3232 | } |
| 3233 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3234 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3235 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3236 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3237 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3238 | return LY_EVALID; |
| 3239 | } |
| 3240 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3241 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3242 | action->module = ctx->mod; |
| 3243 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3244 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3245 | action->sp = action_p; |
| 3246 | } |
| 3247 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3248 | |
| 3249 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3250 | * 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] | 3251 | 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] | 3252 | |
| 3253 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3254 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3255 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3256 | 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] | 3257 | 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] | 3258 | |
| 3259 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3260 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3261 | 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] | 3262 | 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] | 3263 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3264 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3265 | 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] | 3266 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3267 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3268 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3269 | |
| 3270 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3271 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3272 | 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] | 3273 | 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] | 3274 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3275 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3276 | 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] | 3277 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3278 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3279 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3280 | |
| 3281 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3282 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3283 | ly_set_add(&ctx->xpath, action, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3284 | } |
| 3285 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3286 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3287 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3288 | return ret; |
| 3289 | } |
| 3290 | |
| 3291 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3292 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3293 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3294 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3295 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3296 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3297 | * @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] | 3298 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3299 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3300 | */ |
| 3301 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3302 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3303 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3304 | { |
| 3305 | LY_ERR ret = LY_SUCCESS; |
| 3306 | struct lysp_node *child_p; |
| 3307 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3308 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3309 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3310 | lysc_update_path(ctx, parent, notif_p->name); |
| 3311 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3312 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3313 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3314 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3315 | notif_p->name, notif)) { |
| 3316 | return LY_EVALID; |
| 3317 | } |
| 3318 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3319 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3320 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3321 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3322 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3323 | return LY_EVALID; |
| 3324 | } |
| 3325 | |
| 3326 | notif->nodetype = LYS_NOTIF; |
| 3327 | notif->module = ctx->mod; |
| 3328 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3329 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3330 | notif->sp = notif_p; |
| 3331 | } |
| 3332 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3333 | |
| 3334 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3335 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3336 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3337 | |
| 3338 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3339 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3340 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3341 | 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] | 3342 | 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] | 3343 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3344 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3345 | ly_set_add(&ctx->xpath, notif, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3346 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3347 | 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] | 3348 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3349 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3350 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3351 | 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] | 3352 | } |
| 3353 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3354 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3355 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3356 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3357 | return ret; |
| 3358 | } |
| 3359 | |
| 3360 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3361 | * @brief Compile parsed container node information. |
| 3362 | * @param[in] ctx Compile context |
| 3363 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3364 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3365 | * is enriched with the container-specific information. |
| 3366 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3367 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3368 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3369 | 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] | 3370 | { |
| 3371 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3372 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3373 | struct lysp_node *child_p; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3374 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3375 | LY_ERR ret = LY_SUCCESS; |
| 3376 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3377 | if (cont_p->presence) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3378 | /* explicit presence */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3379 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3380 | } else if (cont_p->musts) { |
| 3381 | /* container with a must condition */ |
| 3382 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a \"must\" condition.", cont_p->name); |
| 3383 | cont->flags |= LYS_PRESENCE; |
| 3384 | } else if (cont_p->parent) { |
| 3385 | if (cont_p->parent->nodetype == LYS_CHOICE) { |
| 3386 | /* container is an implicit case, so its existence decides the existence of the whole case */ |
| 3387 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it is an implicit case of choice \"%s\".", |
| 3388 | cont_p->name, cont_p->parent->name); |
| 3389 | cont->flags |= LYS_PRESENCE; |
| 3390 | } else if ((cont_p->parent->nodetype == LYS_CASE) |
| 3391 | && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) { |
| 3392 | /* container is the only node in a case, so its existence decides the existence of the whole case */ |
| 3393 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it is the only data node of case \"%s\".", |
| 3394 | cont_p->name, cont_p->parent->name); |
| 3395 | cont->flags |= LYS_PRESENCE; |
| 3396 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3397 | } |
| 3398 | |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3399 | /* more cases when the container has meaning but is kept NP for convenience: |
| 3400 | * - when condition |
| 3401 | * - direct child action/notification |
| 3402 | */ |
| 3403 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3404 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3405 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3406 | } |
| 3407 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3408 | 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] | 3409 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3410 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3411 | ly_set_add(&ctx->xpath, cont, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3412 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3413 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3414 | 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] | 3415 | |
| 3416 | done: |
| 3417 | return ret; |
| 3418 | } |
| 3419 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3420 | /* |
| 3421 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3422 | * @param[in] ctx Compile context. |
| 3423 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3424 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3425 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3426 | * @return LY_ERR value. |
| 3427 | */ |
| 3428 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3429 | 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] | 3430 | { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3431 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3432 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3433 | 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] | 3434 | leaf->units ? NULL : &leaf->units)); |
| 3435 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3436 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3437 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3438 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3439 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3440 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3441 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3442 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3443 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3444 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3445 | } |
| 3446 | } else { |
| 3447 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3448 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3449 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3450 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3451 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3452 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3453 | } |
| 3454 | } |
| 3455 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3456 | /* 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] | 3457 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3458 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3459 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3460 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3461 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3462 | /* 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] | 3463 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3464 | } |
| 3465 | } |
| 3466 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3467 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3468 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3469 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3470 | return LY_EVALID; |
| 3471 | } |
| 3472 | } |
| 3473 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3474 | return LY_SUCCESS; |
| 3475 | } |
| 3476 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3477 | /** |
| 3478 | * @brief Compile parsed leaf node information. |
| 3479 | * @param[in] ctx Compile context |
| 3480 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3481 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3482 | * is enriched with the leaf-specific information. |
| 3483 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3484 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3485 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3486 | 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] | 3487 | { |
| 3488 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3489 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3490 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3491 | LY_ERR ret = LY_SUCCESS; |
| 3492 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3493 | 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] | 3494 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3495 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3496 | ly_set_add(&ctx->xpath, leaf, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3497 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3498 | if (leaf_p->units) { |
| 3499 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3500 | leaf->flags |= LYS_SET_UNITS; |
| 3501 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3502 | |
| 3503 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3504 | leaf->dflt = (void*)leaf_p->dflt; |
| 3505 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3506 | if (ret) { |
| 3507 | leaf->dflt = NULL; |
| 3508 | return ret; |
| 3509 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3510 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3511 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3512 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3513 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3514 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3515 | leaf->dflt->realtype = leaf->type; |
| 3516 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3517 | 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] | 3518 | 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] | 3519 | leaf->dflt->realtype->refcount++; |
| 3520 | if (err) { |
| 3521 | ly_err_print(err); |
| 3522 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3523 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3524 | ly_err_free(err); |
| 3525 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3526 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3527 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3528 | 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] | 3529 | |
| 3530 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3531 | ret = LY_SUCCESS; |
| 3532 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3533 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3534 | leaf->flags |= LYS_SET_DFLT; |
| 3535 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3536 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3537 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3538 | done: |
| 3539 | return ret; |
| 3540 | } |
| 3541 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3542 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3543 | * @brief Compile parsed leaf-list node information. |
| 3544 | * @param[in] ctx Compile context |
| 3545 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3546 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3547 | * is enriched with the leaf-list-specific information. |
| 3548 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3549 | */ |
| 3550 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3551 | 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] | 3552 | { |
| 3553 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3554 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3555 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3556 | LY_ERR ret = LY_SUCCESS; |
| 3557 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3558 | 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] | 3559 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3560 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3561 | ly_set_add(&ctx->xpath, llist, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3562 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3563 | if (llist_p->units) { |
| 3564 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3565 | llist->flags |= LYS_SET_UNITS; |
| 3566 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3567 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3568 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3569 | llist->dflts = (void*)llist_p->dflts; |
| 3570 | 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] | 3571 | if (ret != LY_SUCCESS) { |
| 3572 | /* make sure the defaults are freed correctly */ |
| 3573 | if (llist_p->dflts) { |
| 3574 | llist->dflts = NULL; |
| 3575 | } |
| 3576 | return ret; |
| 3577 | } |
| 3578 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3579 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3580 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3581 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done); |
| 3582 | 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] | 3583 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3584 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3585 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3586 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3587 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3588 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3589 | llist->dflts[u]->realtype = llist->type; |
| 3590 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3591 | 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] | 3592 | 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] | 3593 | llist->dflts[u]->realtype->refcount++; |
| 3594 | if (err) { |
| 3595 | ly_err_print(err); |
| 3596 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3597 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3598 | ly_err_free(err); |
| 3599 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3600 | if (ret == LY_EINCOMPLETE) { |
| 3601 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3602 | 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] | 3603 | |
| 3604 | /* but in general result is so far ok */ |
| 3605 | ret = LY_SUCCESS; |
| 3606 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3607 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3608 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3609 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3610 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3611 | 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] | 3612 | /* configuration data values must be unique - so check the default values */ |
| 3613 | LY_ARRAY_FOR(llist->dflts, u) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3614 | for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3615 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3616 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3617 | 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] | 3618 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3619 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3620 | if (dynamic) { |
| 3621 | free((char*)val); |
| 3622 | } |
| 3623 | return LY_EVALID; |
| 3624 | } |
| 3625 | } |
| 3626 | } |
| 3627 | } |
| 3628 | |
| 3629 | /* 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] | 3630 | |
| 3631 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3632 | if (llist->min) { |
| 3633 | llist->flags |= LYS_MAND_TRUE; |
| 3634 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3635 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3636 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3637 | done: |
| 3638 | return ret; |
| 3639 | } |
| 3640 | |
| 3641 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3642 | * @brief Compile information about list's uniques. |
| 3643 | * @param[in] ctx Compile context. |
| 3644 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3645 | * @param[in] uniques Sized array list of unique statements. |
| 3646 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3647 | * @return LY_ERR value. |
| 3648 | */ |
| 3649 | static LY_ERR |
| 3650 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3651 | { |
| 3652 | LY_ERR ret = LY_SUCCESS; |
| 3653 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3654 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3655 | const char *keystr, *delim; |
| 3656 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3657 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3658 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3659 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3660 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3661 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3662 | config = -1; |
| 3663 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3664 | keystr = uniques[v]; |
| 3665 | while (keystr) { |
| 3666 | delim = strpbrk(keystr, " \t\n"); |
| 3667 | if (delim) { |
| 3668 | len = delim - keystr; |
| 3669 | while (isspace(*delim)) { |
| 3670 | ++delim; |
| 3671 | } |
| 3672 | } else { |
| 3673 | len = strlen(keystr); |
| 3674 | } |
| 3675 | |
| 3676 | /* unique node must be present */ |
| 3677 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3678 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3679 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3680 | if (ret != LY_SUCCESS) { |
| 3681 | if (ret == LY_EDENIED) { |
| 3682 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3683 | "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] | 3684 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3685 | } |
| 3686 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3687 | } else if (flags) { |
| 3688 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3689 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3690 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3691 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3692 | } |
| 3693 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3694 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3695 | /* all referenced leafs must be of the same config type */ |
| 3696 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3697 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3698 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3699 | return LY_EVALID; |
| 3700 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3701 | config = 1; |
| 3702 | } else { /* LYS_CONFIG_R */ |
| 3703 | config = 0; |
| 3704 | } |
| 3705 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3706 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3707 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3708 | if (parent->nodetype == LYS_LIST) { |
| 3709 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3710 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3711 | return LY_EVALID; |
| 3712 | } |
| 3713 | } |
| 3714 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3715 | /* check status */ |
| 3716 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3717 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3718 | |
| 3719 | /* mark leaf as unique */ |
| 3720 | (*key)->flags |= LYS_UNIQUE; |
| 3721 | |
| 3722 | /* next unique value in line */ |
| 3723 | keystr = delim; |
| 3724 | } |
| 3725 | /* next unique definition */ |
| 3726 | } |
| 3727 | |
| 3728 | return LY_SUCCESS; |
| 3729 | } |
| 3730 | |
| 3731 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3732 | * @brief Compile parsed list node information. |
| 3733 | * @param[in] ctx Compile context |
| 3734 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3735 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3736 | * is enriched with the list-specific information. |
| 3737 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3738 | */ |
| 3739 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3740 | 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] | 3741 | { |
| 3742 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3743 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3744 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3745 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3746 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3747 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3748 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3749 | LY_ERR ret = LY_SUCCESS; |
| 3750 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3751 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3752 | if (list->min) { |
| 3753 | list->flags |= LYS_MAND_TRUE; |
| 3754 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3755 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3756 | |
| 3757 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3758 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3759 | } |
| 3760 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3761 | 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] | 3762 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3763 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3764 | ly_set_add(&ctx->xpath, list, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3765 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3766 | |
| 3767 | /* keys */ |
| 3768 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3769 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3770 | return LY_EVALID; |
| 3771 | } |
| 3772 | |
| 3773 | /* find all the keys (must be direct children) */ |
| 3774 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3775 | if (!keystr) { |
| 3776 | /* keyless list */ |
| 3777 | list->flags |= LYS_KEYLESS; |
| 3778 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3779 | while (keystr) { |
| 3780 | delim = strpbrk(keystr, " \t\n"); |
| 3781 | if (delim) { |
| 3782 | len = delim - keystr; |
| 3783 | while (isspace(*delim)) { |
| 3784 | ++delim; |
| 3785 | } |
| 3786 | } else { |
| 3787 | len = strlen(keystr); |
| 3788 | } |
| 3789 | |
| 3790 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3791 | 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] | 3792 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3793 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3794 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3795 | return LY_EVALID; |
| 3796 | } |
| 3797 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3798 | if (key->flags & LYS_KEY) { |
| 3799 | /* the node was already marked as a key */ |
| 3800 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3801 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3802 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3803 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3804 | |
| 3805 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3806 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3807 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3808 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3809 | return LY_EVALID; |
| 3810 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3811 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3812 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3813 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3814 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3815 | "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] | 3816 | return LY_EVALID; |
| 3817 | } |
| 3818 | } else { |
| 3819 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3820 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3821 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3822 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3823 | return LY_EVALID; |
| 3824 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3825 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3826 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3827 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3828 | return LY_EVALID; |
| 3829 | } |
| 3830 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3831 | |
| 3832 | /* check status */ |
| 3833 | 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] | 3834 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3835 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3836 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3837 | if (key->dflt) { |
| 3838 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3839 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3840 | free(key->dflt); |
| 3841 | key->dflt = NULL; |
| 3842 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3843 | } |
| 3844 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3845 | key->flags |= LYS_KEY; |
| 3846 | |
| 3847 | /* move it to the correct position */ |
| 3848 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3849 | /* fix links in closest previous siblings of the key */ |
| 3850 | if (key->next) { |
| 3851 | key->next->prev = key->prev; |
| 3852 | } else { |
| 3853 | /* last child */ |
| 3854 | list->child->prev = key->prev; |
| 3855 | } |
| 3856 | if (key->prev->next) { |
| 3857 | key->prev->next = key->next; |
| 3858 | } |
| 3859 | /* fix links in the key */ |
| 3860 | if (prev_key) { |
| 3861 | key->prev = (struct lysc_node*)prev_key; |
| 3862 | key->next = prev_key->next; |
| 3863 | } else { |
| 3864 | key->prev = list->child->prev; |
| 3865 | key->next = list->child; |
| 3866 | } |
| 3867 | /* fix links in closes future siblings of the key */ |
| 3868 | if (prev_key) { |
| 3869 | if (prev_key->next) { |
| 3870 | prev_key->next->prev = (struct lysc_node*)key; |
| 3871 | } else { |
| 3872 | list->child->prev = (struct lysc_node*)key; |
| 3873 | } |
| 3874 | prev_key->next = (struct lysc_node*)key; |
| 3875 | } else { |
| 3876 | list->child->prev = (struct lysc_node*)key; |
| 3877 | } |
| 3878 | /* fix links in parent */ |
| 3879 | if (!key->prev->next) { |
| 3880 | list->child = (struct lysc_node*)key; |
| 3881 | } |
| 3882 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3883 | |
| 3884 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3885 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3886 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3887 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | /* uniques */ |
| 3891 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3892 | 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] | 3893 | } |
| 3894 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3895 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3896 | 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] | 3897 | |
| 3898 | done: |
| 3899 | return ret; |
| 3900 | } |
| 3901 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3902 | /** |
| 3903 | * @brief Do some checks and set the default choice's case. |
| 3904 | * |
| 3905 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3906 | * |
| 3907 | * @param[in] ctx Compile context. |
| 3908 | * @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, |
| 3909 | * not the reference to the imported module. |
| 3910 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3911 | * @return LY_ERR value. |
| 3912 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3913 | static LY_ERR |
| 3914 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3915 | { |
| 3916 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3917 | const char *prefix = NULL, *name; |
| 3918 | size_t prefix_len = 0; |
| 3919 | |
| 3920 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3921 | name = strchr(dflt, ':'); |
| 3922 | if (name) { |
| 3923 | prefix = dflt; |
| 3924 | prefix_len = name - prefix; |
| 3925 | ++name; |
| 3926 | } else { |
| 3927 | name = dflt; |
| 3928 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3929 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3930 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3931 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3932 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3933 | prefix_len, prefix); |
| 3934 | return LY_EVALID; |
| 3935 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3936 | 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] | 3937 | if (!ch->dflt) { |
| 3938 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3939 | "Default case \"%s\" not found.", dflt); |
| 3940 | return LY_EVALID; |
| 3941 | } |
| 3942 | /* no mandatory nodes directly under the default case */ |
| 3943 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3944 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3945 | break; |
| 3946 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3947 | if (iter->flags & LYS_MAND_TRUE) { |
| 3948 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3949 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3950 | return LY_EVALID; |
| 3951 | } |
| 3952 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3953 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3954 | return LY_SUCCESS; |
| 3955 | } |
| 3956 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3957 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3958 | 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] | 3959 | { |
| 3960 | struct lys_module *mod; |
| 3961 | const char *prefix = NULL, *name; |
| 3962 | size_t prefix_len = 0; |
| 3963 | struct lysc_node_case *cs; |
| 3964 | struct lysc_node *node; |
| 3965 | |
| 3966 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3967 | name = strchr(dflt, ':'); |
| 3968 | if (name) { |
| 3969 | prefix = dflt; |
| 3970 | prefix_len = name - prefix; |
| 3971 | ++name; |
| 3972 | } else { |
| 3973 | name = dflt; |
| 3974 | } |
| 3975 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3976 | if (prefix) { |
| 3977 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3978 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3979 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3980 | "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] | 3981 | return LY_EVALID; |
| 3982 | } |
| 3983 | } else { |
| 3984 | mod = ctx->mod; |
| 3985 | } |
| 3986 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3987 | 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] | 3988 | if (!cs) { |
| 3989 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3990 | "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] | 3991 | return LY_EVALID; |
| 3992 | } |
| 3993 | |
| 3994 | /* check that there is no mandatory node */ |
| 3995 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3996 | if (node->parent != (struct lysc_node*)cs) { |
| 3997 | break; |
| 3998 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3999 | if (node->flags & LYS_MAND_TRUE) { |
| 4000 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4001 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4002 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4003 | return LY_EVALID; |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | /* set the default case in choice */ |
| 4008 | ch->dflt = cs; |
| 4009 | cs->flags |= LYS_SET_DFLT; |
| 4010 | |
| 4011 | return LY_SUCCESS; |
| 4012 | } |
| 4013 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4014 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4015 | * @brief Compile parsed choice node information. |
| 4016 | * @param[in] ctx Compile context |
| 4017 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4018 | * @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] | 4019 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4020 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4021 | */ |
| 4022 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4023 | 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] | 4024 | { |
| 4025 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4026 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4027 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4028 | LY_ERR ret = LY_SUCCESS; |
| 4029 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4030 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4031 | if (child_p->nodetype == LYS_CASE) { |
| 4032 | 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] | 4033 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4034 | } |
| 4035 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4036 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4037 | } |
| 4038 | } |
| 4039 | |
| 4040 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4041 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4042 | 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] | 4043 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4044 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4045 | return ret; |
| 4046 | } |
| 4047 | |
| 4048 | /** |
| 4049 | * @brief Compile parsed anydata or anyxml node information. |
| 4050 | * @param[in] ctx Compile context |
| 4051 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4052 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4053 | * is enriched with the any-specific information. |
| 4054 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4055 | */ |
| 4056 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4057 | 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] | 4058 | { |
| 4059 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4060 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4061 | unsigned int u; |
| 4062 | LY_ERR ret = LY_SUCCESS; |
| 4063 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4064 | 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] | 4065 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4066 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4067 | ly_set_add(&ctx->xpath, any, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4068 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4069 | |
| 4070 | if (any->flags & LYS_CONFIG_W) { |
| 4071 | 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] | 4072 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4073 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4074 | done: |
| 4075 | return ret; |
| 4076 | } |
| 4077 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4078 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4079 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4080 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4081 | * |
| 4082 | * @param[in] ctx Compile context |
| 4083 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4084 | * the choice itself is expected instead of a specific case node. |
| 4085 | * @param[in] node Schema node to connect into the list. |
| 4086 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4087 | * 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] | 4088 | */ |
| 4089 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4090 | 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] | 4091 | { |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4092 | struct lysc_node **children, *start, *end; |
| 4093 | const struct lys_module *mod; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4094 | |
| 4095 | if (node->nodetype == LYS_CASE) { |
| 4096 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4097 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4098 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4099 | } |
| 4100 | if (children) { |
| 4101 | if (!(*children)) { |
| 4102 | /* first child */ |
| 4103 | *children = node; |
| 4104 | } else if (*children != node) { |
| 4105 | /* by the condition in previous branch we cover the choice/case children |
| 4106 | * - the children list is shared by the choice and the the first case, in addition |
| 4107 | * the first child of each case must be referenced from the case node. So the node is |
| 4108 | * actually always already inserted in case it is the first children - so here such |
| 4109 | * a situation actually corresponds to the first branch */ |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4110 | if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module) |
| 4111 | && (strcmp((*children)->prev->module->name, node->module->name) > 0)) { |
| 4112 | /* some augments are already connected and we are connecting new ones, |
| 4113 | * keep module name order and insert the node into the children list */ |
| 4114 | end = (*children); |
| 4115 | do { |
| 4116 | end = end->prev; |
| 4117 | mod = end->module; |
| 4118 | while (end->prev->module == mod) { |
| 4119 | end = end->prev; |
| 4120 | } |
| 4121 | } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module) |
| 4122 | && (strcmp(mod->name, node->module->name) > 0)); |
| 4123 | |
| 4124 | /* we have the last existing node after our node, easily get the first before and connect it */ |
| 4125 | start = end->prev; |
| 4126 | start->next = node; |
| 4127 | node->next = end; |
| 4128 | end->prev = node; |
| 4129 | node->prev = start; |
| 4130 | } else { |
| 4131 | /* insert at the end of the parent's children list */ |
| 4132 | (*children)->prev->next = node; |
| 4133 | node->prev = (*children)->prev; |
| 4134 | (*children)->prev = node; |
| 4135 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4136 | |
| 4137 | /* check the name uniqueness */ |
| 4138 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4139 | lysc_node_notifs(parent), node->name, node)) { |
| 4140 | return LY_EEXIST; |
| 4141 | } |
| 4142 | } |
| 4143 | } |
| 4144 | return LY_SUCCESS; |
| 4145 | } |
| 4146 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4147 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4148 | * @brief Prepare the case structure in choice node for the new data node. |
| 4149 | * |
| 4150 | * 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 |
| 4151 | * created in the choice when the first child was processed. |
| 4152 | * |
| 4153 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4154 | * @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, |
| 4155 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4156 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4157 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4158 | * @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, |
| 4159 | * 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] | 4160 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4161 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4162 | 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] | 4163 | { |
| 4164 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4165 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4166 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4167 | unsigned int u; |
| 4168 | LY_ERR ret; |
| 4169 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4170 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4171 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4172 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4173 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4174 | return NULL; \ |
| 4175 | } \ |
| 4176 | } |
| 4177 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4178 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4179 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4180 | |
| 4181 | /* we have to add an implicit case node into the parent choice */ |
| 4182 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4183 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4184 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4185 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4186 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4187 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4188 | /* the case is already present since the child is not its first children */ |
| 4189 | return (struct lysc_node_case*)ch->cases->prev; |
| 4190 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4191 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4192 | |
| 4193 | /* explicit parent case is not present (this is its first child) */ |
| 4194 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4195 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4196 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4197 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4198 | cs->sp = node_p; |
| 4199 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4200 | /* 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] | 4201 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4202 | |
| 4203 | if (node_p->when) { |
| 4204 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4205 | 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] | 4206 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4207 | |
| 4208 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4209 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4210 | ly_set_add(&ctx->xpath, cs, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4211 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4212 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4213 | 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] | 4214 | } else { |
| 4215 | LOGINT(ctx->ctx); |
| 4216 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4217 | } |
| 4218 | cs->module = ctx->mod; |
| 4219 | cs->prev = (struct lysc_node*)cs; |
| 4220 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4221 | 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] | 4222 | cs->child = child; |
| 4223 | |
| 4224 | return cs; |
| 4225 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4226 | if (cs) { |
| 4227 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4228 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4229 | return NULL; |
| 4230 | |
| 4231 | #undef UNIQUE_CHECK |
| 4232 | } |
| 4233 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4234 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4235 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4236 | * |
| 4237 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4238 | * @param[in] node Target node where the config is supposed to be changed. |
| 4239 | * @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] | 4240 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4241 | * 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] | 4242 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4243 | * @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] | 4244 | * @return LY_ERR value. |
| 4245 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4246 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4247 | 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] | 4248 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4249 | { |
| 4250 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4251 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4252 | |
| 4253 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4254 | /* nothing to do */ |
| 4255 | return LY_SUCCESS; |
| 4256 | } |
| 4257 | |
| 4258 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4259 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4260 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4261 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4262 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4263 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4264 | return LY_EVALID; |
| 4265 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4266 | node->flags |= LYS_SET_CONFIG; |
| 4267 | } else { |
| 4268 | if (node->flags & LYS_SET_CONFIG) { |
| 4269 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4270 | /* setting config flags, but have node with explicit config true */ |
| 4271 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4272 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4273 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4274 | return LY_EVALID; |
| 4275 | } |
| 4276 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4277 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4278 | return LY_SUCCESS; |
| 4279 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4280 | } |
| 4281 | node->flags &= ~LYS_CONFIG_MASK; |
| 4282 | node->flags |= config; |
| 4283 | |
| 4284 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4285 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4286 | 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] | 4287 | } |
| 4288 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4289 | return LY_SUCCESS; |
| 4290 | } |
| 4291 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4292 | /** |
| 4293 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4294 | * |
| 4295 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4296 | * the flag to such parents from a mandatory children. |
| 4297 | * |
| 4298 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4299 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4300 | * (mandatory children was removed). |
| 4301 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4302 | void |
| 4303 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4304 | { |
| 4305 | struct lysc_node *iter; |
| 4306 | |
| 4307 | if (add) { /* set flag */ |
| 4308 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4309 | parent = parent->parent) { |
| 4310 | parent->flags |= LYS_MAND_TRUE; |
| 4311 | } |
| 4312 | } else { /* unset flag */ |
| 4313 | 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] | 4314 | 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] | 4315 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4316 | /* there is another mandatory node */ |
| 4317 | return; |
| 4318 | } |
| 4319 | } |
| 4320 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4321 | parent->flags &= ~LYS_MAND_TRUE; |
| 4322 | } |
| 4323 | } |
| 4324 | } |
| 4325 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4326 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4327 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4328 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4329 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4330 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4331 | */ |
| 4332 | static void |
| 4333 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4334 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4335 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4336 | size_t len; |
| 4337 | |
| 4338 | len = strlen(aug_p->nodeid); |
| 4339 | LY_ARRAY_FOR(result, v) { |
| 4340 | if (strlen(result[v]->nodeid) <= len) { |
| 4341 | continue; |
| 4342 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4343 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4344 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4345 | 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] | 4346 | break; |
| 4347 | } |
| 4348 | } |
| 4349 | result[v] = aug_p; |
| 4350 | LY_ARRAY_INCREMENT(result); |
| 4351 | } |
| 4352 | |
| 4353 | /** |
| 4354 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4355 | * |
| 4356 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4357 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4358 | * |
| 4359 | * @param[in] ctx Compile context. |
| 4360 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4361 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4362 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4363 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4364 | * @return LY_ERR value. |
| 4365 | */ |
| 4366 | LY_ERR |
| 4367 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4368 | { |
| 4369 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4370 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4371 | |
| 4372 | assert(augments); |
| 4373 | |
| 4374 | /* get count of the augments in module and all its submodules */ |
| 4375 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4376 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4377 | } |
| 4378 | LY_ARRAY_FOR(inc_p, u) { |
| 4379 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4380 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4381 | } |
| 4382 | } |
| 4383 | |
| 4384 | if (!count) { |
| 4385 | *augments = NULL; |
| 4386 | return LY_SUCCESS; |
| 4387 | } |
| 4388 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4389 | |
| 4390 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4391 | * together, so there can be even /z/y betwwen them. */ |
| 4392 | LY_ARRAY_FOR(aug_p, u) { |
| 4393 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4394 | } |
| 4395 | LY_ARRAY_FOR(inc_p, u) { |
| 4396 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4397 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4398 | } |
| 4399 | } |
| 4400 | |
| 4401 | *augments = result; |
| 4402 | return LY_SUCCESS; |
| 4403 | } |
| 4404 | |
| 4405 | /** |
| 4406 | * @brief Compile the parsed augment connecting it into its target. |
| 4407 | * |
| 4408 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4409 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4410 | * are already implemented and compiled. |
| 4411 | * |
| 4412 | * @param[in] ctx Compile context. |
| 4413 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4414 | * @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 |
| 4415 | * children in case of the augmenting uses data. |
| 4416 | * @return LY_SUCCESS on success. |
| 4417 | * @return LY_EVALID on failure. |
| 4418 | */ |
| 4419 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4420 | 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] | 4421 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4422 | LY_ERR ret = LY_SUCCESS, rc; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4423 | struct lysp_node *node_p, *case_node_p; |
| 4424 | struct lysc_node *target; /* target target of the augment */ |
| 4425 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4426 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4427 | struct lys_module **aug_mod; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4428 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4429 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4430 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4431 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4432 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4433 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4434 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4435 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4436 | 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] | 4437 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4438 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4439 | if (ret != LY_SUCCESS) { |
| 4440 | if (ret == LY_EDENIED) { |
| 4441 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4442 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4443 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4444 | } |
| 4445 | return LY_EVALID; |
| 4446 | } |
| 4447 | |
| 4448 | /* check for mandatory nodes |
| 4449 | * - new cases augmenting some choice can have mandatory nodes |
| 4450 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4451 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4452 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4453 | allow_mandatory = 1; |
| 4454 | } |
| 4455 | |
| 4456 | when_shared = NULL; |
| 4457 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4458 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4459 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4460 | && !((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] | 4461 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4462 | && !(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] | 4463 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4464 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4465 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4466 | return LY_EVALID; |
| 4467 | } |
| 4468 | |
| 4469 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4470 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4471 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4472 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4473 | } else { |
| 4474 | 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] | 4475 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4476 | } |
| 4477 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4478 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4479 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4480 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4481 | * 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] | 4482 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4483 | /* 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] | 4484 | 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] | 4485 | } else if (target->nodetype == LYS_CHOICE) { |
| 4486 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4487 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4488 | } else { |
| 4489 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4490 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4491 | if (!node) { |
| 4492 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4493 | break; |
| 4494 | } |
| 4495 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4496 | } |
| 4497 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4498 | 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] | 4499 | node->flags &= ~LYS_MAND_TRUE; |
| 4500 | lys_compile_mandatory_parents(target, 0); |
| 4501 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4502 | "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] | 4503 | return LY_EVALID; |
| 4504 | } |
| 4505 | |
| 4506 | /* pass augment's when to all the children */ |
| 4507 | if (aug_p->when) { |
| 4508 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4509 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4510 | 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] | 4511 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4512 | |
| 4513 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4514 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4515 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4516 | } |
| 4517 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4518 | when_shared = *when; |
| 4519 | } else { |
| 4520 | ++when_shared->refcount; |
| 4521 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4522 | |
| 4523 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4524 | /* 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] | 4525 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4526 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4527 | } |
| 4528 | } |
| 4529 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4530 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4531 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4532 | switch (target->nodetype) { |
| 4533 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4534 | 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] | 4535 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4536 | 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] | 4537 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4538 | break; |
| 4539 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4540 | 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] | 4541 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4542 | 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] | 4543 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4544 | break; |
| 4545 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4546 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4547 | if (aug_p->actions) { |
| 4548 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4549 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4550 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4551 | return LY_EVALID; |
| 4552 | } |
| 4553 | if (aug_p->notifs) { |
| 4554 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4555 | "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] | 4556 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4557 | return LY_EVALID; |
| 4558 | } |
| 4559 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4560 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4561 | /* add this module into the target module augmented_by, if not there already */ |
| 4562 | rc = LY_SUCCESS; |
| 4563 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4564 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4565 | rc = LY_EEXIST; |
| 4566 | break; |
| 4567 | } |
| 4568 | } |
| 4569 | if (!rc) { |
| 4570 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4571 | *aug_mod = ctx->mod; |
| 4572 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4573 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4574 | lysc_update_path(ctx, NULL, NULL); |
| 4575 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4576 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4577 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4578 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4579 | return ret; |
| 4580 | } |
| 4581 | |
| 4582 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4583 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4584 | * |
| 4585 | * @param[in] ctx Compile context. |
| 4586 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4587 | * @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] | 4588 | * @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] | 4589 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4590 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4591 | * 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] | 4592 | * @return LY_ERR value. |
| 4593 | */ |
| 4594 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4595 | 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] | 4596 | { |
| 4597 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4598 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4599 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4600 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4601 | return LY_EVALID; |
| 4602 | } |
| 4603 | |
| 4604 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4605 | /* check if node has default value */ |
| 4606 | if (node->nodetype & LYS_LEAF) { |
| 4607 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4608 | if (refine_flag) { |
| 4609 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4610 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4611 | return LY_EVALID; |
| 4612 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4613 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4614 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4615 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4616 | |
| 4617 | /* update the list of incomplete default values if needed */ |
| 4618 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4619 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4620 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4621 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4622 | free(leaf->dflt); |
| 4623 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4624 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4625 | } |
| 4626 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4627 | if (refine_flag) { |
| 4628 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4629 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4630 | return LY_EVALID; |
| 4631 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4632 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4633 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4634 | 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] | 4635 | return LY_EVALID; |
| 4636 | } |
| 4637 | |
| 4638 | node->flags &= ~LYS_MAND_FALSE; |
| 4639 | node->flags |= LYS_MAND_TRUE; |
| 4640 | lys_compile_mandatory_parents(node->parent, 1); |
| 4641 | } else { |
| 4642 | /* make mandatory false */ |
| 4643 | node->flags &= ~LYS_MAND_TRUE; |
| 4644 | node->flags |= LYS_MAND_FALSE; |
| 4645 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4646 | 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] | 4647 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4648 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4649 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4650 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4651 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4652 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4653 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4654 | } |
| 4655 | } |
| 4656 | return LY_SUCCESS; |
| 4657 | } |
| 4658 | |
| 4659 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4660 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4661 | * If present, also apply uses's modificators. |
| 4662 | * |
| 4663 | * @param[in] ctx Compile context |
| 4664 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4665 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4666 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4667 | * the compile context. |
| 4668 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4669 | */ |
| 4670 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4671 | 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] | 4672 | { |
| 4673 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4674 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4675 | /* 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] | 4676 | struct lysc_node_container context_node_fake = |
| 4677 | {.nodetype = LYS_CONTAINER, |
| 4678 | .module = ctx->mod, |
| 4679 | .flags = parent ? parent->flags : 0, |
| 4680 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4681 | .prev = (struct lysc_node*)&context_node_fake, |
| 4682 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4683 | struct lysp_grp *grp = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4684 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4685 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4686 | int found; |
| 4687 | const char *id, *name, *prefix; |
| 4688 | size_t prefix_len, name_len; |
| 4689 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4690 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4691 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4692 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4693 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4694 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4695 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4696 | struct lysp_augment **augments = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4697 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4698 | struct lysc_notif **notifs = NULL; |
| 4699 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4700 | |
| 4701 | /* search for the grouping definition */ |
| 4702 | found = 0; |
| 4703 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4704 | 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] | 4705 | if (prefix) { |
| 4706 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4707 | if (!mod) { |
| 4708 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4709 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4710 | return LY_EVALID; |
| 4711 | } |
| 4712 | } else { |
| 4713 | mod = ctx->mod_def; |
| 4714 | } |
| 4715 | if (mod == ctx->mod_def) { |
| 4716 | 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] | 4717 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4718 | LY_ARRAY_FOR(grp, u) { |
| 4719 | if (!strcmp(grp[u].name, name)) { |
| 4720 | grp = &grp[u]; |
| 4721 | found = 1; |
| 4722 | break; |
| 4723 | } |
| 4724 | } |
| 4725 | } |
| 4726 | } |
| 4727 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4728 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4729 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4730 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4731 | for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4732 | if (!strcmp(grp[u].name, name)) { |
| 4733 | grp = &grp[u]; |
| 4734 | found = 1; |
| 4735 | } |
| 4736 | } |
| 4737 | } |
| 4738 | if (!found && mod->parsed->includes) { |
| 4739 | /* ... and all the submodules */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4740 | for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4741 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4742 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4743 | for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4744 | if (!strcmp(grp[v].name, name)) { |
| 4745 | grp = &grp[v]; |
| 4746 | found = 1; |
| 4747 | } |
| 4748 | } |
| 4749 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4750 | } |
| 4751 | } |
| 4752 | } |
| 4753 | if (!found) { |
| 4754 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4755 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4756 | return LY_EVALID; |
| 4757 | } |
| 4758 | |
| 4759 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4760 | grp_stack_count = ctx->groupings.count; |
| 4761 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4762 | if (grp_stack_count == ctx->groupings.count) { |
| 4763 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4764 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4765 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4766 | return LY_EVALID; |
| 4767 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4768 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4769 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4770 | grp->flags |= LYS_USED_GRP; |
| 4771 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4772 | |
| 4773 | /* switch context's mod_def */ |
| 4774 | mod_old = ctx->mod_def; |
| 4775 | ctx->mod_def = mod; |
| 4776 | |
| 4777 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4778 | 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] | 4779 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4780 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4781 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4782 | /* 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] | 4783 | 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] | 4784 | |
| 4785 | /* some preparation for applying refines */ |
| 4786 | if (grp->data == node_p) { |
| 4787 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4788 | if (parent) { |
| 4789 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4790 | } else if (ctx->mod->compiled->data) { |
| 4791 | child = ctx->mod->compiled->data; |
| 4792 | } else { |
| 4793 | child = NULL; |
| 4794 | } |
| 4795 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4796 | } |
| 4797 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4798 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4799 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4800 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4801 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4802 | /* 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] | 4803 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4804 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4805 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4806 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 4807 | |
| 4808 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4809 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4810 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4811 | } |
| 4812 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4813 | when_shared = *when; |
| 4814 | } else { |
| 4815 | ++when_shared->refcount; |
| 4816 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4817 | |
| 4818 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4819 | /* 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] | 4820 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4821 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4822 | } |
| 4823 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4824 | } |
| 4825 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4826 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4827 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4828 | /* 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] | 4829 | 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] | 4830 | } |
| 4831 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4832 | /* compile actions */ |
| 4833 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4834 | if (actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4835 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4836 | 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] | 4837 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4838 | /* 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] | 4839 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 4840 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 4841 | 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] | 4842 | } |
| 4843 | } |
| 4844 | |
| 4845 | /* compile notifications */ |
| 4846 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4847 | if (notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4848 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4849 | 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] | 4850 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4851 | /* 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] | 4852 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 4853 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 4854 | 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] | 4855 | } |
| 4856 | } |
| 4857 | |
| 4858 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4859 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4860 | 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] | 4861 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4862 | 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] | 4863 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4864 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4865 | /* reload previous context's mod_def */ |
| 4866 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4867 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4868 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4869 | /* apply refine */ |
| 4870 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4871 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4872 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4873 | 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] | 4874 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4875 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4876 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4877 | |
| 4878 | /* default value */ |
| 4879 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4880 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4881 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4882 | "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.", |
| 4883 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4884 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4885 | } |
| 4886 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4887 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4888 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4889 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4890 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4891 | } |
| 4892 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4893 | struct ly_err_item *err = NULL; |
| 4894 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4895 | if (leaf->dflt) { |
| 4896 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4897 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4898 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4899 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4900 | } else { |
| 4901 | /* prepare a new one */ |
| 4902 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4903 | leaf->dflt->realtype = leaf->type; |
| 4904 | } |
| 4905 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4906 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4907 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4908 | 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] | 4909 | 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] | 4910 | leaf->dflt->realtype->refcount++; |
| 4911 | if (err) { |
| 4912 | ly_err_print(err); |
| 4913 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4914 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4915 | ly_err_free(err); |
| 4916 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4917 | if (rc == LY_EINCOMPLETE) { |
| 4918 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4919 | 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] | 4920 | |
| 4921 | /* but in general result is so far ok */ |
| 4922 | rc = LY_SUCCESS; |
| 4923 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4924 | LY_CHECK_GOTO(rc, cleanup); |
| 4925 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4926 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4927 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4928 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4929 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4930 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4931 | "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] | 4932 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4933 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4934 | |
| 4935 | /* remove previous set of default values */ |
| 4936 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4937 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4938 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 4939 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 4940 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4941 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4942 | LY_ARRAY_FREE(llist->dflts); |
| 4943 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4944 | LY_ARRAY_FREE(llist->dflts_mods); |
| 4945 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4946 | |
| 4947 | /* create the new set of the default values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4948 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup); |
| 4949 | 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] | 4950 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4951 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4952 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 4953 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4954 | LY_ARRAY_INCREMENT(llist->dflts); |
| 4955 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 4956 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4957 | 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] | 4958 | 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] | 4959 | 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] | 4960 | llist->dflts[u]->realtype->refcount++; |
| 4961 | if (err) { |
| 4962 | ly_err_print(err); |
| 4963 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4964 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 4965 | ly_err_free(err); |
| 4966 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4967 | if (rc == LY_EINCOMPLETE) { |
| 4968 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4969 | 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] | 4970 | |
| 4971 | /* but in general result is so far ok */ |
| 4972 | rc = LY_SUCCESS; |
| 4973 | } |
| 4974 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4975 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4976 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4977 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4978 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4979 | /* unset LYS_SET_DFLT from the current default case */ |
| 4980 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4981 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4982 | 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] | 4983 | } |
| 4984 | } |
| 4985 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4986 | /* description */ |
| 4987 | if (rfn->dsc) { |
| 4988 | FREE_STRING(ctx->ctx, node->dsc); |
| 4989 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4990 | } |
| 4991 | |
| 4992 | /* reference */ |
| 4993 | if (rfn->ref) { |
| 4994 | FREE_STRING(ctx->ctx, node->ref); |
| 4995 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4996 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4997 | |
| 4998 | /* config */ |
| 4999 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5000 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5001 | 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] | 5002 | } else { |
| 5003 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5004 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5005 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5006 | } |
| 5007 | |
| 5008 | /* mandatory */ |
| 5009 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5010 | 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] | 5011 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5012 | |
| 5013 | /* presence */ |
| 5014 | if (rfn->presence) { |
| 5015 | if (node->nodetype != LYS_CONTAINER) { |
| 5016 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5017 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5018 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5019 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5020 | } |
| 5021 | node->flags |= LYS_PRESENCE; |
| 5022 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5023 | |
| 5024 | /* must */ |
| 5025 | if (rfn->musts) { |
| 5026 | switch (node->nodetype) { |
| 5027 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5028 | 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] | 5029 | break; |
| 5030 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5031 | 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] | 5032 | break; |
| 5033 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5034 | 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] | 5035 | break; |
| 5036 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5037 | 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] | 5038 | break; |
| 5039 | case LYS_ANYXML: |
| 5040 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5041 | 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] | 5042 | break; |
| 5043 | default: |
| 5044 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5045 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5046 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5047 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5048 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5049 | ly_set_add(&ctx->xpath, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5050 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5051 | |
| 5052 | /* min/max-elements */ |
| 5053 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5054 | switch (node->nodetype) { |
| 5055 | case LYS_LEAFLIST: |
| 5056 | if (rfn->flags & LYS_SET_MAX) { |
| 5057 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5058 | } |
| 5059 | if (rfn->flags & LYS_SET_MIN) { |
| 5060 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5061 | if (rfn->min) { |
| 5062 | node->flags |= LYS_MAND_TRUE; |
| 5063 | lys_compile_mandatory_parents(node->parent, 1); |
| 5064 | } else { |
| 5065 | node->flags &= ~LYS_MAND_TRUE; |
| 5066 | lys_compile_mandatory_parents(node->parent, 0); |
| 5067 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5068 | } |
| 5069 | break; |
| 5070 | case LYS_LIST: |
| 5071 | if (rfn->flags & LYS_SET_MAX) { |
| 5072 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5073 | } |
| 5074 | if (rfn->flags & LYS_SET_MIN) { |
| 5075 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5076 | if (rfn->min) { |
| 5077 | node->flags |= LYS_MAND_TRUE; |
| 5078 | lys_compile_mandatory_parents(node->parent, 1); |
| 5079 | } else { |
| 5080 | node->flags &= ~LYS_MAND_TRUE; |
| 5081 | lys_compile_mandatory_parents(node->parent, 0); |
| 5082 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5083 | } |
| 5084 | break; |
| 5085 | default: |
| 5086 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5087 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5088 | (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] | 5089 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5090 | } |
| 5091 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5092 | |
| 5093 | /* if-feature */ |
| 5094 | if (rfn->iffeatures) { |
| 5095 | /* 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] | 5096 | 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] | 5097 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5098 | |
| 5099 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5100 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5101 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5102 | /* 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] | 5103 | for (uint32_t i = 0; i < refined.count; ++i) { |
| 5104 | node = (struct lysc_node*)refined.objs[i]; |
| 5105 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5106 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5107 | |
| 5108 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5109 | if ((node->flags & LYS_MAND_TRUE) && |
| 5110 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5111 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5112 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5113 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5114 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5118 | if (node->nodetype == LYS_LIST) { |
| 5119 | min = ((struct lysc_node_list*)node)->min; |
| 5120 | max = ((struct lysc_node_list*)node)->max; |
| 5121 | } else { |
| 5122 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5123 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5124 | } |
| 5125 | if (min > max) { |
| 5126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5127 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5128 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5129 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5130 | } |
| 5131 | } |
| 5132 | } |
| 5133 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5134 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5135 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5136 | |
| 5137 | cleanup: |
| 5138 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5139 | if (context_node_fake.child) { |
| 5140 | context_node_fake.child->prev = child; |
| 5141 | } |
| 5142 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5143 | child->parent = parent; |
| 5144 | } |
| 5145 | |
| 5146 | if (uses_p->augments || uses_p->refines) { |
| 5147 | /* 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] | 5148 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5149 | 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] | 5150 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5151 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5152 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5153 | 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] | 5154 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5155 | } |
| 5156 | } |
| 5157 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5158 | /* reload previous context's mod_def */ |
| 5159 | ctx->mod_def = mod_old; |
| 5160 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5161 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5162 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5163 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5164 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5165 | |
| 5166 | return ret; |
| 5167 | } |
| 5168 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5169 | static int |
| 5170 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5171 | { |
| 5172 | struct lysp_node *iter; |
| 5173 | int len = 0; |
| 5174 | |
| 5175 | *path = NULL; |
| 5176 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5177 | char *s = *path; |
| 5178 | char *id; |
| 5179 | |
| 5180 | switch (iter->nodetype) { |
| 5181 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5182 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5183 | break; |
| 5184 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5185 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5186 | break; |
| 5187 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5188 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5189 | break; |
| 5190 | default: |
| 5191 | id = strdup(iter->name); |
| 5192 | break; |
| 5193 | } |
| 5194 | |
| 5195 | if (!iter->parent) { |
| 5196 | /* print prefix */ |
| 5197 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5198 | } else { |
| 5199 | /* prefix is the same as in parent */ |
| 5200 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5201 | } |
| 5202 | free(s); |
| 5203 | free(id); |
| 5204 | } |
| 5205 | |
| 5206 | if (len < 0) { |
| 5207 | free(*path); |
| 5208 | *path = NULL; |
| 5209 | } else if (len == 0) { |
| 5210 | *path = strdup("/"); |
| 5211 | len = 1; |
| 5212 | } |
| 5213 | return len; |
| 5214 | } |
| 5215 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5216 | /** |
| 5217 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5218 | * |
| 5219 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5220 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5221 | */ |
| 5222 | static LY_ERR |
| 5223 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5224 | { |
| 5225 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5226 | char *path; |
| 5227 | int len; |
| 5228 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5229 | struct lysp_node_uses fake_uses = { |
| 5230 | .parent = node_p, |
| 5231 | .nodetype = LYS_USES, |
| 5232 | .flags = 0, .next = NULL, |
| 5233 | .name = grp->name, |
| 5234 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5235 | .refines = NULL, .augments = NULL |
| 5236 | }; |
| 5237 | struct lysc_node_container fake_container = { |
| 5238 | .nodetype = LYS_CONTAINER, |
| 5239 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5240 | .module = ctx->mod, |
| 5241 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5242 | .prev = (struct lysc_node*)&fake_container, |
| 5243 | .name = "fake", |
| 5244 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5245 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5246 | }; |
| 5247 | |
| 5248 | if (grp->parent) { |
| 5249 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5250 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5251 | |
| 5252 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5253 | if (len < 0) { |
| 5254 | LOGMEM(ctx->ctx); |
| 5255 | return LY_EMEM; |
| 5256 | } |
| 5257 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5258 | ctx->path_len = (uint16_t)len; |
| 5259 | free(path); |
| 5260 | |
| 5261 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5262 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5263 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5264 | lysc_update_path(ctx, NULL, NULL); |
| 5265 | lysc_update_path(ctx, NULL, NULL); |
| 5266 | |
| 5267 | ctx->path_len = 1; |
| 5268 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5269 | |
| 5270 | /* cleanup */ |
| 5271 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5272 | |
| 5273 | return ret; |
| 5274 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5275 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5276 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5277 | * @brief Compile parsed schema node information. |
| 5278 | * @param[in] ctx Compile context |
| 5279 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5280 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5281 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5282 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5283 | * @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). |
| 5284 | * 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] | 5285 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5286 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5287 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5288 | 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] | 5289 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5290 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5291 | struct lysc_node *node; |
| 5292 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5293 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5294 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5295 | 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] | 5296 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5297 | if (node_p->nodetype != LYS_USES) { |
| 5298 | lysc_update_path(ctx, parent, node_p->name); |
| 5299 | } else { |
| 5300 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5301 | lysc_update_path(ctx, NULL, node_p->name); |
| 5302 | } |
| 5303 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5304 | switch (node_p->nodetype) { |
| 5305 | case LYS_CONTAINER: |
| 5306 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5307 | node_compile_spec = lys_compile_node_container; |
| 5308 | break; |
| 5309 | case LYS_LEAF: |
| 5310 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5311 | node_compile_spec = lys_compile_node_leaf; |
| 5312 | break; |
| 5313 | case LYS_LIST: |
| 5314 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5315 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5316 | break; |
| 5317 | case LYS_LEAFLIST: |
| 5318 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5319 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5320 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5321 | case LYS_CHOICE: |
| 5322 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5323 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5324 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5325 | case LYS_ANYXML: |
| 5326 | case LYS_ANYDATA: |
| 5327 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5328 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5329 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5330 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5331 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5332 | lysc_update_path(ctx, NULL, NULL); |
| 5333 | lysc_update_path(ctx, NULL, NULL); |
| 5334 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5335 | default: |
| 5336 | LOGINT(ctx->ctx); |
| 5337 | return LY_EINT; |
| 5338 | } |
| 5339 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5340 | node->nodetype = node_p->nodetype; |
| 5341 | node->module = ctx->mod; |
| 5342 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5343 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5344 | |
| 5345 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5346 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5347 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5348 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5349 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5350 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5351 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5352 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5353 | node->flags |= LYS_CONFIG_R; |
| 5354 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5355 | /* config not explicitely set, inherit it from parent */ |
| 5356 | if (parent) { |
| 5357 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5358 | } else { |
| 5359 | /* default is config true */ |
| 5360 | node->flags |= LYS_CONFIG_W; |
| 5361 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5362 | } else { |
| 5363 | /* config set explicitely */ |
| 5364 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5365 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5366 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5367 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5368 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5369 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5370 | goto error; |
| 5371 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5372 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5373 | /* *list ordering */ |
| 5374 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5375 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5376 | 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] | 5377 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5378 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5379 | node->flags &= ~LYS_ORDBY_MASK; |
| 5380 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5381 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5382 | /* default ordering is system */ |
| 5383 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5384 | } |
| 5385 | } |
| 5386 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5387 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5388 | * 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] | 5389 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5390 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5391 | * 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] | 5392 | 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] | 5393 | } |
| 5394 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5395 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5396 | node->sp = node_p; |
| 5397 | } |
| 5398 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5399 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5400 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5401 | if (node_p->when) { |
| 5402 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5403 | 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] | 5404 | |
| 5405 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5406 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5407 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5408 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5409 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5410 | 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] | 5411 | |
| 5412 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5413 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5414 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5415 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5416 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5417 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5418 | if (node->flags & LYS_MAND_TRUE) { |
| 5419 | lys_compile_mandatory_parents(parent, 1); |
| 5420 | } |
| 5421 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5422 | lysc_update_path(ctx, NULL, NULL); |
| 5423 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5424 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5425 | if (parent) { |
| 5426 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5427 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5428 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5429 | } else { |
| 5430 | lysc_update_path(ctx, parent, node->name); |
| 5431 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5432 | 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] | 5433 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5434 | if (uses_status) { |
| 5435 | |
| 5436 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5437 | /* 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] | 5438 | * it directly gets the same status flags as the choice; |
| 5439 | * 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] | 5440 | 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] | 5441 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5442 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5443 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5444 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5445 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5446 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5447 | 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] | 5448 | |
| 5449 | if (parent->nodetype == LYS_CHOICE) { |
| 5450 | lysc_update_path(ctx, NULL, NULL); |
| 5451 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5452 | } else { |
| 5453 | /* top-level element */ |
| 5454 | if (!ctx->mod->compiled->data) { |
| 5455 | ctx->mod->compiled->data = node; |
| 5456 | } else { |
| 5457 | /* insert at the end of the module's top-level nodes list */ |
| 5458 | ctx->mod->compiled->data->prev->next = node; |
| 5459 | node->prev = ctx->mod->compiled->data->prev; |
| 5460 | ctx->mod->compiled->data->prev = node; |
| 5461 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5462 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5463 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5464 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5465 | return LY_EVALID; |
| 5466 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5467 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5468 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5469 | |
| 5470 | return LY_SUCCESS; |
| 5471 | |
| 5472 | error: |
| 5473 | lysc_node_free(ctx->ctx, node); |
| 5474 | return ret; |
| 5475 | } |
| 5476 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5477 | static void |
| 5478 | lysc_disconnect(struct lysc_node *node) |
| 5479 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5480 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5481 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5482 | int remove_cs = 0; |
| 5483 | |
| 5484 | parent = node->parent; |
| 5485 | |
| 5486 | /* parent's first child */ |
| 5487 | if (!parent) { |
| 5488 | return; |
| 5489 | } |
| 5490 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5491 | cs = (struct lysc_node_case*)node; |
| 5492 | } else if (parent->nodetype == LYS_CASE) { |
| 5493 | /* disconnecting some node in a case */ |
| 5494 | cs = (struct lysc_node_case*)parent; |
| 5495 | parent = cs->parent; |
| 5496 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5497 | if (child == node) { |
| 5498 | if (cs->child == child) { |
| 5499 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5500 | /* case with a single child -> remove also the case */ |
| 5501 | child->parent = NULL; |
| 5502 | remove_cs = 1; |
| 5503 | } else { |
| 5504 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5505 | } |
| 5506 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5507 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5508 | } |
| 5509 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5510 | if (!remove_cs) { |
| 5511 | cs = NULL; |
| 5512 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5513 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5514 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | if (cs) { |
| 5518 | if (remove_cs) { |
| 5519 | /* cs has only one child which is being also removed */ |
| 5520 | lysc_disconnect((struct lysc_node*)cs); |
| 5521 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5522 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5523 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5524 | /* default case removed */ |
| 5525 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5526 | } |
| 5527 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5528 | /* first case removed */ |
| 5529 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5530 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5531 | if (cs->child) { |
| 5532 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5533 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5534 | prev = cs->child->prev; |
| 5535 | } /* else all the children are under a single case */ |
| 5536 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5537 | if (child->parent != (struct lysc_node*)cs) { |
| 5538 | break; |
| 5539 | } |
| 5540 | lysc_node_free(node->module->ctx, child); |
| 5541 | } |
| 5542 | if (prev) { |
| 5543 | if (prev->next) { |
| 5544 | prev->next = child; |
| 5545 | } |
| 5546 | if (child) { |
| 5547 | child->prev = prev; |
| 5548 | } else { |
| 5549 | /* link from the first child under the cases */ |
| 5550 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5551 | } |
| 5552 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5553 | } |
| 5554 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5555 | } |
| 5556 | |
| 5557 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5558 | if (node->prev->next) { |
| 5559 | node->prev->next = node->next; |
| 5560 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5561 | if (node->next) { |
| 5562 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5563 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5564 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5565 | if (child) { |
| 5566 | child->prev = node->prev; |
| 5567 | } |
| 5568 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5569 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5570 | } |
| 5571 | } |
| 5572 | |
| 5573 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5574 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5575 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5576 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5577 | struct ly_set devs_p = {0}; |
| 5578 | struct ly_set targets = {0}; |
| 5579 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5580 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5581 | struct lysc_action *rpcs; |
| 5582 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5583 | struct lysp_deviation *dev; |
| 5584 | struct lysp_deviate *d, **dp_new; |
| 5585 | struct lysp_deviate_add *d_add; |
| 5586 | struct lysp_deviate_del *d_del; |
| 5587 | struct lysp_deviate_rpl *d_rpl; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5588 | LY_ARRAY_COUNT_TYPE u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5589 | struct lysc_deviation { |
| 5590 | const char *nodeid; |
| 5591 | struct lysc_node *target; /* target node of the deviation */ |
| 5592 | 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] | 5593 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5594 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5595 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5596 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5597 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5598 | const char *prefix, *name, *nodeid, *dflt; |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 5599 | struct lys_module *mod, **dev_mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5600 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5601 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5602 | |
| 5603 | /* get all deviations from the module and all its submodules ... */ |
| 5604 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5605 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5606 | } |
| 5607 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5608 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5609 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5610 | } |
| 5611 | } |
| 5612 | if (!devs_p.count) { |
| 5613 | /* nothing to do */ |
| 5614 | return LY_SUCCESS; |
| 5615 | } |
| 5616 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5617 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5618 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5619 | /* ... and group them by the target node */ |
| 5620 | devs = calloc(devs_p.count, sizeof *devs); |
| 5621 | for (u = 0; u < devs_p.count; ++u) { |
| 5622 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5623 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5624 | |
| 5625 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5626 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5627 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5628 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5629 | /* move the target pointer to input/output to make them different from the action and |
| 5630 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5631 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5632 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5633 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5634 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5635 | flags |= LYSC_OPT_INTERNAL; |
| 5636 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5637 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5638 | flags |= LYSC_OPT_INTERNAL; |
| 5639 | } |
| 5640 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5641 | /* insert into the set of targets with duplicity detection */ |
| 5642 | i = ly_set_add(&targets, target, 0); |
| 5643 | if (!devs[i]) { |
| 5644 | /* new record */ |
| 5645 | devs[i] = calloc(1, sizeof **devs); |
| 5646 | devs[i]->target = target; |
| 5647 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5648 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5649 | } |
| 5650 | /* add deviates into the deviation's list of deviates */ |
| 5651 | for (d = dev->deviates; d; d = d->next) { |
| 5652 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5653 | *dp_new = d; |
| 5654 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5655 | devs[i]->not_supported = 1; |
| 5656 | } |
| 5657 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5658 | |
| 5659 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | /* MACROS for deviates checking */ |
| 5663 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5664 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5665 | 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] | 5666 | goto cleanup; \ |
| 5667 | } |
| 5668 | |
| 5669 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5670 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5671 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5672 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5673 | goto cleanup; \ |
| 5674 | } |
| 5675 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5676 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5677 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5678 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5679 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5680 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5681 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5682 | goto cleanup; \ |
| 5683 | } |
| 5684 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5685 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5686 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5687 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5688 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5689 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5690 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5691 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5692 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5693 | goto cleanup; \ |
| 5694 | } |
| 5695 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5696 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5697 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5698 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5699 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5700 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5701 | goto cleanup; \ |
| 5702 | } |
| 5703 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5704 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5705 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5706 | 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] | 5707 | goto cleanup; \ |
| 5708 | } |
| 5709 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5710 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5711 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5712 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5713 | "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] | 5714 | goto cleanup; \ |
| 5715 | } |
| 5716 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5717 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5718 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5719 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5720 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5721 | 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] | 5722 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5723 | if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5724 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5725 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5726 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5727 | goto cleanup; \ |
| 5728 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5729 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5730 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5731 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5732 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5733 | (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] | 5734 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5735 | if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5736 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5737 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5738 | } |
| 5739 | |
| 5740 | /* apply deviations */ |
| 5741 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5742 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5743 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5744 | struct ly_err_item *err = NULL; |
| 5745 | |
| 5746 | dflt = NULL; |
| 5747 | changed_type = 0; |
| 5748 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5749 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5750 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5751 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5752 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5753 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5754 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5755 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5756 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5757 | } |
| 5758 | } |
| 5759 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5760 | /* not-supported */ |
| 5761 | if (devs[u]->not_supported) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5762 | if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) { |
| 5763 | LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.", |
| 5764 | LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5765 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5766 | |
| 5767 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5768 | if (devs[u]->target->parent) { \ |
| 5769 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5770 | } else { \ |
| 5771 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5772 | } \ |
| 5773 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5774 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5775 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5776 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5777 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5778 | 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] | 5779 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5780 | } |
| 5781 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5782 | if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5783 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5784 | /* remove RPC's/action's input */ |
| 5785 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5786 | 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] | 5787 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5788 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5789 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5790 | /* remove RPC's/action's output */ |
| 5791 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5792 | 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] | 5793 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5794 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5795 | } else { |
| 5796 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5797 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5798 | } |
| 5799 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5800 | /* remove Notification */ |
| 5801 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5802 | } else { |
| 5803 | /* remove the target node */ |
| 5804 | lysc_disconnect(devs[u]->target); |
| 5805 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5806 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5807 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5808 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5809 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5810 | continue; |
| 5811 | } |
| 5812 | |
| 5813 | /* list of deviates (not-supported is not present in the list) */ |
| 5814 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5815 | d = devs[u]->deviates[v]; |
| 5816 | |
| 5817 | switch (d->mod) { |
| 5818 | case LYS_DEV_ADD: |
| 5819 | d_add = (struct lysp_deviate_add*)d; |
| 5820 | /* [units-stmt] */ |
| 5821 | if (d_add->units) { |
| 5822 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5823 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5824 | |
| 5825 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5826 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5827 | } |
| 5828 | |
| 5829 | /* *must-stmt */ |
| 5830 | if (d_add->musts) { |
| 5831 | switch (devs[u]->target->nodetype) { |
| 5832 | case LYS_CONTAINER: |
| 5833 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5834 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 5835 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5836 | break; |
| 5837 | case LYS_LEAF: |
| 5838 | case LYS_LEAFLIST: |
| 5839 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5840 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 5841 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5842 | break; |
| 5843 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5844 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 5845 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5846 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5847 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5848 | case LYS_ACTION: |
| 5849 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5850 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 5851 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5852 | break; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 5853 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5854 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 5855 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5856 | break; |
| 5857 | } |
| 5858 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5859 | default: |
| 5860 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5861 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5862 | goto cleanup; |
| 5863 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5864 | ly_set_add(&ctx->xpath, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5865 | } |
| 5866 | |
| 5867 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5868 | if (d_add->uniques) { |
| 5869 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5870 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5871 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5872 | |
| 5873 | /* *default-stmt */ |
| 5874 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5875 | switch (devs[u]->target->nodetype) { |
| 5876 | case LYS_LEAF: |
| 5877 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5878 | 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] | 5879 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5880 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5881 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5882 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5883 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5884 | } else { |
| 5885 | /* prepare new default value storage */ |
| 5886 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5887 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5888 | dflt = d_add->dflts[0]; |
| 5889 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5890 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5891 | /* mark the new default values as leaf's own */ |
| 5892 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5893 | break; |
| 5894 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5895 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5896 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5897 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5898 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5899 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5900 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5901 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5902 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5903 | LY_ARRAY_FREE(llist->dflts); |
| 5904 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5905 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5906 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5907 | } |
| 5908 | /* add new default value(s) */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5909 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup); |
| 5910 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup); |
| 5911 | for (x = y = LY_ARRAY_COUNT(llist->dflts); |
| 5912 | x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5913 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5914 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5915 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5916 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5917 | llist->dflts[x]->realtype = llist->type; |
| 5918 | 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] | 5919 | 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] | 5920 | (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] | 5921 | llist->dflts[x]->realtype->refcount++; |
| 5922 | if (err) { |
| 5923 | ly_err_print(err); |
| 5924 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5925 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5926 | d_add->dflts[x - y], err->msg); |
| 5927 | ly_err_free(err); |
| 5928 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5929 | if (rc == LY_EINCOMPLETE) { |
| 5930 | /* postpone default compilation when the tree is complete */ |
| 5931 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5932 | |
| 5933 | /* but in general result is so far ok */ |
| 5934 | rc = LY_SUCCESS; |
| 5935 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5936 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5937 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5938 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5939 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5940 | break; |
| 5941 | case LYS_CHOICE: |
| 5942 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5943 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5944 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5945 | * 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] | 5946 | 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] | 5947 | goto cleanup; |
| 5948 | } |
| 5949 | break; |
| 5950 | default: |
| 5951 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5952 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5953 | goto cleanup; |
| 5954 | } |
| 5955 | } |
| 5956 | |
| 5957 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5958 | if (d_add->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5959 | 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] | 5960 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5961 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5962 | goto cleanup; |
| 5963 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5964 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5965 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5966 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5967 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5968 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5969 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5970 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5971 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5972 | goto cleanup; |
| 5973 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5974 | 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] | 5975 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5976 | |
| 5977 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5978 | if (d_add->flags & LYS_MAND_MASK) { |
| 5979 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5980 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5981 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5982 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5983 | goto cleanup; |
| 5984 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5985 | 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] | 5986 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5987 | |
| 5988 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5989 | if (d_add->flags & LYS_SET_MIN) { |
| 5990 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5991 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5992 | /* change value */ |
| 5993 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5994 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5995 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5996 | /* change value */ |
| 5997 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5998 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5999 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6000 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 6001 | goto cleanup; |
| 6002 | } |
| 6003 | if (d_add->min) { |
| 6004 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6005 | } |
| 6006 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6007 | |
| 6008 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6009 | if (d_add->flags & LYS_SET_MAX) { |
| 6010 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6011 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6012 | /* change value */ |
| 6013 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6014 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6015 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6016 | /* change value */ |
| 6017 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6018 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6019 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6020 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6021 | goto cleanup; |
| 6022 | } |
| 6023 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6024 | |
| 6025 | break; |
| 6026 | case LYS_DEV_DELETE: |
| 6027 | d_del = (struct lysp_deviate_del*)d; |
| 6028 | |
| 6029 | /* [units-stmt] */ |
| 6030 | if (d_del->units) { |
| 6031 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6032 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6033 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6034 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6035 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6036 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6037 | goto cleanup; |
| 6038 | } |
| 6039 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6040 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6041 | } |
| 6042 | |
| 6043 | /* *must-stmt */ |
| 6044 | if (d_del->musts) { |
| 6045 | switch (devs[u]->target->nodetype) { |
| 6046 | case LYS_CONTAINER: |
| 6047 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6048 | 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] | 6049 | break; |
| 6050 | case LYS_LEAF: |
| 6051 | case LYS_LEAFLIST: |
| 6052 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6053 | 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] | 6054 | break; |
| 6055 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6056 | 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] | 6057 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6058 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6059 | case LYS_ACTION: |
| 6060 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6061 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6062 | break; |
| 6063 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6064 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6065 | break; |
| 6066 | } |
| 6067 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6068 | default: |
| 6069 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6070 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6071 | goto cleanup; |
| 6072 | } |
| 6073 | } |
| 6074 | |
| 6075 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6076 | if (d_del->uniques) { |
| 6077 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6078 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6079 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6080 | LY_ARRAY_FOR(list->uniques, z) { |
| 6081 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6082 | nodeid = strpbrk(name, " \t\n"); |
| 6083 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 6084 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6085 | break; |
| 6086 | } |
| 6087 | while (isspace(*nodeid)) { |
| 6088 | ++nodeid; |
| 6089 | } |
| 6090 | } else { |
| 6091 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6092 | break; |
| 6093 | } |
| 6094 | } |
| 6095 | } |
| 6096 | if (!name) { |
| 6097 | /* complete match - remove the unique */ |
| 6098 | LY_ARRAY_DECREMENT(list->uniques); |
| 6099 | LY_ARRAY_FREE(list->uniques[z]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6100 | 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] | 6101 | --z; |
| 6102 | break; |
| 6103 | } |
| 6104 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6105 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6106 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6107 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6108 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6109 | goto cleanup; |
| 6110 | } |
| 6111 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6112 | if (!LY_ARRAY_COUNT(list->uniques)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6113 | LY_ARRAY_FREE(list->uniques); |
| 6114 | list->uniques = NULL; |
| 6115 | } |
| 6116 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6117 | |
| 6118 | /* *default-stmt */ |
| 6119 | if (d_del->dflts) { |
| 6120 | switch (devs[u]->target->nodetype) { |
| 6121 | case LYS_LEAF: |
| 6122 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6123 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6124 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6125 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6126 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6127 | 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] | 6128 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6129 | if (i) { |
| 6130 | free((char*)dflt); |
| 6131 | } |
| 6132 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6133 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6134 | d_del->dflts[0], dflt); |
| 6135 | goto cleanup; |
| 6136 | } |
| 6137 | if (i) { |
| 6138 | free((char*)dflt); |
| 6139 | } |
| 6140 | dflt = NULL; |
| 6141 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6142 | /* update the list of incomplete default values if needed */ |
| 6143 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6144 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6145 | /* remove the default specification */ |
| 6146 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6147 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6148 | free(leaf->dflt); |
| 6149 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6150 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6151 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6152 | break; |
| 6153 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6154 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6155 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6156 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6157 | 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] | 6158 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6159 | if (i) { |
| 6160 | free((char*)dflt); |
| 6161 | } |
| 6162 | dflt = NULL; |
| 6163 | break; |
| 6164 | } |
| 6165 | if (i) { |
| 6166 | free((char*)dflt); |
| 6167 | } |
| 6168 | dflt = NULL; |
| 6169 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6170 | if (y == LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6171 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6172 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6173 | goto cleanup; |
| 6174 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6175 | |
| 6176 | /* update the list of incomplete default values if needed */ |
| 6177 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6178 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6179 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6180 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6181 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6182 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6183 | free(llist->dflts[y]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6184 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 6185 | 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] | 6186 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6187 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6188 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6189 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6190 | LY_ARRAY_FREE(llist->dflts); |
| 6191 | llist->dflts = NULL; |
| 6192 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6193 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6194 | break; |
| 6195 | case LYS_CHOICE: |
| 6196 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6197 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6198 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6199 | 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] | 6200 | if (prefix) { |
| 6201 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6202 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6203 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6204 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6205 | "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] | 6206 | goto cleanup; |
| 6207 | } |
| 6208 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6209 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6210 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6211 | "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] | 6212 | goto cleanup; |
| 6213 | } |
| 6214 | } |
| 6215 | /* else { |
| 6216 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6217 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6218 | * the module of the default case, just matching its name */ |
| 6219 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6220 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6221 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6222 | 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] | 6223 | goto cleanup; |
| 6224 | } |
| 6225 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6226 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6227 | break; |
| 6228 | default: |
| 6229 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6230 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6231 | goto cleanup; |
| 6232 | } |
| 6233 | } |
| 6234 | |
| 6235 | break; |
| 6236 | case LYS_DEV_REPLACE: |
| 6237 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6238 | |
| 6239 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6240 | if (d_rpl->type) { |
| 6241 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6242 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6243 | 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] | 6244 | |
| 6245 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6246 | /* the target has default from the previous type - remove it */ |
| 6247 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6248 | /* update the list of incomplete default values if needed */ |
| 6249 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6250 | |
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 | free(leaf->dflt); |
| 6254 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6255 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6256 | } else { /* LYS_LEAFLIST */ |
| 6257 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6258 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6259 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6260 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6261 | free(llist->dflts[x]); |
| 6262 | } |
| 6263 | LY_ARRAY_FREE(llist->dflts); |
| 6264 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6265 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6266 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6267 | } |
| 6268 | } |
| 6269 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6270 | /* there is no default value, do not set changed_type after type compilation |
| 6271 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6272 | changed_type = -1; |
| 6273 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6274 | 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] | 6275 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6276 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6277 | |
| 6278 | /* [units-stmt] */ |
| 6279 | if (d_rpl->units) { |
| 6280 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6281 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6282 | units, "replacing", "units", d_rpl->units); |
| 6283 | |
| 6284 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6285 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6286 | } |
| 6287 | |
| 6288 | /* [default-stmt] */ |
| 6289 | if (d_rpl->dflt) { |
| 6290 | switch (devs[u]->target->nodetype) { |
| 6291 | case LYS_LEAF: |
| 6292 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6293 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6294 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6295 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6296 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6297 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6298 | dflt = d_rpl->dflt; |
| 6299 | /* 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] | 6300 | break; |
| 6301 | case LYS_CHOICE: |
| 6302 | 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] | 6303 | 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] | 6304 | goto cleanup; |
| 6305 | } |
| 6306 | break; |
| 6307 | default: |
| 6308 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6309 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6310 | goto cleanup; |
| 6311 | } |
| 6312 | } |
| 6313 | |
| 6314 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6315 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6316 | 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] | 6317 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6318 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6319 | goto cleanup; |
| 6320 | } |
| 6321 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6322 | 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] | 6323 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6324 | goto cleanup; |
| 6325 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6326 | 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] | 6327 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6328 | |
| 6329 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6330 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6331 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6332 | 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] | 6333 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6334 | goto cleanup; |
| 6335 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6336 | 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] | 6337 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6338 | |
| 6339 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6340 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6341 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6342 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6343 | /* change value */ |
| 6344 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6345 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6346 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6347 | /* change value */ |
| 6348 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6349 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6350 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6351 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6352 | goto cleanup; |
| 6353 | } |
| 6354 | if (d_rpl->min) { |
| 6355 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6356 | } |
| 6357 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6358 | |
| 6359 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6360 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6361 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6362 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6363 | /* change value */ |
| 6364 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6365 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6366 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6367 | /* change value */ |
| 6368 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6369 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6370 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6371 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6372 | goto cleanup; |
| 6373 | } |
| 6374 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6375 | |
| 6376 | break; |
| 6377 | default: |
| 6378 | LOGINT(ctx->ctx); |
| 6379 | goto cleanup; |
| 6380 | } |
| 6381 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6382 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6383 | /* final check when all deviations of a single target node are applied */ |
| 6384 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6385 | /* check min-max compatibility */ |
| 6386 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6387 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6388 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6389 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6390 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6391 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6392 | } else { |
| 6393 | min = max = 0; |
| 6394 | } |
| 6395 | if (min > max) { |
| 6396 | 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] | 6397 | "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] | 6398 | goto cleanup; |
| 6399 | } |
| 6400 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6401 | if (dflt) { |
| 6402 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6403 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6404 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6405 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6406 | 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] | 6407 | 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] | 6408 | leaf->dflt->realtype->refcount++; |
| 6409 | if (err) { |
| 6410 | ly_err_print(err); |
| 6411 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6412 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6413 | ly_err_free(err); |
| 6414 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6415 | if (rc == LY_EINCOMPLETE) { |
| 6416 | /* postpone default compilation when the tree is complete */ |
| 6417 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6418 | |
| 6419 | /* but in general result is so far ok */ |
| 6420 | rc = LY_SUCCESS; |
| 6421 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6422 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6423 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6424 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6425 | int dynamic; |
| 6426 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6427 | 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] | 6428 | |
| 6429 | /* update the list of incomplete default values if needed */ |
| 6430 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6431 | |
| 6432 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6433 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6434 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6435 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6436 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6437 | 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] | 6438 | 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] | 6439 | leaf->dflt->realtype->refcount++; |
| 6440 | if (err) { |
| 6441 | ly_err_print(err); |
| 6442 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6443 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6444 | ly_err_free(err); |
| 6445 | } |
| 6446 | if (dynamic) { |
| 6447 | free((void*)dflt); |
| 6448 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6449 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6450 | if (rc == LY_EINCOMPLETE) { |
| 6451 | /* postpone default compilation when the tree is complete */ |
| 6452 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6453 | |
| 6454 | /* but in general result is so far ok */ |
| 6455 | rc = LY_SUCCESS; |
| 6456 | } |
| 6457 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6458 | } else { /* LYS_LEAFLIST */ |
| 6459 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6460 | 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] | 6461 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6462 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6463 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6464 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6465 | 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] | 6466 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6467 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6468 | llist->dflts[x]->realtype->refcount++; |
| 6469 | if (err) { |
| 6470 | ly_err_print(err); |
| 6471 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6472 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6473 | dflt, err->msg); |
| 6474 | ly_err_free(err); |
| 6475 | } |
| 6476 | if (dynamic) { |
| 6477 | free((void*)dflt); |
| 6478 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6479 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6480 | if (rc == LY_EINCOMPLETE) { |
| 6481 | /* postpone default compilation when the tree is complete */ |
| 6482 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6483 | |
| 6484 | /* but in general result is so far ok */ |
| 6485 | rc = LY_SUCCESS; |
| 6486 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6487 | LY_CHECK_GOTO(rc, cleanup); |
| 6488 | } |
| 6489 | } |
| 6490 | } |
| 6491 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6492 | /* check mandatory - default compatibility */ |
| 6493 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6494 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6495 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6496 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6497 | "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] | 6498 | goto cleanup; |
| 6499 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6500 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6501 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6502 | 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] | 6503 | goto cleanup; |
| 6504 | } |
| 6505 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6506 | /* mandatory node under a default case */ |
| 6507 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6508 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6509 | 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] | 6510 | goto cleanup; |
| 6511 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6512 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6513 | /* add this module into the target module deviated_by, if not there already */ |
| 6514 | rc = LY_SUCCESS; |
| 6515 | LY_ARRAY_FOR(devs[u]->target->module->compiled->deviated_by, v) { |
| 6516 | if (devs[u]->target->module->compiled->deviated_by[v] == mod_p->mod) { |
| 6517 | rc = LY_EEXIST; |
| 6518 | break; |
| 6519 | } |
| 6520 | } |
| 6521 | if (!rc) { |
| 6522 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup); |
| 6523 | *dev_mod = mod_p->mod; |
| 6524 | } |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 6525 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6526 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6527 | } |
| 6528 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6529 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6530 | ret = LY_SUCCESS; |
| 6531 | |
| 6532 | cleanup: |
| 6533 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6534 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6535 | free(devs[u]); |
| 6536 | } |
| 6537 | free(devs); |
| 6538 | ly_set_erase(&targets, NULL); |
| 6539 | ly_set_erase(&devs_p, NULL); |
| 6540 | |
| 6541 | return ret; |
| 6542 | } |
| 6543 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6544 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6545 | * @brief Compile the given YANG submodule into the main module. |
| 6546 | * @param[in] ctx Compile context |
| 6547 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6548 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6549 | */ |
| 6550 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6551 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6552 | { |
| 6553 | unsigned int u; |
| 6554 | LY_ERR ret = LY_SUCCESS; |
| 6555 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6556 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6557 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6558 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6559 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6560 | if (!mainmod->mod->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6561 | /* features are compiled directly into the compiled module structure, |
| 6562 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6563 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6564 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6565 | LY_CHECK_GOTO(ret, error); |
| 6566 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6567 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6568 | if (!mainmod->mod->dis_identities) { |
| 6569 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities); |
| 6570 | LY_CHECK_GOTO(ret, error); |
| 6571 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6572 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6573 | /* data nodes */ |
| 6574 | LY_LIST_FOR(submod->data, node_p) { |
| 6575 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6576 | LY_CHECK_GOTO(ret, error); |
| 6577 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6578 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6579 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6580 | 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] | 6581 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6582 | error: |
| 6583 | return ret; |
| 6584 | } |
| 6585 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6586 | static void * |
| 6587 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6588 | { |
| 6589 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6590 | if (substmts[u].stmt == stmt) { |
| 6591 | return substmts[u].storage; |
| 6592 | } |
| 6593 | } |
| 6594 | return NULL; |
| 6595 | } |
| 6596 | |
| 6597 | LY_ERR |
| 6598 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6599 | { |
| 6600 | LY_ERR ret = LY_EVALID, r; |
| 6601 | unsigned int u; |
| 6602 | struct lysp_stmt *stmt; |
| 6603 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6604 | |
| 6605 | /* check for invalid substatements */ |
| 6606 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6607 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6608 | continue; |
| 6609 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6610 | for (u = 0; substmts[u].stmt; ++u) { |
| 6611 | if (substmts[u].stmt == stmt->kw) { |
| 6612 | break; |
| 6613 | } |
| 6614 | } |
| 6615 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6616 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6617 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6618 | goto cleanup; |
| 6619 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6620 | } |
| 6621 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6622 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6623 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6624 | /* keep order of the processing the same as the order in the defined substmts, |
| 6625 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6626 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6627 | int stmt_present = 0; |
| 6628 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6629 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6630 | if (substmts[u].stmt != stmt->kw) { |
| 6631 | continue; |
| 6632 | } |
| 6633 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6634 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6635 | if (substmts[u].storage) { |
| 6636 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6637 | case LY_STMT_STATUS: |
| 6638 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6639 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6640 | break; |
| 6641 | case LY_STMT_UNITS: { |
| 6642 | const char **units; |
| 6643 | |
| 6644 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6645 | /* single item */ |
| 6646 | if (*((const char **)substmts[u].storage)) { |
| 6647 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6648 | goto cleanup; |
| 6649 | } |
| 6650 | units = (const char **)substmts[u].storage; |
| 6651 | } else { |
| 6652 | /* sized array */ |
| 6653 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6654 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6655 | } |
| 6656 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6657 | break; |
| 6658 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6659 | case LY_STMT_TYPE: { |
| 6660 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6661 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6662 | |
| 6663 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6664 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6665 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6666 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6667 | goto cleanup; |
| 6668 | } |
| 6669 | compiled = substmts[u].storage; |
| 6670 | } else { |
| 6671 | /* sized array */ |
| 6672 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6673 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6674 | compiled = (void*)type; |
| 6675 | } |
| 6676 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6677 | 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] | 6678 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6679 | 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] | 6680 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6681 | lysp_type_free(ctx->ctx, parsed); |
| 6682 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6683 | break; |
| 6684 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6685 | case LY_STMT_IF_FEATURE: { |
| 6686 | struct lysc_iffeature *iff = NULL; |
| 6687 | |
| 6688 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6689 | /* single item */ |
| 6690 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6692 | goto cleanup; |
| 6693 | } |
| 6694 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6695 | } else { |
| 6696 | /* sized array */ |
| 6697 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6698 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6699 | } |
| 6700 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6701 | break; |
| 6702 | } |
| 6703 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6704 | * 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] | 6705 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6706 | 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.", |
| 6707 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6708 | goto cleanup; |
| 6709 | } |
| 6710 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6711 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6712 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6713 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6714 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6715 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6716 | goto cleanup; |
| 6717 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6718 | } |
| 6719 | |
| 6720 | ret = LY_SUCCESS; |
| 6721 | |
| 6722 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6723 | return ret; |
| 6724 | } |
| 6725 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6726 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6727 | * @brief Check when for cyclic dependencies. |
| 6728 | * @param[in] set Set with all the referenced nodes. |
| 6729 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6730 | * @return LY_ERR value |
| 6731 | */ |
| 6732 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6733 | 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] | 6734 | { |
| 6735 | struct lyxp_set tmp_set; |
| 6736 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6737 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6738 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6739 | int idx; |
| 6740 | struct lysc_when *when; |
| 6741 | LY_ERR ret = LY_SUCCESS; |
| 6742 | |
| 6743 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6744 | |
| 6745 | /* prepare in_ctx of the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6746 | for ( i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6747 | xp_scnode = &set->val.scnodes[i]; |
| 6748 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6749 | if (xp_scnode->in_ctx != -1) { |
| 6750 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6751 | xp_scnode->in_ctx = 1; |
| 6752 | } |
| 6753 | } |
| 6754 | |
| 6755 | for (i = 0; i < set->used; ++i) { |
| 6756 | xp_scnode = &set->val.scnodes[i]; |
| 6757 | if (xp_scnode->in_ctx != 1) { |
| 6758 | /* already checked */ |
| 6759 | continue; |
| 6760 | } |
| 6761 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6762 | 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] | 6763 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6764 | /* no when to check */ |
| 6765 | xp_scnode->in_ctx = 0; |
| 6766 | continue; |
| 6767 | } |
| 6768 | |
| 6769 | node = xp_scnode->scnode; |
| 6770 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6771 | LY_ARRAY_FOR(node->when, u) { |
| 6772 | when = node->when[u]; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 6773 | ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6774 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6775 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6776 | 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] | 6777 | goto cleanup; |
| 6778 | } |
| 6779 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6780 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6781 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6782 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6783 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6784 | 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] | 6785 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6786 | 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] | 6787 | ret = LY_EVALID; |
| 6788 | goto cleanup; |
| 6789 | } |
| 6790 | |
| 6791 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6792 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6793 | } else { |
| 6794 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6795 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6796 | } |
| 6797 | } |
| 6798 | |
| 6799 | /* merge this set into the global when set */ |
| 6800 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6801 | } |
| 6802 | |
| 6803 | /* check when of non-data parents as well */ |
| 6804 | node = node->parent; |
| 6805 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6806 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6807 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6808 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6809 | } |
| 6810 | |
| 6811 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6812 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6813 | return ret; |
| 6814 | } |
| 6815 | |
| 6816 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6817 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6818 | * @param[in] ctx Compile context. |
| 6819 | * @param[in] node Node to check. |
| 6820 | * @return LY_ERR value |
| 6821 | */ |
| 6822 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6823 | 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] | 6824 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6825 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6826 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6827 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6828 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6829 | struct lysc_when **when = NULL; |
| 6830 | struct lysc_must *musts = NULL; |
| 6831 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6832 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6833 | |
| 6834 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6835 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6836 | if (node->flags & LYS_CONFIG_R) { |
| 6837 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent); |
| 6838 | if (op) { |
| 6839 | /* we are actually in output */ |
| 6840 | opts = LYXP_SCNODE_OUTPUT; |
| 6841 | } |
| 6842 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6843 | |
| 6844 | switch (node->nodetype) { |
| 6845 | case LYS_CONTAINER: |
| 6846 | when = ((struct lysc_node_container *)node)->when; |
| 6847 | musts = ((struct lysc_node_container *)node)->musts; |
| 6848 | break; |
| 6849 | case LYS_CHOICE: |
| 6850 | when = ((struct lysc_node_choice *)node)->when; |
| 6851 | break; |
| 6852 | case LYS_LEAF: |
| 6853 | when = ((struct lysc_node_leaf *)node)->when; |
| 6854 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6855 | break; |
| 6856 | case LYS_LEAFLIST: |
| 6857 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6858 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6859 | break; |
| 6860 | case LYS_LIST: |
| 6861 | when = ((struct lysc_node_list *)node)->when; |
| 6862 | musts = ((struct lysc_node_list *)node)->musts; |
| 6863 | break; |
| 6864 | case LYS_ANYXML: |
| 6865 | case LYS_ANYDATA: |
| 6866 | when = ((struct lysc_node_anydata *)node)->when; |
| 6867 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6868 | break; |
| 6869 | case LYS_CASE: |
| 6870 | when = ((struct lysc_node_case *)node)->when; |
| 6871 | break; |
| 6872 | case LYS_NOTIF: |
| 6873 | musts = ((struct lysc_notif *)node)->musts; |
| 6874 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6875 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6876 | case LYS_ACTION: |
| 6877 | /* first process input musts */ |
| 6878 | musts = ((struct lysc_action *)node)->input.musts; |
| 6879 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6880 | default: |
| 6881 | /* nothing to check */ |
| 6882 | break; |
| 6883 | } |
| 6884 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6885 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6886 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6887 | 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] | 6888 | 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] | 6889 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6890 | 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] | 6891 | goto cleanup; |
| 6892 | } |
| 6893 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6894 | ctx->path[0] = '\0'; |
| 6895 | 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] | 6896 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6897 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6898 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6899 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6900 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6901 | /* 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] | 6902 | 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] | 6903 | schema->name); |
| 6904 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6905 | |
| 6906 | /* check dummy node accessing */ |
| 6907 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6908 | 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] | 6909 | ret = LY_EVALID; |
| 6910 | goto cleanup; |
| 6911 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6912 | } |
| 6913 | } |
| 6914 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6915 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6916 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6917 | LY_CHECK_GOTO(ret, cleanup); |
| 6918 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6919 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6920 | } |
| 6921 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6922 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6923 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6924 | LY_ARRAY_FOR(musts, u) { |
| 6925 | 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] | 6926 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6927 | 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] | 6928 | goto cleanup; |
| 6929 | } |
| 6930 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6931 | ctx->path[0] = '\0'; |
| 6932 | 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] | 6933 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6934 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6935 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6936 | /* 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] | 6937 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6938 | 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] | 6939 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6940 | } |
| 6941 | } |
| 6942 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6943 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6944 | } |
| 6945 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6946 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6947 | /* now check output musts */ |
| 6948 | input_done = 1; |
| 6949 | musts = ((struct lysc_action *)node)->output.musts; |
| 6950 | opts = LYXP_SCNODE_OUTPUT; |
| 6951 | goto check_musts; |
| 6952 | } |
| 6953 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6954 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6955 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6956 | return ret; |
| 6957 | } |
| 6958 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6959 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6960 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 6961 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6962 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6963 | struct ly_path *p; |
| 6964 | struct lysc_type *type; |
| 6965 | |
| 6966 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6967 | |
| 6968 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6969 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 6970 | lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, |
| 6971 | lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6972 | |
| 6973 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6974 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6975 | ly_path_free(node->module->ctx, p); |
| 6976 | |
| 6977 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6978 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6979 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 6980 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 6981 | return LY_EVALID; |
| 6982 | } |
| 6983 | |
| 6984 | /* check status */ |
| 6985 | ctx->path[0] = '\0'; |
| 6986 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6987 | ctx->path_len = strlen(ctx->path); |
| 6988 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 6989 | return LY_EVALID; |
| 6990 | } |
| 6991 | ctx->path_len = 1; |
| 6992 | ctx->path[1] = '\0'; |
| 6993 | |
| 6994 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6995 | if (lref->require_instance) { |
| 6996 | for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent); |
| 6997 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6998 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 6999 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 7000 | return LY_EVALID; |
| 7001 | } |
| 7002 | } |
| 7003 | |
| 7004 | /* store the target's type and check for circular chain of leafrefs */ |
| 7005 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 7006 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 7007 | if (type == (struct lysc_type *)lref) { |
| 7008 | /* circular chain detected */ |
| 7009 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7010 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 7011 | return LY_EVALID; |
| 7012 | } |
| 7013 | } |
| 7014 | |
| 7015 | /* check if leafref and its target are under common if-features */ |
| 7016 | if (lys_compile_leafref_features_validate(node, target)) { |
| 7017 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7018 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7019 | " features applicable to the leafref itself.", lref->path->expr); |
| 7020 | return LY_EVALID; |
| 7021 | } |
| 7022 | |
| 7023 | return LY_SUCCESS; |
| 7024 | } |
| 7025 | |
| 7026 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7027 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7028 | { |
| 7029 | struct lysc_ext_instance *ext; |
| 7030 | struct lysp_ext_instance *ext_p = NULL; |
| 7031 | struct lysp_stmt *stmt; |
| 7032 | const struct lys_module *ext_mod; |
| 7033 | LY_ERR ret = LY_SUCCESS; |
| 7034 | |
| 7035 | /* create the parsed extension instance manually */ |
| 7036 | ext_p = calloc(1, sizeof *ext_p); |
| 7037 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 7038 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 7039 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 7040 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7041 | ext_p->insubstmt_index = 0; |
| 7042 | |
| 7043 | stmt = calloc(1, sizeof *ext_p->child); |
| 7044 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 7045 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 7046 | stmt->kw = LY_STMT_TYPE; |
| 7047 | ext_p->child = stmt; |
| 7048 | |
| 7049 | /* allocate new extension instance */ |
| 7050 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7051 | |
| 7052 | /* manually get extension definition module */ |
| 7053 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7054 | |
| 7055 | /* compile the extension instance */ |
| 7056 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7057 | |
| 7058 | cleanup: |
| 7059 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7060 | free(ext_p); |
| 7061 | return ret; |
| 7062 | } |
| 7063 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7064 | static LY_ERR |
| 7065 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7066 | { |
| 7067 | struct lysc_node *node; |
| 7068 | struct lysc_type *type, *typeiter; |
| 7069 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7070 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7071 | uint32_t i; |
| 7072 | |
| 7073 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7074 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7075 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7076 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7077 | node = ctx->leafrefs.objs[i]; |
| 7078 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7079 | type = ((struct lysc_node_leaf *)node)->type; |
| 7080 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7081 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7082 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7083 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7084 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7085 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7086 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7087 | } |
| 7088 | } |
| 7089 | } |
| 7090 | } |
| 7091 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7092 | /* store pointer to the real type */ |
| 7093 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7094 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7095 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7096 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7097 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7098 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
| 7099 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7100 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7101 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7102 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7103 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7104 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7105 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7106 | } |
| 7107 | } |
| 7108 | } |
| 7109 | } |
| 7110 | |
| 7111 | /* check xpath */ |
| 7112 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7113 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7114 | } |
| 7115 | |
| 7116 | /* finish incomplete default values compilation */ |
| 7117 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 7118 | struct ly_err_item *err = NULL; |
| 7119 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
| 7120 | LY_ERR ret; |
| 7121 | ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original), |
| 7122 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7123 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7124 | if (err) { |
| 7125 | ly_err_print(err); |
| 7126 | ctx->path[0] = '\0'; |
| 7127 | lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7128 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7129 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7130 | ly_err_free(err); |
| 7131 | } |
| 7132 | LY_CHECK_RET(ret); |
| 7133 | } |
| 7134 | |
| 7135 | return LY_SUCCESS; |
| 7136 | } |
| 7137 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7138 | LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7139 | lys_compile(struct lys_module **mod, int options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7140 | { |
| 7141 | struct lysc_ctx ctx = {0}; |
| 7142 | struct lysc_module *mod_c; |
| 7143 | struct lysp_module *sp; |
| 7144 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7145 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7146 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7147 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7148 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7149 | uint32_t i; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7150 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7151 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7152 | 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] | 7153 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7154 | if (!(*mod)->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7155 | /* just imported modules are not compiled */ |
| 7156 | return LY_SUCCESS; |
| 7157 | } |
| 7158 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7159 | sp = (*mod)->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7160 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7161 | ctx.ctx = (*mod)->ctx; |
| 7162 | ctx.mod = *mod; |
| 7163 | ctx.mod_def = *mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7164 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7165 | ctx.path_len = 1; |
| 7166 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7167 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7168 | (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7169 | LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM); |
| 7170 | mod_c->mod = *mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7171 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7172 | LY_ARRAY_FOR(sp->imports, u) { |
| 7173 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 7174 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7175 | LY_ARRAY_FOR(sp->includes, u) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7176 | LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7177 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7178 | |
| 7179 | /* features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7180 | if ((*mod)->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7181 | /* there is already precompiled array of features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7182 | mod_c->features = (*mod)->dis_features; |
| 7183 | (*mod)->dis_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7184 | } else { |
| 7185 | /* features are compiled directly into the compiled module structure, |
| 7186 | * 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] | 7187 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7188 | LY_CHECK_GOTO(ret, error); |
| 7189 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7190 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7191 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7192 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7193 | * are present. */ |
| 7194 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7195 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7196 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7197 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7198 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7199 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7200 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7201 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7202 | 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] | 7203 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7204 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7205 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7206 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7207 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7208 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7209 | /* identities, work similarly to features with the precompilation */ |
| 7210 | if ((*mod)->dis_identities) { |
| 7211 | mod_c->identities = (*mod)->dis_identities; |
| 7212 | (*mod)->dis_identities = NULL; |
| 7213 | } else { |
| 7214 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities); |
| 7215 | LY_CHECK_GOTO(ret, error); |
| 7216 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7217 | if (sp->identities) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7218 | 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] | 7219 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7220 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7221 | LY_ARRAY_FOR(sp->includes, v) { |
| 7222 | if (sp->includes[v].submodule->identities) { |
| 7223 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7224 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities); |
| 7225 | LY_CHECK_GOTO(ret, error); |
| 7226 | lysc_update_path(&ctx, NULL, NULL); |
| 7227 | } |
| 7228 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7229 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7230 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7231 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7232 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7233 | 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] | 7234 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7235 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7236 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7237 | 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] | 7238 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7239 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7240 | 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] | 7241 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7242 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7243 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7244 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7245 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7246 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7247 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7248 | /* extension instances TODO cover extension instances from submodules */ |
| 7249 | 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] | 7250 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7251 | /* finish compilation for all unresolved items in the context */ |
| 7252 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7253 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7254 | /* validate non-instantiated groupings from the parsed schema, |
| 7255 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7256 | ctx.options |= LYSC_OPT_GROUPING; |
| 7257 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7258 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7259 | 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] | 7260 | } |
| 7261 | } |
| 7262 | LY_LIST_FOR(sp->data, node_p) { |
| 7263 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7264 | LY_ARRAY_FOR(grps, u) { |
| 7265 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7266 | 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] | 7267 | } |
| 7268 | } |
| 7269 | } |
| 7270 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7271 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7272 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7273 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7274 | } |
| 7275 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7276 | #if 0 |
| 7277 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7278 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7279 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7280 | * the anotation definitions available in the internal schema structure. */ |
| 7281 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7282 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7283 | lys_free(mod, NULL, 1, 1); |
| 7284 | return NULL; |
| 7285 | } |
| 7286 | } |
| 7287 | #endif |
| 7288 | |
| 7289 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7290 | if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) { |
| 7291 | 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] | 7292 | } |
| 7293 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7294 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7295 | ly_set_erase(&ctx.xpath, NULL); |
| 7296 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7297 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7298 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7299 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7300 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7301 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7302 | lysp_module_free((*mod)->parsed); |
| 7303 | (*mod)->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7304 | } |
| 7305 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7306 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7307 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7308 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7309 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7310 | if (m->implemented == 2) { |
| 7311 | m->implemented = 1; |
| 7312 | } |
| 7313 | } |
| 7314 | } |
| 7315 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7316 | (*mod)->compiled = mod_c; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7317 | return LY_SUCCESS; |
| 7318 | |
| 7319 | error: |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7320 | lys_feature_precompile_revert(&ctx, *mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7321 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7322 | ly_set_erase(&ctx.xpath, NULL); |
| 7323 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7324 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7325 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7326 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7327 | lysc_module_free(mod_c, NULL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7328 | (*mod)->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7329 | |
| 7330 | /* revert compilation of modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7331 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7332 | m = ctx.ctx->list.objs[i]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7333 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7334 | /* revert features list to the precompiled state */ |
| 7335 | lys_feature_precompile_revert(&ctx, m); |
| 7336 | /* mark module as imported-only / not-implemented */ |
| 7337 | m->implemented = 0; |
| 7338 | /* free the compiled version of the module */ |
| 7339 | lysc_module_free(m->compiled, NULL); |
| 7340 | m->compiled = NULL; |
| 7341 | } |
| 7342 | } |
| 7343 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7344 | /* remove the module itself from the context and free it */ |
| 7345 | ly_set_rm(&ctx.ctx->list, *mod, NULL); |
| 7346 | lys_module_free(*mod, NULL); |
| 7347 | *mod = NULL; |
| 7348 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7349 | return ret; |
| 7350 | } |