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 |
| 1483 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1484 | 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 |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1646 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1647 | struct lysc_range *base_range, struct lysc_range **range) |
| 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; |
| 3374 | unsigned int u; |
| 3375 | LY_ERR ret = LY_SUCCESS; |
| 3376 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3377 | if (cont_p->presence) { |
| 3378 | cont->flags |= LYS_PRESENCE; |
| 3379 | } |
| 3380 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3381 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3382 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3383 | } |
| 3384 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3385 | 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] | 3386 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3387 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3388 | ly_set_add(&ctx->xpath, cont, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3389 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3390 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3391 | 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] | 3392 | |
| 3393 | done: |
| 3394 | return ret; |
| 3395 | } |
| 3396 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3397 | /* |
| 3398 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3399 | * @param[in] ctx Compile context. |
| 3400 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3401 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3402 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3403 | * @return LY_ERR value. |
| 3404 | */ |
| 3405 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3406 | 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] | 3407 | { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3408 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3409 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3410 | 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] | 3411 | leaf->units ? NULL : &leaf->units)); |
| 3412 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3413 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3414 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3415 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3416 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3417 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3418 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3419 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3420 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3421 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3422 | } |
| 3423 | } else { |
| 3424 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3425 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3426 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3427 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3428 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3429 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3430 | } |
| 3431 | } |
| 3432 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3433 | /* 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] | 3434 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3435 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3436 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3437 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3438 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3439 | /* 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] | 3440 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3441 | } |
| 3442 | } |
| 3443 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3444 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3445 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3446 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3447 | return LY_EVALID; |
| 3448 | } |
| 3449 | } |
| 3450 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3451 | return LY_SUCCESS; |
| 3452 | } |
| 3453 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3454 | /** |
| 3455 | * @brief Compile parsed leaf node information. |
| 3456 | * @param[in] ctx Compile context |
| 3457 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3458 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3459 | * is enriched with the leaf-specific information. |
| 3460 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3461 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3462 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3463 | 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] | 3464 | { |
| 3465 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3466 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3467 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3468 | LY_ERR ret = LY_SUCCESS; |
| 3469 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3470 | 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] | 3471 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3472 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3473 | ly_set_add(&ctx->xpath, leaf, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3474 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3475 | if (leaf_p->units) { |
| 3476 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3477 | leaf->flags |= LYS_SET_UNITS; |
| 3478 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3479 | |
| 3480 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3481 | leaf->dflt = (void*)leaf_p->dflt; |
| 3482 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3483 | if (ret) { |
| 3484 | leaf->dflt = NULL; |
| 3485 | return ret; |
| 3486 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3487 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3488 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3489 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3490 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3491 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3492 | leaf->dflt->realtype = leaf->type; |
| 3493 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3494 | 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] | 3495 | 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] | 3496 | leaf->dflt->realtype->refcount++; |
| 3497 | if (err) { |
| 3498 | ly_err_print(err); |
| 3499 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3500 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3501 | ly_err_free(err); |
| 3502 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3503 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3504 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3505 | 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] | 3506 | |
| 3507 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3508 | ret = LY_SUCCESS; |
| 3509 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3510 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3511 | leaf->flags |= LYS_SET_DFLT; |
| 3512 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3513 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3514 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3515 | done: |
| 3516 | return ret; |
| 3517 | } |
| 3518 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3519 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3520 | * @brief Compile parsed leaf-list node information. |
| 3521 | * @param[in] ctx Compile context |
| 3522 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3523 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3524 | * is enriched with the leaf-list-specific information. |
| 3525 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3526 | */ |
| 3527 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3528 | 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] | 3529 | { |
| 3530 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3531 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3532 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3533 | LY_ERR ret = LY_SUCCESS; |
| 3534 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3535 | 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] | 3536 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3537 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3538 | ly_set_add(&ctx->xpath, llist, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3539 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3540 | if (llist_p->units) { |
| 3541 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3542 | llist->flags |= LYS_SET_UNITS; |
| 3543 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3544 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3545 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3546 | llist->dflts = (void*)llist_p->dflts; |
| 3547 | 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] | 3548 | if (ret != LY_SUCCESS) { |
| 3549 | /* make sure the defaults are freed correctly */ |
| 3550 | if (llist_p->dflts) { |
| 3551 | llist->dflts = NULL; |
| 3552 | } |
| 3553 | return ret; |
| 3554 | } |
| 3555 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3556 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3557 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3558 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done); |
| 3559 | 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] | 3560 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3561 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3562 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3563 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3564 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3565 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3566 | llist->dflts[u]->realtype = llist->type; |
| 3567 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3568 | 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] | 3569 | 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] | 3570 | llist->dflts[u]->realtype->refcount++; |
| 3571 | if (err) { |
| 3572 | ly_err_print(err); |
| 3573 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3574 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3575 | ly_err_free(err); |
| 3576 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3577 | if (ret == LY_EINCOMPLETE) { |
| 3578 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3579 | 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] | 3580 | |
| 3581 | /* but in general result is so far ok */ |
| 3582 | ret = LY_SUCCESS; |
| 3583 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3584 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3585 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3586 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3587 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3588 | 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] | 3589 | /* configuration data values must be unique - so check the default values */ |
| 3590 | LY_ARRAY_FOR(llist->dflts, u) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3591 | for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3592 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3593 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3594 | 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] | 3595 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3596 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3597 | if (dynamic) { |
| 3598 | free((char*)val); |
| 3599 | } |
| 3600 | return LY_EVALID; |
| 3601 | } |
| 3602 | } |
| 3603 | } |
| 3604 | } |
| 3605 | |
| 3606 | /* 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] | 3607 | |
| 3608 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3609 | if (llist->min) { |
| 3610 | llist->flags |= LYS_MAND_TRUE; |
| 3611 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3612 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3613 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3614 | done: |
| 3615 | return ret; |
| 3616 | } |
| 3617 | |
| 3618 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3619 | * @brief Compile information about list's uniques. |
| 3620 | * @param[in] ctx Compile context. |
| 3621 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3622 | * @param[in] uniques Sized array list of unique statements. |
| 3623 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3624 | * @return LY_ERR value. |
| 3625 | */ |
| 3626 | static LY_ERR |
| 3627 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3628 | { |
| 3629 | LY_ERR ret = LY_SUCCESS; |
| 3630 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3631 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3632 | const char *keystr, *delim; |
| 3633 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3634 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3635 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3636 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3637 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3638 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3639 | config = -1; |
| 3640 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3641 | keystr = uniques[v]; |
| 3642 | while (keystr) { |
| 3643 | delim = strpbrk(keystr, " \t\n"); |
| 3644 | if (delim) { |
| 3645 | len = delim - keystr; |
| 3646 | while (isspace(*delim)) { |
| 3647 | ++delim; |
| 3648 | } |
| 3649 | } else { |
| 3650 | len = strlen(keystr); |
| 3651 | } |
| 3652 | |
| 3653 | /* unique node must be present */ |
| 3654 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3655 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3656 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3657 | if (ret != LY_SUCCESS) { |
| 3658 | if (ret == LY_EDENIED) { |
| 3659 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3660 | "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] | 3661 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3662 | } |
| 3663 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3664 | } else if (flags) { |
| 3665 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3666 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3667 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3668 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3669 | } |
| 3670 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3671 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3672 | /* all referenced leafs must be of the same config type */ |
| 3673 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3674 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3675 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3676 | return LY_EVALID; |
| 3677 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3678 | config = 1; |
| 3679 | } else { /* LYS_CONFIG_R */ |
| 3680 | config = 0; |
| 3681 | } |
| 3682 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3683 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3684 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3685 | if (parent->nodetype == LYS_LIST) { |
| 3686 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3687 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3688 | return LY_EVALID; |
| 3689 | } |
| 3690 | } |
| 3691 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3692 | /* check status */ |
| 3693 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3694 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3695 | |
| 3696 | /* mark leaf as unique */ |
| 3697 | (*key)->flags |= LYS_UNIQUE; |
| 3698 | |
| 3699 | /* next unique value in line */ |
| 3700 | keystr = delim; |
| 3701 | } |
| 3702 | /* next unique definition */ |
| 3703 | } |
| 3704 | |
| 3705 | return LY_SUCCESS; |
| 3706 | } |
| 3707 | |
| 3708 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3709 | * @brief Compile parsed list node information. |
| 3710 | * @param[in] ctx Compile context |
| 3711 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3712 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3713 | * is enriched with the list-specific information. |
| 3714 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3715 | */ |
| 3716 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3717 | 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] | 3718 | { |
| 3719 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3720 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3721 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3722 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3723 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3724 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3725 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3726 | LY_ERR ret = LY_SUCCESS; |
| 3727 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3728 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3729 | if (list->min) { |
| 3730 | list->flags |= LYS_MAND_TRUE; |
| 3731 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3732 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3733 | |
| 3734 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3735 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3736 | } |
| 3737 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3738 | 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] | 3739 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3740 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3741 | ly_set_add(&ctx->xpath, list, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3742 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3743 | |
| 3744 | /* keys */ |
| 3745 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3746 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3747 | return LY_EVALID; |
| 3748 | } |
| 3749 | |
| 3750 | /* find all the keys (must be direct children) */ |
| 3751 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3752 | if (!keystr) { |
| 3753 | /* keyless list */ |
| 3754 | list->flags |= LYS_KEYLESS; |
| 3755 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3756 | while (keystr) { |
| 3757 | delim = strpbrk(keystr, " \t\n"); |
| 3758 | if (delim) { |
| 3759 | len = delim - keystr; |
| 3760 | while (isspace(*delim)) { |
| 3761 | ++delim; |
| 3762 | } |
| 3763 | } else { |
| 3764 | len = strlen(keystr); |
| 3765 | } |
| 3766 | |
| 3767 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3768 | 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] | 3769 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3770 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3771 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3772 | return LY_EVALID; |
| 3773 | } |
| 3774 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3775 | if (key->flags & LYS_KEY) { |
| 3776 | /* the node was already marked as a key */ |
| 3777 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3778 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3779 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3780 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3781 | |
| 3782 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3783 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3784 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3785 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3786 | return LY_EVALID; |
| 3787 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3788 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3789 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3790 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3791 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3792 | "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] | 3793 | return LY_EVALID; |
| 3794 | } |
| 3795 | } else { |
| 3796 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3797 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3798 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3799 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3800 | return LY_EVALID; |
| 3801 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3802 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3803 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3804 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3805 | return LY_EVALID; |
| 3806 | } |
| 3807 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3808 | |
| 3809 | /* check status */ |
| 3810 | 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] | 3811 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3812 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3813 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3814 | if (key->dflt) { |
| 3815 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3816 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3817 | free(key->dflt); |
| 3818 | key->dflt = NULL; |
| 3819 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3820 | } |
| 3821 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3822 | key->flags |= LYS_KEY; |
| 3823 | |
| 3824 | /* move it to the correct position */ |
| 3825 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3826 | /* fix links in closest previous siblings of the key */ |
| 3827 | if (key->next) { |
| 3828 | key->next->prev = key->prev; |
| 3829 | } else { |
| 3830 | /* last child */ |
| 3831 | list->child->prev = key->prev; |
| 3832 | } |
| 3833 | if (key->prev->next) { |
| 3834 | key->prev->next = key->next; |
| 3835 | } |
| 3836 | /* fix links in the key */ |
| 3837 | if (prev_key) { |
| 3838 | key->prev = (struct lysc_node*)prev_key; |
| 3839 | key->next = prev_key->next; |
| 3840 | } else { |
| 3841 | key->prev = list->child->prev; |
| 3842 | key->next = list->child; |
| 3843 | } |
| 3844 | /* fix links in closes future siblings of the key */ |
| 3845 | if (prev_key) { |
| 3846 | if (prev_key->next) { |
| 3847 | prev_key->next->prev = (struct lysc_node*)key; |
| 3848 | } else { |
| 3849 | list->child->prev = (struct lysc_node*)key; |
| 3850 | } |
| 3851 | prev_key->next = (struct lysc_node*)key; |
| 3852 | } else { |
| 3853 | list->child->prev = (struct lysc_node*)key; |
| 3854 | } |
| 3855 | /* fix links in parent */ |
| 3856 | if (!key->prev->next) { |
| 3857 | list->child = (struct lysc_node*)key; |
| 3858 | } |
| 3859 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3860 | |
| 3861 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3862 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3863 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3864 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | /* uniques */ |
| 3868 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3869 | 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] | 3870 | } |
| 3871 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3872 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3873 | 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] | 3874 | |
| 3875 | done: |
| 3876 | return ret; |
| 3877 | } |
| 3878 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3879 | /** |
| 3880 | * @brief Do some checks and set the default choice's case. |
| 3881 | * |
| 3882 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3883 | * |
| 3884 | * @param[in] ctx Compile context. |
| 3885 | * @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, |
| 3886 | * not the reference to the imported module. |
| 3887 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3888 | * @return LY_ERR value. |
| 3889 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3890 | static LY_ERR |
| 3891 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3892 | { |
| 3893 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3894 | const char *prefix = NULL, *name; |
| 3895 | size_t prefix_len = 0; |
| 3896 | |
| 3897 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3898 | name = strchr(dflt, ':'); |
| 3899 | if (name) { |
| 3900 | prefix = dflt; |
| 3901 | prefix_len = name - prefix; |
| 3902 | ++name; |
| 3903 | } else { |
| 3904 | name = dflt; |
| 3905 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3906 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3907 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3908 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3909 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3910 | prefix_len, prefix); |
| 3911 | return LY_EVALID; |
| 3912 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3913 | 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] | 3914 | if (!ch->dflt) { |
| 3915 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3916 | "Default case \"%s\" not found.", dflt); |
| 3917 | return LY_EVALID; |
| 3918 | } |
| 3919 | /* no mandatory nodes directly under the default case */ |
| 3920 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3921 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3922 | break; |
| 3923 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3924 | if (iter->flags & LYS_MAND_TRUE) { |
| 3925 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3926 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3927 | return LY_EVALID; |
| 3928 | } |
| 3929 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3930 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3931 | return LY_SUCCESS; |
| 3932 | } |
| 3933 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3934 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3935 | 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] | 3936 | { |
| 3937 | struct lys_module *mod; |
| 3938 | const char *prefix = NULL, *name; |
| 3939 | size_t prefix_len = 0; |
| 3940 | struct lysc_node_case *cs; |
| 3941 | struct lysc_node *node; |
| 3942 | |
| 3943 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3944 | name = strchr(dflt, ':'); |
| 3945 | if (name) { |
| 3946 | prefix = dflt; |
| 3947 | prefix_len = name - prefix; |
| 3948 | ++name; |
| 3949 | } else { |
| 3950 | name = dflt; |
| 3951 | } |
| 3952 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3953 | if (prefix) { |
| 3954 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3955 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3956 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3957 | "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] | 3958 | return LY_EVALID; |
| 3959 | } |
| 3960 | } else { |
| 3961 | mod = ctx->mod; |
| 3962 | } |
| 3963 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3964 | 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] | 3965 | if (!cs) { |
| 3966 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3967 | "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] | 3968 | return LY_EVALID; |
| 3969 | } |
| 3970 | |
| 3971 | /* check that there is no mandatory node */ |
| 3972 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3973 | if (node->parent != (struct lysc_node*)cs) { |
| 3974 | break; |
| 3975 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3976 | if (node->flags & LYS_MAND_TRUE) { |
| 3977 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3978 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3979 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3980 | return LY_EVALID; |
| 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | /* set the default case in choice */ |
| 3985 | ch->dflt = cs; |
| 3986 | cs->flags |= LYS_SET_DFLT; |
| 3987 | |
| 3988 | return LY_SUCCESS; |
| 3989 | } |
| 3990 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3991 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3992 | * @brief Compile parsed choice node information. |
| 3993 | * @param[in] ctx Compile context |
| 3994 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3995 | * @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] | 3996 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3997 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3998 | */ |
| 3999 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4000 | 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] | 4001 | { |
| 4002 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4003 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4004 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4005 | LY_ERR ret = LY_SUCCESS; |
| 4006 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4007 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4008 | if (child_p->nodetype == LYS_CASE) { |
| 4009 | 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] | 4010 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4011 | } |
| 4012 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4013 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4014 | } |
| 4015 | } |
| 4016 | |
| 4017 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4018 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4019 | 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] | 4020 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4021 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4022 | return ret; |
| 4023 | } |
| 4024 | |
| 4025 | /** |
| 4026 | * @brief Compile parsed anydata or anyxml node information. |
| 4027 | * @param[in] ctx Compile context |
| 4028 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4029 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4030 | * is enriched with the any-specific information. |
| 4031 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4032 | */ |
| 4033 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4034 | 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] | 4035 | { |
| 4036 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4037 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4038 | unsigned int u; |
| 4039 | LY_ERR ret = LY_SUCCESS; |
| 4040 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4041 | 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] | 4042 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4043 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4044 | ly_set_add(&ctx->xpath, any, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4045 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4046 | |
| 4047 | if (any->flags & LYS_CONFIG_W) { |
| 4048 | 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] | 4049 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4050 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4051 | done: |
| 4052 | return ret; |
| 4053 | } |
| 4054 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4055 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4056 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4057 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4058 | * |
| 4059 | * @param[in] ctx Compile context |
| 4060 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4061 | * the choice itself is expected instead of a specific case node. |
| 4062 | * @param[in] node Schema node to connect into the list. |
| 4063 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4064 | * 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] | 4065 | */ |
| 4066 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4067 | 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] | 4068 | { |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4069 | struct lysc_node **children, *start, *end; |
| 4070 | const struct lys_module *mod; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4071 | |
| 4072 | if (node->nodetype == LYS_CASE) { |
| 4073 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4074 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4075 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4076 | } |
| 4077 | if (children) { |
| 4078 | if (!(*children)) { |
| 4079 | /* first child */ |
| 4080 | *children = node; |
| 4081 | } else if (*children != node) { |
| 4082 | /* by the condition in previous branch we cover the choice/case children |
| 4083 | * - the children list is shared by the choice and the the first case, in addition |
| 4084 | * the first child of each case must be referenced from the case node. So the node is |
| 4085 | * actually always already inserted in case it is the first children - so here such |
| 4086 | * a situation actually corresponds to the first branch */ |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4087 | if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module) |
| 4088 | && (strcmp((*children)->prev->module->name, node->module->name) > 0)) { |
| 4089 | /* some augments are already connected and we are connecting new ones, |
| 4090 | * keep module name order and insert the node into the children list */ |
| 4091 | end = (*children); |
| 4092 | do { |
| 4093 | end = end->prev; |
| 4094 | mod = end->module; |
| 4095 | while (end->prev->module == mod) { |
| 4096 | end = end->prev; |
| 4097 | } |
| 4098 | } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module) |
| 4099 | && (strcmp(mod->name, node->module->name) > 0)); |
| 4100 | |
| 4101 | /* we have the last existing node after our node, easily get the first before and connect it */ |
| 4102 | start = end->prev; |
| 4103 | start->next = node; |
| 4104 | node->next = end; |
| 4105 | end->prev = node; |
| 4106 | node->prev = start; |
| 4107 | } else { |
| 4108 | /* insert at the end of the parent's children list */ |
| 4109 | (*children)->prev->next = node; |
| 4110 | node->prev = (*children)->prev; |
| 4111 | (*children)->prev = node; |
| 4112 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4113 | |
| 4114 | /* check the name uniqueness */ |
| 4115 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4116 | lysc_node_notifs(parent), node->name, node)) { |
| 4117 | return LY_EEXIST; |
| 4118 | } |
| 4119 | } |
| 4120 | } |
| 4121 | return LY_SUCCESS; |
| 4122 | } |
| 4123 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4124 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4125 | * @brief Prepare the case structure in choice node for the new data node. |
| 4126 | * |
| 4127 | * 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 |
| 4128 | * created in the choice when the first child was processed. |
| 4129 | * |
| 4130 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4131 | * @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, |
| 4132 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4133 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4134 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4135 | * @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, |
| 4136 | * 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] | 4137 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4138 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4139 | 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] | 4140 | { |
| 4141 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4142 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4143 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4144 | unsigned int u; |
| 4145 | LY_ERR ret; |
| 4146 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4147 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4148 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4149 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4150 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4151 | return NULL; \ |
| 4152 | } \ |
| 4153 | } |
| 4154 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4155 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4156 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4157 | |
| 4158 | /* we have to add an implicit case node into the parent choice */ |
| 4159 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4160 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4161 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4162 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4163 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4164 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4165 | /* the case is already present since the child is not its first children */ |
| 4166 | return (struct lysc_node_case*)ch->cases->prev; |
| 4167 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4168 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4169 | |
| 4170 | /* explicit parent case is not present (this is its first child) */ |
| 4171 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4172 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4173 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4174 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4175 | cs->sp = node_p; |
| 4176 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4177 | /* 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] | 4178 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4179 | |
| 4180 | if (node_p->when) { |
| 4181 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4182 | 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] | 4183 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4184 | |
| 4185 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4186 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4187 | ly_set_add(&ctx->xpath, cs, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4188 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4189 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4190 | 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] | 4191 | } else { |
| 4192 | LOGINT(ctx->ctx); |
| 4193 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4194 | } |
| 4195 | cs->module = ctx->mod; |
| 4196 | cs->prev = (struct lysc_node*)cs; |
| 4197 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4198 | 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] | 4199 | cs->child = child; |
| 4200 | |
| 4201 | return cs; |
| 4202 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4203 | if (cs) { |
| 4204 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4205 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4206 | return NULL; |
| 4207 | |
| 4208 | #undef UNIQUE_CHECK |
| 4209 | } |
| 4210 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4211 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4212 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4213 | * |
| 4214 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4215 | * @param[in] node Target node where the config is supposed to be changed. |
| 4216 | * @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] | 4217 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4218 | * 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] | 4219 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4220 | * @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] | 4221 | * @return LY_ERR value. |
| 4222 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4223 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4224 | 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] | 4225 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4226 | { |
| 4227 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4228 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4229 | |
| 4230 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4231 | /* nothing to do */ |
| 4232 | return LY_SUCCESS; |
| 4233 | } |
| 4234 | |
| 4235 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4236 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4237 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4238 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4239 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4240 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4241 | return LY_EVALID; |
| 4242 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4243 | node->flags |= LYS_SET_CONFIG; |
| 4244 | } else { |
| 4245 | if (node->flags & LYS_SET_CONFIG) { |
| 4246 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4247 | /* setting config flags, but have node with explicit config true */ |
| 4248 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4249 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4250 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4251 | return LY_EVALID; |
| 4252 | } |
| 4253 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4254 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4255 | return LY_SUCCESS; |
| 4256 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4257 | } |
| 4258 | node->flags &= ~LYS_CONFIG_MASK; |
| 4259 | node->flags |= config; |
| 4260 | |
| 4261 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4262 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4263 | 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] | 4264 | } |
| 4265 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4266 | return LY_SUCCESS; |
| 4267 | } |
| 4268 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4269 | /** |
| 4270 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4271 | * |
| 4272 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4273 | * the flag to such parents from a mandatory children. |
| 4274 | * |
| 4275 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4276 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4277 | * (mandatory children was removed). |
| 4278 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4279 | void |
| 4280 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4281 | { |
| 4282 | struct lysc_node *iter; |
| 4283 | |
| 4284 | if (add) { /* set flag */ |
| 4285 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4286 | parent = parent->parent) { |
| 4287 | parent->flags |= LYS_MAND_TRUE; |
| 4288 | } |
| 4289 | } else { /* unset flag */ |
| 4290 | 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] | 4291 | 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] | 4292 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4293 | /* there is another mandatory node */ |
| 4294 | return; |
| 4295 | } |
| 4296 | } |
| 4297 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4298 | parent->flags &= ~LYS_MAND_TRUE; |
| 4299 | } |
| 4300 | } |
| 4301 | } |
| 4302 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4303 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4304 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4305 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4306 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4307 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4308 | */ |
| 4309 | static void |
| 4310 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4311 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4312 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4313 | size_t len; |
| 4314 | |
| 4315 | len = strlen(aug_p->nodeid); |
| 4316 | LY_ARRAY_FOR(result, v) { |
| 4317 | if (strlen(result[v]->nodeid) <= len) { |
| 4318 | continue; |
| 4319 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4320 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4321 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4322 | 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] | 4323 | break; |
| 4324 | } |
| 4325 | } |
| 4326 | result[v] = aug_p; |
| 4327 | LY_ARRAY_INCREMENT(result); |
| 4328 | } |
| 4329 | |
| 4330 | /** |
| 4331 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4332 | * |
| 4333 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4334 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4335 | * |
| 4336 | * @param[in] ctx Compile context. |
| 4337 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4338 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4339 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4340 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4341 | * @return LY_ERR value. |
| 4342 | */ |
| 4343 | LY_ERR |
| 4344 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4345 | { |
| 4346 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4347 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4348 | |
| 4349 | assert(augments); |
| 4350 | |
| 4351 | /* get count of the augments in module and all its submodules */ |
| 4352 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4353 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4354 | } |
| 4355 | LY_ARRAY_FOR(inc_p, u) { |
| 4356 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4357 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4358 | } |
| 4359 | } |
| 4360 | |
| 4361 | if (!count) { |
| 4362 | *augments = NULL; |
| 4363 | return LY_SUCCESS; |
| 4364 | } |
| 4365 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4366 | |
| 4367 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4368 | * together, so there can be even /z/y betwwen them. */ |
| 4369 | LY_ARRAY_FOR(aug_p, u) { |
| 4370 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4371 | } |
| 4372 | LY_ARRAY_FOR(inc_p, u) { |
| 4373 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4374 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4375 | } |
| 4376 | } |
| 4377 | |
| 4378 | *augments = result; |
| 4379 | return LY_SUCCESS; |
| 4380 | } |
| 4381 | |
| 4382 | /** |
| 4383 | * @brief Compile the parsed augment connecting it into its target. |
| 4384 | * |
| 4385 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4386 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4387 | * are already implemented and compiled. |
| 4388 | * |
| 4389 | * @param[in] ctx Compile context. |
| 4390 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4391 | * @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 |
| 4392 | * children in case of the augmenting uses data. |
| 4393 | * @return LY_SUCCESS on success. |
| 4394 | * @return LY_EVALID on failure. |
| 4395 | */ |
| 4396 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4397 | 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] | 4398 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4399 | LY_ERR ret = LY_SUCCESS, rc; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4400 | struct lysp_node *node_p, *case_node_p; |
| 4401 | struct lysc_node *target; /* target target of the augment */ |
| 4402 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4403 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4404 | struct lys_module **aug_mod; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4405 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4406 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4407 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4408 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4409 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4410 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4411 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4412 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4413 | 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] | 4414 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4415 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4416 | if (ret != LY_SUCCESS) { |
| 4417 | if (ret == LY_EDENIED) { |
| 4418 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4419 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4420 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4421 | } |
| 4422 | return LY_EVALID; |
| 4423 | } |
| 4424 | |
| 4425 | /* check for mandatory nodes |
| 4426 | * - new cases augmenting some choice can have mandatory nodes |
| 4427 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4428 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4429 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4430 | allow_mandatory = 1; |
| 4431 | } |
| 4432 | |
| 4433 | when_shared = NULL; |
| 4434 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4435 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4436 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4437 | && !((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] | 4438 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4439 | && !(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] | 4440 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4441 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4442 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4443 | return LY_EVALID; |
| 4444 | } |
| 4445 | |
| 4446 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4447 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4448 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4449 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4450 | } else { |
| 4451 | 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] | 4452 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4453 | } |
| 4454 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4455 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4456 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4457 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4458 | * 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] | 4459 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4460 | /* 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] | 4461 | 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] | 4462 | } else if (target->nodetype == LYS_CHOICE) { |
| 4463 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4464 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4465 | } else { |
| 4466 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4467 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4468 | if (!node) { |
| 4469 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4470 | break; |
| 4471 | } |
| 4472 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4473 | } |
| 4474 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4475 | 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] | 4476 | node->flags &= ~LYS_MAND_TRUE; |
| 4477 | lys_compile_mandatory_parents(target, 0); |
| 4478 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4479 | "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] | 4480 | return LY_EVALID; |
| 4481 | } |
| 4482 | |
| 4483 | /* pass augment's when to all the children */ |
| 4484 | if (aug_p->when) { |
| 4485 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4486 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4487 | 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] | 4488 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4489 | |
| 4490 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4491 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4492 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4493 | } |
| 4494 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4495 | when_shared = *when; |
| 4496 | } else { |
| 4497 | ++when_shared->refcount; |
| 4498 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4499 | |
| 4500 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4501 | /* 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] | 4502 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4503 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4504 | } |
| 4505 | } |
| 4506 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4507 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4508 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4509 | switch (target->nodetype) { |
| 4510 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4511 | 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] | 4512 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4513 | 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] | 4514 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4515 | break; |
| 4516 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4517 | 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] | 4518 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4519 | 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] | 4520 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4521 | break; |
| 4522 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4523 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4524 | if (aug_p->actions) { |
| 4525 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4526 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4527 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4528 | return LY_EVALID; |
| 4529 | } |
| 4530 | if (aug_p->notifs) { |
| 4531 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4532 | "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] | 4533 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4534 | return LY_EVALID; |
| 4535 | } |
| 4536 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4537 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4538 | /* add this module into the target module augmented_by, if not there already */ |
| 4539 | rc = LY_SUCCESS; |
| 4540 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4541 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4542 | rc = LY_EEXIST; |
| 4543 | break; |
| 4544 | } |
| 4545 | } |
| 4546 | if (!rc) { |
| 4547 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4548 | *aug_mod = ctx->mod; |
| 4549 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4550 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4551 | lysc_update_path(ctx, NULL, NULL); |
| 4552 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4553 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4554 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4555 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4556 | return ret; |
| 4557 | } |
| 4558 | |
| 4559 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4560 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4561 | * |
| 4562 | * @param[in] ctx Compile context. |
| 4563 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4564 | * @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] | 4565 | * @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] | 4566 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4567 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4568 | * 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] | 4569 | * @return LY_ERR value. |
| 4570 | */ |
| 4571 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4572 | 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] | 4573 | { |
| 4574 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4575 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4576 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4577 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4578 | return LY_EVALID; |
| 4579 | } |
| 4580 | |
| 4581 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4582 | /* check if node has default value */ |
| 4583 | if (node->nodetype & LYS_LEAF) { |
| 4584 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4585 | if (refine_flag) { |
| 4586 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4587 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4588 | return LY_EVALID; |
| 4589 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4590 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4591 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4592 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4593 | |
| 4594 | /* update the list of incomplete default values if needed */ |
| 4595 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4596 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4597 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4598 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4599 | free(leaf->dflt); |
| 4600 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4601 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4602 | } |
| 4603 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4604 | if (refine_flag) { |
| 4605 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4606 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4607 | return LY_EVALID; |
| 4608 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4609 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4610 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4611 | 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] | 4612 | return LY_EVALID; |
| 4613 | } |
| 4614 | |
| 4615 | node->flags &= ~LYS_MAND_FALSE; |
| 4616 | node->flags |= LYS_MAND_TRUE; |
| 4617 | lys_compile_mandatory_parents(node->parent, 1); |
| 4618 | } else { |
| 4619 | /* make mandatory false */ |
| 4620 | node->flags &= ~LYS_MAND_TRUE; |
| 4621 | node->flags |= LYS_MAND_FALSE; |
| 4622 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4623 | 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] | 4624 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4625 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4626 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4627 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4628 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4629 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4630 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4631 | } |
| 4632 | } |
| 4633 | return LY_SUCCESS; |
| 4634 | } |
| 4635 | |
| 4636 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4637 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4638 | * If present, also apply uses's modificators. |
| 4639 | * |
| 4640 | * @param[in] ctx Compile context |
| 4641 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4642 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4643 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4644 | * the compile context. |
| 4645 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4646 | */ |
| 4647 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4648 | 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] | 4649 | { |
| 4650 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4651 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4652 | /* 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] | 4653 | struct lysc_node_container context_node_fake = |
| 4654 | {.nodetype = LYS_CONTAINER, |
| 4655 | .module = ctx->mod, |
| 4656 | .flags = parent ? parent->flags : 0, |
| 4657 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4658 | .prev = (struct lysc_node*)&context_node_fake, |
| 4659 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4660 | struct lysp_grp *grp = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4661 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4662 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4663 | int found; |
| 4664 | const char *id, *name, *prefix; |
| 4665 | size_t prefix_len, name_len; |
| 4666 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4667 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4668 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4669 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4670 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4671 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4672 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4673 | struct lysp_augment **augments = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4674 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4675 | struct lysc_notif **notifs = NULL; |
| 4676 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4677 | |
| 4678 | /* search for the grouping definition */ |
| 4679 | found = 0; |
| 4680 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4681 | 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] | 4682 | if (prefix) { |
| 4683 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4684 | if (!mod) { |
| 4685 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4686 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4687 | return LY_EVALID; |
| 4688 | } |
| 4689 | } else { |
| 4690 | mod = ctx->mod_def; |
| 4691 | } |
| 4692 | if (mod == ctx->mod_def) { |
| 4693 | 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] | 4694 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4695 | LY_ARRAY_FOR(grp, u) { |
| 4696 | if (!strcmp(grp[u].name, name)) { |
| 4697 | grp = &grp[u]; |
| 4698 | found = 1; |
| 4699 | break; |
| 4700 | } |
| 4701 | } |
| 4702 | } |
| 4703 | } |
| 4704 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4705 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4706 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4707 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4708 | for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4709 | if (!strcmp(grp[u].name, name)) { |
| 4710 | grp = &grp[u]; |
| 4711 | found = 1; |
| 4712 | } |
| 4713 | } |
| 4714 | } |
| 4715 | if (!found && mod->parsed->includes) { |
| 4716 | /* ... and all the submodules */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4717 | for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4718 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4719 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4720 | for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4721 | if (!strcmp(grp[v].name, name)) { |
| 4722 | grp = &grp[v]; |
| 4723 | found = 1; |
| 4724 | } |
| 4725 | } |
| 4726 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4727 | } |
| 4728 | } |
| 4729 | } |
| 4730 | if (!found) { |
| 4731 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4732 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4733 | return LY_EVALID; |
| 4734 | } |
| 4735 | |
| 4736 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4737 | grp_stack_count = ctx->groupings.count; |
| 4738 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4739 | if (grp_stack_count == ctx->groupings.count) { |
| 4740 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4742 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4743 | return LY_EVALID; |
| 4744 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4745 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4746 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4747 | grp->flags |= LYS_USED_GRP; |
| 4748 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4749 | |
| 4750 | /* switch context's mod_def */ |
| 4751 | mod_old = ctx->mod_def; |
| 4752 | ctx->mod_def = mod; |
| 4753 | |
| 4754 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4755 | 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] | 4756 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4757 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4758 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4759 | /* 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] | 4760 | 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] | 4761 | |
| 4762 | /* some preparation for applying refines */ |
| 4763 | if (grp->data == node_p) { |
| 4764 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4765 | if (parent) { |
| 4766 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4767 | } else if (ctx->mod->compiled->data) { |
| 4768 | child = ctx->mod->compiled->data; |
| 4769 | } else { |
| 4770 | child = NULL; |
| 4771 | } |
| 4772 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4773 | } |
| 4774 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4775 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4776 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4777 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4778 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4779 | /* 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] | 4780 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4781 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4782 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4783 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 4784 | |
| 4785 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4786 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4787 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4788 | } |
| 4789 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4790 | when_shared = *when; |
| 4791 | } else { |
| 4792 | ++when_shared->refcount; |
| 4793 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4794 | |
| 4795 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4796 | /* 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] | 4797 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4798 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4799 | } |
| 4800 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4801 | } |
| 4802 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4803 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4804 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4805 | /* 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] | 4806 | 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] | 4807 | } |
| 4808 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4809 | /* compile actions */ |
| 4810 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4811 | if (actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4812 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4813 | 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] | 4814 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4815 | /* 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] | 4816 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 4817 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 4818 | 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] | 4819 | } |
| 4820 | } |
| 4821 | |
| 4822 | /* compile notifications */ |
| 4823 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4824 | if (notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4825 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4826 | 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] | 4827 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4828 | /* 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] | 4829 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 4830 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 4831 | 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] | 4832 | } |
| 4833 | } |
| 4834 | |
| 4835 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4836 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4837 | 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] | 4838 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4839 | 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] | 4840 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4841 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4842 | /* reload previous context's mod_def */ |
| 4843 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4844 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4845 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4846 | /* apply refine */ |
| 4847 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4848 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4849 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4850 | 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] | 4851 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4852 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4853 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4854 | |
| 4855 | /* default value */ |
| 4856 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4857 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4858 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4859 | "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.", |
| 4860 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4861 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4862 | } |
| 4863 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4864 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4865 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4866 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4867 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4868 | } |
| 4869 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4870 | struct ly_err_item *err = NULL; |
| 4871 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4872 | if (leaf->dflt) { |
| 4873 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4874 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4875 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4876 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4877 | } else { |
| 4878 | /* prepare a new one */ |
| 4879 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4880 | leaf->dflt->realtype = leaf->type; |
| 4881 | } |
| 4882 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4883 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4884 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4885 | 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] | 4886 | 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] | 4887 | leaf->dflt->realtype->refcount++; |
| 4888 | if (err) { |
| 4889 | ly_err_print(err); |
| 4890 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4891 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4892 | ly_err_free(err); |
| 4893 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4894 | if (rc == LY_EINCOMPLETE) { |
| 4895 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4896 | 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] | 4897 | |
| 4898 | /* but in general result is so far ok */ |
| 4899 | rc = LY_SUCCESS; |
| 4900 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4901 | LY_CHECK_GOTO(rc, cleanup); |
| 4902 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4903 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4904 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4905 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4906 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4907 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4908 | "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] | 4909 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4910 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4911 | |
| 4912 | /* remove previous set of default values */ |
| 4913 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4914 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4915 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 4916 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 4917 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4918 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4919 | LY_ARRAY_FREE(llist->dflts); |
| 4920 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4921 | LY_ARRAY_FREE(llist->dflts_mods); |
| 4922 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4923 | |
| 4924 | /* create the new set of the default values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4925 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup); |
| 4926 | 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] | 4927 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4928 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4929 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 4930 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4931 | LY_ARRAY_INCREMENT(llist->dflts); |
| 4932 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 4933 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4934 | 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] | 4935 | 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] | 4936 | 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] | 4937 | llist->dflts[u]->realtype->refcount++; |
| 4938 | if (err) { |
| 4939 | ly_err_print(err); |
| 4940 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4941 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 4942 | ly_err_free(err); |
| 4943 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4944 | if (rc == LY_EINCOMPLETE) { |
| 4945 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4946 | 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] | 4947 | |
| 4948 | /* but in general result is so far ok */ |
| 4949 | rc = LY_SUCCESS; |
| 4950 | } |
| 4951 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4952 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4953 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4954 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4955 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4956 | /* unset LYS_SET_DFLT from the current default case */ |
| 4957 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4958 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4959 | 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] | 4960 | } |
| 4961 | } |
| 4962 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4963 | /* description */ |
| 4964 | if (rfn->dsc) { |
| 4965 | FREE_STRING(ctx->ctx, node->dsc); |
| 4966 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4967 | } |
| 4968 | |
| 4969 | /* reference */ |
| 4970 | if (rfn->ref) { |
| 4971 | FREE_STRING(ctx->ctx, node->ref); |
| 4972 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4973 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4974 | |
| 4975 | /* config */ |
| 4976 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4977 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4978 | 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] | 4979 | } else { |
| 4980 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4981 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4982 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4983 | } |
| 4984 | |
| 4985 | /* mandatory */ |
| 4986 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4987 | 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] | 4988 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4989 | |
| 4990 | /* presence */ |
| 4991 | if (rfn->presence) { |
| 4992 | if (node->nodetype != LYS_CONTAINER) { |
| 4993 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4994 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4995 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4996 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4997 | } |
| 4998 | node->flags |= LYS_PRESENCE; |
| 4999 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5000 | |
| 5001 | /* must */ |
| 5002 | if (rfn->musts) { |
| 5003 | switch (node->nodetype) { |
| 5004 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5005 | 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] | 5006 | break; |
| 5007 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5008 | 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] | 5009 | break; |
| 5010 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5011 | 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] | 5012 | break; |
| 5013 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5014 | 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] | 5015 | break; |
| 5016 | case LYS_ANYXML: |
| 5017 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5018 | 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] | 5019 | break; |
| 5020 | default: |
| 5021 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5022 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5023 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5024 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5025 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5026 | ly_set_add(&ctx->xpath, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5027 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5028 | |
| 5029 | /* min/max-elements */ |
| 5030 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5031 | switch (node->nodetype) { |
| 5032 | case LYS_LEAFLIST: |
| 5033 | if (rfn->flags & LYS_SET_MAX) { |
| 5034 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5035 | } |
| 5036 | if (rfn->flags & LYS_SET_MIN) { |
| 5037 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5038 | if (rfn->min) { |
| 5039 | node->flags |= LYS_MAND_TRUE; |
| 5040 | lys_compile_mandatory_parents(node->parent, 1); |
| 5041 | } else { |
| 5042 | node->flags &= ~LYS_MAND_TRUE; |
| 5043 | lys_compile_mandatory_parents(node->parent, 0); |
| 5044 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5045 | } |
| 5046 | break; |
| 5047 | case LYS_LIST: |
| 5048 | if (rfn->flags & LYS_SET_MAX) { |
| 5049 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5050 | } |
| 5051 | if (rfn->flags & LYS_SET_MIN) { |
| 5052 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5053 | if (rfn->min) { |
| 5054 | node->flags |= LYS_MAND_TRUE; |
| 5055 | lys_compile_mandatory_parents(node->parent, 1); |
| 5056 | } else { |
| 5057 | node->flags &= ~LYS_MAND_TRUE; |
| 5058 | lys_compile_mandatory_parents(node->parent, 0); |
| 5059 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5060 | } |
| 5061 | break; |
| 5062 | default: |
| 5063 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5064 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5065 | (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] | 5066 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5067 | } |
| 5068 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5069 | |
| 5070 | /* if-feature */ |
| 5071 | if (rfn->iffeatures) { |
| 5072 | /* 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] | 5073 | 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] | 5074 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5075 | |
| 5076 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5077 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5078 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5079 | /* 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] | 5080 | for (uint32_t i = 0; i < refined.count; ++i) { |
| 5081 | node = (struct lysc_node*)refined.objs[i]; |
| 5082 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5083 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5084 | |
| 5085 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5086 | if ((node->flags & LYS_MAND_TRUE) && |
| 5087 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5088 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5089 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5090 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5091 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5092 | } |
| 5093 | |
| 5094 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5095 | if (node->nodetype == LYS_LIST) { |
| 5096 | min = ((struct lysc_node_list*)node)->min; |
| 5097 | max = ((struct lysc_node_list*)node)->max; |
| 5098 | } else { |
| 5099 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5100 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5101 | } |
| 5102 | if (min > max) { |
| 5103 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5104 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5105 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5106 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5107 | } |
| 5108 | } |
| 5109 | } |
| 5110 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5111 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5112 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5113 | |
| 5114 | cleanup: |
| 5115 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5116 | if (context_node_fake.child) { |
| 5117 | context_node_fake.child->prev = child; |
| 5118 | } |
| 5119 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5120 | child->parent = parent; |
| 5121 | } |
| 5122 | |
| 5123 | if (uses_p->augments || uses_p->refines) { |
| 5124 | /* 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] | 5125 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5126 | 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] | 5127 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5128 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5129 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5130 | 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] | 5131 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5132 | } |
| 5133 | } |
| 5134 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5135 | /* reload previous context's mod_def */ |
| 5136 | ctx->mod_def = mod_old; |
| 5137 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5138 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5139 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5140 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5141 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5142 | |
| 5143 | return ret; |
| 5144 | } |
| 5145 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5146 | static int |
| 5147 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5148 | { |
| 5149 | struct lysp_node *iter; |
| 5150 | int len = 0; |
| 5151 | |
| 5152 | *path = NULL; |
| 5153 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5154 | char *s = *path; |
| 5155 | char *id; |
| 5156 | |
| 5157 | switch (iter->nodetype) { |
| 5158 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5159 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5160 | break; |
| 5161 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5162 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5163 | break; |
| 5164 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5165 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5166 | break; |
| 5167 | default: |
| 5168 | id = strdup(iter->name); |
| 5169 | break; |
| 5170 | } |
| 5171 | |
| 5172 | if (!iter->parent) { |
| 5173 | /* print prefix */ |
| 5174 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5175 | } else { |
| 5176 | /* prefix is the same as in parent */ |
| 5177 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5178 | } |
| 5179 | free(s); |
| 5180 | free(id); |
| 5181 | } |
| 5182 | |
| 5183 | if (len < 0) { |
| 5184 | free(*path); |
| 5185 | *path = NULL; |
| 5186 | } else if (len == 0) { |
| 5187 | *path = strdup("/"); |
| 5188 | len = 1; |
| 5189 | } |
| 5190 | return len; |
| 5191 | } |
| 5192 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5193 | /** |
| 5194 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5195 | * |
| 5196 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5197 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5198 | */ |
| 5199 | static LY_ERR |
| 5200 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5201 | { |
| 5202 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5203 | char *path; |
| 5204 | int len; |
| 5205 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5206 | struct lysp_node_uses fake_uses = { |
| 5207 | .parent = node_p, |
| 5208 | .nodetype = LYS_USES, |
| 5209 | .flags = 0, .next = NULL, |
| 5210 | .name = grp->name, |
| 5211 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5212 | .refines = NULL, .augments = NULL |
| 5213 | }; |
| 5214 | struct lysc_node_container fake_container = { |
| 5215 | .nodetype = LYS_CONTAINER, |
| 5216 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5217 | .module = ctx->mod, |
| 5218 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5219 | .prev = (struct lysc_node*)&fake_container, |
| 5220 | .name = "fake", |
| 5221 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5222 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5223 | }; |
| 5224 | |
| 5225 | if (grp->parent) { |
| 5226 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5227 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5228 | |
| 5229 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5230 | if (len < 0) { |
| 5231 | LOGMEM(ctx->ctx); |
| 5232 | return LY_EMEM; |
| 5233 | } |
| 5234 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5235 | ctx->path_len = (uint16_t)len; |
| 5236 | free(path); |
| 5237 | |
| 5238 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5239 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5240 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5241 | lysc_update_path(ctx, NULL, NULL); |
| 5242 | lysc_update_path(ctx, NULL, NULL); |
| 5243 | |
| 5244 | ctx->path_len = 1; |
| 5245 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5246 | |
| 5247 | /* cleanup */ |
| 5248 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5249 | |
| 5250 | return ret; |
| 5251 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5252 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5253 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5254 | * @brief Compile parsed schema node information. |
| 5255 | * @param[in] ctx Compile context |
| 5256 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5257 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5258 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5259 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5260 | * @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). |
| 5261 | * 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] | 5262 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5263 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5264 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5265 | 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] | 5266 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5267 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5268 | struct lysc_node *node; |
| 5269 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5270 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5271 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5272 | 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] | 5273 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5274 | if (node_p->nodetype != LYS_USES) { |
| 5275 | lysc_update_path(ctx, parent, node_p->name); |
| 5276 | } else { |
| 5277 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5278 | lysc_update_path(ctx, NULL, node_p->name); |
| 5279 | } |
| 5280 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5281 | switch (node_p->nodetype) { |
| 5282 | case LYS_CONTAINER: |
| 5283 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5284 | node_compile_spec = lys_compile_node_container; |
| 5285 | break; |
| 5286 | case LYS_LEAF: |
| 5287 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5288 | node_compile_spec = lys_compile_node_leaf; |
| 5289 | break; |
| 5290 | case LYS_LIST: |
| 5291 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5292 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5293 | break; |
| 5294 | case LYS_LEAFLIST: |
| 5295 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5296 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5297 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5298 | case LYS_CHOICE: |
| 5299 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5300 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5301 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5302 | case LYS_ANYXML: |
| 5303 | case LYS_ANYDATA: |
| 5304 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5305 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5306 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5307 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5308 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5309 | lysc_update_path(ctx, NULL, NULL); |
| 5310 | lysc_update_path(ctx, NULL, NULL); |
| 5311 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5312 | default: |
| 5313 | LOGINT(ctx->ctx); |
| 5314 | return LY_EINT; |
| 5315 | } |
| 5316 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5317 | node->nodetype = node_p->nodetype; |
| 5318 | node->module = ctx->mod; |
| 5319 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5320 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5321 | |
| 5322 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5323 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5324 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5325 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5326 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5327 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5328 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5329 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5330 | node->flags |= LYS_CONFIG_R; |
| 5331 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5332 | /* config not explicitely set, inherit it from parent */ |
| 5333 | if (parent) { |
| 5334 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5335 | } else { |
| 5336 | /* default is config true */ |
| 5337 | node->flags |= LYS_CONFIG_W; |
| 5338 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5339 | } else { |
| 5340 | /* config set explicitely */ |
| 5341 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5342 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5343 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5344 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5345 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5346 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5347 | goto error; |
| 5348 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5349 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5350 | /* *list ordering */ |
| 5351 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5352 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5353 | 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] | 5354 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5355 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5356 | node->flags &= ~LYS_ORDBY_MASK; |
| 5357 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5358 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5359 | /* default ordering is system */ |
| 5360 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5361 | } |
| 5362 | } |
| 5363 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5364 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5365 | * 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] | 5366 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5367 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5368 | * 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] | 5369 | 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] | 5370 | } |
| 5371 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5372 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5373 | node->sp = node_p; |
| 5374 | } |
| 5375 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5376 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5377 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5378 | if (node_p->when) { |
| 5379 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5380 | 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] | 5381 | |
| 5382 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5383 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5384 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5385 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5386 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5387 | 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] | 5388 | |
| 5389 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5390 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5391 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5392 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5393 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5394 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5395 | if (node->flags & LYS_MAND_TRUE) { |
| 5396 | lys_compile_mandatory_parents(parent, 1); |
| 5397 | } |
| 5398 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5399 | lysc_update_path(ctx, NULL, NULL); |
| 5400 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5401 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5402 | if (parent) { |
| 5403 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5404 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5405 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5406 | } else { |
| 5407 | lysc_update_path(ctx, parent, node->name); |
| 5408 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5409 | 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] | 5410 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5411 | if (uses_status) { |
| 5412 | |
| 5413 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5414 | /* 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] | 5415 | * it directly gets the same status flags as the choice; |
| 5416 | * 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] | 5417 | 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] | 5418 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5419 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5420 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5421 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5422 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5423 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5424 | 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] | 5425 | |
| 5426 | if (parent->nodetype == LYS_CHOICE) { |
| 5427 | lysc_update_path(ctx, NULL, NULL); |
| 5428 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5429 | } else { |
| 5430 | /* top-level element */ |
| 5431 | if (!ctx->mod->compiled->data) { |
| 5432 | ctx->mod->compiled->data = node; |
| 5433 | } else { |
| 5434 | /* insert at the end of the module's top-level nodes list */ |
| 5435 | ctx->mod->compiled->data->prev->next = node; |
| 5436 | node->prev = ctx->mod->compiled->data->prev; |
| 5437 | ctx->mod->compiled->data->prev = node; |
| 5438 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5439 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5440 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5441 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5442 | return LY_EVALID; |
| 5443 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5444 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5445 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5446 | |
| 5447 | return LY_SUCCESS; |
| 5448 | |
| 5449 | error: |
| 5450 | lysc_node_free(ctx->ctx, node); |
| 5451 | return ret; |
| 5452 | } |
| 5453 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5454 | static void |
| 5455 | lysc_disconnect(struct lysc_node *node) |
| 5456 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5457 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5458 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5459 | int remove_cs = 0; |
| 5460 | |
| 5461 | parent = node->parent; |
| 5462 | |
| 5463 | /* parent's first child */ |
| 5464 | if (!parent) { |
| 5465 | return; |
| 5466 | } |
| 5467 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5468 | cs = (struct lysc_node_case*)node; |
| 5469 | } else if (parent->nodetype == LYS_CASE) { |
| 5470 | /* disconnecting some node in a case */ |
| 5471 | cs = (struct lysc_node_case*)parent; |
| 5472 | parent = cs->parent; |
| 5473 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5474 | if (child == node) { |
| 5475 | if (cs->child == child) { |
| 5476 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5477 | /* case with a single child -> remove also the case */ |
| 5478 | child->parent = NULL; |
| 5479 | remove_cs = 1; |
| 5480 | } else { |
| 5481 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5482 | } |
| 5483 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5484 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5485 | } |
| 5486 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5487 | if (!remove_cs) { |
| 5488 | cs = NULL; |
| 5489 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5490 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5491 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5492 | } |
| 5493 | |
| 5494 | if (cs) { |
| 5495 | if (remove_cs) { |
| 5496 | /* cs has only one child which is being also removed */ |
| 5497 | lysc_disconnect((struct lysc_node*)cs); |
| 5498 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5499 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5500 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5501 | /* default case removed */ |
| 5502 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5503 | } |
| 5504 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5505 | /* first case removed */ |
| 5506 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5507 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5508 | if (cs->child) { |
| 5509 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5510 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5511 | prev = cs->child->prev; |
| 5512 | } /* else all the children are under a single case */ |
| 5513 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5514 | if (child->parent != (struct lysc_node*)cs) { |
| 5515 | break; |
| 5516 | } |
| 5517 | lysc_node_free(node->module->ctx, child); |
| 5518 | } |
| 5519 | if (prev) { |
| 5520 | if (prev->next) { |
| 5521 | prev->next = child; |
| 5522 | } |
| 5523 | if (child) { |
| 5524 | child->prev = prev; |
| 5525 | } else { |
| 5526 | /* link from the first child under the cases */ |
| 5527 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5528 | } |
| 5529 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5530 | } |
| 5531 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5532 | } |
| 5533 | |
| 5534 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5535 | if (node->prev->next) { |
| 5536 | node->prev->next = node->next; |
| 5537 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5538 | if (node->next) { |
| 5539 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5540 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5541 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5542 | if (child) { |
| 5543 | child->prev = node->prev; |
| 5544 | } |
| 5545 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5546 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5551 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5552 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5553 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5554 | struct ly_set devs_p = {0}; |
| 5555 | struct ly_set targets = {0}; |
| 5556 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5557 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5558 | struct lysc_action *rpcs; |
| 5559 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5560 | struct lysp_deviation *dev; |
| 5561 | struct lysp_deviate *d, **dp_new; |
| 5562 | struct lysp_deviate_add *d_add; |
| 5563 | struct lysp_deviate_del *d_del; |
| 5564 | struct lysp_deviate_rpl *d_rpl; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5565 | LY_ARRAY_COUNT_TYPE u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5566 | struct lysc_deviation { |
| 5567 | const char *nodeid; |
| 5568 | struct lysc_node *target; /* target node of the deviation */ |
| 5569 | 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] | 5570 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5571 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5572 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5573 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5574 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5575 | const char *prefix, *name, *nodeid, *dflt; |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 5576 | struct lys_module *mod, **dev_mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5577 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5578 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5579 | |
| 5580 | /* get all deviations from the module and all its submodules ... */ |
| 5581 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5582 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5583 | } |
| 5584 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5585 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5586 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5587 | } |
| 5588 | } |
| 5589 | if (!devs_p.count) { |
| 5590 | /* nothing to do */ |
| 5591 | return LY_SUCCESS; |
| 5592 | } |
| 5593 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5594 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5595 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5596 | /* ... and group them by the target node */ |
| 5597 | devs = calloc(devs_p.count, sizeof *devs); |
| 5598 | for (u = 0; u < devs_p.count; ++u) { |
| 5599 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5600 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5601 | |
| 5602 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5603 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5604 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5605 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5606 | /* move the target pointer to input/output to make them different from the action and |
| 5607 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5608 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5609 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5610 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5611 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5612 | flags |= LYSC_OPT_INTERNAL; |
| 5613 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5614 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5615 | flags |= LYSC_OPT_INTERNAL; |
| 5616 | } |
| 5617 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5618 | /* insert into the set of targets with duplicity detection */ |
| 5619 | i = ly_set_add(&targets, target, 0); |
| 5620 | if (!devs[i]) { |
| 5621 | /* new record */ |
| 5622 | devs[i] = calloc(1, sizeof **devs); |
| 5623 | devs[i]->target = target; |
| 5624 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5625 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5626 | } |
| 5627 | /* add deviates into the deviation's list of deviates */ |
| 5628 | for (d = dev->deviates; d; d = d->next) { |
| 5629 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5630 | *dp_new = d; |
| 5631 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5632 | devs[i]->not_supported = 1; |
| 5633 | } |
| 5634 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5635 | |
| 5636 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5637 | } |
| 5638 | |
| 5639 | /* MACROS for deviates checking */ |
| 5640 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5641 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5642 | 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] | 5643 | goto cleanup; \ |
| 5644 | } |
| 5645 | |
| 5646 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5647 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5648 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5649 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5650 | goto cleanup; \ |
| 5651 | } |
| 5652 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5653 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5654 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5655 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5657 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5658 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5659 | goto cleanup; \ |
| 5660 | } |
| 5661 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5662 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5663 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5664 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5665 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5666 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5667 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5668 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5669 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5670 | goto cleanup; \ |
| 5671 | } |
| 5672 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5673 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5674 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5675 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5676 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5677 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5678 | goto cleanup; \ |
| 5679 | } |
| 5680 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5681 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5682 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5683 | 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] | 5684 | goto cleanup; \ |
| 5685 | } |
| 5686 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5687 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5688 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5689 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5690 | "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] | 5691 | goto cleanup; \ |
| 5692 | } |
| 5693 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5694 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5695 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5696 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5697 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5698 | 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] | 5699 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5700 | if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5701 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5702 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5703 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5704 | goto cleanup; \ |
| 5705 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5706 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5707 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5708 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5709 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5710 | (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] | 5711 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5712 | if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5713 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5714 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5715 | } |
| 5716 | |
| 5717 | /* apply deviations */ |
| 5718 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5719 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5720 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5721 | struct ly_err_item *err = NULL; |
| 5722 | |
| 5723 | dflt = NULL; |
| 5724 | changed_type = 0; |
| 5725 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5726 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5727 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5728 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5729 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5730 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5731 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5732 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5733 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5734 | } |
| 5735 | } |
| 5736 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5737 | /* not-supported */ |
| 5738 | if (devs[u]->not_supported) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5739 | if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) { |
| 5740 | LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.", |
| 5741 | LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5742 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5743 | |
| 5744 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5745 | if (devs[u]->target->parent) { \ |
| 5746 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5747 | } else { \ |
| 5748 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5749 | } \ |
| 5750 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5751 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5752 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5753 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5754 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5755 | 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] | 5756 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5757 | } |
| 5758 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5759 | if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5760 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5761 | /* remove RPC's/action's input */ |
| 5762 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5763 | 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] | 5764 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5765 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5766 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5767 | /* remove RPC's/action's output */ |
| 5768 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5769 | 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] | 5770 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5771 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5772 | } else { |
| 5773 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5774 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5775 | } |
| 5776 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5777 | /* remove Notification */ |
| 5778 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5779 | } else { |
| 5780 | /* remove the target node */ |
| 5781 | lysc_disconnect(devs[u]->target); |
| 5782 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5783 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5784 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5785 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5786 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5787 | continue; |
| 5788 | } |
| 5789 | |
| 5790 | /* list of deviates (not-supported is not present in the list) */ |
| 5791 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5792 | d = devs[u]->deviates[v]; |
| 5793 | |
| 5794 | switch (d->mod) { |
| 5795 | case LYS_DEV_ADD: |
| 5796 | d_add = (struct lysp_deviate_add*)d; |
| 5797 | /* [units-stmt] */ |
| 5798 | if (d_add->units) { |
| 5799 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5800 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5801 | |
| 5802 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5803 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5804 | } |
| 5805 | |
| 5806 | /* *must-stmt */ |
| 5807 | if (d_add->musts) { |
| 5808 | switch (devs[u]->target->nodetype) { |
| 5809 | case LYS_CONTAINER: |
| 5810 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5811 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 5812 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5813 | break; |
| 5814 | case LYS_LEAF: |
| 5815 | case LYS_LEAFLIST: |
| 5816 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5817 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 5818 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5819 | break; |
| 5820 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5821 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 5822 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5823 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5824 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5825 | case LYS_ACTION: |
| 5826 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5827 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 5828 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5829 | break; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 5830 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5831 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 5832 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5833 | break; |
| 5834 | } |
| 5835 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5836 | default: |
| 5837 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5838 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5839 | goto cleanup; |
| 5840 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5841 | ly_set_add(&ctx->xpath, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5842 | } |
| 5843 | |
| 5844 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5845 | if (d_add->uniques) { |
| 5846 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5847 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5848 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5849 | |
| 5850 | /* *default-stmt */ |
| 5851 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5852 | switch (devs[u]->target->nodetype) { |
| 5853 | case LYS_LEAF: |
| 5854 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5855 | 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] | 5856 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5857 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5858 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5859 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5860 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5861 | } else { |
| 5862 | /* prepare new default value storage */ |
| 5863 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5864 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5865 | dflt = d_add->dflts[0]; |
| 5866 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5867 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5868 | /* mark the new default values as leaf's own */ |
| 5869 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5870 | break; |
| 5871 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5872 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5873 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5874 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5875 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5876 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5877 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5878 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5879 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5880 | LY_ARRAY_FREE(llist->dflts); |
| 5881 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5882 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5883 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5884 | } |
| 5885 | /* add new default value(s) */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5886 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup); |
| 5887 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup); |
| 5888 | for (x = y = LY_ARRAY_COUNT(llist->dflts); |
| 5889 | x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5890 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5891 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5892 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5893 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5894 | llist->dflts[x]->realtype = llist->type; |
| 5895 | 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] | 5896 | 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] | 5897 | (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] | 5898 | llist->dflts[x]->realtype->refcount++; |
| 5899 | if (err) { |
| 5900 | ly_err_print(err); |
| 5901 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5902 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5903 | d_add->dflts[x - y], err->msg); |
| 5904 | ly_err_free(err); |
| 5905 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5906 | if (rc == LY_EINCOMPLETE) { |
| 5907 | /* postpone default compilation when the tree is complete */ |
| 5908 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5909 | |
| 5910 | /* but in general result is so far ok */ |
| 5911 | rc = LY_SUCCESS; |
| 5912 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5913 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5914 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5915 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5916 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5917 | break; |
| 5918 | case LYS_CHOICE: |
| 5919 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5920 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5921 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5922 | * 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] | 5923 | 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] | 5924 | goto cleanup; |
| 5925 | } |
| 5926 | break; |
| 5927 | default: |
| 5928 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5929 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5930 | goto cleanup; |
| 5931 | } |
| 5932 | } |
| 5933 | |
| 5934 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5935 | if (d_add->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5936 | 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] | 5937 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5938 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5939 | goto cleanup; |
| 5940 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5941 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5942 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5943 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5944 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5945 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5946 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5947 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5948 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5949 | goto cleanup; |
| 5950 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5951 | 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] | 5952 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5953 | |
| 5954 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5955 | if (d_add->flags & LYS_MAND_MASK) { |
| 5956 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5957 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5958 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5959 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5960 | goto cleanup; |
| 5961 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5962 | 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] | 5963 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5964 | |
| 5965 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5966 | if (d_add->flags & LYS_SET_MIN) { |
| 5967 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5968 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5969 | /* change value */ |
| 5970 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5971 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5972 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5973 | /* change value */ |
| 5974 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5975 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5976 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5977 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5978 | goto cleanup; |
| 5979 | } |
| 5980 | if (d_add->min) { |
| 5981 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5982 | } |
| 5983 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5984 | |
| 5985 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5986 | if (d_add->flags & LYS_SET_MAX) { |
| 5987 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5988 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5989 | /* change value */ |
| 5990 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5991 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5992 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5993 | /* change value */ |
| 5994 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5995 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5996 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5997 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 5998 | goto cleanup; |
| 5999 | } |
| 6000 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6001 | |
| 6002 | break; |
| 6003 | case LYS_DEV_DELETE: |
| 6004 | d_del = (struct lysp_deviate_del*)d; |
| 6005 | |
| 6006 | /* [units-stmt] */ |
| 6007 | if (d_del->units) { |
| 6008 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6009 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6010 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6011 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6012 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6013 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6014 | goto cleanup; |
| 6015 | } |
| 6016 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6017 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6018 | } |
| 6019 | |
| 6020 | /* *must-stmt */ |
| 6021 | if (d_del->musts) { |
| 6022 | switch (devs[u]->target->nodetype) { |
| 6023 | case LYS_CONTAINER: |
| 6024 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6025 | 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] | 6026 | break; |
| 6027 | case LYS_LEAF: |
| 6028 | case LYS_LEAFLIST: |
| 6029 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6030 | 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] | 6031 | break; |
| 6032 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6033 | 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] | 6034 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6035 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6036 | case LYS_ACTION: |
| 6037 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6038 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6039 | break; |
| 6040 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6041 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6042 | break; |
| 6043 | } |
| 6044 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6045 | default: |
| 6046 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6047 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6048 | goto cleanup; |
| 6049 | } |
| 6050 | } |
| 6051 | |
| 6052 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6053 | if (d_del->uniques) { |
| 6054 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6055 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6056 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6057 | LY_ARRAY_FOR(list->uniques, z) { |
| 6058 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6059 | nodeid = strpbrk(name, " \t\n"); |
| 6060 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 6061 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6062 | break; |
| 6063 | } |
| 6064 | while (isspace(*nodeid)) { |
| 6065 | ++nodeid; |
| 6066 | } |
| 6067 | } else { |
| 6068 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6069 | break; |
| 6070 | } |
| 6071 | } |
| 6072 | } |
| 6073 | if (!name) { |
| 6074 | /* complete match - remove the unique */ |
| 6075 | LY_ARRAY_DECREMENT(list->uniques); |
| 6076 | LY_ARRAY_FREE(list->uniques[z]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6077 | 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] | 6078 | --z; |
| 6079 | break; |
| 6080 | } |
| 6081 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6082 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6083 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6084 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6085 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6086 | goto cleanup; |
| 6087 | } |
| 6088 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6089 | if (!LY_ARRAY_COUNT(list->uniques)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6090 | LY_ARRAY_FREE(list->uniques); |
| 6091 | list->uniques = NULL; |
| 6092 | } |
| 6093 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6094 | |
| 6095 | /* *default-stmt */ |
| 6096 | if (d_del->dflts) { |
| 6097 | switch (devs[u]->target->nodetype) { |
| 6098 | case LYS_LEAF: |
| 6099 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6100 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6101 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6102 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6103 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6104 | 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] | 6105 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6106 | if (i) { |
| 6107 | free((char*)dflt); |
| 6108 | } |
| 6109 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6110 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6111 | d_del->dflts[0], dflt); |
| 6112 | goto cleanup; |
| 6113 | } |
| 6114 | if (i) { |
| 6115 | free((char*)dflt); |
| 6116 | } |
| 6117 | dflt = NULL; |
| 6118 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6119 | /* update the list of incomplete default values if needed */ |
| 6120 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6121 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6122 | /* remove the default specification */ |
| 6123 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6124 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6125 | free(leaf->dflt); |
| 6126 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6127 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6128 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6129 | break; |
| 6130 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6131 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6132 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6133 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6134 | 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] | 6135 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6136 | if (i) { |
| 6137 | free((char*)dflt); |
| 6138 | } |
| 6139 | dflt = NULL; |
| 6140 | break; |
| 6141 | } |
| 6142 | if (i) { |
| 6143 | free((char*)dflt); |
| 6144 | } |
| 6145 | dflt = NULL; |
| 6146 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6147 | if (y == LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6148 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6149 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6150 | goto cleanup; |
| 6151 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6152 | |
| 6153 | /* update the list of incomplete default values if needed */ |
| 6154 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6155 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6156 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6157 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6158 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6159 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6160 | free(llist->dflts[y]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6161 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 6162 | 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] | 6163 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6164 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6165 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6166 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6167 | LY_ARRAY_FREE(llist->dflts); |
| 6168 | llist->dflts = NULL; |
| 6169 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6170 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6171 | break; |
| 6172 | case LYS_CHOICE: |
| 6173 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6174 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6175 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6176 | 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] | 6177 | if (prefix) { |
| 6178 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6179 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6180 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6181 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6182 | "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] | 6183 | goto cleanup; |
| 6184 | } |
| 6185 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6186 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6187 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6188 | "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] | 6189 | goto cleanup; |
| 6190 | } |
| 6191 | } |
| 6192 | /* else { |
| 6193 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6194 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6195 | * the module of the default case, just matching its name */ |
| 6196 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6197 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6198 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6199 | 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] | 6200 | goto cleanup; |
| 6201 | } |
| 6202 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6203 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6204 | break; |
| 6205 | default: |
| 6206 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6207 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6208 | goto cleanup; |
| 6209 | } |
| 6210 | } |
| 6211 | |
| 6212 | break; |
| 6213 | case LYS_DEV_REPLACE: |
| 6214 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6215 | |
| 6216 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6217 | if (d_rpl->type) { |
| 6218 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6219 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6220 | 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] | 6221 | |
| 6222 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6223 | /* the target has default from the previous type - remove it */ |
| 6224 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6225 | /* update the list of incomplete default values if needed */ |
| 6226 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6227 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6228 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6229 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6230 | free(leaf->dflt); |
| 6231 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6232 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6233 | } else { /* LYS_LEAFLIST */ |
| 6234 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6235 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6236 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6237 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6238 | free(llist->dflts[x]); |
| 6239 | } |
| 6240 | LY_ARRAY_FREE(llist->dflts); |
| 6241 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6242 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6243 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6244 | } |
| 6245 | } |
| 6246 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6247 | /* there is no default value, do not set changed_type after type compilation |
| 6248 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6249 | changed_type = -1; |
| 6250 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6251 | 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] | 6252 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6253 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6254 | |
| 6255 | /* [units-stmt] */ |
| 6256 | if (d_rpl->units) { |
| 6257 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6258 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6259 | units, "replacing", "units", d_rpl->units); |
| 6260 | |
| 6261 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6262 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6263 | } |
| 6264 | |
| 6265 | /* [default-stmt] */ |
| 6266 | if (d_rpl->dflt) { |
| 6267 | switch (devs[u]->target->nodetype) { |
| 6268 | case LYS_LEAF: |
| 6269 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6270 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6271 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6272 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6273 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6274 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6275 | dflt = d_rpl->dflt; |
| 6276 | /* 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] | 6277 | break; |
| 6278 | case LYS_CHOICE: |
| 6279 | 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] | 6280 | 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] | 6281 | goto cleanup; |
| 6282 | } |
| 6283 | break; |
| 6284 | default: |
| 6285 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6286 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6287 | goto cleanup; |
| 6288 | } |
| 6289 | } |
| 6290 | |
| 6291 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6292 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6293 | 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] | 6294 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6295 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6296 | goto cleanup; |
| 6297 | } |
| 6298 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6299 | 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] | 6300 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6301 | goto cleanup; |
| 6302 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6303 | 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] | 6304 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6305 | |
| 6306 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6307 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6308 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6309 | 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] | 6310 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6311 | goto cleanup; |
| 6312 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6313 | 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] | 6314 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6315 | |
| 6316 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6317 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6318 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6319 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6320 | /* change value */ |
| 6321 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6322 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6323 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6324 | /* change value */ |
| 6325 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6326 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6327 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6328 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6329 | goto cleanup; |
| 6330 | } |
| 6331 | if (d_rpl->min) { |
| 6332 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6333 | } |
| 6334 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6335 | |
| 6336 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6337 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6338 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6339 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6340 | /* change value */ |
| 6341 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6342 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6343 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6344 | /* change value */ |
| 6345 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6346 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6347 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6348 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6349 | goto cleanup; |
| 6350 | } |
| 6351 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6352 | |
| 6353 | break; |
| 6354 | default: |
| 6355 | LOGINT(ctx->ctx); |
| 6356 | goto cleanup; |
| 6357 | } |
| 6358 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6359 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6360 | /* final check when all deviations of a single target node are applied */ |
| 6361 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6362 | /* check min-max compatibility */ |
| 6363 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6364 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6365 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6366 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6367 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6368 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6369 | } else { |
| 6370 | min = max = 0; |
| 6371 | } |
| 6372 | if (min > max) { |
| 6373 | 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] | 6374 | "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] | 6375 | goto cleanup; |
| 6376 | } |
| 6377 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6378 | if (dflt) { |
| 6379 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6380 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6381 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6382 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6383 | 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] | 6384 | 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] | 6385 | leaf->dflt->realtype->refcount++; |
| 6386 | if (err) { |
| 6387 | ly_err_print(err); |
| 6388 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6389 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6390 | ly_err_free(err); |
| 6391 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6392 | if (rc == LY_EINCOMPLETE) { |
| 6393 | /* postpone default compilation when the tree is complete */ |
| 6394 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6395 | |
| 6396 | /* but in general result is so far ok */ |
| 6397 | rc = LY_SUCCESS; |
| 6398 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6399 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6400 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6401 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6402 | int dynamic; |
| 6403 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6404 | 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] | 6405 | |
| 6406 | /* update the list of incomplete default values if needed */ |
| 6407 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6408 | |
| 6409 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6410 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6411 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6412 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6413 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6414 | 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] | 6415 | 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] | 6416 | leaf->dflt->realtype->refcount++; |
| 6417 | if (err) { |
| 6418 | ly_err_print(err); |
| 6419 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6420 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6421 | ly_err_free(err); |
| 6422 | } |
| 6423 | if (dynamic) { |
| 6424 | free((void*)dflt); |
| 6425 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6426 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6427 | if (rc == LY_EINCOMPLETE) { |
| 6428 | /* postpone default compilation when the tree is complete */ |
| 6429 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6430 | |
| 6431 | /* but in general result is so far ok */ |
| 6432 | rc = LY_SUCCESS; |
| 6433 | } |
| 6434 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6435 | } else { /* LYS_LEAFLIST */ |
| 6436 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6437 | 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] | 6438 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6439 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6440 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6441 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6442 | 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] | 6443 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6444 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6445 | llist->dflts[x]->realtype->refcount++; |
| 6446 | if (err) { |
| 6447 | ly_err_print(err); |
| 6448 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6449 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6450 | dflt, err->msg); |
| 6451 | ly_err_free(err); |
| 6452 | } |
| 6453 | if (dynamic) { |
| 6454 | free((void*)dflt); |
| 6455 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6456 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6457 | if (rc == LY_EINCOMPLETE) { |
| 6458 | /* postpone default compilation when the tree is complete */ |
| 6459 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6460 | |
| 6461 | /* but in general result is so far ok */ |
| 6462 | rc = LY_SUCCESS; |
| 6463 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6464 | LY_CHECK_GOTO(rc, cleanup); |
| 6465 | } |
| 6466 | } |
| 6467 | } |
| 6468 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6469 | /* check mandatory - default compatibility */ |
| 6470 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6471 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6472 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6473 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6474 | "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] | 6475 | goto cleanup; |
| 6476 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6477 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6478 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6479 | 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] | 6480 | goto cleanup; |
| 6481 | } |
| 6482 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6483 | /* mandatory node under a default case */ |
| 6484 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6485 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6486 | 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] | 6487 | goto cleanup; |
| 6488 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6489 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6490 | /* add this module into the target module deviated_by, if not there already */ |
| 6491 | rc = LY_SUCCESS; |
| 6492 | LY_ARRAY_FOR(devs[u]->target->module->compiled->deviated_by, v) { |
| 6493 | if (devs[u]->target->module->compiled->deviated_by[v] == mod_p->mod) { |
| 6494 | rc = LY_EEXIST; |
| 6495 | break; |
| 6496 | } |
| 6497 | } |
| 6498 | if (!rc) { |
| 6499 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup); |
| 6500 | *dev_mod = mod_p->mod; |
| 6501 | } |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 6502 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6503 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6504 | } |
| 6505 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6506 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6507 | ret = LY_SUCCESS; |
| 6508 | |
| 6509 | cleanup: |
| 6510 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6511 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6512 | free(devs[u]); |
| 6513 | } |
| 6514 | free(devs); |
| 6515 | ly_set_erase(&targets, NULL); |
| 6516 | ly_set_erase(&devs_p, NULL); |
| 6517 | |
| 6518 | return ret; |
| 6519 | } |
| 6520 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6521 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6522 | * @brief Compile the given YANG submodule into the main module. |
| 6523 | * @param[in] ctx Compile context |
| 6524 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6525 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6526 | */ |
| 6527 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6528 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6529 | { |
| 6530 | unsigned int u; |
| 6531 | LY_ERR ret = LY_SUCCESS; |
| 6532 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6533 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6534 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6535 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6536 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6537 | if (!mainmod->mod->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6538 | /* features are compiled directly into the compiled module structure, |
| 6539 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6540 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6541 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6542 | LY_CHECK_GOTO(ret, error); |
| 6543 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6544 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6545 | if (!mainmod->mod->dis_identities) { |
| 6546 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities); |
| 6547 | LY_CHECK_GOTO(ret, error); |
| 6548 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6549 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6550 | /* data nodes */ |
| 6551 | LY_LIST_FOR(submod->data, node_p) { |
| 6552 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6553 | LY_CHECK_GOTO(ret, error); |
| 6554 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6555 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6556 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6557 | 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] | 6558 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6559 | error: |
| 6560 | return ret; |
| 6561 | } |
| 6562 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6563 | static void * |
| 6564 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6565 | { |
| 6566 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6567 | if (substmts[u].stmt == stmt) { |
| 6568 | return substmts[u].storage; |
| 6569 | } |
| 6570 | } |
| 6571 | return NULL; |
| 6572 | } |
| 6573 | |
| 6574 | LY_ERR |
| 6575 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6576 | { |
| 6577 | LY_ERR ret = LY_EVALID, r; |
| 6578 | unsigned int u; |
| 6579 | struct lysp_stmt *stmt; |
| 6580 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6581 | |
| 6582 | /* check for invalid substatements */ |
| 6583 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6584 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6585 | continue; |
| 6586 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6587 | for (u = 0; substmts[u].stmt; ++u) { |
| 6588 | if (substmts[u].stmt == stmt->kw) { |
| 6589 | break; |
| 6590 | } |
| 6591 | } |
| 6592 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6593 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6594 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6595 | goto cleanup; |
| 6596 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6597 | } |
| 6598 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6599 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6600 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6601 | /* keep order of the processing the same as the order in the defined substmts, |
| 6602 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6603 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6604 | int stmt_present = 0; |
| 6605 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6606 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6607 | if (substmts[u].stmt != stmt->kw) { |
| 6608 | continue; |
| 6609 | } |
| 6610 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6611 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6612 | if (substmts[u].storage) { |
| 6613 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6614 | case LY_STMT_STATUS: |
| 6615 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6616 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6617 | break; |
| 6618 | case LY_STMT_UNITS: { |
| 6619 | const char **units; |
| 6620 | |
| 6621 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6622 | /* single item */ |
| 6623 | if (*((const char **)substmts[u].storage)) { |
| 6624 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6625 | goto cleanup; |
| 6626 | } |
| 6627 | units = (const char **)substmts[u].storage; |
| 6628 | } else { |
| 6629 | /* sized array */ |
| 6630 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6631 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6632 | } |
| 6633 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6634 | break; |
| 6635 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6636 | case LY_STMT_TYPE: { |
| 6637 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6638 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6639 | |
| 6640 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6641 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6642 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6643 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6644 | goto cleanup; |
| 6645 | } |
| 6646 | compiled = substmts[u].storage; |
| 6647 | } else { |
| 6648 | /* sized array */ |
| 6649 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6650 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6651 | compiled = (void*)type; |
| 6652 | } |
| 6653 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6654 | 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] | 6655 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6656 | 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] | 6657 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6658 | lysp_type_free(ctx->ctx, parsed); |
| 6659 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6660 | break; |
| 6661 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6662 | case LY_STMT_IF_FEATURE: { |
| 6663 | struct lysc_iffeature *iff = NULL; |
| 6664 | |
| 6665 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6666 | /* single item */ |
| 6667 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6668 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6669 | goto cleanup; |
| 6670 | } |
| 6671 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6672 | } else { |
| 6673 | /* sized array */ |
| 6674 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6675 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6676 | } |
| 6677 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6678 | break; |
| 6679 | } |
| 6680 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6681 | * 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] | 6682 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6683 | 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.", |
| 6684 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6685 | goto cleanup; |
| 6686 | } |
| 6687 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6688 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6689 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6690 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6692 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6693 | goto cleanup; |
| 6694 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6695 | } |
| 6696 | |
| 6697 | ret = LY_SUCCESS; |
| 6698 | |
| 6699 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6700 | return ret; |
| 6701 | } |
| 6702 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6703 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6704 | * @brief Check when for cyclic dependencies. |
| 6705 | * @param[in] set Set with all the referenced nodes. |
| 6706 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6707 | * @return LY_ERR value |
| 6708 | */ |
| 6709 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6710 | 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] | 6711 | { |
| 6712 | struct lyxp_set tmp_set; |
| 6713 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6714 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6715 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6716 | int idx; |
| 6717 | struct lysc_when *when; |
| 6718 | LY_ERR ret = LY_SUCCESS; |
| 6719 | |
| 6720 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6721 | |
| 6722 | /* prepare in_ctx of the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6723 | for ( i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6724 | xp_scnode = &set->val.scnodes[i]; |
| 6725 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6726 | if (xp_scnode->in_ctx != -1) { |
| 6727 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6728 | xp_scnode->in_ctx = 1; |
| 6729 | } |
| 6730 | } |
| 6731 | |
| 6732 | for (i = 0; i < set->used; ++i) { |
| 6733 | xp_scnode = &set->val.scnodes[i]; |
| 6734 | if (xp_scnode->in_ctx != 1) { |
| 6735 | /* already checked */ |
| 6736 | continue; |
| 6737 | } |
| 6738 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6739 | 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] | 6740 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6741 | /* no when to check */ |
| 6742 | xp_scnode->in_ctx = 0; |
| 6743 | continue; |
| 6744 | } |
| 6745 | |
| 6746 | node = xp_scnode->scnode; |
| 6747 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6748 | LY_ARRAY_FOR(node->when, u) { |
| 6749 | when = node->when[u]; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 6750 | ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6751 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6752 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6753 | 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] | 6754 | goto cleanup; |
| 6755 | } |
| 6756 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6757 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6758 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6759 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6760 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6761 | 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] | 6762 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6763 | 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] | 6764 | ret = LY_EVALID; |
| 6765 | goto cleanup; |
| 6766 | } |
| 6767 | |
| 6768 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6769 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6770 | } else { |
| 6771 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6772 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6773 | } |
| 6774 | } |
| 6775 | |
| 6776 | /* merge this set into the global when set */ |
| 6777 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6778 | } |
| 6779 | |
| 6780 | /* check when of non-data parents as well */ |
| 6781 | node = node->parent; |
| 6782 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6783 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6784 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6785 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6786 | } |
| 6787 | |
| 6788 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6789 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6790 | return ret; |
| 6791 | } |
| 6792 | |
| 6793 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6794 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6795 | * @param[in] ctx Compile context. |
| 6796 | * @param[in] node Node to check. |
| 6797 | * @return LY_ERR value |
| 6798 | */ |
| 6799 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6800 | 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] | 6801 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6802 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6803 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6804 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6805 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6806 | struct lysc_when **when = NULL; |
| 6807 | struct lysc_must *musts = NULL; |
| 6808 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6809 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6810 | |
| 6811 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6812 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6813 | if (node->flags & LYS_CONFIG_R) { |
| 6814 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent); |
| 6815 | if (op) { |
| 6816 | /* we are actually in output */ |
| 6817 | opts = LYXP_SCNODE_OUTPUT; |
| 6818 | } |
| 6819 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6820 | |
| 6821 | switch (node->nodetype) { |
| 6822 | case LYS_CONTAINER: |
| 6823 | when = ((struct lysc_node_container *)node)->when; |
| 6824 | musts = ((struct lysc_node_container *)node)->musts; |
| 6825 | break; |
| 6826 | case LYS_CHOICE: |
| 6827 | when = ((struct lysc_node_choice *)node)->when; |
| 6828 | break; |
| 6829 | case LYS_LEAF: |
| 6830 | when = ((struct lysc_node_leaf *)node)->when; |
| 6831 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6832 | break; |
| 6833 | case LYS_LEAFLIST: |
| 6834 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6835 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6836 | break; |
| 6837 | case LYS_LIST: |
| 6838 | when = ((struct lysc_node_list *)node)->when; |
| 6839 | musts = ((struct lysc_node_list *)node)->musts; |
| 6840 | break; |
| 6841 | case LYS_ANYXML: |
| 6842 | case LYS_ANYDATA: |
| 6843 | when = ((struct lysc_node_anydata *)node)->when; |
| 6844 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6845 | break; |
| 6846 | case LYS_CASE: |
| 6847 | when = ((struct lysc_node_case *)node)->when; |
| 6848 | break; |
| 6849 | case LYS_NOTIF: |
| 6850 | musts = ((struct lysc_notif *)node)->musts; |
| 6851 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6852 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6853 | case LYS_ACTION: |
| 6854 | /* first process input musts */ |
| 6855 | musts = ((struct lysc_action *)node)->input.musts; |
| 6856 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6857 | default: |
| 6858 | /* nothing to check */ |
| 6859 | break; |
| 6860 | } |
| 6861 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6862 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6863 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6864 | 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] | 6865 | 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] | 6866 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6867 | 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] | 6868 | goto cleanup; |
| 6869 | } |
| 6870 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6871 | ctx->path[0] = '\0'; |
| 6872 | 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] | 6873 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6874 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6875 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6876 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6877 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6878 | /* 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] | 6879 | 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] | 6880 | schema->name); |
| 6881 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6882 | |
| 6883 | /* check dummy node accessing */ |
| 6884 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6885 | 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] | 6886 | ret = LY_EVALID; |
| 6887 | goto cleanup; |
| 6888 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6889 | } |
| 6890 | } |
| 6891 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6892 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6893 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6894 | LY_CHECK_GOTO(ret, cleanup); |
| 6895 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6896 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6897 | } |
| 6898 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6899 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6900 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6901 | LY_ARRAY_FOR(musts, u) { |
| 6902 | 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] | 6903 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6904 | 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] | 6905 | goto cleanup; |
| 6906 | } |
| 6907 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6908 | ctx->path[0] = '\0'; |
| 6909 | 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] | 6910 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6911 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6912 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6913 | /* 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] | 6914 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6915 | 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] | 6916 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6917 | } |
| 6918 | } |
| 6919 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6920 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6921 | } |
| 6922 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6923 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6924 | /* now check output musts */ |
| 6925 | input_done = 1; |
| 6926 | musts = ((struct lysc_action *)node)->output.musts; |
| 6927 | opts = LYXP_SCNODE_OUTPUT; |
| 6928 | goto check_musts; |
| 6929 | } |
| 6930 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6931 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6932 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6933 | return ret; |
| 6934 | } |
| 6935 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6936 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6937 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 6938 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6939 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6940 | struct ly_path *p; |
| 6941 | struct lysc_type *type; |
| 6942 | |
| 6943 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6944 | |
| 6945 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6946 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 6947 | lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, |
| 6948 | lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6949 | |
| 6950 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6951 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6952 | ly_path_free(node->module->ctx, p); |
| 6953 | |
| 6954 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6955 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6956 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 6957 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 6958 | return LY_EVALID; |
| 6959 | } |
| 6960 | |
| 6961 | /* check status */ |
| 6962 | ctx->path[0] = '\0'; |
| 6963 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6964 | ctx->path_len = strlen(ctx->path); |
| 6965 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 6966 | return LY_EVALID; |
| 6967 | } |
| 6968 | ctx->path_len = 1; |
| 6969 | ctx->path[1] = '\0'; |
| 6970 | |
| 6971 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6972 | if (lref->require_instance) { |
| 6973 | for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent); |
| 6974 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6975 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 6976 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 6977 | return LY_EVALID; |
| 6978 | } |
| 6979 | } |
| 6980 | |
| 6981 | /* store the target's type and check for circular chain of leafrefs */ |
| 6982 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 6983 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 6984 | if (type == (struct lysc_type *)lref) { |
| 6985 | /* circular chain detected */ |
| 6986 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6987 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 6988 | return LY_EVALID; |
| 6989 | } |
| 6990 | } |
| 6991 | |
| 6992 | /* check if leafref and its target are under common if-features */ |
| 6993 | if (lys_compile_leafref_features_validate(node, target)) { |
| 6994 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6995 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 6996 | " features applicable to the leafref itself.", lref->path->expr); |
| 6997 | return LY_EVALID; |
| 6998 | } |
| 6999 | |
| 7000 | return LY_SUCCESS; |
| 7001 | } |
| 7002 | |
| 7003 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7004 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7005 | { |
| 7006 | struct lysc_ext_instance *ext; |
| 7007 | struct lysp_ext_instance *ext_p = NULL; |
| 7008 | struct lysp_stmt *stmt; |
| 7009 | const struct lys_module *ext_mod; |
| 7010 | LY_ERR ret = LY_SUCCESS; |
| 7011 | |
| 7012 | /* create the parsed extension instance manually */ |
| 7013 | ext_p = calloc(1, sizeof *ext_p); |
| 7014 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 7015 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 7016 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 7017 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7018 | ext_p->insubstmt_index = 0; |
| 7019 | |
| 7020 | stmt = calloc(1, sizeof *ext_p->child); |
| 7021 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 7022 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 7023 | stmt->kw = LY_STMT_TYPE; |
| 7024 | ext_p->child = stmt; |
| 7025 | |
| 7026 | /* allocate new extension instance */ |
| 7027 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7028 | |
| 7029 | /* manually get extension definition module */ |
| 7030 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7031 | |
| 7032 | /* compile the extension instance */ |
| 7033 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7034 | |
| 7035 | cleanup: |
| 7036 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7037 | free(ext_p); |
| 7038 | return ret; |
| 7039 | } |
| 7040 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7041 | static LY_ERR |
| 7042 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7043 | { |
| 7044 | struct lysc_node *node; |
| 7045 | struct lysc_type *type, *typeiter; |
| 7046 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7047 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7048 | uint32_t i; |
| 7049 | |
| 7050 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7051 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7052 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7053 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7054 | node = ctx->leafrefs.objs[i]; |
| 7055 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7056 | type = ((struct lysc_node_leaf *)node)->type; |
| 7057 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7058 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7059 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7060 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7061 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7062 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7063 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7064 | } |
| 7065 | } |
| 7066 | } |
| 7067 | } |
| 7068 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7069 | /* store pointer to the real type */ |
| 7070 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7071 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7072 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7073 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7074 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7075 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
| 7076 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7077 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7078 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7079 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7080 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7081 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7082 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7083 | } |
| 7084 | } |
| 7085 | } |
| 7086 | } |
| 7087 | |
| 7088 | /* check xpath */ |
| 7089 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7090 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7091 | } |
| 7092 | |
| 7093 | /* finish incomplete default values compilation */ |
| 7094 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 7095 | struct ly_err_item *err = NULL; |
| 7096 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
| 7097 | LY_ERR ret; |
| 7098 | ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original), |
| 7099 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7100 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7101 | if (err) { |
| 7102 | ly_err_print(err); |
| 7103 | ctx->path[0] = '\0'; |
| 7104 | lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7105 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7106 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7107 | ly_err_free(err); |
| 7108 | } |
| 7109 | LY_CHECK_RET(ret); |
| 7110 | } |
| 7111 | |
| 7112 | return LY_SUCCESS; |
| 7113 | } |
| 7114 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7115 | LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7116 | lys_compile(struct lys_module **mod, int options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7117 | { |
| 7118 | struct lysc_ctx ctx = {0}; |
| 7119 | struct lysc_module *mod_c; |
| 7120 | struct lysp_module *sp; |
| 7121 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7122 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7123 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7124 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7125 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7126 | uint32_t i; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7127 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7128 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7129 | 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] | 7130 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7131 | if (!(*mod)->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7132 | /* just imported modules are not compiled */ |
| 7133 | return LY_SUCCESS; |
| 7134 | } |
| 7135 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7136 | sp = (*mod)->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7137 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7138 | ctx.ctx = (*mod)->ctx; |
| 7139 | ctx.mod = *mod; |
| 7140 | ctx.mod_def = *mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7141 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7142 | ctx.path_len = 1; |
| 7143 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7144 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7145 | (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7146 | LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM); |
| 7147 | mod_c->mod = *mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7148 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7149 | LY_ARRAY_FOR(sp->imports, u) { |
| 7150 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 7151 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7152 | LY_ARRAY_FOR(sp->includes, u) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7153 | LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7154 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7155 | |
| 7156 | /* features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7157 | if ((*mod)->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7158 | /* there is already precompiled array of features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7159 | mod_c->features = (*mod)->dis_features; |
| 7160 | (*mod)->dis_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7161 | } else { |
| 7162 | /* features are compiled directly into the compiled module structure, |
| 7163 | * 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] | 7164 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7165 | LY_CHECK_GOTO(ret, error); |
| 7166 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7167 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7168 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7169 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7170 | * are present. */ |
| 7171 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7172 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7173 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7174 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7175 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7176 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7177 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7178 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7179 | 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] | 7180 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7181 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7182 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7183 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7184 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7185 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7186 | /* identities, work similarly to features with the precompilation */ |
| 7187 | if ((*mod)->dis_identities) { |
| 7188 | mod_c->identities = (*mod)->dis_identities; |
| 7189 | (*mod)->dis_identities = NULL; |
| 7190 | } else { |
| 7191 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities); |
| 7192 | LY_CHECK_GOTO(ret, error); |
| 7193 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7194 | if (sp->identities) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7195 | 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] | 7196 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7197 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7198 | LY_ARRAY_FOR(sp->includes, v) { |
| 7199 | if (sp->includes[v].submodule->identities) { |
| 7200 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7201 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities); |
| 7202 | LY_CHECK_GOTO(ret, error); |
| 7203 | lysc_update_path(&ctx, NULL, NULL); |
| 7204 | } |
| 7205 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7206 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7207 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7208 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7209 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7210 | 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] | 7211 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7212 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7213 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7214 | 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] | 7215 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7216 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7217 | 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] | 7218 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7219 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7220 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7221 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7222 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7223 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7224 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7225 | /* extension instances TODO cover extension instances from submodules */ |
| 7226 | 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] | 7227 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7228 | /* finish compilation for all unresolved items in the context */ |
| 7229 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7230 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7231 | /* validate non-instantiated groupings from the parsed schema, |
| 7232 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7233 | ctx.options |= LYSC_OPT_GROUPING; |
| 7234 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7235 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7236 | 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] | 7237 | } |
| 7238 | } |
| 7239 | LY_LIST_FOR(sp->data, node_p) { |
| 7240 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7241 | LY_ARRAY_FOR(grps, u) { |
| 7242 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7243 | 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] | 7244 | } |
| 7245 | } |
| 7246 | } |
| 7247 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7248 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7249 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7250 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7251 | } |
| 7252 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7253 | #if 0 |
| 7254 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7255 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7256 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7257 | * the anotation definitions available in the internal schema structure. */ |
| 7258 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7259 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7260 | lys_free(mod, NULL, 1, 1); |
| 7261 | return NULL; |
| 7262 | } |
| 7263 | } |
| 7264 | #endif |
| 7265 | |
| 7266 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7267 | if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) { |
| 7268 | 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] | 7269 | } |
| 7270 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7271 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7272 | ly_set_erase(&ctx.xpath, NULL); |
| 7273 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7274 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7275 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7276 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7277 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7278 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7279 | lysp_module_free((*mod)->parsed); |
| 7280 | (*mod)->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7281 | } |
| 7282 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7283 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7284 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7285 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7286 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7287 | if (m->implemented == 2) { |
| 7288 | m->implemented = 1; |
| 7289 | } |
| 7290 | } |
| 7291 | } |
| 7292 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7293 | (*mod)->compiled = mod_c; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7294 | return LY_SUCCESS; |
| 7295 | |
| 7296 | error: |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7297 | lys_feature_precompile_revert(&ctx, *mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7298 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7299 | ly_set_erase(&ctx.xpath, NULL); |
| 7300 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7301 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7302 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7303 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7304 | lysc_module_free(mod_c, NULL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7305 | (*mod)->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7306 | |
| 7307 | /* revert compilation of 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]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7310 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7311 | /* revert features list to the precompiled state */ |
| 7312 | lys_feature_precompile_revert(&ctx, m); |
| 7313 | /* mark module as imported-only / not-implemented */ |
| 7314 | m->implemented = 0; |
| 7315 | /* free the compiled version of the module */ |
| 7316 | lysc_module_free(m->compiled, NULL); |
| 7317 | m->compiled = NULL; |
| 7318 | } |
| 7319 | } |
| 7320 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7321 | /* remove the module itself from the context and free it */ |
| 7322 | ly_set_rm(&ctx.ctx->list, *mod, NULL); |
| 7323 | lys_module_free(*mod, NULL); |
| 7324 | *mod = NULL; |
| 7325 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7326 | return ret; |
| 7327 | } |